如何在Loopback中自动迁移

JD.*_*JD. 9 loopback strongloop loopbackjs

我在我的loopback应用程序中重命名了许多模型和表,但是现在我必须迁移到这个模型定义.

我需要运行autoMigrate().它必须在dataSource对象上运行,但文档没有提供有关获取其中一个的帮助.

到目前为止,我已经创建了一个新脚本/boot:

var loopback = require('loopback');
var app = module.exports = loopback();
app.loopback.DataSource.automigrate()
Run Code Online (Sandbox Code Playgroud)

但是这个数据源对象不包含autoMigrate函数......

我已经尝试运行strongloop arc来使用那里存在的自动迁移按钮,但页面崩溃时出现此错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module Arc due to:
Error: [$injector:modulerr] Failed to instantiate module Metrics due to:
Error: [$injector:nomod] Module 'Metrics' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.3.20/$injector/nomod?p0=Metrics
    at http://localhost:56073/scripts/vendor/angular/angular.js:63:12
    at http://localhost:56073/scripts/vendor/angular/angular.js:1778:17
    at ensure (http://localhost:56073/scripts/vendor/angular/angular.js:1702:38)
    at module (http://localhost:56073/scripts/vendor/angular/angular.js:1776:14)
    at http://localhost:56073/scripts/vendor/angular/angular.js:4131:22
    at forEach (http://localhost:56073/scripts/vendor/angular/angular.js:326:20)
    at loadModules (http://localhost:56073/scripts/vendor/angular/angular.js:4115:5)
    at http://localhost:56073/scripts/vendor/angular/angular.js:4132:40
    at forEach (http://localhost:56073/scripts/vendor/angular/angular.js:326:20)
    at loadModules (http://localhost:56073/scripts/vendor/angular/angular.js:4115:5)
http://errors.angularjs.org/1.3.20/$injector/modulerr?p0=Metrics&p1=Error%3…F%2Flocalhost%3A56073%2Fscripts%2Fvendor%2Fangular%2Fangular.js%3A4115%3A5)
    at http://localhost:56073/scripts/vendor/angular/angular.js:63:12
    at http://localhost:56073/scripts/vendor/angular/angular.js:4154:15
    at forEach (http://localhost:56073/scripts/vendor/angular/angular.js:326:20)
    at loadModules (http://localhost:56073/scripts/vendor/angular/angular.js:4115:5)
    at http://localhost:56073/scripts/vendor/angular/angular.js:4132:40
    at forEach (http://localhost:56073/scripts/vendor/angular/angular.js:326:20)
    at loadModules (http://localhost:56073/scripts/vendor/angular/angular.js:4115:5)
    at createInjector (http://localhost:56073/scripts/vendor/angular/angular.js:4041:11)
    at doBootstrap (http://localhost:56073/scripts/vendor/angular/angular.js:1455:20)
    at bootstrap (http://localhost:56073/scripts/vendor/angular/angular.js:1476:12)
http://errors.angularjs.org/1.3.20/$injector/modulerr?p0=Arc&p1=Error%3A%20…%2Flocalhost%3A56073%2Fscripts%2Fvendor%2Fangular%2Fangular.js%3A1476%3A12)
Run Code Online (Sandbox Code Playgroud)

我只需更新模型,不明白为什么这么难.有人知道如何克服这些障碍吗?谢谢!

Ove*_*ivr 15

两个问题.

首先,您显示的是Angular客户端错误,而您的问题出在服务器端.

现在,回到问题的核心.

引导脚本必须导出单个函数,该函数在同步引导脚本的情况下仅接受环回对象(您的服务器),或者接受回送对象和回调(对于异步引导脚本).

此处记录编写引导脚本

该功能datasource.automigrate可以同步调用.如果您在没有参数的情况下调用它,默认情况下它将针对您应用中的所有模型执行,这很可能是您想要的.

因此,如果您定义的数据库具有名称mysql(定义./server/datasources.json),则引导脚本为:

module.exports = function (app) {
   app.dataSources.mysql.automigrate();
   console.log("Performed automigration.");
}
Run Code Online (Sandbox Code Playgroud)

您可以通过运行检查这是否有效,node .不是通过浏览器访问服务器正在侦听的地址.(您的Angular相关错误与启动脚本无关,它与客户端文件有关,很可能位于client文件夹中).

它应该显示在终端中

Performed automigration.
Run Code Online (Sandbox Code Playgroud)