2.3.2 • Published 2 years ago
@subie_/datastorage v2.3.2
DataStorage
An NPM Module used for saving and loading JSON data.
Install
npm i @subie_/datastorage
Demo
const DataStorage = require("@subie_/datastorage"); // Require the file
// Create an instance of DataStorage
// Arguments required/optional
// ID (required) -> The ID of the DataStorage just used for having multiple storages storing different things like player data or server data
// Options (Optional) -> Just for configuration it's a JSON object that can have 2 values, path and suffix
// The path is where the data will be stored on your pc the default is ./src/CurrentDataStorageID
// The Suffix is what each file ends with for example Javascript ends with .js but for our case anything works it's for cosmetics
// The autosave is used for autosaving after each setDataKey function
const dataStorage = new DataStorage("player_data", {
path: "./player_data",
suffix: "data" /* coins.data - inventory.data - items.data */,
autosave: false
});
// Data Values are what store each things
// You can add multiple data values to each storage
// For example storing coins inventories or items
// Options are the default values used when setupDataKey() is used
dataStorage.addDataValue("coins", { value: 0 });
dataStorage.addDataValue("inventory", { items: [] });
dataStorage.addDataValue("items", { amount: 0 });
// Loading should be done at the start of the script
// It just loads what each file that was previosly saved stored
// You should use await but it doesn't matter
/* await */ dataStorage.load();
// This function sets the key
// parsed in the function with each
// Data value DEFAULT value parsed when
// creating the data value
// if the key already has data in the data
// value it the function won't affect the
// Key.
dataStorage.setupDataKey("subie", "coins");
dataStorage.setupDataKey("subie", "inventory");
dataStorage.setupDataKey("subie", "items");
// Saving some player data to the data values
dataStorage.setDataKey("subie", /* Data Value */ "coins", /* JSON Object */ {
value: 999999
});
dataStorage.setDataKey("subie", /* Data Value */ "inventory", /* JSON Object */ {
items: [
{
name: "diamond_sword"
}
]
});
// Saving to a file
dataStorage.save();
// Getting player data
// and having conditions
// for it
if (dataStorage.hasDataKey("subie")) return;
const playerCoins = dataStorage.getDataKey(message.author.id, "coins");
// Remove a data value
dataStorage.removeDataValue("coins");
// Remove a data storage
DataStorage.cache.remove("player_data");