# node-little-db

> 将对象内容同步到文件中的一个Node.js库。

Latest version **1.1.4** (published 2023-08-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-little-db
pnpm add node-little-db
yarn add node-little-db
bun add node-little-db
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2023-08-26 |
| First published | 2021-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 18.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | aimaier4869 |
| Maintainers | aimaier4869 |
| Keywords | database, electron, JSON, local, lowdb, littledb, reactive, jsondb, localStorage |

## Links

- npm: https://www.npmjs.com/package/node-little-db
- Repository: https://gitee.com/aimaier4869/little-db
- Issues: https://gitee.com/aimaier4869/little-db/issues
- npm.io page: https://npm.io/package/node-little-db

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.4 (latest) — 2023-08-26
- 1.1.3 — 2023-08-26
- 1.1.2 — 2022-06-21
- 1.1.1 — 2022-06-19
- 1.1.0 — 2022-05-20
- 1.0.2 — 2021-08-09
- 1.0.1 — 2021-08-09
- 1.0.0 — 2021-08-09

## README

# node-little-db

## 简介

`node-little-db`是一个基于 Node.js 的第三方库，用于将对象内容同步到文件中。它的原理非常简单，当您更改对象的内容时，它会自动调用`fs.writeFileSync()`方法将对象内容写入文件。

## 文档

[English](./README.en.md)

## 安装

通过以下命令安装`node-little-db`：

```sh
npm i node-little-db
```

## 使用

首先，引入`node-little-db`库：

```js
const db = require('node-little-db')
```

然后，获取一个响应式对象：

```js
const user = db.use('user')
```

现在，您可以对`user`对象进行操作了。

#### 设置属性

```js
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'
```

运行后的文件内容示例

```js
{
    "name": "jack",
    "age": 18,
    "hobbies": [
        "eating",
        "sleeping",
        "playing Peas",
        "fish"
    ],
    "address": {
        "country": "China",
        "city": "Shanghai"
    },
    "gender": true
}
```

#### 读取属性

```js
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`。

---
_Source: https://npm.io/package/node-little-db · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
