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

object-tie

v0.0.5

Published

create a link between a JSON object and a file that updates the file anytime a change is made

Downloads

5

Readme

object-tie

Build Status Coverage Status

Usage

Installation

$ npm install object-tie

Purpose

This module was created as a way to link an object to a file and automatically update that file anytime the object is changed or modified so that you will never lose your data. This works great for an object that handles dynamic data that is continually changing and you wish to keep the file up to date through the process of your program.

Methods

config( object )

sets the configurations of the module

newLink( object )

creates a bond to the object

addKey( object, object )

adds a key/value pair to the object, where argument[0] is the object and argument[1] is the key/value pair being added

deleteKey( object, string )

removes a key/value pair from the object, where object is the object containing the key to be deleted in the first level (so obj.level2.level3 if the key resides in the third level) and string is the name of the key

retrieve( string )

creates abond between an existing object saved to a file, where string is the filepath to the file

unlink( object )

removes the bond to the object

Functionality and Examples

When using this to save an object to a file it will automatically save to saved_object.json in the current working directory. This functionality leverages the use of Object.defineProperty employing getters and setters, leaving your object alone but tied to the file on any change. One adverse effect of this is when you print the object using console.log( obj ) it will return the values as [Getter/Setter] and to see the actual object as you would like just use console.log( JSON.stringify( obj, null, 4 ) ).

The following examples are only showing an object with depth of one, but any depth object behaves similarly, with all keys bound to the file.

Adding object-tie to a file

// /yourFile.js
var object_tie = require('object-tie').config({
    file: 'myObjects/persistantFile.json',
    warnings: true
});;

the config is optional. Default values are file: saved_object.json, and warnings: true the methods can be implemented without the config but for production environment the warnings and file location should probably be changed. file is the location of the file based on current working directory (cwd) warnings just display use warnings, useful for dev but probably best to be muted for prod

tying an object to the file

// /yourFile.js
var object_tie = require('object-tie').config({
    file: 'saved_object.json',
    warnings: true
});;

var greetings = {
    english: 'hello',
    spanish: 'hola',
    chinese: '你好'
};

object_tie.newLink( greetings );

the object greetings is now linked to a new file named saved_object.json in the current working directory. By changing any of the greetings it will update the file to display the new greeting.

So by running the line greetings.english = 'hey' the object in the file and the object will look like:

// /yourFile.js

console.log( JSON.stringify( greetings, null, 4 );
// {
//     english: 'hey',
//     spanish: 'hola',
//     chinese: '你好'
// }

and

// /saved_object.json
{
    "english": "hey",
    "spanish": "hola",
    "chinese": "你好"
}

Now what if you want to add a key to the object? Unfortunately I have not figured out a way to do this with JavaScript native style but there is a function for that

// /yourFile.js

object_tie.addKey( greetings, { korean: '안녕하세요' } );

console.log( JSON.stringify( greetings, null, 4 );
// {
//     english: 'hey',
//     spanish: 'hola',
//     chinese: '你好',
//     korean: '안녕하세요'
// }

On a future release I hope to not require this method, and on a sooner release I hope to add this as a prototype method to the object so that it can be called as obj.addKey( keysToBeAdded ).

This will now update the file to the addition made and will also allow the same functionality for korean as the other keys in the object have, where the file will be updated on any change made in standard JavaScript practice. When adding a key like this, you can also add multi-level objects and each key/value of that object will be linked to the file.

Now let's say you want to delete a key, unfortunately, it's the same scenario as addKey, and my plans to change it are the same as they are with addKey. But for now, this is how it's done

// /yourFile.js

object_tie.deleteKey( greetings, 'english' );

console.log( JSON.stringify( greetings, null, 4 );
// {
//     spanish: 'hola',
//     chinese: '你好',
//     korean: '안녕하세요'
// }

And the file will now be updated to match this.

Now let's pretend you want to stop watching this object and watch a new, already existing object

// /yourFile.js

object_tie.unlink( greetings );

greetings.english = 'hi!';
// this change is made to greetings, but the file will not have recorded it

var newGreetings = object_tie.retrieve( 'filepath/my_greetings.json' );
console.log( JSON.stringify( newGreetings, null, 4 );
// {
//     english: 'how are you?'
//     spanish: 'como estas?',
//     chinese: '你好吗?'
// }

This object now has all the same functionality as before but instead of being updated to saved_object.json it will be updated in whatever the supplied file was.

What to expect next

New features currently being worked on

  • optimization
  • addKey and deleteKey added onto the linked object as prototype methods (maybe/maybe not.. I don't want to mess with js standard objects or anything in the global namespace)
  • different places to save the object to. instead of it just being a json file it could be stored in a DB
  • was going to add an async method but would have to add promises to that to not overwrite the file, and then the performance stays the same as before... One thought is to add a promiseFlag that only allows writes when the flag is set, where it is set within the fs async callback function.

Features for the more distant future

  • removal of addKey/deleteKey methods

Contact Me