用StrongLoop自动创buildMySQL表
我想用MySql的Strongloop,但不知道如何迁移或自动创build表到MySql数据库。
是否至less有一种方法将模型导出到MySql模式或我必须手动创build表?
我一直在尝试与MySQL演示应用程序,并通过了一段时间的文档,但没有运气 – http://docs.strongloop.com/display/DOC/MySQL+connector
谢谢!
LoopBack称之为自动迁移。 检查这些链接并search该术语:
LoopBack模型的配方,第5部分:与关系数据库的模型同步
数据源和连接器
我创build了/server/boot/autoupdate.js
。 它在应用启动时运行。 它加载“model-config”和“datasources”JSON,并将所有模型迁移或更新到为其定义的数据源。
# /server/boot/autoupdate.js module.exports = function(app) { var path = require('path'); var models = require(path.resolve(__dirname, '../model-config.json')); var datasources = require(path.resolve(__dirname, '../datasources.json')); function autoUpdateAll(){ Object.keys(models).forEach(function(key) { if (typeof models[key].dataSource != 'undefined') { if (typeof datasources[models[key].dataSource] != 'undefined') { app.dataSources[models[key].dataSource].autoupdate(key, function (err) { if (err) throw err; console.log('Model ' + key + ' updated'); }); } } }); } function autoMigrateAll(){ Object.keys(models).forEach(function(key) { if (typeof models[key].dataSource != 'undefined') { if (typeof datasources[models[key].dataSource] != 'undefined') { app.dataSources[models[key].dataSource].automigrate(key, function (err) { if (err) throw err; console.log('Model ' + key + ' migrated'); }); } } }); } //TODO: change to autoUpdateAll when ready for CI deployment to production autoMigrateAll(); //autoUpdateAll(); };
在app.start方法之前,您可以简单地通过在server.js文件中添加以下行来迁移模型:
app.datasources['mySqlConnection'].automigrate(['orders','customers', 'User', 'ACL'], function(err) { console.log(err); });
- 根据需要将模型添加到arrays。
- 通过
slc run
运行应用程序。
注意:
mySqlConnection
是连接名称,将其replace为您自己的连接名称。
为你的模型更新和/或创build所有的mysql表:
var dataSource = app.dataSources.mysql; dataSource.autoupdate(null, function (err) { if(err) return cb(err); return cb(); });
在我的情况下,我手动创buildMySQL表,然后创build模型。 对于现有的MySQL表,我创build了属性名称与MySQL字段名称相同的模型。
所以这里是我使用StrongLoop LoopBack与MySQL数据库的步骤:
- 创buildMySQL数据库和表(或使用现有的数据库)。
- 使用
npm install loopback-connector-mysql --save
来安装MySQL连接器npm install loopback-connector-mysql --save
- 在
datasources.json
文件中添加您的MySQL数据库详细信息。 - 使用
slc lb model tablename -i
OR编辑models.json
文件为每个表创build一个模型并手动添加属性。 (文档: http : //docs.strongloop.com/display/DOC/Creating+a+LoopBack+application#CreatingaLoopBackapplication-Creatingmodels ) - 属性的名称应该与MySQL字段的名称相同(有关将MySQL映射到JSON数据types的更多信息: http : //docs.strongloop.com/display/DOC/MySQL+connector#MySQLconnector-MySQLtoJSONtypes )
在同一类问题中,如果您需要自动创build数据库,则可以在dataSource JSON文件中使用createDatabase
选项。
"mysql": { "host": "localhost", "port": 0, "database": "db", "username": "root", "password": "", "name": "mysql", "connector": "mysql", "debug": false, "createDatabase": true }
所以你不需要编写自己的查询来创build基础。 希望能帮助到你。
我发现了一个简单的方法来完成这项任务。 参考链接是: Clique Here
你可以使用原型或不使用,在我的情况下,我不会使用。
对于文档,您应该使用:
ds.autoupdate (models, function (error) { if (!error) { console.log( "Updated models."); }else{ console.log( "An error has occurred:" + error); } ds.disconnect(); });
哪里:
var path = require ( 'path'); var app = require (path.resolve (__ dirname, '../server/server')); var ds = app.datasources.x;
x是数据源属性名称,例如/server/datasources.json:
{ "x": { "Host": "localhost" "Port": 3306, "Database", "loopapp" "Password": "", "Name": "x" "User", "root" "Connector": "mysql" } }
注意(1): 模型可以是string模型名称或string数组(模型名称)。
注意(2): 如果你不想放置模型,基本属性等于“PersistedModel”的文件的所有模型都将被更新。
有了这个,我用这个:
autoupdate函数(){ ds.autoupdate(function(error){ 如果(!错误){ console.log(“更新所有模型”); } else { console.log(“发生错误:”+错误); } ds.disconnect(); }); }
我称之为: autoupdate();
你可以把这段代码放在一个file.js文件中,然后调用命令行:node file.js.
如果您希望每次启动程序时调用此文件,请将其放在/server/boot/file.jspath中。
显然,如果你想使用automigrate,只需用上面代码中的autoupdate字来代替automigrate。
jduhls的答案是美丽的,但我需要稍微调整,以添加一些静态数据到表中。 下面是我调整的版本,以及将数据加载到一个简单的SystemSettings表(id,settingName,settingValue)的示例:
var async = require('async'); var SYSTEM_SETTINGS = [ { "settingName": "mustPayInAdvance", "settingValue": "false", } ]; module.exports = function(app) { var path = require('path'); var models = require(path.resolve(__dirname, '../model-config.json')); var datasources = require(path.resolve(__dirname, '../datasources.json')); var modelUpdates = []; function buildModelListForOperation(){ Object.keys(models).forEach(function(key) { if (typeof models[key].dataSource != 'undefined') { if (typeof datasources[models[key].dataSource] != 'undefined') { modelUpdates.push({operation: app.dataSources[models[key].dataSource], key: key}); } } }); } function createStaticData() { app.models.SystemSettings.create(SYSTEM_SETTINGS, function(err, created) { if (err) throw err; else console.log('Sample data was imported.'); }); } function processModelsAndData(operationType) { buildModelListForOperation(); // Create all models async.each(modelUpdates, function(item, callback) { item.operation[operationType](item.key, function (err) { if (err) throw err; console.log('Model ' + item.key + ' migrated'); callback(); }); }, function (err) { if (err) throw err; createStaticData(); }); } //TODO: change to 'autoupdate' when ready for CI deployment to production processModelsAndData('automigrate'); };