npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

travix-persistent-object

v1.0.0

Published

An object wrapper for NodeJs that automatically (and asynchronously) saves content of the object to a JSON file as soon as object has been modified

Downloads

7

Readme

travix-persistent-object

NPM

Build Status Code Climate Test Coverage

An object wrapper for NodeJs that automatically (and asynchronously) saves content of an object to a JSON file as soon as the object has been modified.

This implementation is much more efficient than any timer based change tracking mechanism because it is based on the Proxy object which was introduced in ESMAScript 2015.

Installation

Install it via npm:

$ npm install travix-persistent-object

Since some features of ES2015 (Proxies, Symbols) are used by this module, it requires NodeJS 6.9 or higher to run.

Usage

Example code:

const { random } = Math;
const persistent = require('travix-persistent-object');

const delay = value => new Promise(
  resolve => setTimeout(() => resolve(value), 10)
);

const watcher = (error, object) => (
  error
    ? console.log('Fail: ', error.message)
    : console.log('Save:', object)
);

let i = 0;

const test = () => persistent('path/to/object.json', watcher)
  .then(object => {
    console.log('Load:', object);
    object.value = ++i;
    console.log('Set value:', i);
    return object;
  })
  .then(delay)
  .then(object => {
    Object.defineProperty(object, 'property', {
      configurable: true, enumerable: true, value: ++i
    });
    console.log('Define property:', i);
    return object;
  })
  .then(delay)
  .then(object => {
    object.array = [];
    object.array.push(++i);
    console.log('Create array with item:', i);
    return object;
  })
  .then(delay)
  .then(object => {
    object.array.length = 0;
    console.log('Clear array');
    return object;
  })
  .then(delay)
  .then(object => {
    delete object.array;
    delete object.property;
    delete object.value;
    console.log('Delete all properies');
    return object;
  });

test().then(delay).then(test);

Console output:

Load: {}
Set value: 1
Save: { value: 1 }
Define property: 2
Save: { value: 1, property: 2 }
Create array with item: 3
Save: { value: 1, property: 2, array: [ 3 ] }
Clear array
Save: { value: 1, property: 2, array: [] }
Delete all properies
Save: {}

Load: {}
Set value: 4
Save: { value: 4 }
Define property: 5
Save: { value: 4, property: 5 }
Create array with item: 6
Save: { value: 4, property: 5, array: [ 6 ] }
Clear array
Save: { value: 4, property: 5, array: [] }
Delete all properies
Save: {}

API

persistentObject(path, ...options)

Creates new persistent object and returns promise eventually resolving to the object wrapped with proxy if it has been successfully loaded, or rejecting with error returned by fs.readFile otherwise.

Arguments:

  1. path (String): Path to the file to load object from and to save it to. Should be not empty string;
  2. [options] (Object): Object with options:
  • [delay=0]: Delay before saving changes.
  • [depth=0]: Depth to track changes on (how deep nested object are recursively prozied). Zero means unlimited depth.
  • [prototype={}]: Object to use by default when path points to non-existing file. Can be any sort of object. If a function passed to this argument, it is considered to be a watcher.
  • [watcher]: Function to be called each time the object has been saved or attempt to save has failed. Accepts two arguments:
    • [error]: Error returned by fs.writeFile if any, or null,
    • [proxy]: Proxy initially returned by persistentObject.

The returned proxy will track the following operations for the wrapped object and all the nested objects (depending on depth option):

  • delete:
    • delete object.property
    • delete object.child.property
  • defineProperty:
    • Object.defineProperty(object, 'property', { value: 42 })
    • Object.defineProperty(object.child, 'property', { value: 42 })
  • set:
    • object.property = 42
    • object.child.property = 42

If any nested object contains readonly enumerable property, proxying will fail with "Property ${key} cannot be proxied" error.

License

MIT © Travix International