繁体   English   中英

Express.js:启动时填充mongodb表的最佳方法

[英]Express.js: the best way to populate mongodb table on start

我从Ruby on Rails来到Node.js(Express.js)。 在那很容易通过迁移对数据库进行任何更改。

我的主要思想是:我有一个类似Dictionary的表(因此,如果此表中没有值,则应该在启动时预先填充一些key:values)。

我有两个模型:

MaintenanceMaintenanceType

Maintenance使用MaintenanceType通过ref

维护模式:

const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
const MaintenanceType = mongoose.model("MaintenanceType");

const maintenanceSchema = new mongoose.Schema(
  {
    number: {
      type: String,
      trim: true,
      uppercase: true,
      required: "enter a maintenance number"
    },
    maintenanceType: {
        type: mongoose.Schema.ObjectId,
        ref: "MaintenanceType"
    },
    description: {
      type: String,
      trim: true,
      required: "enter a maintenance description"
    }
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
  }
);

maintenanceSchema.index({
  number: "text"
});

module.exports = mongoose.model("Maintenance", maintenanceSchema);

MaintenanceType型号:

const mongoose = require("mongoose");
mongoose.Promise = global.Promise;

const maintenanceTypeSchema = new mongoose.Schema(
    {
        name: {
            type: String,
            trim: true,
            required: "enter a maintenanceType name",
            unique: "enter a unique maintenanceType name",
        },
        isDefault: {
            type: Boolean,
            default: false
        }
    },
    {
        toJSON: { virtuals: true },
        toObject: { virtuals: true }
    }
);

// Define our indexes
maintenanceTypeSchema.index({
    number: "text"
});

module.exports = mongoose.model("MaintenanceType", maintenanceTypeSchema);

start.js:

const mongoose = require("mongoose");

// import environmental variables from variables.env file
require("dotenv").config({ path: "variables.env" });

mongoose.connect(process.env.DATABASE);
mongoose.Promise = global.Promise;
mongoose.connection.on("error", err => {
  console.error(`🚫 → ${err.message}`);
});

require("./models/MaintenanceType");
require("./models/Maintenance");

const app = require("./app");
app.set("port", process.env.PORT || 7777);
const server = app.listen(app.get("port"), () => {
  console.log('started');
});

因此:启动服务器->如果'MaintenanceType'表没有默认值->添加一些默认值(如果不存在默认值,例如: [{name: 'wheels', isDefault: true}, {name: 'engine', isDefault: true}]

我想到了app.listen部分。 但是,也许这不是放置.find.create bulk操作的最佳位置?

看看这是为Node / MongoDB应用程序植入种子的最佳方法是什么? 这可能会有所帮助。

另一种方法是手动执行此操作,例如检查MaintenanceType集合中是否有项目,如果没有则插入初始值。 看下面的代码。 假设您正在使用get请求转到主页/索引页面。

routes.get('/', (request, response) => {
    let initialValues = [{name: 'wheels', isDefault: true}, {name: 'engine', isDefault: true}];

    MaintenanceType.find({}, (error, doc) => {
        if (doc.length === 0) {
            let maintenanceType = new MaintenanceType(initialValues);

            maintenanceType.save(error => {

            });
        }
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM