Giter Club home page Giter Club logo

salak-mongo's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.