2.1.1 • Published 6 years ago

bolt-json-db v2.1.1

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

bolt-json-db npm david-dm

⚡️ Persistence JSON without active save.

Usage

npm i bolt-json-db
const BoltJsonDb = require('bolt-json-db');

const json = BoltJsonDb('./data/db.json', {
    YourAttrs: 'YourValue'
});

// Use json just like a js Object
// and the json will save to `./data/db.json` auto.

want Faster And Safer

You can use jsonschema at 3nd argument.

const json = BoltJsonDb('./data/db.json', {
    YourAttrs: 'YourValue'
}, {
    title: 'Example Schema',
    type: 'object',
    properties: {
        YourAttrs: {
            type: 'string'
        }
    }
});

Guide

It should be noted that properties in data are only reactive if they existed when the instance was created.

That means if you add a new property, like:

const db1 = BoltJsonDb('./data/db1.json', { a: 0 });
const db2 = BoltJsonDb('./data/db2.json', { a: 0 });
const db3 = BoltJsonDb('./data/db3.json', { a: 0 });

db1.a = 1; // right
db1.b = 1; // wrong, because can't listen setter of db1.b

db2.a = {
    b: 2
}; // right
db2.a.b = 3; // right
db2.a.c = 3; // wrong, because can't listen setter of db2.a.c

db3.a = [];
db3.a.push(1); // right
db3.a.push({
    b: 3
}); // right
db3.a[1].b = 4; // right
db3.a[1].c = 4; // wrong, because can't listen setter of db3.a[1].c
db3.a[0] = 2; //right
db3.a[2] = 2; // wrong, because can't listen setter of db3.a[2]

// But result maybe right when auto save is going, not recommend.

// When use jsonschema
const db4 = BoltJsonDb('./data/db3.json', {
    a: 0,
    b: 1
}, {
    title: 'Example Schema',
    type: 'object',
    properties: {
        a: {
            type: 'string'
        },
        c: {
            type: 'string'
        }
    }
});

db4.a = 1;  // wrong, but db4.a === '1' at result file, because type is string at Schema
db4.b = 2;  // wrong, because not at Schema
db4.c = 3;  // wrong, because can't listen setter of db4.c
db4.a = db4;    // wrong, but db4.a === '[object Object]' at result file, because type is string at Schema

you’ll need to set some initial value.

Next