项目作者: DimaCrafter

项目描述 :
MongoDB driver for dc-api-core
高级语言: JavaScript
项目地址: git://github.com/DimaCrafter/dc-api-mongo.git
创建时间: 2018-12-10T13:17:09Z
项目社区:https://github.com/DimaCrafter/dc-api-mongo

开源协议:MIT License

下载


Mongoose based MongoDB driver for dc-api-core

NPM

Provides mongo database driver and session store.

Dependencies


Installation

1) Install package - npm i dc-api-mongo --save or yarn add dc-api-mongo
2) Add dc-api-mongo to plugins array in config.json
3) Fill db field in config.json by template
4) Create directory models in back-end root
5) Create directory mongo in models
6) 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

  1. {
  2. "db": {
  3. "mongo.billing": {
  4. "uri": " mongodb+srv://admin:password@project-00000.provider.mongodb.net/billing?authSource=admin&retryWrites=true&w=majority"
  5. },
  6. "mongo.control-panel"
  7. "host": "project-00000.provider.mongodb.net",
  8. "user": "admin",
  9. "pass": "password",
  10. "name": "control-panel",
  11. "srv": true,
  12. "authSource": "admin",
  13. "retryWrites": true,
  14. "w": "majority"
  15. }
  16. }
  17. }

Creating model

1) Create file ModelName.js in models/mongo directory
2) Fill model file.
3) Done!

Example:

  1. module.exports = {
  2. // Any mongoose schema constuctions allowed
  3. field: { type: String, required: true },
  4. otherField: { type: Number, default: Date.now() + 1000 * 60 },
  5. // Second argument of schema constuctor
  6. // new Schema(..., $options)
  7. $options: { strict: false },
  8. // Register default auto-increment field "uid"
  9. $increment: 'uid',
  10. // Same with options
  11. $increment: { field: 'uid', start: 1 },
  12. // Mongoose virtual fields
  13. $virtuals: {
  14. timestamp: {
  15. // schema.virtual('timestamp').get(<this function>)
  16. get () {
  17. // `document.timestamp` will return creation time in ms
  18. return this._id.getTimestamp().getTime();
  19. }
  20. }
  21. }
  22. };

Other examples you can find in Mongoose Docs.