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

fson-db

v2.1.1

Published

Data Persistence for DUMMIES

Downloads

32

Readme

Data Persistence for DUMMIES

it gives you a magical javascript object that persists changes even after restart, and you can watch for changes too!

both Node.js and Browser environment are supported.

Quick Guide:

//get the magical object
import Fson from 'fson-db';
const starwars = Fson('./file/path');

// save data
starwars.owner = 'Disney';
starwars.movies = [
  {
    title: 'A New Home',
    date: new Date(1997, 5, 25),
    directory: {
      name: 'George Locas',
    }
  },
  {
    title: 'The Force Awakens',
    date: new Date(2015, 12, 18),
    directory: {
      name: 'J.J. Abraham',
    }
  },
]
//read data just like regular js object
const filtered = starwars.movies.filter(it => it.date.getUTCFullYear() > 2012);
console.log(`${filtered.length} movies since ${starwars.owner}`)

//watch for changes
import {watch} from 'fson-db';
watch(starwars,(field,newValue,oldValue) => {
  if (field === 'owner') {
    console.log(`${newValue} is the new boss here!`)
  }
  if (field === 'movies') {
    console.log(`a new movie has been released!`)
  }
})

How does it work?

fson-db load object from DataStorage, keep a copy of object in memory and apply effects to DataStorage.

DataStorage saves data with JSON format in LocalStorage for Browsers and FileSystem for Node.JS.

the following apis are supported, full js object apis support is in progress.

Full Example:

//browser
import Fson from 'fson-db';

//Node.js
const Fson = require('fson-db');

//specify a directory path
const dbPath = './config';

//db is a js object! but any changes to in will be persistent!
//dbPath is only required for Node.js
const db = Fson(dbPath); //db must be a constant!

//save any type of literals
db.name = 'john doe';
db.age = 24;
db.pi = 3.1415;
db.isActive = true;
db.date = new Date();

//fetch literals
console.log(db.name) // john doe
console.log(db.age) //  24
console.log(db.pi) //  3.1415
console.log(db.isActive) //  true
console.log(db.date) //  "1997-02-19T20:30:00.000Z"


//save objects!
db.object = {
  foo: 'bar',
}

//fetch objest and nested fields
console.log(db.object); // { foo:"bar" }
console.log(db.object.foo); // bar

//modify nested objects
db.object.foo = 'rab';
console.log(db.object.foo); // rab


//save arrays!
db.numbers = [1, 2, 3, 4, 5, 6];

//modify arrays!
db.numbers = db.numbers.filter(i => i % 2 == 0);
console.log(db.numbers); // [ 2, 4, 6 ]

//push to arrays
db.numbers.push(8, 10, 12);
console.log(db.numbers); // [ 2, 4, 6, 8, 9, 10 ]

//save array of objects
db.users = [{id: 1}, {id: 2}]
console.log(db.users[0].id); // 1
console.log(db.users.length); // 2

//modify with indexes!
db.users[1] = {id: 3}
console.log(db.users[1].id); // 3

Loop through entries

//list entries with Object.keys
db.obj = {
  one: 'the_one',
  two: 'the_two',
  three: 'the_three',
};
console.log(Object.keys(db.obj)) // ['one', 'two', 'three']

//loop through object keys
for (const key in db.obj) {
  const value = db.obj[key];
  console.log(`${key}: ${value}`);
}

for (const [key, value] of Object.entries(db.obj)) {
  console.log(`${key}: ${value}`);
}


//the above example also works for nested objects

Delete Operator

db.obj = {};
delete db.obj;
console.log(typeof db.obj) // 'undefined'

//nested objects!
db.obj = {
  nested: {},
};
delete db.obj.nested;
console.log(typeof db.obj.nested) // 'undefined'

Note: high performance is not a priority for now, everything is synchronous, so use db inside async functions