ES2016 decorator functions for building Mongoose models.
npm install mongoose-model-decoratorsES2016 decorator functions for building Mongoose models.
> As of Mongoose 4.7.0, Mongoose includes a loadClass function with which ES classes can be used to define Mongoose models.
> It's a bit different than this module, but it may suit your needs.
> See the docs for more.
Installation - Usage - API -
Translations - Licence
``bash`
npm install --save mongoose-model-decorators
Currently, there is no official Babel transformer for decorators. To use the
@Model decorator syntax, you need to add the decorators-legacy transformer.babelrc
to your or other Babel configuration.
`bash`
npm install --save-dev babel-plugin-transform-decorators-legacy
`js
import mongoose from 'mongoose'
import { Model } from 'mongoose-model-decorators'
@Model
class Channel {
static schema = {
channelName: { type: String, index: true },
channelTopic: String,
users: Array,
favorited: { type: Boolean, default: false }
}
get summary () {
const users = this.users.length
return ${this.channelName} : ${this.channelTopic} (${users} active)
}
}
Channel.findOne({ channelName: '#mongoose' }).then(channel =>
console.log(channel.summary)
// → "#mongoose: Now with class syntax! (7 active)"
})
`
Creates a Mongoose Schema from a Class
definition.
Define your schema in a static schema property. The contents of that property
will be passed to the Mongoose Schema constructor.
`js`
@Schema
class User {
static schema = {
name: String,
age: Number,
email: { type: String, required: true }
}
}
You can also define a configureSchema method which will be called on the schemamongoose-model-decorators
after it is instantiated, so you can do anything to it that may not be supported
by otherwise:
`js`
@Schema
class User {
static configureSchema (schema) {
schema.query.byName = function (name) {
return this.find({ username: name })
}
schema.index({ username: 1, joinedAt: 1 }, { unique: true })
}
}
Creates a Mongoose Schema from a Class
definition.
The possible options are passed straight to the Mongoose Schema constructor.
Options defined in the options object take precedence over options that were
defined as static properties on the Schema class. Thus:
`js`
@Schema({ collection: 'vip_users' })
class User {
static autoIndex = false
static collection = 'users'
}
…results in { autoIndex: false, collection: 'vip_users' } being passed to
Mongoose.
Creates a Mongoose schema from a class definition, and defines it on the global
mongoose connection.
You can specify the Mongoose connection to attach the model to in the
connection option (defaults to mongoose). Other options are passed straight@Schema
through to .
`js`
@Model({ connection: myConnection, collection: 'best_users' })
class User {
// …
}
is equivalent to:
`js`
@Schema({ collection: 'best_users' })
class UserSchema {
// …
}
myConnection.model('User', new UserSchema)
And without a connection option:
`js`
@Model
class User { / … / }
is equivalent to:
`js`
@Schema
class UserSchema { / … / }
require('mongoose').model('User', new UserSchema)
Alias to @Schema. This one reads a bit nicer if you're not using decorators:
`js`
const UserSchema = createSchema(UserClass)
Alias to @Model. Reads a bit nicer if you're not using decorators:
`js`
const UserModel = createModel({ collection: 'best_users' })(UserClass)
If your project configuration doesn't support decorators, you can still use most
mongoose-model-decorators translations. Instead of using the @Decorator
syntax, you can call the decorator as a function, passing the class definition:
`js
import { createSchema, createModel } from 'mongoose-model-decorators'
class UserTemplate {
static schema = {
// ...
}
getFriends() {
// ...
}
}
// then use any of:
const UserSchema = createSchema(UserTemplate)
const UserSchema = createSchema()(UserTemplate)
const UserSchema = createSchema(options)(UserTemplate)
// or even:
const UserSchema = createSchema(class {
// ...
})
// or for models:
const User = createModel(UserTemplate)
const User = createModel()(UserTemplate)
const User = createModel(options)(UserTemplate)
// or even:
createModel(class User {
// ...
})
`
mongoose-model-decorators` translates as many ES2015 class things to their
Mongoose Schema and Model equivalents as possible, as transparently as possible.
| Feature | mongoose-model-decorators | plain Mongoose |
|---|---|---|
| Instance methods | | |
| Instance getters | | |
| Instance setters | | |
| Pre/post hooks | | |
| Static methods | | |
| Static properties | static prop = 'SOME_CONSTANT' | Static properties are a bit hacky, because Mongoose doesn't have a shorthand for them (only for static methods). They work well though :) NB: static properties that are also Schema options are also copied. |
| Static getters and setters | | |