Skip to content

SalakJS/salak-mongo

Repository files navigation

salak-mongo

NPM version build status David deps NPM download

Mongoose for SalakJS 2.0, support for multiple mongodb.

Install

$ npm install --save salak-mongo

Usage

Config

In plugin:

module.exports = {
  plugin: [
    {
      name: 'mongo',
      package: 'salak-mongo'
    }
  ],
  mongo: {
    client: {
      uri: 'mongodb://localhost:27017/yourdb'
    }
  }
}

Model

Salak will autoload the model directory from all modules.

Such as common/model/user.js:

module.exports = ({ db, Schema }, app) => {
  const User = new Schema({
    username: {
      type: String,
      unique: true
    }
  })

  return db.model('User', User, 'User')
}

Use in Service or Controller

common/service/user.js

const { Service } = require('salak')

class User extends Service {
  async createUser (username) {
    const user = await this.model('user').create({
      username
    })

    return user
  }
}

module.exports = User

API

Options

  • dirname {String} directory for storing models, default model
  • extend {Array} extend properties, default ['service']
  • client {Object} mongoose connection info, like { uri: '', options: {} }
  • client.options {Object} default { useNewUrlParser: true, server: { reconnectTries: 60 * 60 * 24, reconnectInterval: 1000 } }
  • clients {Object} mongoose connections, { key: client }
  • options {Object} mongoose global connection options

mongoose

Ref to mongoose

Service.prototype.model(name, module)

The function which register on Service

  • name {String} model filename
  • module {String} the model located in, default: the current module

@return model

License

MIT

About

Mongoose for SalakJS.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published