[Node.js] Create a model to persist data in a Node.js LoopBack API

LoopBack创建REST API
本文介绍如何使用LoopBack CLI创建基于PersistedModel的产品模型,并利用API Explorer进行数据的增删改查操作。此外还介绍了如何配置数据源以保存数据。

In this lesson you will learn what a LoopBack model is, you will create a Product model using the LoopbBack CLI. The product model will be based off the built-in PersistedModel which gives it basic functionality like Create, Ready, Update, Delete and some more. Using the API Explorer you can interact with the new model, store, retrieve, edit and delete the product data.

 

Create a REST API model:

lb model

Give the information needed, it can generate all the rest api.

 

There are few files will be created.

common/models/[model_name].js

common/models/[model_name].json

There is one file will be modified:

server/model-config.json:

  "Product": {
    "dataSource": "db",
    "public": true
  }

 

So if you want to remove the model, you can delete those generated stuff.

 

If the data are stored in memory, it means once you restart the server, the data will lost, one way to keep the data for development:

datasources.json:

{
  "db": {
    "name": "db",
    "connector": "memory",
    "file": "db.json"
  }
}

Add "file" prop, it will store data inside the db.json.

转载于:https://www.cnblogs.com/Answer1215/p/6794439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值