安装strongloop框架以及集成sequlize
first install node and npm
run
npm install -g loopback-cli
run
npm install -g apiconnect
run
npm install -g strongloop
if you want to look version, you can run slc -v
if has error not find commond
you need
run npm uninstall -g strongloop
then run
npm clear cache
then run
npm install -g strongloop
secusse install
create project run
slc loopback [project name]
run
cd [project name]
run
npm install
install nessesery packageinstall StrongLoop MySQL 连接器
npm install --save loopback-connector-mysql
配置数据源
slc loopback:datasource AccountDb
接受默认数据源名称。对于数据库类型,选择 MySQL。
创建数据模型
slc loopback:model Account 按照提示配置该数据模型。对于模型的“base class”,使用“PersistedModel”并选择通过 REST API 公开该模型。添加模型属性如下: Name:Account Datasource:AccountDb Base class:PersistedModel Expose via REST:Yes Custom plural form:Leave blank 对于数据模型上的属性,创建以下 3 个属性。 Property name: email Property type: string Required?Not required Property name: createdAt Property type: date Required?Not required Property name: lastModifiedAt Property type: date Required?Not required
添加脚本来创建数据库中的模式
在您项目的根目录下的 ./server 文件夹中创建一个 bin 目录。
cd server
mkdir bin
在 server/bin 目录中,使用以下代码创建一个名为 automigrate.js 的文件。
var app = require('../server'); var accounts = [ { email:'foo@bar.com', createdAt: new Date(), lastModifiedAt: new Date() }, { email:'baz@qux.com', createdAt: new Date(), lastModifiedAt: new Date() } ]; // this loads the accountDb configuration in ~/server/datasources.json var dataSource = app.dataSources.AccountDb; // this automigrates the Account model dataSource.automigrate('Account', function(err) { if (err) throw err; // this loads the Account model from ~/common/models/Account.json var Account = app.models.Account; var count = accounts.length; accounts.forEach(function(account) { // insert new records into the Account table Account.create(account, function(err, record) { if (err) return console.log(err); console.log('Record created:', record); count--; if (count === 0) { console.log('done'); dataSource.disconnect(); } }); }); });
run
node server/bin/automigrate.js
extend
slc loopback:property
orsudo lb property
add new propssudo lb relation
orsudo slc loopback:relation
add new relation
3. sudo lb acl
or sudo slc loopback:acl
定义访问权限
use sequelize orm
npm install --save sequelize
install sequelize packgenpm install --save mysql2
install mysql2 packgenpm install --save co
install co 异步控制库npm install -g sequelize-auto
将数据库映射成modelrun
sequelize-auto -h localhost -d [database] -u [username] -x [password] -p 3306 -t [tablename]