Code Monkey home page Code Monkey logo

egg-mongoose's Introduction

egg-mongoose

mongoose plugin in egg NPM version build status Test coverage David deps Known Vulnerabilities npm download

Egg's mongoose plugin.

Install

$ npm i egg-mongoose --save # (just use npm link)

Usage

// {app_root}/config/plugin.js
exports.mongoose = {
  enable: true,
  package: 'egg-mongoose',
};

Configuration

// {app_root}/config/config.default.js
exports.mongoose = {
  url: 'mongodb://127.0.0.1/example',
  options: {}
};

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

Example

// app/model/user.js
module.exports = mongoose => {
  const UserSchema = new mongoose.Schema({
    userName: { type: String  },
    password: { type: String  }
  });

  return mongoose.model('User', UserSchema);
}

// app/controller/user.js
exports.index = function* () {
  this.body = yield this.model.user.find({});
}

Questions & Suggestions

Please open an issue here.

License

MIT

egg-mongoose's People

Contributors

villins avatar

Stargazers

Wade Deng avatar

Watchers

 avatar

egg-mongoose's Issues

this 下没有 model?

"egg-mongoose": "^1.1.1"

基本按照 README 来的。

// config/plugin.js
exports.mongoose = {
  enable: true,
  package: 'egg-mongoose',
};

// config/config.default.js

module.exports = appInfo => {
  const config = {};

  // should change to your own
  config.keys = appInfo.name + 'xxx';

  config.mongoose = {
    url: 'mongodb://127.0.0.1/xx',
    options: {},
  };

  return config;
};

// app/model/user.js

module.exports = mongoose => {
  const UserSchema = new mongoose.Schema({
    _id: { type: mongoose.Schema.Types.ObjectId },
    // ...
  });

  return mongoose.model('User', UserSchema);
};

// app/controller/home
module.exports = app => {
  class HomeController extends app.Controller {
    async index() {
      const { ctx, service, model } = this;
      console.log(model); // undefined
      ctx.body = {
        message: 'success',
      };
    }
  }

  return HomeController;
}

输出 undefined
漏了什么东西吗?

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.