I want to write my mongoose model in ES6. Basically replace module.exports
and other ES5 things wherever possible. Here is what I have.
import mongo
For those who find this searching around, the original question seems pretty valid to me. I'm using Babel transpiling ES6+ down to 5. My custom mongoose methods did not play well with my async/await code in my calling class. Notably this
was null
in my instance methods. Using the solution provided here, I was able to arrive at this solution that hopefully helps others searching around.
import mongoose from 'mongoose'
class Tenant extends mongoose.Schema {
constructor() {
const tenant = super({
pg_id: Number,
name: String,
...
})
tenant.methods.getAccountFields = this.getAccountFields
tenant.methods.getCustomerTypes = this.getCustomerTypes
tenant.methods.getContactFields = this.getContactFields
...
tenant.methods.getModelFields = this.getModelFields
return tenant
}
getAccountFields() {
return this.getModelFields(this.account_fields_mapping)
}
getCustomerTypes() {
//code
}
getContactFields() {
//code
}
...
getModelFields(fields_mapping) {
//code
}
}
export default mongoose.model('Tenant', new Tenant)
To do things the ES6, class-like way, as the question states, I simply had to invoke the class with new
in the exported mongoose.model
function.
export default mongoose.model('Blacklist', new Blacklist)
This will work:
import mongoose from 'mongoose';
const { Schema } = mongoose;
const userSchema = new Schema({
email: {
type: String,
required: true
},
firstName: {
type: String,
},
lastName: {
type: String
},
age: {
type: Number
}
});
export default mongoose.model('User', userSchema);
Why would you want to do it? mongoose.Schema
is not expected to be used in this way. It doesn't use inheritance.
mongoose.Schema
is a constructor that takes an object as the first parameter both in ES5 and ES6. No need for ES6 classes here.
Thus even with ES6 the proper way is to have:
const Blacklist = mongoose.Schema({
type: String,
ip: String,
details: String,
reason: String,
});
This might be late to reply still, this may help someone who looking for this.
With ES6 Classes Schemas have a loadClass()
method that you can use to create a Mongoose schema from an ES6 class:
Here's an example of using loadClass() to create a schema from an ES6 class:
class MyClass {
myMethod() { return 42; }
static myStatic() { return 42; }
get myVirtual() { return 42; }
}
const schema = new mongoose.Schema();
schema.loadClass(MyClass);
console.log(schema.methods); // { myMethod: [Function: myMethod] }
console.log(schema.statics); // { myStatic: [Function: myStatic] }
console.log(schema.virtuals); // { myVirtual: VirtualType { ... } }
Reference: this is a sample code from mongoose documentation, for more details mongoose doc
Mongoose can natively support es6 classes (since 4.7, and with no transpiler…).
Just write:
const mongoose = require('mongoose')
const { Model, Schema } = mongoose
const schema = new Schema({
type: String,
ip: String,
details: String,
reason: String,
})
class Tenant extends Model {}
module.exports = mongoose.model(Tenant, schema, 'tenant');