Options
All
  • Public
  • Public/Protected
  • All
Menu

concisedb

concisedb

npm version codecov GitHub Stars NPM Downloads License Package size

English | 简体中文

A database library stores JSON file for Node.js.

Here is what updated every version if you want to know.

API Document

Usage

Basic usage

  1. Install this library

You can also use other package managers like yarn and pnpm instead

npm install concisedb@0.2.3
  1. Example code

This library also supports TypeScript

const ConciseDb = require('concisedb')
const path = require('path')

// The third argument is Options for the class.
// Access https://v0.concisedb.top/ if you wanna know what options can change
const db = new ConciseDb(path.join(__dirname, 'db.json'), { test: [] })

db.data.test.push(1) // It will update JSON file automatically by using Proxy
// So for performance, if you need to change the data many times at once
// you can use db.getData() to get a copy of data
// Of course, you can give the class { realtimeUpdate: false } to the third argument
// to prevent the program from updating the JSON file automatically

console.log(db.data) // Output: { test: [ 1 ] }
import ConciseDb from 'concisedb'
import { join } from 'path'

interface Database {
test: number[]
}

// The third argument is Options for the class.
// Access https://v0.concisedb.top/ if you wanna know what options can change
const db = new ConciseDb<Database>(join(__dirname, 'db.json'), { test: [] })

db.data.test.push(1) // It will update JSON file automatically by using Proxy
// So for performance, if you need to change the data many times at once
// you can use db.getData() to get a copy of data
// Of course, you can give the class { realtimeUpdate: false } to the third argument
// to prevent the program from updating the JSON file automatically

console.log(db.data) // Output: { test: [ 1 ] }
  1. Don't worry if you change the type of T

The program will automatically update the content of the JSON file

If the JSON file is exist, the program will compare the content of the JSON file with data you give.

The exist data of the JSON file will take the place of data you give

and the non-exist data of the JSON file will use the default data which you give.

Advanced usage

Update the data of JSON file manually

If you change the content of JSON file and you want to get the latest content, use db.read() or db.readSync() to get the latest content.

const ConciseDb = require('concisedb')
const path = require('path')

const db = new ConciseDb(path.join(__dirname, 'db.json'), { test: [] })

console.log(db.data)
// Try changing the content of JSON file
setTimeout(() => {
db.readSync()
console.log(db.data)
}, 5000)
db.read()
import ConciseDb from 'concisedb'
import { join } from 'path'

interface Database {
test: number[]
}

const db = new ConciseDb<Database>(join(__dirname, 'db.json'), { test: [] })

console.log(db.data)
// Try changing the content of JSON file
setTimeout(() => {
db.readSync()
console.log(db.data)
}, 5000)

Async and Sync APIs

Here are three APIs and they all support Async

  • db.read() and db.readSync()
  • db.write() and db.writeSync()
  • db.updateFile() and db.updateFileSync()

db.updateFile() and db.updateFileSync() are declared for compatibility. They are the same as db.write() and db.writeSync()

Generated using TypeDoc