{"id":13781579,"url":"https://github.com/thinkjs/think-mongoose","last_synced_at":"2025-04-06T17:31:37.429Z","repository":{"id":24120914,"uuid":"100557219","full_name":"thinkjs/think-mongoose","owner":"thinkjs","description":"Wrap mongoose for ThinkJS 3.x","archived":false,"fork":false,"pushed_at":"2023-02-01T16:00:57.000Z","size":290,"stargazers_count":13,"open_issues_count":7,"forks_count":3,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-02T15:01:45.330Z","etag":null,"topics":["mongoose","think-extend","think-model"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thinkjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-17T03:27:40.000Z","updated_at":"2022-02-11T13:11:06.000Z","dependencies_parsed_at":"2023-02-17T07:30:59.800Z","dependency_job_id":null,"html_url":"https://github.com/thinkjs/think-mongoose","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-mongoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-mongoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-mongoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-mongoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thinkjs","download_url":"https://codeload.github.com/thinkjs/think-mongoose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247522316,"owners_count":20952520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongoose","think-extend","think-model"],"created_at":"2024-08-03T18:01:27.297Z","updated_at":"2025-04-06T17:31:37.068Z","avatar_url":"https://github.com/thinkjs.png","language":"JavaScript","readme":"# think-mongoose\n\n[![npm](https://img.shields.io/npm/v/think-mongoose.svg?style=flat-square)]()\n[![Travis](https://img.shields.io/travis/thinkjs/think-mongoose.svg?style=flat-square)]()\n\u003c!-- [![Coveralls](https://img.shields.io/coveralls/thinkjs/think-mongoose/master.svg?style=flat-square)]() --\u003e\n\u003c!-- [![David](https://img.shields.io/david/thinkjs/think-mongoose.svg?style=flat-square)]() --\u003e\n\nWrap mongoose for ThinkJS 3.x\n\n## Install\n\n```sh\nnpm install think-mongoose --save\n```\n\n## How to use\n\n### Config extend\n\nChange file `src/config/extend.js` (in multi module project, file is `src/common/config/extend.js`), add config:\n\n```js\nconst mongoose = require('think-mongoose');\n\nmodule.exports = [\n  mongoose(think.app)\n]\n```\n\nWhen add mongoose extend, it will add some below methods:\n\n* `think.Mongoose` {Class} Model definition base class, model class must be extends this class and implement get schama().\n* `think.mongoose` {Function} get mongoose instance\n* `ctx.mongoose` {Function} get mongoose instance, it's wrapped from think.mongoose\n* `controller.mongoose` {Function} get mongoose instance, it's wrapped from think.mongoose\n* `service.mongoose` {Function} get mongoose instance, it's wrapped from think.mongoose\n\n### Config adapter\n\nChange file `src/config/adapter.js` (in multi module project, file is `src/common/config/adapter.js`), add config:\n\n```js\nexports.model = {\n  type: 'mongoose',\n  mongoose: {\n    host: '127.0.0.1',\n    user: '',\n    password: '',\n    database: 'test',\n    useCollectionPlural: false,\n    options: {}\n  }\n}\n```\n\nBy default mongoose pluralizes the model name, if you don't want the default action, set `useCollectionPlural: false`.\n\nor config connection string:\n\n```js\nexports.model = {\n  type: 'mongoose',\n  mongoose: {\n    connectionString: 'mongodb://user:pass@localhost:port/database',\n    options: {\n      config: {\n        autoIndex: false\n      }\n    }\n  }\n}\n```\n\n### Create model class\n\nCreate model class extends from `think.Mongoose`, like:\n\n```js\n// src/model/user.js\n\nconst {SchameTypes} = require('mongoose');\n\nmodule.exports = class extends think.Mongoose {\n  get schema() {\n    return {\n      name:    String,\n      binary:  Buffer,\n      living:  Boolean,\n      updated: { type: Date, default: Date.now },\n      age:     { type: Number, min: 18, max: 65 },\n      mixed:   Schema.Types.Mixed,\n      _someId: Schema.Types.ObjectId,\n      array:      [],\n      ofMixed:    [SchameTypes.Mixed],\n      ofObjectId: [SchameTypes.ObjectId],\n      ofArrays:   [[]],\n      ofArrayOfNumbers: [[Number]],\n      nested: {\n        stuff: { type: String, lowercase: true, trim: true }\n      }\n    }\n  }\n}\n```\n\nSometime, you want to add some methods to schema, then you can get schema instance by yourself and return instance:\n\n```js\nimport {Schema} from 'mongoose';\n\nmodule.exports = class extends think.Mongoose {\n  get schema() {\n    const schema = new Schema({ name: String, type: String });\n    schema.methods.findSimilarTypes = function(cb) {\n      return this.model('think_Animal').find({ type: this.type }, cb); // model name `think_Animal` must have table prefix\n    };\n    return schema;\n  }\n}\n```\n\n### Get model instance\n\nYou can get mongoose class instance by `think.mongoose`, `ctx.mongoose`, `service.mongoose` or `controller.mongoose`.\n\n```js\nmodule.exports = class extends think.Controller {\n  async indexAction() {\n    const user = this.mongoose('user');\n    const data = await user.find();\n  }\n}\n```\nIf method is defined in schema, then you must be get class instance by yourself\n\n```js\nmodule.exports = class extends think.Controller {\n  async indexAction() {\n    const Cls = this.mongoose('user');\n    const instance = new Cls();\n    const data = await user.findSimilarTypes(); // findSimilarTypes method is defined in schema.methods.findSimilarTypes\n  }\n}\n```\n\n\nIf default model adapter type is not `mongoose`, must be set second argument when get instance by `think.mongoose`, such as:\n\n```js\nmodule.exports = class extends think.Controller {\n  async indexAction() {\n    const user = this.mongoose('user', 'mongoose'); // use `mongoose` adapter type\n    const data = await user.find();\n  }\n}\n```\n\n### CRUD\n\nYou can use all of mongoose model methods (except `new model \u0026 save`) to query or execute documents. Read documents \u003chttp://mongoosejs.com/docs/guide.html\u003e get more information.\n","funding_links":[],"categories":["Extends"],"sub_categories":["websocket"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkjs%2Fthink-mongoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthinkjs%2Fthink-mongoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkjs%2Fthink-mongoose/lists"}