Socket
Socket
Sign inDemoInstall

filestore-json

Package Overview
Dependencies
1
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    filestore-json

Easily sync JSON objects of any shape with the filesystem.


Version published
Maintainers
1
Install size
27.7 kB
Created

Readme

Source

filestore-json

Checks

  • Build
  • Code Style Analysis
  • Test CI

Easily sync JSON objects of any shape with the filesystem.

Features

  • Read & write to JSON files on a storage device without the need to interact with the filesystem.
  • Persistant data between process restarts.
  • Ability to determine age (ms since last write).
  • Cache JSON responses from network requests.
  • Automaticly updates internal value when JSON file is modified.
  • Strong type internal value with type annotations.

Examples

Creating a store

import JSONStore from "filestore-json";
import path from "path";

// Initialize the store.
const store = JSONStore.from(path.resolve("path/to/your/store.json"));

// With types
type Type = Record<string, any>;
const store = JSONStore.from<Type>(path.resolve("path/to/your/store.json"));

Reading & writing to the store

// Read value of store.
console.log(store.value); // -> {}

// Update store value.
store.value = { myObject: false };

// Read new value of store.
console.log(store.value); // -> { myObject: false }

Creating a store with default values

// Create object with store defaults
const defaults = { defaultValue: true };

// Initialize store.
const store = JSONStore.from(path.resolve("path/to/your/store.json"), defaults);

// Read value of store.
console.log(store.value); // -> { defaultValue: true }

// Update store value.
store.value = { myObject: false };

// Read new value of store.
console.log(store.value); // -> { myObject: false, defaultValue: true }

Resetting a stores value back to the default

// Update store value.
store.value = { myObject: false };

// Read new value of store.
console.log(store.value); // -> { myObject: false }

// Clear value
store.clear();

// Read value of store.
console.log(store.value); // -> {}

Getting the age of the store (ms since last write)

// Read age of store.
console.log(store.age); // -> 0

Keywords

FAQs

Last updated on 26 Aug 2021

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