A simple save data module for my Node.js projects.
npm install kool-save-datakool-save-datajs
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
`
`js
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
`
$3
`js
const {SaveData} = koolSaveData;
module.exports = new SaveData('save-data');
`
$3
`
{
"color": "red"
}
`
$3
`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'
];
`
$3
Creating a SaveDataCollection:
`js
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"
}
``