1 Star 2 Fork 0

AI麥爾 / node-little-db

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.en.md 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
AI麥爾 提交于 2023-08-26 14:34 . 📃 docs: 更新文档

node-little-db

Introduction

node-little-db is a third-party Node.js library that helps you synchronize object content to a file. Its principle is simple: when you change the content of an object, it automatically calls fs.writeFileSync() to write the object content to a file.

Installation

Install node-little-db using the following command:

npm i node-little-db

Usage

First, import the node-little-db library:

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

Then, get a database object:

const user = db.use('user')

Now, you can perform operations on the user object.

Set Properties

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'

After running the above code, the content of your file is:

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

Read Properties

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'

Methods

db.use([filename, options])

Creates a database object using the specified file. Calling this method will return a proxy object, and any operations performed on this object will be synchronized to the file.

  • filename (optional): The file name. If not provided, the default file name db-${Date.now()}.json will be used.
  • options (optional): Options object.
    • options.path: The file storage path.
    • options.initialData: Initial data used when creating the file for the first time.
    • options.encoding: File encoding, defaults to utf-8.
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/aimaier4869/little-db.git
git@gitee.com:aimaier4869/little-db.git
aimaier4869
little-db
node-little-db
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891