UNPKG

nope.db

Version:

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

145 lines (144 loc) 4.7 kB
import { StorageManager } from "./utils.js"; import { DatabaseError } from "./error.js"; import * as pathUtils from "path"; export 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 DatabaseError("The 'path' setting must be a string."); if (!path.endsWith(".json")) throw new DatabaseError("The database 'path' must end with '.json'."); if (typeof spaces !== 'number' || spaces < 0) throw new DatabaseError("The 'spaces' setting must be a positive number."); if (typeof separator !== 'string') throw new DatabaseError("The 'separator' setting must be a string."); if (separator.length !== 1) throw new DatabaseError("Invalid 'separator'. Must be a single character."); this.settings = { path: path, spaces: spaces, separator: separator, file: pathUtils.resolve(path) }; this.storage = new 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); } }