Socket
Socket
Sign inDemoInstall

@kifuan/json-db

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @kifuan/json-db

Database Based on JSON Files


Version published
Maintainers
1
Created

Readme

Source

JSON Database

This is a simple database powered by JSON files.

Installation

npm i @kifuan/json-db
# or yarn add @kifuan/json-db

Usage

It's easy to use this lightweight module.

import { connect } from '@kifuan/json-db'

type Data = {
    foo: string
}

const conn = await connect<Data>({
    file: './data.json',
    delay: 1000,
    defaultData: {
        foo: 'bar'
    },
    onSaved() {
        console.log(`Callback onSaved is called. this.foo is ${this.foo}`)
    }
})

conn.foo = 'baz'

The connection object conn is just like a normal JavaScript object.

However, if the value has changed, it would save the object to given file after delay.

Additionally, this will be the data object in onSaved callback function.

Customize

If you want it to read a object directly, you can try this:

import { connect, createObjectJSONFile } from '@kifuan/json-db'

type Data = {
    foo: string
}

const conn = await connect<Data>({
    file: createObjectJSONFile({ foo: 114514 }),
    delay: 1000,
    defaultData: {
        foo: 'bar'
    }
})

console.assert(conn.foo === 114514)

It will create a "file" in memory for the connections to read.

Here is another example to create your own file object:

import { connect, createObjectJSONFile } from '@kifuan/json-db'

type Data = {
    foo: string
}

const conn = await connect<Data>({
    file: {
        async read() {
            return await GET()
        },
        async write(json) {
            await PUT(json)
        }
    },
    delay: 1000,
    defaultData: {
        foo: 'bar'
    }
})

console.log(conn.foo)

However, this example can not run directly. The only thing I want to tell you is that you can customize the file object with two async methods: read and write.

Keywords

FAQs

Last updated on 08 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc