github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/options/createcollectionoptions.go (about) 1 // Copyright (C) MongoDB, Inc. 2017-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7 package options 8 9 import "time" 10 11 // DefaultIndexOptions represents the default options for a collection to apply on new indexes. This type can be used 12 // when creating a new collection through the CreateCollectionOptions.SetDefaultIndexOptions method. 13 type DefaultIndexOptions struct { 14 // Specifies the storage engine to use for the index. The value must be a document in the form 15 // {<storage engine name>: <options>}. The default value is nil, which means that the default storage engine 16 // will be used. 17 StorageEngine interface{} 18 } 19 20 // DefaultIndex creates a new DefaultIndexOptions instance. 21 func DefaultIndex() *DefaultIndexOptions { 22 return &DefaultIndexOptions{} 23 } 24 25 // SetStorageEngine sets the value for the StorageEngine field. 26 func (d *DefaultIndexOptions) SetStorageEngine(storageEngine interface{}) *DefaultIndexOptions { 27 d.StorageEngine = storageEngine 28 return d 29 } 30 31 // TimeSeriesOptions specifies options on a time-series collection. 32 type TimeSeriesOptions struct { 33 // TimeField is the top-level field to be used for time. Inserted documents must have this field, 34 // and the field must be of the BSON UTC datetime type (0x9). 35 TimeField string 36 37 // MetaField is the name of the top-level field describing the series. This field is used to group 38 // related data and may be of any BSON type, except for array. This name may not be the same 39 // as the TimeField or _id. This field is optional. 40 MetaField *string 41 42 // Granularity is the granularity of time-series data. Allowed granularity options are 43 // "seconds", "minutes" and "hours". This field is optional. 44 Granularity *string 45 46 // BucketMaxSpan is the maximum range of time values for a bucket. The 47 // time.Duration is rounded down to the nearest second and applied as 48 // the command option: "bucketRoundingSeconds". This field is optional. 49 BucketMaxSpan *time.Duration 50 51 // BucketRounding is used to determine the minimum time boundary when 52 // opening a new bucket by rounding the first timestamp down to the next 53 // multiple of this value. The time.Duration is rounded down to the 54 // nearest second and applied as the command option: 55 // "bucketRoundingSeconds". This field is optional. 56 BucketRounding *time.Duration 57 } 58 59 // TimeSeries creates a new TimeSeriesOptions instance. 60 func TimeSeries() *TimeSeriesOptions { 61 return &TimeSeriesOptions{} 62 } 63 64 // SetTimeField sets the value for the TimeField. 65 func (tso *TimeSeriesOptions) SetTimeField(timeField string) *TimeSeriesOptions { 66 tso.TimeField = timeField 67 return tso 68 } 69 70 // SetMetaField sets the value for the MetaField. 71 func (tso *TimeSeriesOptions) SetMetaField(metaField string) *TimeSeriesOptions { 72 tso.MetaField = &metaField 73 return tso 74 } 75 76 // SetGranularity sets the value for Granularity. 77 func (tso *TimeSeriesOptions) SetGranularity(granularity string) *TimeSeriesOptions { 78 tso.Granularity = &granularity 79 return tso 80 } 81 82 // SetBucketMaxSpan sets the value for BucketMaxSpan. 83 func (tso *TimeSeriesOptions) SetBucketMaxSpan(dur time.Duration) *TimeSeriesOptions { 84 tso.BucketMaxSpan = &dur 85 86 return tso 87 } 88 89 // SetBucketRounding sets the value for BucketRounding. 90 func (tso *TimeSeriesOptions) SetBucketRounding(dur time.Duration) *TimeSeriesOptions { 91 tso.BucketRounding = &dur 92 93 return tso 94 } 95 96 // CreateCollectionOptions represents options that can be used to configure a CreateCollection operation. 97 type CreateCollectionOptions struct { 98 // Specifies if the collection is capped (see https://www.mongodb.com/docs/manual/core/capped-collections/). If true, 99 // the SizeInBytes option must also be specified. The default value is false. 100 Capped *bool 101 102 // Specifies the default collation for the new collection. This option is only valid for MongoDB versions >= 3.4. 103 // For previous server versions, the driver will return an error if this option is used. The default value is nil. 104 Collation *Collation 105 106 // Specifies how change streams opened against the collection can return pre- and post-images of updated 107 // documents. The value must be a document in the form {<option name>: <options>}. This option is only valid for 108 // MongoDB versions >= 6.0. The default value is nil, which means that change streams opened against the collection 109 // will not return pre- and post-images of updated documents in any way. 110 ChangeStreamPreAndPostImages interface{} 111 112 // Specifies a default configuration for indexes on the collection. This option is only valid for MongoDB versions 113 // >= 3.4. The default value is nil, meaning indexes will be configured using server defaults. 114 DefaultIndexOptions *DefaultIndexOptions 115 116 // Specifies the maximum number of documents allowed in a capped collection. The limit specified by the SizeInBytes 117 // option takes precedence over this option. If a capped collection reaches its size limit, old documents will be 118 // removed, regardless of the number of documents in the collection. The default value is 0, meaning the maximum 119 // number of documents is unbounded. 120 MaxDocuments *int64 121 122 // Specifies the maximum size in bytes for a capped collection. The default value is 0. 123 SizeInBytes *int64 124 125 // Specifies the storage engine to use for the index. The value must be a document in the form 126 // {<storage engine name>: <options>}. The default value is nil, which means that the default storage engine 127 // will be used. 128 StorageEngine interface{} 129 130 // Specifies what should happen if a document being inserted does not pass validation. Valid values are "error" and 131 // "warn". See https://www.mongodb.com/docs/manual/core/schema-validation/#accept-or-reject-invalid-documents for more 132 // information. This option is only valid for MongoDB versions >= 3.2. The default value is "error". 133 ValidationAction *string 134 135 // Specifies how strictly the server applies validation rules to existing documents in the collection during update 136 // operations. Valid values are "off", "strict", and "moderate". See 137 // https://www.mongodb.com/docs/manual/core/schema-validation/#existing-documents for more information. This option is 138 // only valid for MongoDB versions >= 3.2. The default value is "strict". 139 ValidationLevel *string 140 141 // A document specifying validation rules for the collection. See 142 // https://www.mongodb.com/docs/manual/core/schema-validation/ for more information about schema validation. This option 143 // is only valid for MongoDB versions >= 3.2. The default value is nil, meaning no validator will be used for the 144 // collection. 145 Validator interface{} 146 147 // Value indicating after how many seconds old time-series data should be deleted. See 148 // https://www.mongodb.com/docs/manual/reference/command/create/ for supported options, and 149 // https://www.mongodb.com/docs/manual/core/timeseries-collections/ for more information on time-series 150 // collections. 151 // 152 // This option is only valid for MongoDB versions >= 5.0 153 ExpireAfterSeconds *int64 154 155 // Options for specifying a time-series collection. See 156 // https://www.mongodb.com/docs/manual/reference/command/create/ for supported options, and 157 // https://www.mongodb.com/docs/manual/core/timeseries-collections/ for more information on time-series 158 // collections. 159 // 160 // This option is only valid for MongoDB versions >= 5.0 161 TimeSeriesOptions *TimeSeriesOptions 162 163 // EncryptedFields configures encrypted fields. 164 // 165 // This option is only valid for MongoDB versions >= 6.0 166 EncryptedFields interface{} 167 168 // ClusteredIndex is used to create a collection with a clustered index. 169 // 170 // This option is only valid for MongoDB versions >= 5.3 171 ClusteredIndex interface{} 172 } 173 174 // CreateCollection creates a new CreateCollectionOptions instance. 175 func CreateCollection() *CreateCollectionOptions { 176 return &CreateCollectionOptions{} 177 } 178 179 // SetCapped sets the value for the Capped field. 180 func (c *CreateCollectionOptions) SetCapped(capped bool) *CreateCollectionOptions { 181 c.Capped = &capped 182 return c 183 } 184 185 // SetCollation sets the value for the Collation field. 186 func (c *CreateCollectionOptions) SetCollation(collation *Collation) *CreateCollectionOptions { 187 c.Collation = collation 188 return c 189 } 190 191 // SetChangeStreamPreAndPostImages sets the value for the ChangeStreamPreAndPostImages field. 192 func (c *CreateCollectionOptions) SetChangeStreamPreAndPostImages(csppi interface{}) *CreateCollectionOptions { 193 c.ChangeStreamPreAndPostImages = &csppi 194 return c 195 } 196 197 // SetDefaultIndexOptions sets the value for the DefaultIndexOptions field. 198 func (c *CreateCollectionOptions) SetDefaultIndexOptions(opts *DefaultIndexOptions) *CreateCollectionOptions { 199 c.DefaultIndexOptions = opts 200 return c 201 } 202 203 // SetMaxDocuments sets the value for the MaxDocuments field. 204 func (c *CreateCollectionOptions) SetMaxDocuments(max int64) *CreateCollectionOptions { 205 c.MaxDocuments = &max 206 return c 207 } 208 209 // SetSizeInBytes sets the value for the SizeInBytes field. 210 func (c *CreateCollectionOptions) SetSizeInBytes(size int64) *CreateCollectionOptions { 211 c.SizeInBytes = &size 212 return c 213 } 214 215 // SetStorageEngine sets the value for the StorageEngine field. 216 func (c *CreateCollectionOptions) SetStorageEngine(storageEngine interface{}) *CreateCollectionOptions { 217 c.StorageEngine = &storageEngine 218 return c 219 } 220 221 // SetValidationAction sets the value for the ValidationAction field. 222 func (c *CreateCollectionOptions) SetValidationAction(action string) *CreateCollectionOptions { 223 c.ValidationAction = &action 224 return c 225 } 226 227 // SetValidationLevel sets the value for the ValidationLevel field. 228 func (c *CreateCollectionOptions) SetValidationLevel(level string) *CreateCollectionOptions { 229 c.ValidationLevel = &level 230 return c 231 } 232 233 // SetValidator sets the value for the Validator field. 234 func (c *CreateCollectionOptions) SetValidator(validator interface{}) *CreateCollectionOptions { 235 c.Validator = validator 236 return c 237 } 238 239 // SetExpireAfterSeconds sets the value for the ExpireAfterSeconds field. 240 func (c *CreateCollectionOptions) SetExpireAfterSeconds(eas int64) *CreateCollectionOptions { 241 c.ExpireAfterSeconds = &eas 242 return c 243 } 244 245 // SetTimeSeriesOptions sets the options for time-series collections. 246 func (c *CreateCollectionOptions) SetTimeSeriesOptions(timeSeriesOpts *TimeSeriesOptions) *CreateCollectionOptions { 247 c.TimeSeriesOptions = timeSeriesOpts 248 return c 249 } 250 251 // SetEncryptedFields sets the encrypted fields for encrypted collections. 252 func (c *CreateCollectionOptions) SetEncryptedFields(encryptedFields interface{}) *CreateCollectionOptions { 253 c.EncryptedFields = encryptedFields 254 return c 255 } 256 257 // SetClusteredIndex sets the value for the ClusteredIndex field. 258 func (c *CreateCollectionOptions) SetClusteredIndex(clusteredIndex interface{}) *CreateCollectionOptions { 259 c.ClusteredIndex = clusteredIndex 260 return c 261 } 262 263 // MergeCreateCollectionOptions combines the given CreateCollectionOptions instances into a single 264 // CreateCollectionOptions in a last-one-wins fashion. 265 // 266 // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a 267 // single options struct instead. 268 func MergeCreateCollectionOptions(opts ...*CreateCollectionOptions) *CreateCollectionOptions { 269 cc := CreateCollection() 270 271 for _, opt := range opts { 272 if opt == nil { 273 continue 274 } 275 276 if opt.Capped != nil { 277 cc.Capped = opt.Capped 278 } 279 if opt.Collation != nil { 280 cc.Collation = opt.Collation 281 } 282 if opt.ChangeStreamPreAndPostImages != nil { 283 cc.ChangeStreamPreAndPostImages = opt.ChangeStreamPreAndPostImages 284 } 285 if opt.DefaultIndexOptions != nil { 286 cc.DefaultIndexOptions = opt.DefaultIndexOptions 287 } 288 if opt.MaxDocuments != nil { 289 cc.MaxDocuments = opt.MaxDocuments 290 } 291 if opt.SizeInBytes != nil { 292 cc.SizeInBytes = opt.SizeInBytes 293 } 294 if opt.StorageEngine != nil { 295 cc.StorageEngine = opt.StorageEngine 296 } 297 if opt.ValidationAction != nil { 298 cc.ValidationAction = opt.ValidationAction 299 } 300 if opt.ValidationLevel != nil { 301 cc.ValidationLevel = opt.ValidationLevel 302 } 303 if opt.Validator != nil { 304 cc.Validator = opt.Validator 305 } 306 if opt.ExpireAfterSeconds != nil { 307 cc.ExpireAfterSeconds = opt.ExpireAfterSeconds 308 } 309 if opt.TimeSeriesOptions != nil { 310 cc.TimeSeriesOptions = opt.TimeSeriesOptions 311 } 312 if opt.EncryptedFields != nil { 313 cc.EncryptedFields = opt.EncryptedFields 314 } 315 if opt.ClusteredIndex != nil { 316 cc.ClusteredIndex = opt.ClusteredIndex 317 } 318 } 319 320 return cc 321 } 322 323 // CreateViewOptions represents options that can be used to configure a CreateView operation. 324 type CreateViewOptions struct { 325 // Specifies the default collation for the new collection. This option is only valid for MongoDB versions >= 3.4. 326 // For previous server versions, the driver will return an error if this option is used. The default value is nil. 327 Collation *Collation 328 } 329 330 // CreateView creates an new CreateViewOptions instance. 331 func CreateView() *CreateViewOptions { 332 return &CreateViewOptions{} 333 } 334 335 // SetCollation sets the value for the Collation field. 336 func (c *CreateViewOptions) SetCollation(collation *Collation) *CreateViewOptions { 337 c.Collation = collation 338 return c 339 } 340 341 // MergeCreateViewOptions combines the given CreateViewOptions instances into a single CreateViewOptions in a 342 // last-one-wins fashion. 343 // 344 // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a 345 // single options struct instead. 346 func MergeCreateViewOptions(opts ...*CreateViewOptions) *CreateViewOptions { 347 cv := CreateView() 348 349 for _, opt := range opts { 350 if opt == nil { 351 continue 352 } 353 354 if opt.Collation != nil { 355 cv.Collation = opt.Collation 356 } 357 } 358 359 return cv 360 }