Skip to content

Latest commit

 

History

History
1027 lines (595 loc) · 32.3 KB

API.md

File metadata and controls

1027 lines (595 loc) · 32.3 KB

Table of Contents

cosa

lib/index.js:10-10

Main module that provides access to library classes and functions.

db

lib/index.js:16-16

Database connection instance.

init

lib/index.js:26-29

Initialize cosa and connect to the database. Explicitly calling the function is not needed if process.env.COSA_DB_URI is properly set.

Parameters

Returns Promise db.init promise

Database

lib/db.js:75-403

Singleton that provides methods for connecting to a MongoDB collection.

init

lib/db.js:99-137

Initialize the database connection.

Parameters

Returns Promise returns the db when connected

collection

lib/db.js:146-167

Fetch a specific database collection.

Parameters

  • name string Collection name.
  • options object? Optional collection settings.

Returns Promise resolves with the connection

find

lib/db.js:196-247

Fetches documents from a collection with the given query.

Parameters

  • 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':1
    • options.projection object? The projection to return in the query. Object of projection to include or exclude (not both), {'a':1
    • options.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, default 0)
    • options.limit object Sets the limit of documents returned in the query. (optional, default 1000)
    • options.count object get a count of the items, instead of the items themselves. (optional, default false)
    • options.findOne object Should a single item be returned. (optional, default false)
    • options.readPreference object? the read preference for the query with one of the read constants
    • options.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 false
    • options.maxTimeMS number? maximum amount of time (in ms) this cursor is allowed to live
    • options.session object? Mongo session or Cosa Mongo session wrapper

Returns Cursor returns Cursor object

insert

lib/db.js:261-278

Inserts the given docs into a collection.

Parameters

  • collectionName string Name of the collection.

  • docs (object | Array) Documents objects to insert.

  • options object options on insert (optional, default {})

    • options.writeConcern object? the write concern
    • options.session object? Mongo session or Cosa Mongo session wrapper

Returns Promise resolves with an object with results, and ops as keys

update

lib/db.js:295-304

Updates docs in a collection.

Parameters

  • 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, default false)
    • options.upsert boolean Should documents be inserted if they don't already exist. (optional, default false)
    • options.writeConcern object? the write concern options
    • options.session object? Mongo session or Cosa Mongo session wrapper

Returns Promise resolves with an object with results, and ops as keys

remove

lib/db.js:319-326

Removes docs from a collection.

Parameters

  • collectionName string Name of the collection.

  • query object Query to find which documents to remove.

  • options object? Optional settings see mongo documentation (optional, default {})

    • options.multiple boolean Should multiple documents be removed. (optional, default false)
    • options.writeConcern object? the write concern options
    • options.session object? Mongo session or Cosa Mongo session wrapper

Returns Promise resolves with an object with results, and ops as keys

aggregate

lib/db.js:343-352

Executes aggregation pipeline against a collection.

Parameters

  • 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 constants
    • options.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 live
    • options.session object? Mongo session or Cosa Mongo session wrapper

Returns Promise resolves with the result of the aggregation from mongo

distinct

lib/db.js:368-376

Returns list of unique values for the given key across a collection.

Parameters

  • 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 {})

    • options.readPreference string? the read preference for the query
    • options.maxTimeMS number? maximum amount of time (in ms) this cursor is allowed to live
    • options.session object? Mongo session or Cosa Mongo session wrapper

Returns Promise resolves with the result of the distinct query from mongo

replace

lib/db.js:390-401

Replace a doc in a collection.

Parameters

  • 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 {})

    • options.writeConcern object? the write concern options
    • options.session object? Mongo session or Cosa Mongo session wrapper

Returns Promise resolves with the result of the distinct query from mongo

Model

lib/model.js:392-543

Static class for working with Models

define

lib/model.js:392-543

Parameters

  • definition Object the model in object form, properties, methods, virtuals

Returns Object an instance of Model

Cursor

lib/cursor.js:8-199

Wrapper around MongoDB Cursor to convert results to Models.

Parameters

  • cursor object MongoDB cursor object
  • factory function Factory function used to create Models based on document from cursor.

forEach

lib/cursor.js:20-24

Iterates over cursor and executes the iterator for each model returned.

Parameters

  • iterator function a function that will be called for each doc

Returns Promise resolves when list is exhausted.

forEachParallelLimitP

lib/cursor.js:32-55

Executes the iterator for the max number of items in parallel.

