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

ffg-datafile

v1.0.0

Published

Allows for simple data storage, which automatically creates backups to prevent data loss.

Downloads

2

Readme

DataFile by FFGFlash

A simple data storage solution, DataFile automatically backsup any and all datafiles for minimal losses.

Initializing

To initialize the DataFile you must provide it with a root directory.

const DataFile = require("FFG-DataFile")("Put your root directory here (typically '__dirname')");
Creating a DataFile

To create a DataFile you must provide it with a name.

const example = new DataFile("example");

Though you can also provide extra options to modify how your DataFile behaves.

const example = new DataFile("example", {
   auto_save: 500, // The delay between autosaves, by default autosaving is turned off.
   debug: true, // If toggled on then the DataFile will log debug information to the console, by default this is turned off (Warning: Can become quite overwhelming)
   structure: { test: 0 } // This determines the default structure for the DataFile, this only affects the file the first time it is created and by default it is '{}'
});
Loading a DataFile

This will attempt to load the data from the main data file, if that fails it'll attempt the backup file, and finally it will start from the structure it was provided.

example.load().then(data => { // Called only when this load is completed.
   console.log(data);
});

example.on("load", data => { // Called everytime you load the file.
   console.log("Data was loaded.");
});
Saving a DataFile

This will attempt to save the data to the main data file, followed by the backup file, but only if the first is successful (to prevent data loss).

example.save().then(data => { // Called only when this save is completed.
   console.log(data);
});

example.on("save", data => { // Called everytime you save the file.
   console.log("Data was saved.");
});
Error Handling

Some examples of error handling, for saving and loading.

example.load().then(console.log).catch(err => { // Handled only for this load.
   throw err;
});

example.save().then(console.log).catch(err => { // Handled only for this save.
   throw err;
});

example.on("error", err => { // Handled for every error callback.
   throw err;
});
Manipulating the DataFile

When designed this was very important, trying to make this as simple and easy to manipulate as possible. (This example also demonstrates a manual auto-saving system.)

const example = new DataFile("example", {
    structure: {
        saves: 0
    }
});
example.load().then(data => {
    (function next_save() {
        example.save().then(data => {
            example.data.saves++; // Updates the saves value everytime the DataFile saves.
            setTimeout(next_save, 500);
        });
    })();
});
example.on("error", err => (throw err));
Example

A simple example demonstrating most of the things mentioned above.

const DataFile = require("FFG-DataFile")(__dirname);
const Example = new DataFile("example", {
    auto_save: 500,
    debug: true,
    structure: {
        data1: false,
        data2: "example_data",
        data3: 107
    }
});

Example.on("save", () => {
   this.data.data1 = !this.data.data1;
   this.data.data2 += "!";
   this.data.data3 += 1;
});