dc-api-mongo
v0.2.4-1
Published
Mongoose based MongoDB driver for dc-api-core
Downloads
199
Readme
Mongoose based MongoDB driver for dc-api-core
Provides mongo
database driver and session store.
Dependencies
Installation
- Install package -
npm i dc-api-mongo --save
oryarn add dc-api-mongo
- Add
dc-api-mongo
toplugins
array inconfig.json
- Fill
db
field inconfig.json
by template - Create directory
models
in back-end root - Create directory
mongo
inmodels
- Done!
config.json
template
| Field | Default | Description |
|----------------------|---------------|-------------------------------|
| db.mongo.uri
| Autogenerated | MongoDB URI |
| db.mongo.host
| Required | Database hostname |
| db.mongo.name
| Required | Database name |
| db.mongo.port
| 27017
| Database port |
| db.mongo.user
| Optional | Database username |
| db.mongo.pass
| | and password |
| db.mongo.nonStrict
| []
| List of models without schema |
| db.mongo.srv
| false
| If true
using SRV record |
| db.mongo.*
| Optional | Query parameters in URI |
Example configuration
{
"db": {
"mongo.billing": {
"uri": " mongodb+srv://admin:[email protected]/billing?authSource=admin&retryWrites=true&w=majority"
},
"mongo.control-panel"
"host": "project-00000.provider.mongodb.net",
"user": "admin",
"pass": "password",
"name": "control-panel",
"srv": true,
"authSource": "admin",
"retryWrites": true,
"w": "majority"
}
}
}
Creating model
- Create file
ModelName.js
inmodels/mongo
directory - Fill model file.
- Done!
Example:
module.exports = {
// Any mongoose schema constuctions allowed
field: { type: String, required: true },
otherField: { type: Number, default: Date.now() + 1000 * 60 },
// Second argument of schema constuctor
// new Schema(..., $options)
$options: { strict: false },
// Register default auto-increment field "uid"
$increment: 'uid',
// Same with options
$increment: { field: 'uid', start: 1 },
// Mongoose virtual fields
$virtuals: {
timestamp: {
// schema.virtual('timestamp').get(<this function>)
get () {
// `document.timestamp` will return creation time in ms
return this._id.getTimestamp().getTime();
}
}
}
};
Other examples you can find in Mongoose Docs.