How to automate in Loopback

I renamed several models and tables in my feedback application, however now I have to go to this model definition.

I need to run autoMigrate () . It should be run on the dataSource object, but the documentation does not help in acquiring one of them.

so far I have created a new script in /bootcontaining:

var loopback = require('loopback');
var app = module.exports = loopback();
app.loopback.DataSource.automigrate()

but this data source object does not contain the autoMigrate function ...

I tried to launch strongloop arc to use the automatic transfer button present there, but the page crashes with this error:

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)

I just need to update the model, and I don’t understand why it is so complicated. Does anyone know how to overcome these obstacles? Thank!

+8
5

.

-, , .

.

, loopback ( ) , loopback ( ).

datasource.automigrate . , , , , .

, , , mysql ( ./server/datasources.json), :

module.exports = function (app) {
   app.dataSources.mysql.automigrate();
   console.log("Performed automigration.");
}

, , node. , . ( , Angular, , , , , client).

Performed automigration.
+13

app.dataSources.<dataSourceName> app.loopback.DataSource script.

script:

module.exports = function (app) {
'use strict'
var mysql = app.dataSources.mysql;

console.log('-- Models found:', Object.keys(app.models));

for (var model in app.models) {
    console.log("Cheking if table for model " + model + " is created and up-to-date in DB...");
    mysql.isActual(model, function (err, actual) {
        if (actual) {
            console.log("Model " + model + " is up-to-date. No auto-migrated.");
        } else {
            console.log('Difference found! Auto-migrating model ' + model + '...');
            mysql.autoupdate(model, function () {
                console.log("Auto-migrated model " + model + " successfully.");
            });
        }
    });
} };

, GitHub: https://github.com/jeserodz/loopback-models-automigration-example

+1

, , , . , app.model .

( Node 8+ async/await):

'use strict';

// Update (or create) database schema (https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html)
// This is effectively a no-op for the memory connector
module.exports = function(app, cb) {
  updateDatabaseSchema(app).then(() => {
    process.nextTick(cb);
  });
};

async function updateDatabaseSchema(app) {
  const dataSource = app.dataSources.db;

  for (let model of app.models()) {
    if (await doesModelNeedUpdate(dataSource, model.modelName) === true) {
      await updateSchemaForModel(dataSource, model.modelName);
    }
  }
}

function doesModelNeedUpdate(dataSource, model) {
  return new Promise((resolve, reject) => {
    dataSource.isActual(model, (err, actual) => {
      if (err) reject(err);
      resolve(!actual);
    });
  });
}

function updateSchemaForModel(dataSource, model) {
  return new Promise((resolve, reject) => {
    dataSource.autoupdate(model, (err, result) => {
      if (err) reject(err);
      console.log('Autoupdate performed for model ${model}');
      resolve();
    });
  });
}
+1

(automigrate.js) bin . cassandra . . .

var path = require('path');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.cassandra;
ds.automigrate('dataSourceTesting', function(err) {
  if (err) throw err;

  var accounts = [
    {
      name: 'john.doe@ibm.com',
      address: "asdasd"
    },
    {
      name: 'jane.doe@ibm.com',
      address: "asdasdasd"
    },
  ];
  var count = accounts.length;
  accounts.forEach(function(account) {
    app.models.dataSourceTesting.create(account, function(err, model) {
      if (err) throw err;

      console.log('Created:', model);

      count--;
      if (count === 0)
        ds.disconnect();
    });
  });
});

: gythub loopback.

0

, .

, . , . MSSQL /, GET/POST .

/. root.

automigrate.js

'use strict';

module.exports = function (app) {
   app.dataSources.mysql.automigrate();
   console.log("Performed automigration.");
}

, :

C:\Users\ajmal\loopback\telematics\server\server.js: 31 if (err) throw err; ^

TypeError: 'automigrate' Object.module.exports [as func] (C:\Users\ajmal\loopback\telematics\server\boot\automigrate.js: 9: 26) C:\Users\Ajmal\\\node_modules\ \Lib\executor.js: 316: 22

0
source

Source: https://habr.com/ru/post/1657666/


All Articles