kiran-db
Version:
This is how javascript managa json data as the database
121 lines (120 loc) • 4.04 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class DB {
constructor(name) {
this.data = [];
this.error = { exists: false, message: '' };
this.selected = [];
this.selection = false;
this.name = name + '.json';
this.makesure();
}
makesure() {
const filename = path_1.default.join(path_1.default.resolve(), 'dbs', this.name);
if (!fs_1.default.existsSync(filename))
fs_1.default.writeFileSync(filename, '[]');
}
rewrite(affectedIDs) {
const filename = path_1.default.join(path_1.default.resolve(), 'dbs', this.name);
const datastring = JSON.stringify(this.data);
try {
fs_1.default.writeFileSync(filename, datastring);
console.log('Database re-written');
}
catch (_a) {
this.error = { exists: true, message: 'Failed to re-write database!' };
}
this.end();
return this.error.exists ? this.error : { ok: true, affectedIDs };
}
getselected(field) {
return this.selected.map((selected) => selected[field]);
}
getincrementid() {
const lastdata = this.data[this.data.length - 1];
return lastdata ? lastdata.id + 1 : 1;
}
loopcreate(newdata, status) {
newdata.forEach((data) => {
const created = this.create(data);
status.push(created);
});
return status;
}
end() {
this.selected = [];
this.selection = false;
}
create(newdata) {
if (Array.isArray(newdata))
return this.loopcreate(newdata, []);
this.data = this.read();
const id = this.getincrementid();
this.data.push(Object.assign({ id }, newdata));
return this.rewrite(id);
}
read() {
if (this.selection)
return this.selected;
const filename = path_1.default.join(path_1.default.resolve(), 'dbs', this.name);
const datastring = fs_1.default.readFileSync(filename, 'utf8');
return JSON.parse(datastring);
}
update(newdata) {
const selectedids = this.getselected('id');
if (!selectedids.length)
return { ok: false, message: 'No item selected!' };
this.data = this.data.map((data) => {
if (selectedids.includes(data.id))
for (let [key, val] of Object.entries(newdata))
data[key] = val;
return data;
});
return this.rewrite(selectedids);
}
delete() {
const selectedids = this.getselected('id');
if (!selectedids.length)
return { ok: false, message: 'No item selected' };
this.data = this.data.filter((data) => {
if (!selectedids.includes(data.id))
return data;
});
return this.rewrite(selectedids);
}
where(callback) {
this.selection = false;
this.data = this.read();
this.selected = this.data.filter(callback);
this.selection = true;
return this;
}
join(other, field) {
const selectedids = this.getselected('id');
if (!selectedids.length) {
console.log('No selected items');
return [];
}
else
console.log('Joining into ' + other);
const selecteddata = this.selected.map(selected => {
const joinmodel = new Join(other);
const joindata = joinmodel.where((data) => selectedids.includes(data[field])).read();
selected[other] = joindata;
return selected;
});
this.end();
return selecteddata;
}
}
exports.default = DB;
class Join extends DB {
constructor(name) {
super(name);
}
}