nodejs项目的轻量级数据持久化方案,node-json-db,直接使用json文件保存,查询数据。

前言

作为一名前端开发,我们做网站时,难免会遇到需要保存数据的场景,比如做一个小官网,没有注册,没有登陆,只有一个给我们提建议,如下面的,
在这里插入图片描述
网站上只有一处需要填写数据。

场景

网站需要保存的数据非常少,比如只有注册登陆和提建议,联系我们。我们在做数据持久化的时候 需要考虑成本的问题,可以使用一些轻量级方案,不必使用数据库。
我们总不能为了一点醋,包一顿饺子吧。而且一旦用上了数据库来保存这些数据,无论学习成本还是维护成本都是比较大的。

聪明的人不会选择最好的方案,总是选择最适合的方案。这也是降本增效的重要手段。

使用方式

下面我就介绍一下,在遇到这种简单的业务场景,如何快速地实现数据的持久化。
在本文中我使用的是 开源库 node-json-db,这是一个可以将数据以json文件的格式保存到本地,在nodejs中使用。

该库的的github 地址 https://github.com/Belphemur/node-json-db

使用以下命令在项目中安装

yarn add node-json-db

你可以通过以下带注释的代码,来了解这个库。

import {
    
     JsonDB, Config } from 'node-json-db';

// 第一个参数是数据库文件名。如果没有写扩展名,则默认为“.json”并自动添加。
// 第二个参数用于告诉数据库在每次推送后保存,如果设置false,则必须手动调用save()方法。
// 第三个参数是要求JsonDB以人类可读的格式保存数据库。(默认为false)
// 最后一个参数是分隔符。默认情况下为斜线(/) 
var db = new JsonDB(new Config("myDataBase", true, false, '/'));

// 将数据推入数据库
// 使用想要的的数据路径
// 默认情况下,新值将覆盖旧的值
await db.push("/test1","super test");

// 如果数据路径不存在,它将在推送新数据时自动创建层次结构
await db.push("/test2/my/test",5);

// 你可以直接推送一个多层的json对象
await db.push("/test3", {
    
    test:"test", json: {
    
    test:["test"]}});

// 如果你希望在推送数据时不是覆盖旧值,而是合并它们。你可以设置push方法的第二个参数为false。
// 合并是递归的,可以使用Object和Array。
await db.push("/test3", {
    
    
    new:"cool",
    json: {
    
    
        important : 5
    }
}, false);

/*
最终结果
{
   "test":"test",
   "json":{
      "test":[
         "test"
      ],
      "important":5
   },
   "new":"cool"
}
*/

// 你无法合并原始值,像下面这样,数据将会被覆盖
await db.push("/test2/my/test/",10,false);
 
// 获取根路径下的所有数据
var data = await db.getData("/");

// 从一个数据路径中获取数据
var data = await db.getData("/test1");

// 如果你无法确认数据路径是否存在,可以使用tr catch来包裹它,如果不存在,将进入catch块中。
try {
    
    
    var data = await db.getData("/test1/test/dont/work");
} catch(error) {
    
    
    // The error will tell you where the DataPath stopped. In this case test1
    // Since /test1/test does't exist.
    console.error(error);
};

 
// 删除数据
await db.delete("/test1");

// 保存数据,如果你禁用了在推送时保存。
await db.save();

// 为了防止数据库文件被外部修改,你可以使用reload(),方法重载数据库文件,以此获取最新的数据。
await db.reload();

更多的案例注入数组的处理

import {
    
     JsonDB, Config } from 'node-json-db';

// The first argument is the database filename. If no extension, '.json' is assumed and automatically added.
// The second argument is used to tell the DB to save after each push
// If you put false, you'll have to call the save() method.
// The third argument is to ask JsonDB to save the database in an human readable format. (default false)
const db = new JsonDB(new Config("myDataBase", true, false, '/'));

// This will create an array 'myarray' with the object '{obj:'test'}' at index 0
await db.push("/arraytest/myarray[0]", {
    
    
    obj:'test'
}, true);

// You can retrieve a property of an object included in an array
// testString = 'test';
var testString = await db.getData("/arraytest/myarray[0]/obj");

// Doing this will delete the object stored at the index 0 of the array.
// Keep in mind this won't delete the array even if it's empty.
await db.delete("/arraytest/myarray[0]");

// You can also easily append new item to an existing array
// This set the next index with {obj: 'test'}
await db.push("/arraytest/myarray[]", {
    
    
    obj:'test'
}, true);


// The append feature can be used in conjuction with properties
// This will set the next index as an object {myTest: 'test'}
await db.push("/arraytest/myarray[]/myTest", 'test', true);

基于以上的api,我实现了一个简单的注册,登陆,和创建单的功能。以此来验证在使用中的问题。如下是作者编写的代码。

完整代码

var express = require('express');
var router = express.Router();
const {
    
     JsonDB, Config } = require('node-json-db');
const db = new JsonDB(new Config("cardshop", true, false, '/'));
const successInfo = {
    
     code: 200, msg: 'success' }

/* POST 登录 */
router.post('/login', async function (req, res, next) {
    
    
  const {
    
     username, password, vertify } = req.body
  try {
    
    
    var data = await db.getData(`/${
      
      username}`);
    if (data.password === password) {
    
    
      res.json(successInfo)
    } else {
    
    
      res.json({
    
     code: 403, msg: '账号或密码不匹配' })
    }
  } catch (error) {
    
    
    res.json({
    
     code: 403, msg: '账号或密码不匹配' })
  };
});

/* POST 注册 */
router.post('/register', async function (req, res, next) {
    
    
  console.log(req.body, 'req.body')
  const {
    
     username } = req.body
  const userModel = {
    
     ...req.body }
  userModel.createTime = new Date()
  userModel.orders = []
  await db.push(`/${
      
      username}`, userModel);
  res.json(successInfo)
});

/* POST 创建一个订单 */
router.post('/createOrder', async function (req, res, next) {
    
    
  const {
    
     username } = req.body
  const orderModel = {
    
     ...req.body }
  orderModel.createTime = new Date()
  await db.push(`/${
      
      username}/orders[]`, orderModel);
  res.json(successInfo)
});

module.exports = router;

在使用中最大的问题可能还是确实各种方便的查询方法。只能应对简单的查询,要想实现复杂的查询,需要做二次的数据处理,或者开发。

后记

掌握这种本地json文件保存数据的方案能够使我们的工作变得非常简洁,便利。

猜你喜欢

转载自blog.csdn.net/github_35631540/article/details/130165402