...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/operation

     1  // Copyright (C) MongoDB, Inc. 2019-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 operation
     8  
     9  import (
    10  	"context"
    11  	"errors"
    12  
    13  	"go.mongodb.org/mongo-driver/event"
    14  	"go.mongodb.org/mongo-driver/mongo/description"
    15  	"go.mongodb.org/mongo-driver/mongo/writeconcern"
    16  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    17  	"go.mongodb.org/mongo-driver/x/mongo/driver"
    18  	"go.mongodb.org/mongo-driver/x/mongo/driver/session"
    19  )
    20  
    21  // Create represents a create operation.
    22  type Create struct {
    23  	capped                       *bool
    24  	collation                    bsoncore.Document
    25  	changeStreamPreAndPostImages bsoncore.Document
    26  	collectionName               *string
    27  	indexOptionDefaults          bsoncore.Document
    28  	max                          *int64
    29  	pipeline                     bsoncore.Document
    30  	size                         *int64
    31  	storageEngine                bsoncore.Document
    32  	validationAction             *string
    33  	validationLevel              *string
    34  	validator                    bsoncore.Document
    35  	viewOn                       *string
    36  	session                      *session.Client
    37  	clock                        *session.ClusterClock
    38  	monitor                      *event.CommandMonitor
    39  	crypt                        driver.Crypt
    40  	database                     string
    41  	deployment                   driver.Deployment
    42  	selector                     description.ServerSelector
    43  	writeConcern                 *writeconcern.WriteConcern
    44  	serverAPI                    *driver.ServerAPIOptions
    45  	expireAfterSeconds           *int64
    46  	timeSeries                   bsoncore.Document
    47  	encryptedFields              bsoncore.Document
    48  	clusteredIndex               bsoncore.Document
    49  }
    50  
    51  // NewCreate constructs and returns a new Create.
    52  func NewCreate(collectionName string) *Create {
    53  	return &Create{
    54  		collectionName: &collectionName,
    55  	}
    56  }
    57  
    58  func (c *Create) processResponse(driver.ResponseInfo) error {
    59  	return nil
    60  }
    61  
    62  // Execute runs this operations and returns an error if the operation did not execute successfully.
    63  func (c *Create) Execute(ctx context.Context) error {
    64  	if c.deployment == nil {
    65  		return errors.New("the Create operation must have a Deployment set before Execute can be called")
    66  	}
    67  
    68  	return driver.Operation{
    69  		CommandFn:         c.command,
    70  		ProcessResponseFn: c.processResponse,
    71  		Client:            c.session,
    72  		Clock:             c.clock,
    73  		CommandMonitor:    c.monitor,
    74  		Crypt:             c.crypt,
    75  		Database:          c.database,
    76  		Deployment:        c.deployment,
    77  		Selector:          c.selector,
    78  		WriteConcern:      c.writeConcern,
    79  		ServerAPI:         c.serverAPI,
    80  	}.Execute(ctx)
    81  }
    82  
    83  func (c *Create) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
    84  	if c.collectionName != nil {
    85  		dst = bsoncore.AppendStringElement(dst, "create", *c.collectionName)
    86  	}
    87  	if c.capped != nil {
    88  		dst = bsoncore.AppendBooleanElement(dst, "capped", *c.capped)
    89  	}
    90  	if c.changeStreamPreAndPostImages != nil {
    91  		dst = bsoncore.AppendDocumentElement(dst, "changeStreamPreAndPostImages", c.changeStreamPreAndPostImages)
    92  	}
    93  	if c.collation != nil {
    94  		if desc.WireVersion == nil || !desc.WireVersion.Includes(5) {
    95  			return nil, errors.New("the 'collation' command parameter requires a minimum server wire version of 5")
    96  		}
    97  		dst = bsoncore.AppendDocumentElement(dst, "collation", c.collation)
    98  	}
    99  	if c.indexOptionDefaults != nil {
   100  		dst = bsoncore.AppendDocumentElement(dst, "indexOptionDefaults", c.indexOptionDefaults)
   101  	}
   102  	if c.max != nil {
   103  		dst = bsoncore.AppendInt64Element(dst, "max", *c.max)
   104  	}
   105  	if c.pipeline != nil {
   106  		dst = bsoncore.AppendArrayElement(dst, "pipeline", c.pipeline)
   107  	}
   108  	if c.size != nil {
   109  		dst = bsoncore.AppendInt64Element(dst, "size", *c.size)
   110  	}
   111  	if c.storageEngine != nil {
   112  		dst = bsoncore.AppendDocumentElement(dst, "storageEngine", c.storageEngine)
   113  	}
   114  	if c.validationAction != nil {
   115  		dst = bsoncore.AppendStringElement(dst, "validationAction", *c.validationAction)
   116  	}
   117  	if c.validationLevel != nil {
   118  		dst = bsoncore.AppendStringElement(dst, "validationLevel", *c.validationLevel)
   119  	}
   120  	if c.validator != nil {
   121  		dst = bsoncore.AppendDocumentElement(dst, "validator", c.validator)
   122  	}
   123  	if c.viewOn != nil {
   124  		dst = bsoncore.AppendStringElement(dst, "viewOn", *c.viewOn)
   125  	}
   126  	if c.expireAfterSeconds != nil {
   127  		dst = bsoncore.AppendInt64Element(dst, "expireAfterSeconds", *c.expireAfterSeconds)
   128  	}
   129  	if c.timeSeries != nil {
   130  		dst = bsoncore.AppendDocumentElement(dst, "timeseries", c.timeSeries)
   131  	}
   132  	if c.encryptedFields != nil {
   133  		dst = bsoncore.AppendDocumentElement(dst, "encryptedFields", c.encryptedFields)
   134  	}
   135  	if c.clusteredIndex != nil {
   136  		dst = bsoncore.AppendDocumentElement(dst, "clusteredIndex", c.clusteredIndex)
   137  	}
   138  	return dst, nil
   139  }
   140  
   141  // Capped specifies if the collection is capped.
   142  func (c *Create) Capped(capped bool) *Create {
   143  	if c == nil {
   144  		c = new(Create)
   145  	}
   146  
   147  	c.capped = &capped
   148  	return c
   149  }
   150  
   151  // Collation specifies a collation. This option is only valid for server versions 3.4 and above.
   152  func (c *Create) Collation(collation bsoncore.Document) *Create {
   153  	if c == nil {
   154  		c = new(Create)
   155  	}
   156  
   157  	c.collation = collation
   158  	return c
   159  }
   160  
   161  // ChangeStreamPreAndPostImages specifies how change streams opened against the collection can return pre-
   162  // and post-images of updated documents. This option is only valid for server versions 6.0 and above.
   163  func (c *Create) ChangeStreamPreAndPostImages(csppi bsoncore.Document) *Create {
   164  	if c == nil {
   165  		c = new(Create)
   166  	}
   167  
   168  	c.changeStreamPreAndPostImages = csppi
   169  	return c
   170  }
   171  
   172  // CollectionName specifies the name of the collection to create.
   173  func (c *Create) CollectionName(collectionName string) *Create {
   174  	if c == nil {
   175  		c = new(Create)
   176  	}
   177  
   178  	c.collectionName = &collectionName
   179  	return c
   180  }
   181  
   182  // IndexOptionDefaults specifies a default configuration for indexes on the collection.
   183  func (c *Create) IndexOptionDefaults(indexOptionDefaults bsoncore.Document) *Create {
   184  	if c == nil {
   185  		c = new(Create)
   186  	}
   187  
   188  	c.indexOptionDefaults = indexOptionDefaults
   189  	return c
   190  }
   191  
   192  // Max specifies the maximum number of documents allowed in a capped collection.
   193  func (c *Create) Max(max int64) *Create {
   194  	if c == nil {
   195  		c = new(Create)
   196  	}
   197  
   198  	c.max = &max
   199  	return c
   200  }
   201  
   202  // Pipeline specifies the agggregtion pipeline to be run against the source to create the view.
   203  func (c *Create) Pipeline(pipeline bsoncore.Document) *Create {
   204  	if c == nil {
   205  		c = new(Create)
   206  	}
   207  
   208  	c.pipeline = pipeline
   209  	return c
   210  }
   211  
   212  // Size specifies the maximum size in bytes for a capped collection.
   213  func (c *Create) Size(size int64) *Create {
   214  	if c == nil {
   215  		c = new(Create)
   216  	}
   217  
   218  	c.size = &size
   219  	return c
   220  }
   221  
   222  // StorageEngine specifies the storage engine to use for the index.
   223  func (c *Create) StorageEngine(storageEngine bsoncore.Document) *Create {
   224  	if c == nil {
   225  		c = new(Create)
   226  	}
   227  
   228  	c.storageEngine = storageEngine
   229  	return c
   230  }
   231  
   232  // ValidationAction specifies what should happen if a document being inserted does not pass validation.
   233  func (c *Create) ValidationAction(validationAction string) *Create {
   234  	if c == nil {
   235  		c = new(Create)
   236  	}
   237  
   238  	c.validationAction = &validationAction
   239  	return c
   240  }
   241  
   242  // ValidationLevel specifies how strictly the server applies validation rules to existing documents in the collection
   243  // during update operations.
   244  func (c *Create) ValidationLevel(validationLevel string) *Create {
   245  	if c == nil {
   246  		c = new(Create)
   247  	}
   248  
   249  	c.validationLevel = &validationLevel
   250  	return c
   251  }
   252  
   253  // Validator specifies validation rules for the collection.
   254  func (c *Create) Validator(validator bsoncore.Document) *Create {
   255  	if c == nil {
   256  		c = new(Create)
   257  	}
   258  
   259  	c.validator = validator
   260  	return c
   261  }
   262  
   263  // ViewOn specifies the name of the source collection or view on which the view will be created.
   264  func (c *Create) ViewOn(viewOn string) *Create {
   265  	if c == nil {
   266  		c = new(Create)
   267  	}
   268  
   269  	c.viewOn = &viewOn
   270  	return c
   271  }
   272  
   273  // Session sets the session for this operation.
   274  func (c *Create) Session(session *session.Client) *Create {
   275  	if c == nil {
   276  		c = new(Create)
   277  	}
   278  
   279  	c.session = session
   280  	return c
   281  }
   282  
   283  // ClusterClock sets the cluster clock for this operation.
   284  func (c *Create) ClusterClock(clock *session.ClusterClock) *Create {
   285  	if c == nil {
   286  		c = new(Create)
   287  	}
   288  
   289  	c.clock = clock
   290  	return c
   291  }
   292  
   293  // CommandMonitor sets the monitor to use for APM events.
   294  func (c *Create) CommandMonitor(monitor *event.CommandMonitor) *Create {
   295  	if c == nil {
   296  		c = new(Create)
   297  	}
   298  
   299  	c.monitor = monitor
   300  	return c
   301  }
   302  
   303  // Crypt sets the Crypt object to use for automatic encryption and decryption.
   304  func (c *Create) Crypt(crypt driver.Crypt) *Create {
   305  	if c == nil {
   306  		c = new(Create)
   307  	}
   308  
   309  	c.crypt = crypt
   310  	return c
   311  }
   312  
   313  // Database sets the database to run this operation against.
   314  func (c *Create) Database(database string) *Create {
   315  	if c == nil {
   316  		c = new(Create)
   317  	}
   318  
   319  	c.database = database
   320  	return c
   321  }
   322  
   323  // Deployment sets the deployment to use for this operation.
   324  func (c *Create) Deployment(deployment driver.Deployment) *Create {
   325  	if c == nil {
   326  		c = new(Create)
   327  	}
   328  
   329  	c.deployment = deployment
   330  	return c
   331  }
   332  
   333  // ServerSelector sets the selector used to retrieve a server.
   334  func (c *Create) ServerSelector(selector description.ServerSelector) *Create {
   335  	if c == nil {
   336  		c = new(Create)
   337  	}
   338  
   339  	c.selector = selector
   340  	return c
   341  }
   342  
   343  // WriteConcern sets the write concern for this operation.
   344  func (c *Create) WriteConcern(writeConcern *writeconcern.WriteConcern) *Create {
   345  	if c == nil {
   346  		c = new(Create)
   347  	}
   348  
   349  	c.writeConcern = writeConcern
   350  	return c
   351  }
   352  
   353  // ServerAPI sets the server API version for this operation.
   354  func (c *Create) ServerAPI(serverAPI *driver.ServerAPIOptions) *Create {
   355  	if c == nil {
   356  		c = new(Create)
   357  	}
   358  
   359  	c.serverAPI = serverAPI
   360  	return c
   361  }
   362  
   363  // ExpireAfterSeconds sets the seconds to wait before deleting old time-series data.
   364  func (c *Create) ExpireAfterSeconds(eas int64) *Create {
   365  	if c == nil {
   366  		c = new(Create)
   367  	}
   368  
   369  	c.expireAfterSeconds = &eas
   370  	return c
   371  }
   372  
   373  // TimeSeries sets the time series options for this operation.
   374  func (c *Create) TimeSeries(timeSeries bsoncore.Document) *Create {
   375  	if c == nil {
   376  		c = new(Create)
   377  	}
   378  
   379  	c.timeSeries = timeSeries
   380  	return c
   381  }
   382  
   383  // EncryptedFields sets the EncryptedFields for this operation.
   384  func (c *Create) EncryptedFields(ef bsoncore.Document) *Create {
   385  	if c == nil {
   386  		c = new(Create)
   387  	}
   388  
   389  	c.encryptedFields = ef
   390  	return c
   391  }
   392  
   393  // ClusteredIndex sets the ClusteredIndex option for this operation.
   394  func (c *Create) ClusteredIndex(ci bsoncore.Document) *Create {
   395  	if c == nil {
   396  		c = new(Create)
   397  	}
   398  
   399  	c.clusteredIndex = ci
   400  	return c
   401  }
   402  

View as plain text