json可以当作数据库吗,数据库应不应该存json

本文目录一览:

LowDB 轻量级 JSON 本地数据库

作为轻量级的本地存储方式,对于构建不依赖服务器的小型项目,用LowDB存储和管理数据是十分理想的选择。在Nodejs, Electron and browser等一些小型项目中经常能看到LowDB的身影。

npm install lowdb

或者:

yarn add lowdb

const low = require(‘lowdb’);

const FileSync = require(‘lowdb/adapters/FileSync’); // 有多种适配器可选择

const adapter = new FileSync(‘db.json’); // 申明一个适配器

const db = low(adapter);

db.defaults({posts: [], user: {}, count: 0})

.write();

db.get(‘posts’)

.push({id: 1, title: ‘lowdb is awesome’})

.write()

db.set(‘user.name’, ‘typicode’)

.write()

db.update(‘count’, n = n + 1)

.write()

运行程序会在项目中添加db.json文件,里面存储了添加的数据:

{

“posts”: [

{

“id”: 1,

“title”: “lowdb is awesome”

}

],

“user”: {

“name”: “typicode”

},

“count”: 1

}

lowdb是基于lodash构建的,所以可以使用任何lodash强大的函数,比如: _.get() 和 _.find(),并且可以串联地使用:

db.get(‘users’)

.find({sex: ‘male’})

.value()

函数 功能

low(adapter) 返回一个具有特定属性和功能的 lodash chain

db.[…].write() / .value() 写 / 读数据

db.getState() / .setState() 获取 / 设置数据库的状态

db._ 数据库lodash的实例,可以利用这个添加自己的函数或者第三方的mixins,比如lodash-id

db._.mixin({

second: function(array) {

return array[1]

}

})

db.get(‘posts’)

.second()

.value()

针对lowdb自带的适配器:FileSync、FileAsync 和 LocalBrowser,有以下可选参数:

defaultValue: 文件不存在时的默认值;

serialize/deserialize: 写之前和读之后的操作。

const adapter = new FilSync(‘db.json’,{

serialize: (data) = encrypt(JSON.stringify(data)),

deserialize: (data) = JSON.parse(decrypt(data))

})

可以直接使用lodash的函数进行查询。需要注意的是有些操作可能会导致原数据被修改,为了避免这种误操作,需要使用 .cloneDeep(),操作都是惰性的,只有调用 .value()或 .write()后才会正式执行。

检查users是是否存在

db.has(‘users’)

.value()

设置users

db.set(‘users’, [])

.write()

排序、选择

db.get(‘users’)

.filter({sex: ‘male’})

.sortBy(‘age’)

.take(5)

.value()

获取特定字段

db.get(‘users’)

.map(‘name’)

.value()

获取数量

db.get(‘users’)

.size()

.value()

获取特定信息

db.get(‘users[0].name’)

.value()

更新信息

db.get(‘users’)

.find({name: ‘Tom’})

.assign({name: ‘Tim’})

.write()

删除信息

db.get(‘users’)

.remove({name: ‘Time’})

.write()

移除属性

db.unset(‘users.name)

.write()

深拷贝

db.get(‘users’)

.cloneDeep()

.value()

可以使用 shortid 和 lodash-id 为数据库中的每一条记录创建唯一的id索引,然后通过id检索操作记录:

const shortid = require(‘shortid’)

const postId = db

.get(‘posts’)

.push({ id: shortid.generate(), title: ‘low!’ })

.write()

.id

const post = db

.get(‘posts’)

.find({ id: postId })

.value()

const lodashId = require(‘lodash-id’)

const FileSync = require(‘lowdb/adapters/FileSync’)

const adapter = new FileSync(‘db.json’)

const db = low(adapter)

db._.mixin(lodashId)

// We need to set some default values, if the collection does not exist yet

// We also can store our collection

const collection = db

.defaults({ posts: [] })

.get(‘posts’)

// Insert a new post…

const newPost = collection

.insert({ title: ‘low!’ })

.write()

// …and retrieve it using its id

const post = collection

.getById(newPost.id)

.value()

low( ) 函数接受自定义的Adapter

class MyStorage {

constructor() {

// …

}

read() {

// Should return data (object or array) or a Promise

}

write(data) {

// Should return nothing or a Promise

}

}

const adapter = new MyStorage(args)

const db = low(adapter);

==============================================

英文官网介绍,更加简洁

Lowdb 3 is a pure ESM package. If you’re having trouble importing it in your project, please read this.

You can use TypeScript to type check your data.

You can also add lodash or other utility libraries to improve lowdb.

For CLI, server and browser usage, see examples/ directory.

Lowdb has two classes (for asynchronous and synchronous adapters).

Calls adapter.read() and sets db.data .

Note: JSONFile and JSONFileSync adapters will set db.data to null if file doesn’t exist.

Calls adapter.write(db.data) .

Holds your db content. If you’re using the adapters coming with lowdb, it can be any type supported by JSON.stringify .

