UNPKG

kool-save-data

Version:

A simple save data module for my Node.js projects.

71 lines (56 loc) 1.51 kB
'use-strict'; const path = require('path'); class SaveDataCollection { constructor(dataPath, savePath) { this.dataPath = path.resolve(dataPath); this.savePath = path.resolve(savePath); this.collection = new Map(); this.load(); } getDataPath() { return this.dataPath; } setDataPath(newPath) { this.dataPath = path.resolve(newPath); } getSavePath() { return this.savePath; } setSavePath(newPath) { this.savePath = path.resolve(newPath); } getData(id) { return this.collection.get(id).data; } setData(id, data) { this.collection.get(id).data = data; } modifyData(id, data) { this.collection.get(id).modifyData(data); } getSaved(savePath = this.savePath) { savePath = path.resolve(savePath); let files = require(this.dataPath), dataCollection = new Map(); files.forEach(file => { let data = require(path.join(this.dataPath, file)); data.load(savePath); dataCollection.set(data.id, data); }); return dataCollection; } load(savePath = this.savePath) { savePath = path.resolve(savePath); this.collection = this.getSaved(savePath); } save(savePath = this.savePath, collection = this.collection) { savePath = path.resolve(savePath); collection.forEach(data => { data.save(savePath); }); } clear() { this.collection.clear(); } } module.exports = SaveDataCollection;