new MongoDBAdapter(opts)
MongoDBAdapter class.
Parameters:
Name | Type | Argument | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts |
Object |
<optional> |
Configuration opts. Properties
|
Example
// Use Container instead of DataStore on the server import {Container} from 'js-data' import MongoDBAdapter from 'js-data-mongodb' // Create a store to hold your Mappers const store = new Container({ mapperDefaults: { // MongoDB uses "_id" as the primary key idAttribute: '_id' } }) // Create an instance of MongoDBAdapter with default settings const adapter = new MongoDBAdapter() // Mappers in "store" will use the MongoDB adapter by default store.registerAdapter('mongodb', adapter, { default: true }) // Create a Mapper that maps to a "user" collection store.defineMapper('user')
Members
-
client :Object
-
A Promise that resolves to a reference to the MongoDB client being used by this adapter.
Type:
- Object
-
debug :boolean
-
Whether to log debugging information.
Type:
- boolean
-
findOneOpts :Object
-
Default options to pass to collection#findOne.
Type:
- Object
-
findOpts :Object
-
Default options to pass to collection#find.
Type:
- Object
-
insertManyOpts :Object
-
Default options to pass to collection#insertMany.
Type:
- Object
-
insertOpts :Object
-
Default options to pass to collection#insert.
Type:
- Object
-
raw :boolean
-
Whether to return detailed result objects instead of just record data.
Type:
- boolean
-
removeOpts :Object
-
Default options to pass to collection#update.
Type:
- Object
-
translateId :boolean
-
Convert ObjectIDs to strings when pulling records out of the database.
Type:
- boolean
-
updateOpts :Object
-
Default options to pass to collection#update.
Type:
- Object
-
uri :string
-
MongoDB URI.
Type:
- string
Methods
-
_translateId()
-
Translate ObjectIDs to strings.
Returns:
- Type
- *
-
afterCreate()
-
afterCreateMany()
-
afterDestroy()
-
afterDestroyAll()
-
afterFind()
-
afterFindAll()
-
afterUpdate()
-
afterUpdateAll()
-
afterUpdateMany()
-
beforeCreate()
-
beforeCreateMany()
-
beforeDestroy()
-
beforeDestroyAll()
-
beforeFind()
-
beforeFindAll()
-
beforeUpdate()
-
beforeUpdateAll()
-
beforeUpdateMany()
-
create(mapper, props, opts)
-
Create a new record.
Parameters:
Name Type Argument Description mapper
Object The mapper.
props
Object The record to be created.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description insertOpts
Object <optional>
Options to pass to collection#insert.
raw
boolean <optional>
false Whether to return a more detailed response object.
Returns:
- Type
- Promise
-
createMany(mapper, props, opts)
-
Create multiple records in a single batch.
Parameters:
Name Type Argument Description mapper
Object The mapper.
props
Object The records to be created.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description insertManyOpts
Object <optional>
Options to pass to collection#insertMany.
raw
boolean <optional>
false Whether to return a more detailed response object.
Returns:
- Type
- Promise
-
dbg()
-
destroy(Resource, id, opts)
-
Destroy the record with the given primary key.
Parameters:
Name Type Argument Description Resource
Object The Resource.
id
string | number Primary key of the record to destroy.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description raw
boolean <optional>
false Whether to return a more detailed response object.
removeOpts
Object <optional>
Options to pass to collection#remove.
Returns:
- Type
- Promise
-
destroyAll(mapper, query, opts)
-
Destroy the records that match the selection query.
Parameters:
Name Type Argument Description mapper
Object the mapper.
query
Object <optional>
Selection query.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description raw
boolean <optional>
false Whether to return a more detailed response object.
removeOpts
Object <optional>
Options to pass to collection#remove.
Returns:
- Type
- Promise
-
find(mapper, id, opts)
-
Retrieve the record with the given primary key.
Parameters:
Name Type Argument Description mapper
Object The mapper.
id
string | number Primary key of the record to retrieve.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description findOneOpts
Object <optional>
Options to pass to collection#findOne.
raw
boolean <optional>
false Whether to return a more detailed response object.
with
Array.<string> <optional>
[] Relations to eager load.
Returns:
- Type
- Promise
-
findAll(mapper, query, opts)
-
Retrieve the records that match the selection query.
Parameters:
Name Type Argument Description mapper
Object The mapper.
query
Object Selection query.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description findOpts
Object <optional>
Options to pass to collection#find.
raw
boolean <optional>
false Whether to return a more detailed response object.
with
Array.<string> <optional>
[] Relations to eager load.
Returns:
- Type
- Promise
-
getClient()
-
Return a Promise that resolves to a reference to the MongoDB client being used by this adapter.
Useful when you need to do anything custom with the MongoDB client library.
Returns:
MongoDB client.
- Type
- Object
-
getOpt(opt, opts)
-
Resolve the value of the specified option based on the given options and this adapter's settings.
Parameters:
Name Type Argument Description opt
string The name of the option.
opts
Object <optional>
Configuration options.
Returns:
The value of the specified option.
- Type
- *
-
getQuery()
-
Map filtering params in a selection query to MongoDB a filtering object.
Handles the following:
- where
- and bunch of filtering operators
Returns:
- Type
- Object
- where
-
getQueryOptions()
-
Map non-filtering params in a selection query to MongoDB query options.
Handles the following:
- limit
- skip/offset
- orderBy/sort
Returns:
- Type
- Object
-
loadBelongsTo()
-
Load a belongsTo relationship.
Returns:
- Type
- Promise
-
loadHasMany()
-
Load a hasMany relationship.
Returns:
- Type
- Promise
-
loadHasOne()
-
Load a hasOne relationship.
Returns:
- Type
- Promise
-
log()
-
Logging utility method.
-
makeBelongsToForeignKey()
-
Return the foreignKey from the given record for the provided relationship.
Returns:
- Type
- *
-
makeHasManyForeignKey()
-
Return the foreignKey from the given record for the provided relationship.
If the foreignKeys in your database are saved as ObjectIDs, then override this method and change it to something like:
return this.toObjectID(mapper, this.constructor.prototype.makeHasManyForeignKey.call(this, mapper, def, record))
There may be other reasons why you may want to override this method, like when the id of the parent doesn't exactly match up to the key on the child.
Returns:
- Type
- *
-
makeHasManyForeignKeys()
-
Return the foreignKeys from the given record for the provided relationship.
Returns:
- Type
- *
-
toObjectID()
-
Turn an _id into an ObjectID if it isn't already an ObjectID.
Returns:
- Type
- *
-
update(mapper, id, props, opts)
-
Apply the given update to the record with the specified primary key.
Parameters:
Name Type Argument Description mapper
Object The mapper.
id
string | number The primary key of the record to be updated.
props
Object The update to apply to the record.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description raw
boolean <optional>
false Whether to return a more detailed response object.
updateOpts
Object <optional>
Options to pass to collection#update.
Returns:
- Type
- Promise
-
updateAll(mapper, props, query, opts)
-
Apply the given update to all records that match the selection query.
Parameters:
Name Type Argument Description mapper
Object The mapper.
props
Object The update to apply to the selected records.
query
Object <optional>
Selection query.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description raw
boolean <optional>
false Whether to return a more detailed response object.
updateOpts
Object <optional>
Options to pass to collection#update.
Returns:
- Type
- Promise
-
updateMany()
-
Not supported.