将内置模型迁移到数据库 [英] Migrating built-in models to Databases

查看:77
本文介绍了将内置模型迁移到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将内置模型(如用户,角色,用户角色映射等)移至我们创建的数据库而不是默认数据源:db?内置模型未在Arc中列出.

How to move built-in models like User, Roles, User-Role-Mapping etc... to the database we've created instead of the default datasource:db? The built-in models are not listing in Arc.

我尝试创建一个继承这些基本模型的新模型.但是,数据不会保存到新模型中.

I've tried creating a new model which inherits these base model. But, data is not saved into the new model.

请告知...我已经坐了几周了.谢谢

Please advice...I've been sitting on it for a couple of weeks. Thanks

推荐答案

默认的"db"数据源位于内存中.这就是为什么在重新启动应用程序后数据无法持久保存的原因.您必须安装适当的数据库连接器,然后必须在server/datasources.js中添加数据库的数据源.

Default "db" datasource is placed in memory. That is why your data are not persisted after you restart application. You have to install appropriate database connector and then you have to add datasource for your database inside server/datasources.js.

http://docs.strongloop.com/display/public/LB/Connecting + models + to + data + sources

如果使用"slc loopback"命令创建了应用程序,则您的数据源仅包含内存连接器.检查datasources.js文件,您将看到类似以下内容:

If you created application with "slc loopback" command, then your datasource contains only memory connector. Check datasources.js file and you will see something like this:

{
  "db": {
  "name": "db",
  "connector": "memory"
  }
}

例如,如果要将数据持久保存在postgresql数据库中(任何支持的连接器的处理过程几乎相同),则必须使用数据库信息来扩展datasoruces.json文件:

If you want to persist your data for example in postgresql database (process is almost same for any supported connector), you have to expand your datasoruces.json file with your database information:

{
  "db": {
  "name": "db",
  "connector": "memory"
  },

  "mydata": {
    "host": "db_host",
    "database": "your_database_name",
    "username": "your_db_username",
    "password": "your_db_password",
    "connector": "postgresql"
  }
}

您也可以使用"slc loopback:datasource"命令来执行此操作.向导将帮助您定义数据源.不要忘记安装数据库连接器.

You can also do this using "slc loopback:datasource" command. Wizard will help you to define your datasource. Don't forget to install db connector.

npm install loopback-connector-postgresql

最后要做的是将您的数据源分配给所需的模型.您可以使用向导执行此操作(请参阅slc loopback:model命令),也可以手动编辑server/model-config.json文件.

Last thing to do is to assign your datasource to desired models. You can do this using wizard (see slc loopback:model command), or you can edit server/model-config.json file manually.

{
  "User": {
    "dataSource": "mydata",
    "public": true
  },
  "AccessToken": {
    "dataSource": "mydata",
    "public": false
  },
  "ACL": {
    "dataSource": "mydata",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "mydata",
    "public": false
  },
  "Role": {
    "dataSource": "mydata",
    "public": false
  }
}

更新 您可以尝试使用此代码段从模型更新表.将他的代码放在server/server.js中

UPDATE You can try this code snippet to update your tables from models. Put his code somewhere in server/server.js

var appModels = ['User', 'AccessToken', 'ACL', 'RoleMapping', 'Role'];

var ds = app.dataSources.mydata;
ds.isActual(appModels, function(err, actual) {
  if (!actual) {
    ds.autoupdate(appModels, function(err) {
      if (err) throw (err);
    });
  }
});

我建议您阅读有关从Strongloop页面上的模型创建/更新数据库架构的信息.请注意自动更新和自动迁移功能之间的区别

I would recommend you to read about creating/updating database schema from models on strongloop page. Pay attention abut difference between autoupdate and automigrate functions

http://docs.strongloop. com/display/public/LB/Creating + a + database + schema + from + models

这篇关于将内置模型迁移到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