kool-save-data
v1.1.0
Published
A simple save data module for my Node.js projects.
Downloads
13
Maintainers
Readme
kool-save-data
A simple save data module for my Node.js projects.
Require kool-save-data
const koolSaveData = require('kool-save-data');
Example Usage
Save Data
Used for creating a save data item.
project-directory
|
|____save-data.js
|
|____save-data.json
const {SaveData} = koolSaveData;
module.exports = new SaveData('save-data');
Save Data Collections
Used for saving a collection of save data. Maybe you would store settings and/or informative data here.
In this case, we are storing a person's favorite things.
Paths to save data files must look like so (./project-directory/save-data-path
):
project-directory
|
|____save-data-path
| |
| |____favorites.js
| |
| |____favorites.json
| |
| |____index.js
|
|____index.js
./save-data-path/favorites.js
const {SaveData} = koolSaveData;
module.exports = new SaveData('save-data');
./save-data-path/favorites.json
{
"color": "red"
}
./save-data-path/index.js
/*
This is a list of the names of the save data js files. I could have put 'favorites.js'
in this list, but this way requires less typing
*/
module.exports = [
'favorites'
];
./index.js
Creating a SaveDataCollection
:
const {SaveDataCollection} = koolSaveData;
let saveDataCollection = new SaveDataCollection('./save-data-path');
saveDataCollection.setData('favorites', {color: 'green'});
console.log(saveDataCollection.getData('favorites')); // { color: green }
saveDataCollection.modifyData('favorites', {season: 'summer'});
console.log(saveDataCollection.getData('favorites')); // { color: 'green', season: 'summer' }
saveDataCollection.setData('favorites', {season: 'spring'});
console.log(saveDataCollection.getData('favorites')); // { season: 'spring' }
saveDataCollection.modifyData('favorites', {color: 'blue'});
console.log(saveDataCollection.getData('favorites')); // { season: 'spring', color: 'blue' }
saveDataCollection.save();
All data will be saved in their corresponding json files after calling saveDataCollection.save()
like so:
favorites.json
:
{
"season": "spring",
"color": "blue"
}