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

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.

posted @   Zhentiw  阅读(197)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-05-02 [Flexbox] Using flex-direction to layout content horizontally and vertically
2016-05-02 [AngularJS] Design Pattern: Simple Mediator
2016-05-02 [Core Javascirpt] Basic Metaprogramming: Dynamic Method
点击右上角即可分享
微信分享提示