loopback配置数据源(2)

1. 添加数据源

执行 >slc loopback:datasource ? Enter the data-source name: mysqlDs (输入数据源名称) ? Select the connector for mysqlDs: MySQL (supported by StrongLoop) (选择数据源类型)

2.数据源配置文件

位置:/server/datasources.json 内容: { "db": { "name": "db", "connector": "memory" }, "mysqlDs": { "name": "mysqlDs", "connector": "mysql" } } 其中db是默认配置,mysqlDs 是上一步生成的。

此时执行slc run提示如下:

WARNING: LoopBack connector "mysql" is not installed as any of the following modules:

./connectors/mysql loopback-connector-mysql

To fix, run:

npm install loopback-connector-mysql

events.js:85 throw er; // Unhandled 'error' event ^ Error: WARNING: LoopBack connector "mysql" is not installed as any of the following modules:

所以需要首先进行下一步

3. 安装数据库连接器`

>npm install loopback-connector-mysql --save

npm WARN package.json study@1.0.0 No license field. loopback-connector-mysql@2.1.1 node_modules\loopback-connector-mysql ├── sl-blip@1.0.0 ├── async@0.9.2 ├── loopback-connector@2.2.1 (async@1.2.1) ├── debug@2.2.0 (ms@0.7.1) └── mysql@2.7.0 (require-all@1.0.0, bignumber.js@2.0.7, readable-stream@1.1.13)

安装好了。

再执行slc run ERROR strong-agent error: failed to instrument mysql INFO strong-agent not profiling, agent metrics requires a valid license. Please contact sales@strongloop.com for assistance. Browse your REST API at http://localhost:3000/explorer Web server listening at: http://localhost:3000/

可见依然有错,但可以启动

4.配置数据库连接

####1. 本机需要安装好mysql ####2. 配置path路径,追加:C:\Program Files\MySQL\MySQL Server 5.6\bin; ####3. >mysql -V 检查是否安装成功。
####4.配置数据库连接文件 datasources.json 配置后如图: "mysqlDs": { "name": "mysqlDs", "connector": "mysql", "host":"localhost", "port":3306, "database":"loopback_study", "user_name":"root", "password": "root" }

5. 修改/server/model-config.json

"CoffeeShop": { "dataSource": "mysqlDs", "public": true }

6. 新增启动脚本,用于启动时创建表并增加数据。

位置: /server/boot/create-sample-models.js 内容: module.exports = function(app) { app.dataSources.mysqlDs.automigrate('CoffeeShop', function(err) { if (err) throw err;

app.models.CoffeeShop.create([
  {name: 'Bel Cafe', city: 'Vancouver'},
  {name: 'Three Bees Coffee House', city: 'San Mateo'},
  {name: 'Caffe Artigiano', city: 'Vancouver'},
], function(err, coffeeShops) {
  if (err) throw err;

  console.log('Models created: \n', coffeeShops);
});

}); };

7.启动

slc run Browse your REST API at http://localhost:3000/explorer Web server listening at: http://localhost:3000/ Models created: [ { name: 'Bel Cafe', city: 'Vancouver', id: 1 }, { name: 'Three Bees Coffee House', city: 'San Mateo', id: 2 }, { name: 'Caffe Artigiano', city: 'Vancouver', id: 3 } ]

启动成功。

8.mysql中观察

mysql> show tables; +--------------------------+ | Tables_in_loopback_study | +--------------------------+ | coffeeshop | +--------------------------+ 1 row in set (0.00 sec)

mysql> select * from coffeeshop; +----+-------------------------+ | id | name | +----+-------------------------+ | 1 | Bel Cafe | | 2 | Three Bees Coffee House | | 3 | Caffe Artigiano | +----+-------------------------+ 3 rows in set (0.00 sec)

9. API explorer中观察

GET /CoffeeShops 可以看到3条数据

转载于:https://my.oschina.net/u/2280693/blog/469357

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值