How to write a Mongoose model in ES6 / ES2015

前端 未结 7 1917
自闭症患者
自闭症患者 2021-02-18 23:55

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         


        
相关标签:
7条回答
  • 2021-02-19 00:02

    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)
    
    0 讨论(0)
  • 2021-02-19 00:03

    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)
    
    0 讨论(0)
  • 2021-02-19 00:13

    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);
    
    0 讨论(0)
  • 2021-02-19 00:14

    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,
    });
    
    0 讨论(0)
  • 2021-02-19 00:18

    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:

    • ES6 class methods become Mongoose methods
    • ES6 class statics become Mongoose statics
    • ES6 getters and setters become Mongoose virtuals

    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

    0 讨论(0)
  • 2021-02-19 00:22

    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');
    
    0 讨论(0)
提交回复
热议问题