How to write a Mongoose model in ES6 / ES2015

前端 未结 7 1923
自闭症患者
自闭症患者 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: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');
    

提交回复
热议问题