UNPKG

quickpostgres

Version:

An easy, beginner-friendly PostgreSQL database wrapper similar to quick.db.

288 lines 11.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; const events_1 = __importDefault(require("events")); const pg_1 = require("pg"); const add_1 = __importDefault(require("./methods/add")); const all_1 = __importDefault(require("./methods/all")); const clear_1 = __importDefault(require("./methods/clear")); const delete_1 = __importDefault(require("./methods/delete")); const drop_1 = __importDefault(require("./methods/drop")); const fetch_1 = __importDefault(require("./methods/fetch")); const has_1 = __importDefault(require("./methods/has")); const push_1 = __importDefault(require("./methods/push")); const set_1 = __importDefault(require("./methods/set")); const subtract_1 = __importDefault(require("./methods/subtract")); const type_1 = __importDefault(require("./methods/type")); class Client extends events_1.default { constructor(dbUrl, options) { var _a; super(); this.dbUrl = dbUrl; this.options = options; this.tableName = (_a = options === null || options === void 0 ? void 0 : options.table) !== null && _a !== void 0 ? _a : undefined; this.client = new pg_1.Client(this.dbUrl); this.connected = false; } /** * Connects to the database * @returns {Promise<Client>} the client * @example await db.connect(); */ async connect() { if (this.connected) throw new Error("Client has already been connected."); await this.client.connect(); this.connected = true; /** * Emitted when the database has been connected * @event Client#ready * @param {Client} client the client */ this.emit("ready", this); return this; } /** * Ends the connection to the database * @returns {Promise<Client>} the client * @example await db.end(); */ async end() { if (!this.connected) throw new Error("Client has not been connected yet."); await this.client.end(); this.connected = false; /** * Emitted when the database connection has been ended * @event Client#end * @param {Client} client the client */ this.emit("end", this); return this; } /** * Fetches data from a key in the database * @param {string} key any string as a key, allows dot notation * @param {Options} options any options to be added to the request * @returns {Promise<any>} the data requested * @alias Client#get * @example const data = await db.fetch("users.1234567890.inventory"); */ async fetch(key, ops) { if (!key) throw new TypeError("No key specified."); return await this.arbitrate(fetch_1.default, { id: key, ops: ops || {} }); } /** * Fetches data from a key in the database * @param {string} key any string as a key, allows dot notation * @param {options} options any options to be added to the request * @returns {Promise<any>} the data requested * @alias Client#fetch * @example const data = await db.fetch("users.1234567890.inventory"); */ async get(key, ops) { return await this.fetch(key, ops); } /** * Sets new data based on a key in the database * @param {string} key any string as a key, allows dot notation * @param value value of the data to be set * @param {Options} options any options to be added to the request * @returns {Promise<any>} the updated data * @example const data = await db.set("users.1234567890.level", 100); */ async set(key, value, ops) { if (!key) throw new TypeError("No key specified."); return await this.arbitrate(set_1.default, { id: key, data: value, ops: ops || {}, }); } /** * Adds a number to a key in the database. If no existing number, it will add to 0 * @param {string} key any string as a key, allows dot notation * @param value value to add * @param {Options} options any options to be added to the request * @returns {Promise<any>} the updated data * @example const data = await db.add("users.1234567890.level", 1); */ async add(key, value, ops) { if (!key) throw new TypeError("No key specified."); if (isNaN(value)) throw new TypeError("No value specified to add."); return await this.arbitrate(add_1.default, { id: key, data: value, ops: ops || {}, }); } /** * Subtracts a number to a key in the database. If no existing number, it will subtract to 0 * @param {string} key any string as a key, allows dot notation * @param value value to subtract * @param {Options} options any options to be added to the request * @returns {Promise<any>} the updated data * @example const data = await db.subtract("users.1234567890.level", 10); */ async subtract(key, value, ops) { if (!key) throw new TypeError("No key specified."); if (isNaN(value)) throw new TypeError("No value specified to subtract."); return await this.arbitrate(subtract_1.default, { id: key, data: value, ops: ops || {}, }); } /** * Push into an array in the database based on the key. If no existing array, it will create one * @param {string} key any string as a key, allows dot notation * @param value value to push * @param {Options} options any options to be added to the request * @returns {Promise<any>} the updated data * @example const data = await db.push("users.1234567890.inventory", "Slice of Cheese"); */ async push(key, value, ops) { if (!key) throw new TypeError("No key specified."); if (!value && value != 0) throw new TypeError("No value specified to push."); return await this.arbitrate(push_1.default, { id: key, data: value, ops: ops || {}, }); } /** * Delete an object (or property) in the database * @param {string} key any string as a key, allows dot notation * @param {Options} options any options to be added to the request * @returns {boolean} `true` if success, if not found `false` * @example await db.delete("users.1234567890"); */ async delete(key, ops) { if (!key) throw new TypeError("No key specified."); return await this.arbitrate(delete_1.default, { id: key, ops: ops || {} }); } /** * Returns a boolean indicating whether an element with the specified key exists or not * @param {string} key any string as a key, allows dot notation * @param {Options} options any options to be added to the request * @returns {boolean} boolean * @alias Client#includes * @example const data = await db.has("users.1234567890"); */ async has(key, ops) { if (!key) throw new TypeError("No key specified."); return await this.arbitrate(has_1.default, { id: key, ops: ops || {} }); } /** * Returns a boolean indicating whether an element with the specified key exists or not. * @param {string} key any string as a key, allows dot notation * @param {Options} options any options to be added to the request * @returns {Promise<boolean>} boolean * @alias Client#has * @example const data = await db.has("users.1234567890"); */ async includes(key, ops) { return await this.has(key, ops); } /** * Deletes all rows from the entire active table. * Note: This does not delete the table itself. To delete the table itself along with the rows, use `drop()`. * @returns {Promise<number>} amount of rows deleted * @example const data = await db.clear(); */ async clear() { return await this.arbitrate(clear_1.default, { ops: {} }, this.tableName); } /** * Deletes the entire active table. * @returns {Promise<void>} void * @example await db.drop(); */ async drop() { return await this.arbitrate(drop_1.default, { ops: {} }, this.tableName); } /** * Fetches the entire active table * @param {Options} options any options to be added to the request * @returns {Promise<any>} entire table as an object * @alias Client#fetchAll * @example const data = await db.all(); */ async all(ops) { return await this.arbitrate(all_1.default, { ops: ops || {} }); } /** * Fetches the entire active table * @param {Options} options any options to be added to the request * @returns {Promise<any>} entire table as an object * @alias Client#all * @example const data = await db.all(); */ async fetchAll(ops) { return await this.arbitrate(all_1.default, { ops: ops || {} }); } /** * Used to get the type of the value * @param {string} key any string as a key, allows dot notation * @param {Options} options any options to be added to the request * @returns {Promise<"bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined">} type from `typeof` */ async type(key, ops) { if (!key) throw new TypeError("No key specified."); return await this.arbitrate(type_1.default, { id: key, ops: ops || {} }); } /** * @private Arbitrate * @param {PgClient} client * @param {Params} params * @param {Options} options */ async arbitrate(method, params, tableName) { var _a, _b; if (!this.connected) throw new Error("Database is not connected. Use `connect()` to connect."); if (typeof params.id == "number") params.id = params.id.toString(); const options = { table: ((_a = this.options) === null || _a === void 0 ? void 0 : _a.table) || params.ops.table || tableName || "quickpostgres", }; // Access database await this.client.query(`CREATE TABLE IF NOT EXISTS ${options.table} (ID TEXT, json TEXT)`); // Verify options if (params.ops.target && params.ops.target[0] === ".") params.ops.target = params.ops.target.slice(1); if (params.data && params.data === Infinity) throw new TypeError(`Infinity cannot be used as a field. (ID: ${params.id})`); // Stringify try { params.data = JSON.stringify(params.data); } catch (error) { throw new TypeError(`Please supply a valid input. (ID: ${params.id})\nError: ${error}`); } // Translate dot notation from keys if ((_b = params.id) === null || _b === void 0 ? void 0 : _b.includes(".")) { const unparsed = params.id.split("."); params.id = unparsed.shift(); params.ops.target = unparsed.join("."); } // Run and return method return await method(this.client, params, options); } } exports.Client = Client; //# sourceMappingURL=index.js.map