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条数据