0.1.1 • Published 1 month ago

@bsnext/livejson v0.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

LiveJSON

Package for working with "live" JSON documents, and easy get access to updates.

Case 1: Store some minor project settings, as example "Disabled Registration" or "Enabled Captcha". Manage this status from your code, or just by edit file with text-editor.

Case 2: Share and manage some variables between few Node.JS instances. Set variable from Process #1, and have access to that from Process #2.

Installing:

npm install @bsnext/livejson

Usage:

new LiveJSON(filePath: string [, { interval: number = 50 }])
new LiveJSONSync(filePath: string [, { interval: number = 50 }])
  • filePath - Path to your JSON file.
  • interval - Delay betweeb checking file updates.
import { LiveJSONSync, LiveJSON } from "@bsnext/livejson";
const exampleFile = new LiveJSONSync(`myFile.json`);

// Get Value
exampleFile.get(`variableName`); 

// Set Value
exampleFile.set(`variableName`, 1337);

// Set Multiple Values
exampleFile.set(
    {
        variableName: 777,
        oneMoreVariable: `Hi!`
    }
);

API:

LiveJSONSync.get(key: string) // Get Value
LiveJSONSync.getAll() // Get All Values
LiveJSONSync.save() // Manual Save File
LiveJSONSync.reload(strict: boolean = false) // Manual Reload File
LiveJSONSync.set(key: string, value: any[, save: boolean = true]) // Set Value
LiveJSONSync.set({[key: string]: any}, [, save: boolean = true]) // Set Multiple Values

LiveJSON.get(key: string)
LiveJSON.getAll()
await LiveJSON.save()
await LiveJSON.reload(strict: boolean = false)
await LiveJSON.set(key: string, value: any[, save: boolean = true])
await LiveJSON.set({[key: string]: any}, [, save: boolean = true])

Save on "set" methods is enabled by default. You can set it as "false", if make much changes in one moment. Just disable that and use "save" method after your changes.

"strict" parameter used in "reload" method for throw Error, if JSON can not be parsed.

Future:

  • onReload event
  • onReloadError event
  • Tests
  • Benchmark
0.1.1

1 month ago

0.1.0

1 month ago