簡體   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