Parameters

  • maxParallel number the max number of items to run in parallel
  • iterator function a function that will be called for each doc

Returns Promise resolves when list is exhausted.

count

lib/cursor.js:64-66

Returns the count of documents on the cursor.

Parameters

  • applySkipLimit boolean the number of docs to skip over in the count
  • options object mongo cursor count options

Returns number the count of items in the list

close

lib/cursor.js:73-75

Close the underlying MongoDB cursor.

Returns boolean true if cursor is successfully closed

isClosed

lib/cursor.js:82-84

Returns true if the cursor is closed.

Returns boolean true if the cursor is closed.

filter

lib/cursor.js:92-95

Sets the cursor query filter.

Parameters

  • filter object The filter object used for the cursor.

Returns Cursor the cursor instance

limit

lib/cursor.js:103-106

Sets the cursor limit.

Parameters

  • limit number A number to limit the cursor by.

Returns Cursor the cursor instance

map

lib/cursor.js:114-118

Maps cursor results using the provided function.

Parameters

  • transform function A function that will be called by each document in the cursor.

Returns Cursor the cursor instance

max

lib/cursor.js:126-129

Sets the cursor max.

Parameters

  • 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

min

lib/cursor.js:137-140

Sets the cursor min.

Parameters

  • 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

next

lib/cursor.js:147-150

Get the next available document from the cursor.

Returns Promise resolves with the next document from the cursor.

serialForEach

lib/cursor.js:158-166

Like Cursor.forEach but guarantees non-parallel execution of iterator.

Parameters

  • 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

skip

lib/cursor.js:174-177

Sets the cursor skip.

Parameters

  • value number The skip for the cursor query.

Returns Cursor the cursor instance

sort

lib/cursor.js:186-189

Sets the cursor sort.

Parameters

  • sortOrList (string | Array) The key or keys set for the sort.
  • direction number 1 or -1 for ascending or descending

Returns Cursor the cursor instance

toArray

lib/cursor.js:196-198

Returns the cursor results as an array.

Returns Promise that resolves into an array from the cursor

errors

lib/errors.js:19-39

Custom errors.

Conflict

lib/errors.js:24-28

Error used when an action results in a conflict between documents.

Validation

lib/errors.js:33-37

Error used when validation of a document fails.

ImmutableDefinedObject

lib/defined-object.js:16-58

Immutable handler used to create an immutable object based on definition describing the object properties and methods.

Parameters

  • data object Underlying defined object

  • builder object Builder instance

  • options object Optional settings

    • options.definition object Defintion settings

      • options.definition.properties object? Describes the properties of the immutable object.
      • options.definition.virtuals object? Describes the virtual properties of the immutable object.
      • options.definition.methods object? Describes the methods of the immutable object.

Returns undefined no return value

ImmutableArray

lib/array.js:12-76

Immutable handler used to wrap a javascript array.

Parameters

  • data object Underlying array
  • builder object Builder instance
  • options object Optional settings

Returns undefined no return value

ImmutableDate

lib/date.js:11-43

Immutable handler used to wrap javascript Dates.

Parameters

  • data object Underlying date
  • builder object Builder instance
  • options object Optional settings

Returns undefined no return value

ImmutableBSONObjectId

lib/bson-objectId.js:9-35

Immutable handler used to wrap bson ObjectId.

Parameters

  • data object the object id
  • builder object the immutable handler to wrapper the object id.

Returns undefined this adds methods on to the builder.

ImmutableObject

lib/object.js:20-26

Immutable handler used to wrap a simple javascript object.

Parameters

  • data object Underlying array
  • builder object Builder instance

Returns undefined no return value

Immutable

lib/immutable.js:35-126

Static class for working with immutable data types.

use

lib/immutable.js:44-53

Registers an immutable handler for the given data type. Handlers are used to wrap specific data types. For example BSON ObjectIds or Dates.

Parameters

  • type string the name of the type (optional, default *)
  • handler function the handler for the type

Returns undefined no return value

isImmutable

lib/immutable.js:60-63

Returns true if the given value is an Immutable.

Parameters

  • value object a value to check

Returns boolean if it is an immutable instance

isImmutableType

lib/immutable.js:71-73

Returns true if the given value is an Immutable of the given type.

Parameters

  • value object a value to check
  • type string type to check against

Returns boolean if the value is an immutable instance of that type

create

lib/immutable.js:81-125

Creates an immutable object based on the given data.

Parameters

  • 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