首页 文章

如何在启动另一个之前等待自动迁移启动脚本完成?

提问于
浏览
0

我正在使用启动脚本来创建一些模型和关系 . 例如,我可能有这个:

+-------+                +----------+
| Order | --belongsTo--> | Customer |
+-------+                +----------+

我想创建:1 Customer ,和属于 Customer 的1 Order .

我知道loopback-boot在filename alphabetical order中执行 server/boot 中的脚本,所以我有以下启动脚本:

// 0-create-customer.js
module.exports = function(app) {
    app.dataSources.mongoDs.autoupdate('Customer', function(err) {
        if (err) throw err;
        var obj = {name: 'Bob'};
        app.models.Customer.findOrCreate({where: obj}, obj
        , function(err, customer) {
            if (err) throw err;
        });
    });
};

对于 Order ,我首先找到 Customer 并使用 customer.id 创建订单:

// 1-create-order.js
module.exports = function(app) {
    app.dataSources.mongoDs.autoupdate('Order', function(err) {
        if (err) throw err;
        app.models.Customer.findOne({where: {name: 'Bob'}}
        , function(err, customer) {
            if (err) throw err;
            var obj = {customerId: customer.id, amount: 42};
            app.models.Order.findOrCreate({where: obj}, obj
            , function(err, order) {
                if (err) throw err;
            });
        });
    });
};

问题是,似乎启动脚本不会等到模型在退出之前创建,因此有时我会在第二个脚本中遇到这些错误:

TypeError: Cannot read property 'id' of null

提到这一行:

var obj = {customerId: customer.id, amount: 42};
                                   ^

我宁愿不在创建_2960765之前添加一个小的等待,因为这看起来很脆弱,并且特别是如果数据源恰好很慢的话,它会存在't guarantee the parent model' .

我也不必将所有这些代码组合到一个文件中,因为我的真实项目有很多模型,这将导致一个巨大的不可维护的文件 .

Is there a good way to wait for the parent model auto-migration to complete before starting on the child?

2 回答

  • 0

    您可以使用额外的回调参数创建异步启动脚本,并在脚本准备好时调用它 .

    例:

    module.exports = function (app, cb) {
      var db = app.dataSources.db;
    
      // update all database models
      db.autoupdate(function (err) {
        if (err) throw err;
        cb();
      });
    };
    
  • 4

    一种方法是keep looping,直到在数据源中找到 Customer .

    因此 Order 创建脚本可能如下所示:

    // 1-create-order.js
    module.exports = function(app) {
        app.dataSources.mongoDs.autoupdate('Order', function(err) {
            if (err) throw err;
            var customerId = null;
            function addOrder(customerName, obj) {
                if (customerId === null) {
                    app.models.Customer.findOne(
                        {where: {name: customerName}}, function(err, customer) {
                        if (err) throw err;
                        if (customer !== null) {
                            customerId = customer.id;
                        }
                    });
                    setTimeout(addOrder, 1000, customerName, obj);
                    return;
                }
    
                obj.customerId = customerId;
                app.models.Order.findOrCreate({where: obj}, obj
                , function(err, order) {
                    if (err) throw err;
                });
            }
            addOrder('Bob', {amount: 42});
        });
    }
    

    因此函数 addOrder 将继续使用 setTimeout 调用自身,直到 Customer 已创建并在数据库中找到,它将用于创建 Order .

相关问题