UNPKG

nope.db

Version:

A modern, simple, and async JSON database for Node.js with zero dependencies and a data-safe queue.

182 lines (181 loc) 6.25 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.NopeDB = void 0; const utils_js_1 = require("./utils.js"); const error_js_1 = require("./error.js"); const pathUtils = __importStar(require("path")); class NopeDB { settings; storage; /** * Creates a new database instance or loads an existing one. * @param {NopeDbSettings} [settings] Database settings. * @throws {DatabaseError} Throws an error if the provided settings are invalid. */ constructor(settings = {}) { const defaultPath = pathUtils.join(process.cwd(), 'db.json'); const path = settings.path ?? defaultPath; const spaces = settings.spaces ?? 2; const separator = settings.separator ?? '.'; if (typeof path !== 'string') throw new error_js_1.DatabaseError("The 'path' setting must be a string."); if (!path.endsWith(".json")) throw new error_js_1.DatabaseError("The database 'path' must end with '.json'."); if (typeof spaces !== 'number' || spaces < 0) throw new error_js_1.DatabaseError("The 'spaces' setting must be a positive number."); if (typeof separator !== 'string') throw new error_js_1.DatabaseError("The 'separator' setting must be a string."); if (separator.length !== 1) throw new error_js_1.DatabaseError("Invalid 'separator'. Must be a single character."); this.settings = { path: path, spaces: spaces, separator: separator, file: pathUtils.resolve(path) }; this.storage = new utils_js_1.StorageManager({ file: this.settings.file, spaces: this.settings.spaces, separator: this.settings.separator, }); } /** * Adds a value to an element in the database. * @returns {Promise<number>} The total result. */ add(id, value) { return this.storage.add(id, value); } /** * Returns all data in the database. * @returns {Promise<object>} All data. */ all() { return this.storage.all(); } /** * Clears all data in the database. * Requires a confirmation object to prevent accidental deletion. * @param {ClearOptions} options Must be { confirm: true } * @returns {Promise<true>} */ clear(options) { return this.storage.clear(options); } /** * Deletes an element from the database. * @returns {Promise<boolean>} Indicates if the deletion was successful. */ delete(id) { return this.storage.delete(id); } /** * Gets an element from the database. * @returns {Promise<any>} The requested data. */ get(id) { return this.storage.get(id); } /** * Checks if data exists in the database. * @returns {Promise<boolean>} Indicates the presence of the data. */ has(id) { return this.storage.has(id); } /** * Pushes an element into an array in the database. * @returns {Promise<any[]>} The updated array. */ push(id, value) { return this.storage.push(id, value); } /** * Sets or updates an element in the database. * @returns {Promise<any>} The value that was set. */ set(id, value) { return this.storage.set(id, value); } /** * Subtracts a value from an element in the database. * @returns {Promise<number>} The remaining result. */ subtract(id, value) { return this.storage.subtract(id, value); } /** * Creates a backup of the current database to a new file. * @param {string} filePath The full path for the backup file (e.g., './my-backup.json'). * @returns {Promise<true>} */ backup(filePath) { return this.storage.backup(filePath); } /** * Loads data from a backup file, overwriting the current database. * @param {string} filePath The full path of the backup file to load. * @returns {Promise<true>} */ loadBackup(filePath) { return this.storage.loadBackup(filePath); } // --- Aliases --- /** * Gets an element from the database (alias for get). * @returns {Promise<any>} The requested data. */ fetch(id) { return this.get(id); } /** * Deletes an element from the database (alias for delete). * @returns {Promise<boolean>} Indicates if the deletion was successful. */ remove(id) { return this.delete(id); } /** * Clears all data in the database (alias for clear). * Requires a confirmation object to prevent accidental deletion. * @param {ClearOptions} options Must be { confirm: true } * @returns {Promise<true>} */ reset(options) { return this.clear(options); } } exports.NopeDB = NopeDB;