{"_id":"@codeanker/feathers-mongoose","_rev":"5-aef0c5c9bfa62ac5de947f775cfc42ac","name":"@codeanker/feathers-mongoose","dist-tags":{"latest":"6.1.4"},"versions":{"6.1.4":{"name":"@codeanker/feathers-mongoose","description":"A Feathers service adapter for the Mongoose ORM","version":"6.1.4","homepage":"https://github.com/feathersjs-ecosystem/feathers-mongoose","main":"lib/","keywords":["feathers","feathers-plugin","REST","Socket.io","realtime","mongodb","mongo","mongoose","service"],"license":"MIT","repository":{"type":"git","url":"git://github.com/feathersjs-ecosystem/feathers-mongoose.git"},"author":{"name":"Feathers contributors","email":"hello@feathersjs.com","url":"https://feathersjs.com"},"contributors":[{"name":"Eric Kryski","email":"e.kryski@gmail.com","url":"http://erickryski.com"},{"name":"Glavin Wiechert","email":"glavin.wiechert@gmail.com","url":"https://github.com/Glavin001"},{"name":"Marshall Thompson","email":"marshall@creativeideal.net","url":"https://github.com/marshallswain"}],"bugs":{"url":"https://github.com/feathersjs-ecosystem/feathers-mongoose/issues"},"engines":{"node":">= 6"},"scripts":{"publish":"git push origin --tags && npm run changelog && git push origin","changelog":"github_changelog_generator && git add CHANGELOG.md && git commit -am \"Updating changelog\"","release:patch":"npm version patch && npm publish","release:minor":"npm version minor && npm publish","release:major":"npm version major && npm publish","start":"node example/app","lint":"semistandard --fix","mocha":"mocha --opts mocha.opts","coverage":"istanbul cover node_modules/mocha/bin/_mocha -- --opts mocha.opts","test":"npm run lint && npm run coverage"},"semistandard":{"env":["mocha"]},"directories":{"lib":"lib"},"peerDependencies":{"mongoose":"^5.2.5"},"dependencies":{"@feathersjs/commons":"^1.3.0","@feathersjs/errors":"^3.2.0","lodash.omit":"^4.3.0","uberproto":"^2.0.0"},"devDependencies":{"@feathersjs/express":"^1.1.2","@feathersjs/feathers":"^3.0.1","@feathersjs/socketio":"^3.0.1","body-parser":"^1.14.1","chai":"^4.0.0","feathers-service-tests":"^0.10.0","istanbul":"^1.1.0-alpha.1","mocha":"^5.0.0","mongoose":"^5.2.5","semistandard":"^12.0.0","sinon-chai":"^3.0.0","sinon":"^6.0.0"},"gitHead":"8fa06e0689fc1564ad11ca74385efb145588aec8","_id":"@codeanker/feathers-mongoose@6.1.4","_npmVersion":"6.2.0","_nodeVersion":"10.9.0","_npmUser":{"name":"superbarne","email":"barne.juergensen@googlemail.com"},"dist":{"integrity":"sha512-UG01mBgL9EaavoGaDpCHkOLxehCoZqqMzWsUHd428KFALk4Ojk/wwRcUklvXABKvLDrwO5CNCsrhk0DnIwjUFg==","shasum":"d54c9a8d611da858aba4014f917cfba0e329ec65","tarball":"https://registry.npmjs.org/@codeanker/feathers-mongoose/-/feathers-mongoose-6.1.4.tgz","fileCount":9,"unpackedSize":63488,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbm+ZSCRA9TVsSAnZWagAAtjcP/0k6vXw1opwMp+pCXKaE\nJqF/jiYexd16V43d2yVwbmCFtHwY1sP2hmaYqZpTpWVY0y+ONHwyEFxsY+gT\n1S5g/zGM76gbU6x6X7iIiXukI6A0/MIRkpr8dlRPA1Ym50MXmqlY53nF5jlH\ngkFyFnRVFHWoyk3dgHR4hAAchkXbmjnvvNpmHpzFtiQKvKxO9EYqzLaPfen8\nRZfuuswlo/aLAjpm3ow9mMTzuig7uyaEzbxZtsLkaeX7jC4JApqswwyGQGU2\n3dL0sYnGmVXYJoewtADDuvG9dh5dvTyiXH0nmbXAt46J5dFR3Bp8i55o+PIv\ne75m1UMNUdfDylS+vCJgH3OCuYPph9RdznOcnz+cG8mZrayyCrbKDHAnGqLT\nxEnoMqwrQEMnayjVHjmwMoTiYoe5edQdQKTsC8dRL/HWyFqlne4VeWNfBDsX\n7nWE0SnVHCBrD7bxY5axTAC5h0FfD3d7zzcfSWanhusJ1/X2DZtM7soF74dy\n8W11TFQB1SzEwtat35F+ftWWmDj13Ss2rbO6pORgmMVFj1CgTN+xDP02s66i\nfmmZB4GGZUa0pMv0SgPSqBpLhOeOSPh6idT+gQ4/GPtjFHeCJd6Dpd/qAyhI\nx3EAZWVeuGIs1d7T825yFlhyBlBeCNNEWeNShHqoaGg8t+HpXIqdRISVv12C\nAIQC\r\n=tVAE\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGqXwQ7dpEH1sU651d51V0PHmxkibkpR7LxxUnOrpC0AAiEAyapQv+VHdnU9euRQid1Q6kvMmdP0Lfznso+Z5WNqVLk="}]},"maintainers":[{"name":"superbarne","email":"barne.juergensen@googlemail.com"},{"name":"supermomme","email":"momme@juergensen.me"},{"name":"swiatek","email":"daniel.swiatek@codeanker.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/feathers-mongoose_6.1.4_1536943697845_0.43286719653443106"},"_hasShrinkwrap":false}},"time":{"created":"2018-09-14T16:48:17.703Z","6.1.4":"2018-09-14T16:48:17.996Z","modified":"2022-06-08T08:19:53.140Z"},"maintainers":[{"email":"barne.juergensen@googlemail.com","name":"superbarne"},{"email":"daniel.swiatek@codeanker.de","name":"swiatek"}],"description":"A Feathers service adapter for the Mongoose ORM","homepage":"https://github.com/feathersjs-ecosystem/feathers-mongoose","keywords":["feathers","feathers-plugin","REST","Socket.io","realtime","mongodb","mongo","mongoose","service"],"repository":{"type":"git","url":"git://github.com/feathersjs-ecosystem/feathers-mongoose.git"},"contributors":[{"name":"Eric Kryski","email":"e.kryski@gmail.com","url":"http://erickryski.com"},{"name":"Glavin Wiechert","email":"glavin.wiechert@gmail.com","url":"https://github.com/Glavin001"},{"name":"Marshall Thompson","email":"marshall@creativeideal.net","url":"https://github.com/marshallswain"}],"author":{"name":"Feathers contributors","email":"hello@feathersjs.com","url":"https://feathersjs.com"},"bugs":{"url":"https://github.com/feathersjs-ecosystem/feathers-mongoose/issues"},"license":"MIT","readme":"# feathers-mongoose\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/feathersjs-ecosystem/feathers-mongoose.svg)](https://greenkeeper.io/)\n\n[![Build Status](https://travis-ci.org/feathersjs-ecosystem/feathers-mongoose.png?branch=master)](https://travis-ci.org/feathersjs-ecosystem/feathers-mongoose)\n[![Dependency Status](https://img.shields.io/david/feathersjs-ecosystem/feathers-mongoose.svg?style=flat-square)](https://david-dm.org/feathersjs-ecosystem/feathers-mongoose)\n[![Download Status](https://img.shields.io/npm/dm/feathers-mongoose.svg?style=flat-square)](https://www.npmjs.com/package/feathers-mongoose)\n\nA [Feathers](https://feathersjs.com) database adapter for [Mongoose](http://mongoosejs.com/), an object modeling tool for [MongoDB](https://www.mongodb.org/).\n\n```bash\n$ npm install --save mongoose feathers-mongoose\n```\n\n> __Important:__ `feathers-mongoose` implements the [Feathers Common database adapter API](https://docs.feathersjs.com/api/databases/common.html) and [querying syntax](https://docs.feathersjs.com/api/databases/querying.html).\n\n> This adapter also requires a [running MongoDB](https://docs.mongodb.com/getting-started/shell/#) database server.\n\n\n## API\n\n### `service(options)`\n\nReturns a new service instance initialized with the given options. `Model` has to be a Mongoose model. See the [Mongoose Guide](http://mongoosejs.com/docs/guide.html) for more information on defining your model.\n\n```js\nconst mongoose = require('mongoose');\nconst service = require('feathers-mongoose');\n\n// A module that exports your Mongoose model\nconst Model = require('./models/message');\n\n// Make Mongoose use the ES6 promise\nmongoose.Promise = global.Promise;\n\n// Connect to a local database called `feathers`\nmongoose.connect('mongodb://localhost:27017/feathers');\n\napp.use('/messages', service({ Model }));\napp.use('/messages', service({ Model, lean, id, events, paginate }));\n```\n\n__Options:__\n\n- `Model` (**required**) - The Mongoose model definition\n- `lean` (*optional*, default: `true`) - Runs queries faster by returning plain objects instead of Mongoose models.\n- `id` (*optional*, default: `'_id'`) - The name of the id field property.\n- `events` (*optional*) - A list of [custom service events](https://docs.feathersjs.com/api/events.html#custom-events) sent by this service\n- `paginate` (*optional*) - A [pagination object](https://docs.feathersjs.com/api/databases/common.html#pagination) containing a `default` and `max` page size\n- `discriminators` (*optional*) - A list of mongoose models that inherit from `Model`.\n\n> **Important:** To avoid odd error handling behaviour, always set `mongoose.Promise = global.Promise`. If not available already, Feathers comes with a polyfill for native Promises.\n\n<!-- -->\n\n> **Important:** When setting `lean` to `false`, Mongoose models will be returned which can not be modified unless they are converted to a regular JavaScript object via `toObject`.\n\n<!-- -->\n\n> **Note:** You can get access to the Mongoose model via `this.Model` inside a [hook](https://docs.feathersjs.com/api/hooks.html) and use it as usual. See the [Mongoose Guide](http://mongoosejs.com/docs/guide.html) for more information on defining your model.\n\n### params.mongoose\n\nWhen making a [service method](https://docs.feathersjs.com/api/services.html) call, `params` can contain a `mongoose` property which allows you to modify the options used to run the Mongoose query. Normally, this will be set in a before [hook](https://docs.feathersjs.com/api/hooks.html):\n\n```js\napp.service('messages').hooks({\n  before: {\n    patch(context) {\n      // Set some additional Mongoose options\n      // The adapter tries to use these settings by defaults\n      // but they can always be changed here\n      context.params.mongoose = {\n        runValidators: true,\n        setDefaultsOnInsert: true\n      }\n    }\n  }\n});\n```\n\nThe `mongoose` property is also useful for performing upserts on a `patch` request.  \"Upserts\" do an update if a matching record is found, or insert a record if there's no existing match.  The following example will create a document that matches the `data`, or if there's already a record that matches the `params.query`, that record will be updated.\n\n```js\nconst data = { address: '123', identifier: 'my-identifier' }\nconst params = {\n  query: { address: '123' },\n  mongoose: { upsert: true }\n}\napp.service('address-meta').patch(null, data, params)\n```\n\n\n## Example\n\nHere's a complete example of a Feathers server with a `messages` Mongoose service.\n\n```\n$ npm install @feathersjs/feathers @feathersjs/errors @feathersjs/express mongoose feathers-mongoose\n```\n\nIn `message-model.js`:\n\n```js\nconst mongoose = require('mongoose');\n\nconst Schema = mongoose.Schema;\nconst MessageSchema = new Schema({\n  text: {\n    type: String,\n    required: true\n  }\n});\nconst Model = mongoose.model('Message', MessageSchema);\n\nmodule.exports = Model;\n```\n\nThen in `app.js`:\n\n```js\nconst feathers = require('@feathersjs/feathers');\nconst express = require('@feathersjs/express');\nconst socketio = require('@feathersjs/socketio');\n\nconst mongoose = require('mongoose');\nconst service = require('feathers-mongoose');\n\nconst Model = require('./message-model');\n\nmongoose.Promise = global.Promise;\n\n// Connect to your MongoDB instance(s)\nmongoose.connect('mongodb://localhost:27017/feathers');\n\n// Create an Express compatible Feathers application instance.\nconst app = express(feathers());\n\n// Turn on JSON parser for REST services\napp.use(express.json());\n// Turn on URL-encoded parser for REST services\napp.use(express.urlencoded({extended: true}));\n// Enable REST services\napp.configure(express.rest());\n// Enable Socket.io services\napp.configure(socketio());\n// Connect to the db, create and register a Feathers service.\napp.use('/messages', service({\n  Model,\n  lean: true, // set to false if you want Mongoose documents returned\n  paginate: {\n    default: 2,\n    max: 4\n  }\n}));\napp.use(express.errorHandler());\n\n// Create a dummy Message\napp.service('messages').create({\n  text: 'Message created on server'\n}).then(function(message) {\n  console.log('Created message', message);\n});\n\n// Start the server.\nconst port = 3030;\napp.listen(port, () => {\n    console.log(`Feathers server listening on port ${port}`);\n});\n```\n\nYou can run this example by using `node app` and go to [localhost:3030/messages](http://localhost:3030/messages).\n\n## Querying, Validation\n\nMongoose by default gives you the ability to add [validations at the model level](http://mongoosejs.com/docs/validation.html). Using an error handler like the one that [comes with Feathers](https://github.com/feathersjs/feathers-errors/blob/master/src/error-handler.js) your validation errors will be formatted nicely right out of the box!\n\nFor more information on querying and validation refer to the [Mongoose documentation](http://mongoosejs.com/docs/guide.html).\n\n## $populate\n\nFor Mongoose, the special `$populate` query parameter can be used to allow [Mongoose query population](http://mongoosejs.com/docs/populate.html).\n\n```js\napp.service('posts').find({\n  query: { $populate: 'user' }\n});\n```\n\n## Discriminators (Inheritance)\n\nInstead of strict inheritance, Mongoose uses [discriminators](http://mongoosejs.com/docs/discriminators.html) as their schema inheritance model.\nTo use them, pass in a `discriminatorKey` option to your schema object and use `Model.discriminator('modelName', schema)` instead of `mongoose.model()`\n\nFeathers comes with full support for mongoose discriminators, allowing for automatic fetching of inherited types. A typical use case might look like:\n\n```js\nvar mongoose = require('mongoose');\nvar Schema = mongoose.Schema;\nvar Post = require('./post');\nvar feathers = require('@feathersjs/feathers');\nvar app = feathers();\nvar service = require('feathers-mongoose');\n\n// Discriminator key, we'll use this later to refer to all text posts\nvar options = {\n  discriminatorKey: '_type'\n};\n\nvar TextPostSchema = new Schema({\n  text: { type: String, default: null }\n}, options);\n\n// Note the use of `Post.discriminator` rather than `mongoose.discriminator`.\nvar TextPost = Post.discriminator('text', TextPostSchema);\n\n// Using the discriminators option, let feathers know about any inherited models you may have\n// for that service\napp.use('/posts', service({\n  Model: Post,\n  discriminators: [TextPost]\n}))\n\n```\n\nWithout support for discriminators, when you perform a `.get` on the posts service, you'd only get back `Post` models, not `TextPost` models.\nNow in your query, you can specify a value for your discriminatorKey:\n\n```js\n{\n  _type: 'text'\n}\n```\n\nand Feathers will automatically swap in the correct model and execute the query it instead of its parent model.\n\n## License\n\n[MIT](LICENSE)\n\n## Authors\n\n- [Feathers contributors](https://github.com/feathersjs-ecosystem/feathers-mongoose/graphs/contributors)\n","readmeFilename":"README.md"}