For example:

Adapters for reading and writing JSON files.

In-memory adapters. Useful for speeding up unit tests.

Synchronous adapter for window.localStorage .

Adapters for reading and writing text. Useful for creating custom adapters.

If you’ve published an adapter for lowdb, feel free to create a PR to add it here.

You may want to create an adapter to write db.data to YAML, XML, encrypt data, a remote storage, …

An adapter is a simple class that just needs to expose two methods:

For example, let’s say you have some async storage and want to create an adapter for it:

See src/adapters/ for more examples.

To create an adapter for another format than JSON, you can use TextFile or TextFileSync .

For example:

Lowdb doesn’t support Node’s cluster module.

If you have large JavaScript objects ( ~10-100MB ) you may hit some performance issues. This is because whenever you call db.write , the whole db.data is serialized using JSON.stringify and written to storage.

Depending on your use case, this can be fine or not. It can be mitigated by doing batch operations and calling db.write only when you need it.

If you plan to scale, it’s highly recommended to use databases like PostgreSQL or MongoDB instead.

json是不是相当于我们的数据库

json不是一种文件传输的格式吗。。。。是我记错了嘛。。。(⊙o⊙)… 就像xml,正则表达式那样的数据格式

json是什么意思

json的意思就是一种轻量级的数据交换格式。其中的具体情况如下:

它基于ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。

简洁和清晰的层次结构使得json成为理想的数据交换语言,易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

扩展资料

据了解,json的交互方式主要分为:

1、同步交互

发送一个请求,需要等待返回,然后才能够发送下一个请求,有个等待过程;

2、异步交互

发送一个请求,不需要等待返回,随时可以再发送下一个请求,即不需要等待。

由此看来,区别在于一个需要等待,一个不需要等待,在部分情况下,项目开发中都会优先选择不需要等待的异步交互方式。

原创文章,作者:YLCZ,如若转载,请注明出处:https://www.506064.com/n/141571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
YLCZYLCZ
上一篇 2024-10-08 17:42
下一篇 2024-10-08 17:42

相关推荐

  • Python 常用数据库有哪些?

    在Python编程中,数据库是不可或缺的一部分。随着互联网应用的不断扩大,处理海量数据已成为一种趋势。Python有许多成熟的数据库管理系统,接下来我们将从多个方面介绍Python…

    编程 2025-04-29
  • openeuler安装数据库方案

    本文将介绍在openeuler操作系统中安装数据库的方案,并提供代码示例。 一、安装MariaDB 下面介绍如何在openeuler中安装MariaDB。 1、更新软件源 sudo…

    编程 2025-04-29
  • 数据库第三范式会有删除插入异常

    如果没有正确设计数据库,第三范式可能导致删除和插入异常。以下是详细解释: 一、什么是第三范式和范式理论? 范式理论是关系数据库中的一个规范化过程。第三范式是范式理论中的一种常见形式…

    编程 2025-04-29
  • JSON的MD5

    在Web开发过程中,JSON(JavaScript Object Notation)是最常用的数据格式之一。MD5(Message-Digest Algorithm 5)是一种常用…

    编程 2025-04-29
  • 使用Java将JSON写入HDFS

    本篇文章将从以下几个方面详细阐述Java将JSON写入HDFS的方法: 一、HDFS简介 首先,先来了解一下Hadoop分布式文件系统(HDFS)。HDFS是一个可扩展性高的分布式…

    编程 2025-04-29
  • leveldb和unqlite:两个高性能的数据库存储引擎

    本文将介绍两款高性能的数据库存储引擎:leveldb和unqlite,并从多个方面对它们进行详细的阐述。 一、leveldb:轻量级的键值存储引擎 1、leveldb概述: lev…

    编程 2025-04-28
  • Python怎么导入数据库

    Python是一种高级编程语言。它具有简单、易读的语法和广泛的库,让它成为一个灵活和强大的工具。Python的数据库连接类型可以多种多样,其中包括MySQL、Oracle、Post…

    编程 2025-04-28
  • 如何使用Newtonsoft datatable转Json

    Newtonsoft DataTable 是一个基于.NET的JSON框架,也是一个用于序列化和反序列化JSON的强大工具。 在本文中,我们将学习如何使用Newtonsoft Da…

    编程 2025-04-28
  • JPRC – 轻松创建可读性强的 JSON API

    本文将介绍一个全新的 JSON API 框架 JPRC,通过该框架,您可以轻松创建可读性强的 JSON API,提高您的项目开发效率和代码可维护性。接下来将从以下几个方面对 JPR…

    编程 2025-04-27
  • Think-ORM数据模型及数据库核心操作

    本文主要介绍Think-ORM数据模型建立和数据库核心操作。 一、模型定义 Think-ORM是一个开源的ORM框架,用于简化在PHP应用中(特别是ThinkPHP)与关系数据库之…

    编程 2025-04-27

发表回复

登录后才能评论