ffg-datafile

1.0.0 • Public • Published

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;
});

Package Sidebar

Install

npm i ffg-datafile

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

8.68 kB

Total Files

4

Last publish

Collaborators

  • ffgflash