1. 介绍

LoopBack 框架是由一组Node.js的模块构成的
应用通过LoopBack model API可以使用以下三种方式访问数据:

  1. 将模型作为一个标准的Node对象使用
  2. 通过HTTP REST API 调用
  3. 通过封装好的API SDK,包括iOS, Android 和 Angular

应用程序通过LoopBack model API用以上三种方式查询数据,储存数据,上传文件,发送email, 推送消息,注册/登陆用户等远程或本地的服务。用户也可以通过Strong Remoting将后端的API通过REST, WebSocket(或其他传输协议)供客户端调用。

核心LoopBack模之间的关系图:

模块类型 描述 用途 模块
数据模型 数据模型和API服务 迅速创建动态的数据模型及API且无需关注数据是如何储存的。 loopback
抽象方式 数据模型的抽象层,用与实现和不同数据库的交互 可以连接到不同的数据库或服务,并且提供一个对象用于CRUD,通过这个对象用户不必关心数据层具体使用的是什么数据库,可以是RDBS, MongoDB, Memory。 loopback-datasource-juggler
初始化 初始化应用 用于配置数据源, 定制/设置数据模型,并且将数据模型加载到指定的数据源上。同时可以 配置应用本身和执行启动脚本。 loopback-boot
执行序列 执行Express中间件 设置中间件,使得这些中间件可以在应用程序的不同阶段被执行。 loopback-phase
数据 连接RDBMS 和 noSQL 数据库 用于连接各种RDBMS和noSQL数据源的模块。 loopback-connector-mongodb loopback-connector-mysql loopback-connector-postgresql loopback-connector-msssql loopback-connector-oracle
集成 连接现有的企业或外部数据服务 用于连接到已有的REST或SOAP数据服务。 loopback-connector-rest loopback-connector-soap
服务 连接常用服务 整合已有的服务到Loopback应用中,包括推送消息,第三方验证等…… loopback-component-push loopback-component-storage loopback-component-passport loopback-component-sync�
(开发中)
Gateway API gateway Secure your APIs and inject quality of service aspects to the invocation and response workflow. loopback-gateway loopback-component-oauth2
客户端 客户端 SDK 可以使用通过REST访问LoopBack API的原生平台组件 (iOS, Android, AngularJS) loopback-sdk-ios loopback-sdk-android loopback-sdk-angular

2. hello-world

安装命令行工具

1
npm i -g @loopback/cli

工具会搭建项目脚手架,配置编译TS,安装依赖。

1
lb4 app

项目带有一个“ping”路由来便于测试。
启动项目

1
2
cd getting-started
npm start

打开http://127.0.0.1:3000/ping

返回值

1
{"greeting":"Hello from LoopBack","date":"2018-11-12T07:37:52.476Z","url":"/ping","headers":{"host":"127.0.0.1:3000","connection":"keep-alive","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate, br","accept-language":"zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7"}}

增加Controller

1
lb4 controller

修改/src/controllers/hello.controller.ts

1
2
3
4
5
6
7
8
import {get} from '@loopback/rest';

export class HelloController {
@get('/hello')
hello(): string {
return 'Hello world!';
}
}

运行npm start命令后打开 http://127.0.0.1:3000/hello
返回值是Hello world!

3. Todo Application Tutorial

1
lb4 example

1
2
cd loopback4-example-todo/
npm start

打开http://127.0.0.1:3000可查看API文档

添加模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
lb4 model
? Model class name: todo
? Please select the model base class: Entity

Let's add a property to Todo
Enter an empty property name when done

? Enter the property name: id
? Property type: number
? Is id the ID property? Yes
? Is it required?: No
? Default value [leave blank for none]:

Let's add another property to Todo
Enter an empty property name when done

? Enter the property name: title
? Property type: string
? Is it required?: Yes
? Default value [leave blank for none]:

Let's add another property to Todo
Enter an empty property name when done

? Enter the property name: desc
? Property type: string
? Is it required?: No
? Default value [leave blank for none]:

Let's add another property to Todo
Enter an empty property name when done

? Enter the property name: isComplete
? Property type: boolean
? Is it required?: No
? Default value [leave blank for none]:

Let's add another property to Todo
Enter an empty property name when done

? Enter the property name:

create src/models/test.model.ts
update src/models/index.ts

Model todo was created in src/models/

生成文件见/src/models/todo.model.ts

创建数据源

1
2
3
4
5
6
7
8
9
10
11
lb4 datasource
? Datasource name: db
? Select the connector for db: In-memory db (supported by StrongLoop)
? window.localStorage key to use for persistence (browser only):
? Full path to file for persistence (server only): ./data/db.json

create src/datasources/db.datasource.json
create src/datasources/db.datasource.ts
update src/datasources/index.ts

Datasource db was created in src/datasources/

生成文件见src/datasources/

在./data/db.json位置添加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"ids": {
"Todo": 5
},
"models": {
"Todo": {
"1": "{\"title\":\"Take over the galaxy\",\"desc\":\"MWAHAHAHAHAHAHAHAHAHAHAHAHAMWAHAHAHAHAHAHAHAHAHAHAHAHA\",\"id\":1}",
"2": "{\"title\":\"destroy alderaan\",\"desc\":\"Make sure there are no survivors left!\",\"id\":2}",
"3": "{\"title\":\"terrorize senate\",\"desc\":\"Tell them they're getting a budget cut.\",\"id\":3}",
"4": "{\"title\":\"crush rebel scum\",\"desc\":\"Every.Last.One.\",\"id\":4}"
}
}
}

TBC
https://loopback.io/doc/en/lb4/todo-tutorial-repository.html