zexson_toolkit
Version:
Zexson Toolkit is a powerful encryption and tokenization library developed by Zexson Team. It offers proprietary encryption algorithms, high-security random token generation, and advanced object comparison features. It includes many advanced security func
68 lines (67 loc) • 2.33 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonModel = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
class JsonModel {
dist;
data;
constructor(dist = "db.json") {
this.dist = path_1.default.join(process.cwd(), dist);
if (!(0, fs_1.existsSync)(this.dist))
(0, fs_1.writeFileSync)(this.dist, JSON.stringify({}), "utf-8");
this.data = this.readData();
}
readData() {
try {
return JSON.parse((0, fs_1.readFileSync)(this.dist, "utf-8"));
}
catch (error) {
console.error("Dosya okunurken hata oluştu:", error);
return {};
}
}
async saveData() {
try {
(0, fs_1.writeFileSync)(this.dist, JSON.stringify(this.data, null, 2), 'utf-8');
}
catch (error) {
console.error("Dosya yazılırken hata oluştu:", error);
}
}
getAll(table) {
return this.data[table] || null;
}
select(table, where) {
const tableData = this.getAll(table);
if (!tableData)
return [];
return tableData.filter((row) => Object.entries(where).every(([key, value]) => row[key] === value));
}
async insert(table, newData) {
if (!this.data[table])
this.data[table] = [];
this.data[table].push(newData);
await this.saveData();
}
async update(table, where, newValues) {
const tableData = this.getAll(table);
if (!tableData)
return;
this.data[table] = tableData.map((row) => Object.entries(where).every(([key, value]) => row[key] === value)
? { ...row, ...newValues }
: row);
await this.saveData();
}
async delete(table, where) {
const tableData = this.getAll(table);
if (!tableData)
return;
this.data[table] = tableData.filter((row) => !Object.entries(where).every(([key, value]) => row[key] === value));
await this.saveData();
}
}
exports.JsonModel = JsonModel;
;