- cosa
- db
- init
- Database
- Model
- Cursor
- errors
- ImmutableDefinedObject
- ImmutableArray
- ImmutableDate
- ImmutableBSONObjectId
- ImmutableObject
- Immutable
- use
- isImmutable
- isImmutableType
- create
Main module that provides access to library classes and functions.
- See: Database
Database connection instance.
Initialize cosa and connect to the database. Explicitly calling the
function is not needed if process.env.COSA_DB_URI
is properly set.
uri
string? URI to the database. If no URI is provided, the value ofprocess.env.COSA_DB_URI
is used. See https://docs.mongodb.com/manual/reference/connection-string/ for information on the URI format.
Returns Promise db.init promise
Singleton that provides methods for connecting to a MongoDB collection.
- See: init
Initialize the database connection.
uri
string? URI to the database. If no URI is provided, the value ofprocess.env.COSA_DB_URI
is used. See https://docs.mongodb.com/manual/reference/connection-string/ for information on the URI format.options
object? Optional collection settings.
Returns Promise returns the db when connected
Fetch a specific database collection.
Returns Promise resolves with the connection
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#find
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#findOne
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#count
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#readPreference
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#batchSize
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#noCursorTimeout
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#maxTimeMS
Fetches documents from a collection with the given query.
-
collectionName
string Name of the collection. -
query
object MongoDB query object. -
options
object? MongoDB query options. (optional, default{}
)options.fields
object? (deprecated, will be mapped to projection) The fields to return in the query. Object of fields to include or exclude (not both), {'a':1options.projection
object? The projection to return in the query. Object of projection to include or exclude (not both), {'a':1options.sort
(object | Array)? Set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.options.skip
number Set to skip N documents ahead in your query (useful for pagination). (optional, default0
)options.limit
object Sets the limit of documents returned in the query. (optional, default1000
)options.count
object get a count of the items, instead of the items themselves. (optional, defaultfalse
)options.findOne
object Should a single item be returned. (optional, defaultfalse
)options.readPreference
object? the read preference for the query with one of the read constantsoptions.batchSize
number? number of items per batch (default in mongo driver is 1000)options.noCursorTimeout
boolean? boolan, if the cursor can time out after being idle, mongo driver default is falseoptions.maxTimeMS
number? maximum amount of time (in ms) this cursor is allowed to liveoptions.session
object? Mongo session or Cosa Mongo session wrapper
Returns Cursor returns Cursor object
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#insertMany
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#insertOne
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/BulkWriteOptions.html#writeConcern
Inserts the given docs into a collection.
-
collectionName
string Name of the collection. -
options
object options on insert (optional, default{}
)
Returns Promise resolves with an object with results, and ops as keys
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#updateMany
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#updateOne
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/BulkWriteOptions.html#writeConcern
Updates docs in a collection.
-
collectionName
string Name of the collection. -
query
object Query to find which documents to update. -
update
object Document properties to update. -
options
object? Optional settings see mongo documentation (optional, default{}
)options.multiple
boolean Should multiple documents be updated. (optional, defaultfalse
)options.upsert
boolean Should documents be inserted if they don't already exist. (optional, defaultfalse
)options.writeConcern
object? the write concern optionsoptions.session
object? Mongo session or Cosa Mongo session wrapper
Returns Promise resolves with an object with results, and ops as keys
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#deleteMany
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#deleteOne
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/DeleteOptions.html#writeConcern
Removes docs from a collection.
-
collectionName
string Name of the collection. -
query
object Query to find which documents to remove. -
options
object? Optional settings see mongo documentation (optional, default{}
)
Returns Promise resolves with an object with results, and ops as keys
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#aggregate
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/AggregateOptions.html#readPreference
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/AggregateOptions.html#batchSize
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/AggregateOptions.html#maxTimeMS
Executes aggregation pipeline against a collection.
-
collectionName
string Name of the collection. -
pipeline
object Aggregation pipeline. -
options
object? Optional settings see mongo documentation (optional, default{}
)options.readPreference
object? the read preference for the query with one of the read constantsoptions.batchSize
number? number of items per batch (default in mongo driver is 1000)options.maxTimeMS
number? maximum amount of time (in ms) this cursor is allowed to liveoptions.session
object? Mongo session or Cosa Mongo session wrapper
Returns Promise resolves with the result of the aggregation from mongo
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#distinct
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/CommandOperationOptions.html#readPreference
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/CommandOperationOptions.html#maxTimeMS
Returns list of unique values for the given key across a collection.
-
collectionName
string Name of the collection. -
key
string Document property. -
query
object Query to find which documents evaluate. -
options
object? Optional settings see mongo documentation (optional, default{}
)
Returns Promise resolves with the result of the distinct query from mongo
- See: https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#replaceOne
- See: https://mongodb.github.io/node-mongodb-native/6.3/interfaces/ReplaceOptions.html#writeConcern
Replace a doc in a collection.
-
collectionName
string Name of the collection. -
query
object Query to find which documents evaluate. -
replace
object doc to save on the collection -
options
object? Optional settings see mongo documentation (optional, default{}
)
Returns Promise resolves with the result of the distinct query from mongo
Static class for working with Models
definition
Object the model in object form, properties, methods, virtuals
Returns Object an instance of Model
Wrapper around MongoDB Cursor to convert results to Models.
cursor
object MongoDB cursor objectfactory
function Factory function used to create Models based on document from cursor.
Iterates over cursor and executes the iterator for each model returned.
iterator
function a function that will be called for each doc
Returns Promise resolves when list is exhausted.
Executes the iterator for the max number of items in parallel.
maxParallel
number the max number of items to run in paralleliterator
function a function that will be called for each doc
Returns Promise resolves when list is exhausted.
Returns the count of documents on the cursor.
applySkipLimit
boolean the number of docs to skip over in the countoptions
object mongo cursor count options
Returns number the count of items in the list
Close the underlying MongoDB cursor.
Returns boolean true
if cursor is successfully closed
Returns true
if the cursor is closed.
Returns boolean true
if the cursor is closed.
Sets the cursor query filter.
filter
object The filter object used for the cursor.
Returns Cursor the cursor instance
Sets the cursor limit.
limit
number A number to limit the cursor by.
Returns Cursor the cursor instance
Maps cursor results using the provided function.
transform
function A function that will be called by each document in the cursor.
Returns Cursor the cursor instance
Sets the cursor max.
max
number Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find().
Returns Cursor the cursor instance
Sets the cursor min.
min
number Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find().
Returns Cursor the cursor instance
Get the next available document from the cursor.
Returns Promise resolves with the next document from the cursor.
Like Cursor.forEach
but guarantees non-parallel execution of iterator.
iterator
function the function that will be called by each document serially.
Returns Promise resolves when each item in the cursor has been passed to the iterator function
Sets the cursor skip.
value
number The skip for the cursor query.
Returns Cursor the cursor instance
Sets the cursor sort.
sortOrList
(string | Array) The key or keys set for the sort.direction
number1
or-1
for ascending or descending
Returns Cursor the cursor instance
Returns the cursor results as an array.
Returns Promise that resolves into an array from the cursor
Custom errors.
Error used when an action results in a conflict between documents.
Error used when validation of a document fails.
Immutable handler used to create an immutable object based on definition describing the object properties and methods.
-
data
object Underlying defined object -
builder
object Builder instance -
options
object Optional settings-
options.definition
object Defintion settings
-
Returns undefined no return value
Immutable handler used to wrap a javascript array.
Returns undefined no return value
Immutable handler used to wrap javascript Dates.
Returns undefined no return value
Immutable handler used to wrap bson ObjectId.
Returns undefined this adds methods on to the builder.
Immutable handler used to wrap a simple javascript object.
Returns undefined no return value
Static class for working with immutable data types.
Registers an immutable handler for the given data type. Handlers are used to wrap specific data types. For example BSON ObjectIds or Dates.
Returns undefined no return value
Returns true
if the given value is an Immutable.
value
object a value to check
Returns boolean if it is an immutable instance
Returns true
if the given value is an Immutable of the given type.
Returns boolean if the value is an immutable instance of that type
Creates an immutable object based on the given data.
data
object Object to make immutable.options
object? Options passed to the immutable handler for the given data type. (optional, default{}
)
Returns object the immutable wrapped object