Class: MongoRepository

arpen/repositories/mongo~ MongoRepository

Repository base class

new MongoRepository(app, mongo, util)

Create repository
Parameters:
Name Type Description
app App The application
mongo Mongo Mongo service
util Util Util service

Members


<static> provides :string

Service name is 'repositories.mongo'
Type:
  • string

<static> requires :Array.<string>

Dependencies as constructor arguments
Type:
  • Array.<string>

<static> timeZone :string|null

Table time zone - mongo always uses UTC and converts to local
Type:
  • string | null

Methods


delete(model [, mongo])

Delete a model
Parameters:
Name Type Argument Description
model BaseModel | number Model or ID
mongo MongoClient | string <optional>
Will reuse the Mongo client provided, or if it is a string then will connect to this instance of Mongo.
Returns:
Resolves to number of deleted records
Type
Promise

findAll( [mongo])

Find all models
Parameters:
Name Type Argument Description
mongo MongoClient | string <optional>
Will reuse the Mongo client provided, or if it is a string then will connect to this instance of Mongo.
Returns:
Resolves to array of models
Type
Promise

find(id [, mongo])

Find a model by ID
Parameters:
Name Type Argument Description
id number ID to search by
mongo MongoClient | string <optional>
Will reuse the Mongo client provided, or if it is a string then will connect to this instance of Mongo.
Returns:
Resolves to array of models
Type
Promise

save(model [, mongo])

Save model
Parameters:
Name Type Argument Description
model BaseModel The model
mongo MongoClient | string <optional>
Will reuse the Mongo client provided, or if it is a string then will connect to this instance of Mongo.
Returns:
Resolves to record ID
Type
Promise

Search repository
Parameters:
Name Type Argument Description
options object <optional>
Properties
Name Type Argument Default Description
collection string <optional>
Collection name
search object <optional>
Search object if any
sort object <optional>
Sort object if any
pageSize number <optional>
0 Used in limit(), 0 = all records
pageNumber number <optional>
1 Used in skip()
infoOnly boolean <optional>
false Retrieve data rows or not
mongo MongoClient | string <optional>
Will reuse the Mongo client provided, or if it is a string then will connect to this instance of Mongo.
Returns:
Returns promise resolving to the following:
{
     totalRows: 1, // total rows in result
     totalPages: 1, // total number of pages
     pageSize: 0, // page size
     pageNumber: 1, // returned page number
     search: object, // object used to search the collection
     sort: object, // object used to sort the result
     data: [ ... ], // resulting rows as an array of models
}
Type
Promise