tona-db-mini
Version:
Tona-DB mini is a JavaScript library for simulating small local databases in JSON.
97 lines (96 loc) • 3.47 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Collection = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const loader_1 = require("../config/loader");
/**
* Represents a local collection backed by a JSON file.
*/
class Collection {
filePath;
config;
/**
* @param collectionName Name of the collection (used as file name)
* @param config The configuration options
*/
constructor(collectionName, config = (0, loader_1.loadConfig)()) {
this.config = config;
const dir = path_1.default.resolve(this.config.baseDir);
if (!fs_1.default.existsSync(dir))
fs_1.default.mkdirSync(dir, { recursive: true });
this.filePath = path_1.default.resolve(dir, `${collectionName}.json`);
if (!fs_1.default.existsSync(this.filePath))
fs_1.default.writeFileSync(this.filePath, "[]", "utf-8");
}
/**
* Reads and parses the JSON data
* @returns {T[]} The parsed data
*/
read() {
if (!fs_1.default.existsSync(this.filePath)) {
this.write([]);
return [];
}
const data = fs_1.default.readFileSync(this.filePath, "utf-8");
return JSON.parse(data);
}
/**
* Serializes and writes data to the file
* @param data The data to write
*/
write(data) {
fs_1.default.writeFileSync(this.filePath, JSON.stringify(data, null, this.config.prettyJson ? 2 : undefined), "utf-8");
}
/**
* Checks if an item matches the provided filter.
* @param item The item to check
* @param filter The filter to apply
* @returns {boolean} Whether the item matches the filter
*/
matchesFilter(item, filter) {
if (typeof filter === "function")
return filter(item);
return Object.entries(filter).every(([key, val]) => item[key] === val);
}
/**
* Returns items matching the provided filter.
* @param filter The filter to apply (object or predicate function)
* @returns {T[]} The filtered items
*/
get(filter = {}) {
return this.read().filter((item) => this.matchesFilter(item, filter));
}
/**
* Adds one or more items to the collection.
* @param data A single item or array of items to add
*/
add(data) {
const itemsToAdd = Array.isArray(data) ? data : [data];
const current = this.read();
this.write([...current, ...itemsToAdd.filter(Boolean)]);
}
/**
* Deletes items matching the provided filter.
* @param filter The filter to apply (object or predicate function)
*/
del(filter = {}) {
const items = this.read();
const remaining = items.filter((item) => !this.matchesFilter(item, filter));
this.write(remaining);
}
/**
* Updates items matching the provided filter.
* @param filter The filter to apply (object or predicate function)
* @param data The partial data to update on each matching item
*/
update(filter, data) {
const current = this.read();
const updated = current.map((item) => this.matchesFilter(item, filter) ? { ...item, ...data } : item);
this.write(updated);
}
}
exports.Collection = Collection;