{"id":19020246,"url":"https://github.com/trailsjs/trailpack-mongoose","last_synced_at":"2025-08-23T14:13:08.140Z","repository":{"id":57378848,"uuid":"55272506","full_name":"trailsjs/trailpack-mongoose","owner":"trailsjs","description":":package: Mongoose.js Trailpack http://mongoosejs.com","archived":false,"fork":false,"pushed_at":"2018-03-17T13:23:56.000Z","size":89,"stargazers_count":8,"open_issues_count":4,"forks_count":11,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-08-09T17:54:08.902Z","etag":null,"topics":["mongodb","mongoose","trailpack","trails"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"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/trailsjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2016-04-02T01:45:07.000Z","updated_at":"2017-11-18T22:09:46.000Z","dependencies_parsed_at":"2022-09-02T21:21:07.947Z","dependency_job_id":null,"html_url":"https://github.com/trailsjs/trailpack-mongoose","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/trailsjs/trailpack-mongoose","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-mongoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-mongoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-mongoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-mongoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trailsjs","download_url":"https://codeload.github.com/trailsjs/trailpack-mongoose/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-mongoose/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271722251,"owners_count":24809662,"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","status":"online","status_checked_at":"2025-08-22T02:00:08.480Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["mongodb","mongoose","trailpack","trails"],"created_at":"2024-11-08T20:16:16.867Z","updated_at":"2025-08-23T14:13:08.119Z","avatar_url":"https://github.com/trailsjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# trailpack-mongoose\n:package: Mongoose.js Trailpack [http://mongoosejs.com](http://mongoosejs.com)\n\n[npm-image]: https://img.shields.io/npm/v/trailpack-mongoose.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/trailpack-mongoose\n[ci-image]: https://img.shields.io/travis/trailsjs/trailpack-mongoose/master.svg?style=flat-square\n[ci-url]: https://travis-ci.org/trailsjs/trailpack-mongoose\n[daviddm-image]: http://img.shields.io/david/trailsjs/trailpack-mongoose.svg?style=flat-square\n[daviddm-url]: https://david-dm.org/trailsjs/trailpack-mongoose\n[codeclimate-image]: https://img.shields.io/codeclimate/github/trailsjs/trailpack-mongoose.svg?style=flat-square\n[codeclimate-url]: https://codeclimate.com/github/trailsjs/trailpack-mongoose\n[gitter-image]: http://img.shields.io/badge/+%20GITTER-JOIN%20CHAT%20%E2%86%92-1DCE73.svg?style=flat-square\n[gitter-url]: https://gitter.im/trailsjs/trails\n\n[![Gitter][gitter-image]][gitter-url]\n[![NPM version][npm-image]][npm-url]\n[![Build status][ci-image]][ci-url]\n[![Dependency Status][daviddm-image]][daviddm-url]\n[![Code Climate][codeclimate-image]][codeclimate-url]\n\n\nLoads Application Models (in `api/models`) into the Mongoose ORM; Integrates with [trailpack-router](https://github.com/trailsjs/trailpack-router) to\ngenerate Footprints for routes.\n\n## Usage\n\n### Include trailpack in app.\n\n```js\n// config/main.js\nmodule.exports = {\n  // ...\n  packs: [\n    require('trailpack-mongoose')\n  ]\n}\n```\n\n### Configure stores.\n\n```js\n// config/database.js\nmodule.exports = {\n\n  /**\n   * Define the database stores. A store is typically a single database.\n   *\n   * Use the SQLite3 by default for development purposes.\n   *\n   * Set production connection info in config/env/production.js\n   */\n  stores: {\n\n    /**\n     * Define a store called \"local\" which uses SQLite3 to persist data.\n     */\n    someteststore: {\n      //migration\n      migrate: 'create',\n      // Mongodb URI\n      uri: 'mongodb://localhost:27017/test',\n      // Mongoose connection options\n      options: {\n\n      }\n    }\n  },\n\n  models: {\n    defaultStore: 'someteststore',\n    migrate: 'drop'\n  }\n}\n\n```\n\n### Models\n\n#### Options\n\n##### Model::Schema\n\nSee [Mongoose Documentation for Schemas](http://mongoosejs.com/docs/guide.html).\n\n##### Model::Config\n\n| Option | Type | Default | Description |\n| --- | --- | --- | --- |\n| tableName | String | `modelName.toLowerCase()` | Name of collection to use with this model. For example: MyModel.js defaults to collection named `mymodel`\n| store | String | `app.config.database.models.defaultStore` | Datastore to use for this model; specify the name of one of the stores in `app.config.database.stores`.\n| migrate | String | | Migrate *must* be set to one of `[ 'none', 'drop', 'create' ]`\n| schema | Object | `{}` | [Schema Options](http://mongoosejs.com/docs/guide.html#options) to pass into Mongoose's Schema constructor.\n| statics | Object | `{}` | [Static methods](http://mongoosejs.com/docs/guide.html#statics) to add to the Model.\n| methods | Object | `{}` | [Instance methods](http://mongoosejs.com/docs/guide.html#methods) to add to this model's documents.\n| onSchema | Function | undefined | Funcion which is useful to for adding schema middleware, virtuals, or indexes.\n\n#### Example\n\n```js\n// Use default Schema from Mongoose. See http://mongoosejs.com/docs/schematypes.html\nmodule.exports = class User extends Model {\n\n  static schema (app, Mongoose) {\n    return {\n      username: String,\n      childs: [{\n        type: Mongoose.Schema.ObjectId,\n        ref: 'UserSchema'\n      }],\n      email: {\n        type: String,\n        required: true,\n        unique: true,\n        lowercase: true,\n        trim: true,\n        validate: {\n          validator: function (val) {\n            return isEmail(val)\n          },\n          message: '{VALUE} is not a valid email'\n        }\n      }\n    }\n  }\n\n  static config (app, Mongoose) {\n    return {\n      // Collection name\n      tableName: 'users',\n\n      // Schema options\n      schema: {\n        timestamps: true,\n\n        versionKey: false,\n\n        toObject: {\n          virtuals: true\n        },\n\n        toJSON: {\n          virtuals: true\n        }\n      },\n      // Schema statics\n      statics: {\n        getByEmail: function (email) {\n          return this\n            .findOne({\n              email: _.trim(email)\n            })\n            .exec()\n        },\n      },\n\n      // Schema methods\n      methods: {\n        toJSON: function () {\n          const user = this.toObject()\n          delete user.password\n\n          return user\n        }\n      },\n      \n      /**\n        * After Trails.js will create model Schema you could add anything you want here\n        * @param  {TrailsApp} app TrailsJs app object\n        * @param  {mongoose.Schema} schema mongoose new Schema object\n      */\n      onSchema (app, schema) {\n          // virtuals\n          schema.virtual('name.full').get(function () {\n            return this.name.first + ' ' + this.name.last\n          })\n          // lifecircle events\n          schema.pre('save', function (next) {\n            // performing actions\n            next()\n          })\n          \n          // text indexes\n          schema.index(\n            { \n              username: 'text',\n              email: 'text'\n            },\n            { \n              weights : {\n                username : 10,\n                email: 5\n              },\n              name: \"usersIndex\"\n            }\n          )\n        }\n    }\n  }\n}\n```\n\n### Query\n\n```js\n// api/services/UserService.js\nmodule.exports = class UserService extends Service {\n  /**\n   * Finds people with the given email.\n   * @return Promise\n   * @example {\n   *    name: 'Ludwig Beethoven',\n   *    email: 'someemail@email.com',\n   *    favoriteColors: [\n   *      { name: 'yellow', hex: 'ffff00' },\n   *      { name: 'black', hex: '000000' }\n   *     ]\n   * }\n   */\n  findUser (email) {\n    return this.orm.User.find({ email: email })\n      .exec()\n  }\n}\n```\n\n## Contributing\nWe love contributions! Please check out our [Contributor's Guide](https://github.com/trailsjs/trails/blob/master/CONTRIBUTING.md) for more\ninformation on how our projects are organized and how to get started.\n\n\n## License\n[MIT](https://github.com/trailsjs/trailpack-mongoose/blob/master/LICENSE)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailsjs%2Ftrailpack-mongoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrailsjs%2Ftrailpack-mongoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailsjs%2Ftrailpack-mongoose/lists"}