nomadder-client
Version:
A web based nomadic data sharing framework between non-internet connected servers
58 lines (57 loc) • 2.55 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs = __importStar(require("fs"));
var FilePersistanceStrategy = /** @class */ (function () {
function FilePersistanceStrategy(_a) {
var _b = _a.fileLocation, fileLocation = _b === void 0 ? '.' : _b;
this.fileLocation = fileLocation;
}
FilePersistanceStrategy.prototype.persistData = function (db) {
var collectionLocation = this.fileLocation + "/collections";
if (fs.existsSync(collectionLocation)) {
db.groupedServerData.forEach(function (gds) {
fs.writeFile(collectionLocation + "/" + gds.collectionName + ".json", JSON.stringify(gds.data), function (error) {
// Do nothing
// tslint:disable-next-line: no-console
console.log('Error?:', error);
});
});
}
else {
fs.mkdir(collectionLocation, function () {
db.groupedServerData.forEach(function (gds) {
fs.writeFile(collectionLocation + "/" + gds.collectionName + ".json", JSON.stringify(gds.data), function () {
// Do nothing
});
});
});
}
};
FilePersistanceStrategy.prototype.retrieveCache = function (serverId) {
var localData = { id: serverId, groupedServerData: [] };
var collectionPath = this.fileLocation + "/collections";
if (fs.existsSync(collectionPath)) {
var fileNames = fs.readdirSync(collectionPath, 'utf-8');
fileNames.forEach(function (fileName) {
var collectionData = fs.readFileSync(collectionPath + "/" + fileName, 'utf-8');
var collectionDataJson = JSON.parse(collectionData);
var groupedServerDataItem = {
collectionName: fileName.slice(0, -5),
data: collectionDataJson,
};
localData.groupedServerData.push(groupedServerDataItem);
});
}
// TODO: get cached ID somewhere aswell
return localData;
};
return FilePersistanceStrategy;
}());
exports.FilePersistanceStrategy = FilePersistanceStrategy;