{"id":17968002,"url":"https://github.com/rotten-lkz/concisedb","last_synced_at":"2026-04-11T13:03:56.592Z","repository":{"id":57685478,"uuid":"475345181","full_name":"Rotten-LKZ/concisedb","owner":"Rotten-LKZ","description":"A database library stores JSON file for Node.js.","archived":false,"fork":false,"pushed_at":"2022-04-06T02:48:18.000Z","size":86,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T09:30:03.644Z","etag":null,"topics":["concisedb","database","json","local","localdb","nodejs","npm-package","typescript-library"],"latest_commit_sha":null,"homepage":"https://www.concisedb.top","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Rotten-LKZ.png","metadata":{"files":{"readme":"README-zh-Hans.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-29T08:15:20.000Z","updated_at":"2022-09-04T10:56:55.000Z","dependencies_parsed_at":"2022-09-18T06:56:28.961Z","dependency_job_id":null,"html_url":"https://github.com/Rotten-LKZ/concisedb","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rotten-LKZ%2Fconcisedb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rotten-LKZ%2Fconcisedb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rotten-LKZ%2Fconcisedb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rotten-LKZ%2Fconcisedb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rotten-LKZ","download_url":"https://codeload.github.com/Rotten-LKZ/concisedb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247082539,"owners_count":20880665,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concisedb","database","json","local","localdb","nodejs","npm-package","typescript-library"],"created_at":"2024-10-29T14:10:27.993Z","updated_at":"2025-12-30T23:09:01.810Z","avatar_url":"https://github.com/Rotten-LKZ.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# concisedb\n\n[![npm version](https://badge.fury.io/js/concisedb.svg)](https://badge.fury.io/js/concisedb)\n[![codecov](https://codecov.io/gh/Rotten-LKZ/concisedb/branch/pub/graph/badge.svg?token=6WFQG040FA)](https://codecov.io/gh/Rotten-LKZ/concisedb)\n[![GitHub Stars](https://badgen.net/github/stars/Rotten-LKZ/concisedb)](https://github.com/Rotten-LKZ/concisedb)\n[![NPM Downloads](https://badgen.net/npm/dt/concisedb)](https://www.npmjs.com/package/concisedb)\n[![License](https://img.shields.io/npm/l/concisedb)](https://github.com/Rotten-LKZ/concisedb/blob/main/LICENSE)\n[![Package size](https://img.shields.io/bundlephobia/min/concisedb)](https://www.npmjs.com/package/concisedb)\n\n[English](https://github.com/Rotten-LKZ/concisedb/blob/main/README.md) | [简体中文](https://github.com/Rotten-LKZ/concisedb/blob/main/README-zh-Hans.md)\n\nNode.js 存储数据到 JSON 文件的库\n\n如果你想知道每个版本的更新内容，请点击 [这里](https://github.com/Rotten-LKZ/concisedb/blob/main/update.md)\n\n[主页](https://www.concisedb.top/) | [v0 文档](https://v0.concisedb.top/) | [v1 文档](https://v1.concisedb.top/)\n\n## 用法\n\n### 基础用法\n\n1. 下载库\n\n*你可以使用其他你喜欢的包管理器如 `yarn` and `pnpm` 代替*\n\n```bash\nnpm install concisedb\n```\n\n2. 示例代码\n\n*本库支持 `TypeScript`*\n\n```javascript\nconst ConciseDbSync = require('concisedb').ConciseDbSync\nconst JSONAdapterSync = require('concisedb').JSONAdapterSync\nconst path = require('path')\n\nconst adapter = new JSONAdapterSync({\n  filePath: path.join(__dirname, 'db.json')\n})\n\n// 适配器 (adapter), 默认数据 （可选）, 是否需要实时更新 （默认： true）\nconst db = new ConciseDbSync(adapter, { test: [] })\n\ndb.data.test.push(1)\n\nconsole.log(db.data) // 输出： { test: [ 1 ] }\n\n// 尝试更改 JSON 文件的内容\nsetTimeout(() =\u003e {\n  db.read()\n  console.log(db.data) // 输出：取决于你的更改\n}, 10000)\n```\n\n```typescript\nimport { ConciseDbSync, JSONAdapterSync } from 'concisedb'\nimport { join } from 'path'\n\ninterface Database {\n  test: number[];\n  username: string;\n}\n\nconst init: Database = {\n  test: [],\n  username: 'John',\n}\n\nconst adapter = new JSONAdapterSync({\n  filePath: join(__dirname, 'db.json')\n})\n\n// 适配器 (adapter), 默认数据 （可选）, 是否需要实时更新 （默认： true）\nconst db = new ConciseDbSync(adapter, { test: [] })\n\ndb.data.test.push(1)\n\nconsole.log(db.data) // 输出： { test: [ 1 ] }\n\n// 尝试更改 JSON 文件的内容\nsetTimeout(() =\u003e {\n  db.read()\n  console.log(db.data) // 输出：取决于你的更改\n}, 10000)\n```\n\n\u003e 通过使用 Proxy，JSON 文件里的数据会自动更新\n\u003e\n\u003e 如果你需要一次性大量修改 `data`，可以使用 `db.getData()` 获取 `data` 的副本\n\n3. 关闭自动更新。\n\n```javascript\nconst ConciseDbSync = require('concisedb').ConciseDbSync\nconst JSONAdapterSync = require('concisedb').JSONAdapterSync\nconst path = require('path')\n\nconst adapter = new JSONAdapterSync({\n  filePath: path.join(__dirname, 'db.json')\n})\n// 将 false 传入第三个参数\nconst db = new ConciseDbSync(adapter, { test: [] }, false)\n\ndb.data.test.push(1)\n\n// 用 write 手动更新 JSON 文件的内容\ndb.write()\n```\n\n```typescript\nimport { ConciseDbSync, JSONAdapterSync } from 'concisedb'\nimport { join } from 'path'\n\ninterface Database {\n  test: number[];\n  username: string;\n}\n\nconst init: Database = {\n  test: [],\n  username: 'John',\n}\n\nconst adapter = new JSONAdapterSync({\n  filePath: join(__dirname, 'db.json')\n})\n// 将 false 传入第三个参数\nconst db = new ConciseDbSync(adapter, { test: [] }, false)\n\ndb.data.test.push(1)\n\n// 用 write 手动更新 JSON 文件的内容\ndb.write()\n```\n\n4. 异步 API\n\n\u003e **`db.getData()` 仍然是一个同步方法**\n\n```javascript\nconst ConciseDb = require('concisedb').ConciseDb\nconst JSONAdapter = require('concisedb').JSONAdapter\nconst path = require('path')\n\n(async () =\u003e {\n  const adapter = new JSONAdapter({\n    filePath: path.join(__dirname, 'db.json')\n  })\n  const db = new ConciseDb()\n  // 方法 db.init 应该在初始化类后被调用\n  // 并且需要用 await 等待方法执行完成\n  // 当然，可以用 .then 代替 await\n  await db.init(adapter, { test: [] })\n\n  db.data.test.push(1)\n\n  // db.getData() 仍然是一个同步方法\n  console.log(db.data, db.getData()) // 输出： { test: [ 1 ] } { test: [ 1 ] }\n\n  // 尝试更改 JSON 文件的内容\n  setTimeout(async () =\u003e {\n    await db.read()\n    console.log(db.data) // 输出：取决于你的更改\n  }, 10000)\n})()\n```\n\n```typescript\nimport { ConciseDb, JSONAdapter } from 'concisedb'\nimport { join } from 'path'\n\n(async () =\u003e {\n  interface Database {\n    test: number[];\n    username: string;\n  }\n\n  const init: Database = {\n    test: [],\n    username: 'John',\n  }\n\n  const adapter = new JSONAdapter({\n    filePath: join(__dirname, 'db.json')\n  })\n  const db = new ConciseDb()\n  // 方法 db.init 应该在初始化类后被调用\n  // 并且需要用 await 等待方法执行完成\n  // 当然，可以用 .then 代替 await\n  await db.init\u003cDatabase\u003e(adapter, init)\n\n  db.data.test.push(1)\n\n  // db.getData() 仍然是一个同步方法\n  console.log(db.data, db.getData()) // 输出： { test: [ 1 ] } { test: [ 1 ] }\n\n  // 尝试更改 JSON 文件的内容\n  setTimeout(async () =\u003e {\n    await db.read()\n    console.log(db.data) // 输出：取决于你的更改\n  }, 10000)\n})()\n```\n\n### 高阶用法\n\n#### 制作你自己的适配器 (adapter)\n\n*你需要集成的抽象类*\n\n- 同步\n\n```typescript\n/**\n * 同步存储的适配器 (adapter)\n */\nexport abstract class AdapterSync\u003cT extends object, R\u003e {\n  public adapterOptions: R\n  constructor(adapterOptions: R) {\n    this.adapterOptions = adapterOptions\n  }\n\n  /**\n   * 写入数据\n   * @param data 需要写入的数据\n   * @returns 写入是否成功\n   */\n  public abstract write(data: T): boolean\n  /**\n   * 读取数据\n   * @returns\n   *  如果你返回字符串，ConciseDb 会帮你尝试解析成类型 T\n   *  如果你返回 false，代表着存储有问题或者不存在\n   *  如果你返回类型 T （类型 T 是对象类型），ConciseDb 会直接把它当成数据。\n   */\n  public abstract read(): T | false | string\n}\n```\n\n- 异步\n\n```typescript\n/**\n * 异步存储的适配器 (adapter)\n */\nexport abstract class Adapter\u003cT extends object, R\u003e {\n  public adapterOptions: R\n  constructor(adapterOptions: R) {\n    this.adapterOptions = adapterOptions\n  }\n\n  /**\n   * 写入数据\n   * @param data 需要写入的数据\n   * @returns 写入是否成功\n   */\n  public abstract write(data: T): Promise\u003cboolean\u003e\n  /**\n   * 读取数据\n   * @returns\n   *  如果你返回字符串，ConciseDb 会帮你尝试解析成类型 T\n   *  如果你返回 false，代表着存储有问题或者不存在\n   *  如果你返回类型 T （类型 T 是对象类型），ConciseDb 会直接把它当成数据。\n   */\n  public abstract read(): Promise\u003cT | false | string\u003e\n}\n```\n\n例如： \n\n- 同步\n\n```typescript\nimport { AdapterSync } from 'concisedb'\n\ninterface TestAdapterSyncOptions {\n  readType: number\n}\n\n/**\n * 测试用适配器 (adapter)\n */\nexport default class TestAdapterSync\u003cT extends object\u003e extends AdapterSync\u003cT, TestAdapterSyncOptions\u003e {\n  private _data: T\n  private readType: number\n  constructor(options: TestAdapterSyncOptions, defualtData: T) {\n    super(options)\n    this._data = defualtData\n    this.readType = options.readType\n  }\n\n  public read(): T | false | string {\n    if (this.readType === 1)\n      return false\n    else if (this.readType === 2)\n      return this._data\n    else if (this.readType === 3)\n      return JSON.stringify(this._data)\n    else if (this.readType === 4)\n      return '123'\n    else\n      return false\n  }\n\n  public write(_data: T): boolean {\n    return Math.floor(Math.random() * (264 - 1 + 1) + 1) % 2 === 0\n  }\n}\n```\n\n- 异步\n\n```typescript\nimport { Adapter } from 'concisedb'\n\ninterface TestAdapterOptions {\n  readType: number\n}\n\n/**\n * 测试用适配器 (adapter)\n */\nexport default class TestAdapter\u003cT extends object\u003e extends Adapter\u003cT, TestAdapterOptions\u003e {\n  private _data: T\n  private readType: number\n  constructor(options: TestAdapterOptions, defualtData: T) {\n    super(options)\n    this._data = defualtData\n    this.readType = options.readType\n  }\n\n  public async read(): Promise\u003cT | false | string\u003e {\n    if (this.readType === 1)\n      return false\n    else if (this.readType === 2)\n      return this._data\n    else if (this.readType === 3)\n      return JSON.stringify(this._data)\n    else if (this.readType === 4)\n      return '123'\n    else\n      return false\n  }\n\n  public async write(_data: T): Promise\u003cboolean\u003e {\n    return Math.floor(Math.random() * (264 - 1 + 1) + 1) % 2 === 0\n  }\n}\n```\n\n同步适配器 (adapter) 用 `ConcisDbSync` 初始化\n\n异步适配器 (adapter) 用 `ConcisDb` 初始化\n\n## 版本选择：v0 和 v1\n\n`concisedb` 现在有两个主要版本。以下是他们的示例代码。\n\n\u003e *他们都支持异步 API*\n\n- v0:\n```javascript\nconst ConciseDb = require('concisedb')\nconst path = require('path')\n\nconst db = new ConciseDb(path.join(__dirname, 'db.json'), { test: [] })\n\ndb.data.test.push(1)\n\nconsole.log(db.data) // 输出： { test: [ 1 ] }\n```\n- v1:\n```javascript\nconst ConciseDbSync = require('concisedb').ConciseDbSync\nconst JSONAdapterSync = require('concisedb').JSONAdapterSync\nconst path = require('path')\n\nconst adapter = new JSONAdapterSync({\n  filePath: path.join(__dirname, 'db.json')\n})\n\n// 适配器 (Adapter), 默认数据 （可选）, 是否需要实时更新 （默认： true）\nconst db = new ConciseDbSync(adapter, { test: [] })\n\ndb.data.test.push(1)\n\nconsole.log(db.data) // 输出： { test: [ 1 ] }\n```\n\n所以就是 v1 允许你使用 `适配器 (adapter)` 在不同地方存储数据。\n\n但是，作者仍然会维护 v0 版本。\n\n[GitHub 上查看 v0 分支](https://github.com/Rotten-LKZ/concisedb/tree/v0)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frotten-lkz%2Fconcisedb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frotten-lkz%2Fconcisedb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frotten-lkz%2Fconcisedb/lists"}