Skip to content

A type-friendly class that makes it easier to read/write state from local disk

License

Notifications You must be signed in to change notification settings

transcend-io/persisted-state

Repository files navigation

Table of Contents

Overview

This package contains a class that makes it easy to read/write JSON data from a file on disk. It's built using io-ts to provide runtime and static type validation.

Example

// A sample io-ts validation
const TestCodec = t.type({
  timestamp: t.string,
  time: t.number,
  type: t.type({
    fish: t.partial({
      dog: t.string,
    }),
  }),
  content: t.string,
});

// Create a new state before each test
const testFile = join(__dirname, 'test.json');
const state = new PersistedState(testFile, TestCodec, {
  timestamp: '',
  time: 2,
  type: {
    fish: {
      dog: 'dog',
    },
  },
  content: '',
});

// get values
const twoDeep = state.getValue('type', 'fish'); // { dog: 'dog' }
const threeDeep = state.getValue('type', 'fish', 'dog'); // 'dog'

// set values
state.setValue(3, 'time');
state.setValue('howdy', 'content');
state.setValue({ dog: 'moose' }, 'type', 'fish');

// save and wipe
state.save();
state.wipe();

Typescript Build

Build this package only:

yarn run tsc
yarn run tsc --watch # Watch mode

Create a fresh build:

yarn clean && yarn run tsc

Lint

Lint the typescript files in this package:

yarn lint

About

A type-friendly class that makes it easier to read/write state from local disk

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages