1.0.10 • Published 8 years ago

@gongt/egg-super-mongo v1.0.10

Weekly downloads
-
License
-
Repository
github
Last release
8 years ago

@gongt/egg-super-mongo

NPM version David deps npm download

Egg's MongoDB plugin.

Install

$ npm i @gongt/egg-super-mongo --save

Usage

// {app_root}/config/plugin.js
exports.superMongo = {
  enable: true,
  package: '@gongt/egg-super-mongo',
};

Configuration

// {app_root}/config/config.default.js
exports.mongoose = {
  client: {
    url: 'mongodb://127.0.0.1/example',
  },
  clients: {
    connection_a: {
      url: 'mongodb://172.0.0.1/a',
    },
    connection_b: {
      url: 'mongodb://192.168.1.1/b',
    }
  },
  app: true,
  agent: false,
  allowOverwride: true,
};

see config/config.default.ts for more detail.

Example

// app/super-model/user.js
import {EggMongoose} from "@gongt/egg-super-mongo";
import {Schema} from "mongoose";

export class UserModel extends EggMongoose {
  get connection() { 
    return "connection_a";
  }
  
  get schema() {
    return new Schema({
      userName: { type: String },
      password: { type: String }
    });
  }
  
  getUserById(id){
    this.__model.findOne({ _id: id }); // ...
  }
}

Example (es5)

// app/super-model/user.js
module.exports = app => {
  const EggMongoose = app.SuperMongo;
  
  return class UserModel extends EggMongoose {
    /* ...... */
  };
}

// app/controller/user.js
exports.index = async function (ctx) {
  ctx.body = await ctx.model.user.getUserById("");  // you should use camel case to access mongoose model
}

License

MIT

1.0.10

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago