1 Star 2 Fork 0

AI麥爾 / node-little-db

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

node-little-db

简介

node-little-db是一个基于 Node.js 的第三方库,用于将对象内容同步到文件中。它的原理非常简单,当您更改对象的内容时,它会自动调用fs.writeFileSync()方法将对象内容写入文件。

文档

English

安装

通过以下命令安装node-little-db

npm i node-little-db

使用

首先,引入node-little-db库:

const db = require('node-little-db')

然后,获取一个响应式对象:

const user = db.use('user')

现在,您可以对user对象进行操作了。

设置属性

user.name = 'jack'
user.age = 18
user.hobbies = ['eating', 'sleeping', 'playing Peas']
user.hobbies.push('fish')
user.gender = true
user.address = {
    country: 'China',
    city: 'Beijing',
}
user.address.city = 'Shanghai'

运行后的文件内容示例

{
    "name": "jack",
    "age": 18,
    "hobbies": [
        "eating",
        "sleeping",
        "playing Peas",
        "fish"
    ],
    "address": {
        "country": "China",
        "city": "Shanghai"
    },
    "gender": true
}

读取属性

user.name // 'jack'
user.age // 18
user.hobbies // ['eating', 'sleeping', 'playing Peas']
user.gender // true
user.address // {country: 'China', city: 'Beijing'}
user.address.city // 'Shanghai'

方法

db.use([filename, options])

使用指定的文件获取响应式对象,调用该方法后将返回一个代理对象,通过对这个对象进行操作,更改将被同步到文件中。

  • filename(可选):文件名。如果未提供文件名,则会使用默认文件名 db-${Date.now()}.json
  • options(可选):选项对象。
    • options.path:文件存放路径。
    • options.initialData:初始数据,在首次创建文件时使用。
    • options.encoding:文件编码,默认为 utf-8
MIT License Copyright (c) 2021 AI麥爾 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

小型项目的小型本地 JSON 数据库 展开 收起
JavaScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/aimaier4869/little-db.git
git@gitee.com:aimaier4869/little-db.git
aimaier4869
little-db
node-little-db
master

搜索帮助