Mongoose for SalakJS 2.0, support for multiple mongodb.
$ npm install --save salak-mongo
In plugin:
module.exports = {
plugin: [
{
name: 'mongo',
package: 'salak-mongo'
}
],
mongo: {
client: {
uri: 'mongodb://localhost:27017/yourdb'
}
}
}
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')
}
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
- 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
Ref to mongoose
The function which register on Service
- name {String} model filename
- module {String} the model located in, default: the current module
@return model
MIT