UNPKG

quickpostgres

Version:

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

184 lines (183 loc) 7.18 kB
/// <reference types="node" /> import EventEmitter from "events"; import { Client as PgClient } from "pg"; export declare type ClientOptions = { target?: string | null; table?: string | null; }; export declare type Params = { id?: any; data?: any; ops: Options; }; export declare type Options = { table?: string; target?: any[]; }; export declare type Methods = "fetch" | "set" | "add" | "subtract" | "push" | "delete" | "has" | "clear" | "drop" | "all" | "type"; export declare class Client extends EventEmitter { /** * PostgreSQL database url * @type {string} * @readonly */ readonly dbUrl: string; /** * Client options * @type {ClientOptions | undefined} * @readonly */ readonly options: ClientOptions | undefined; /** * Default table name to use * @type {string | undefined} */ tableName: string | undefined; /** * The `pg` client instance for your database * @type {PgClient} */ client: PgClient; /** * Whether the database has been connected or not * @see connect * @see end */ connected: boolean; constructor(dbUrl: string, options?: ClientOptions); /** * Connects to the database * @returns {Promise<Client>} the client * @example await db.connect(); */ connect(): Promise<Client>; /** * Ends the connection to the database * @returns {Promise<Client>} the client * @example await db.end(); */ end(): Promise<Client>; /** * 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"); */ fetch(key: string, ops?: Options): Promise<any>; /** * 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"); */ get(key: string, ops?: Options): Promise<any>; /** * 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); */ set(key: string, value: any, ops?: Options): Promise<any>; /** * 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); */ add(key: string, value: any, ops?: Options): Promise<any>; /** * 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); */ subtract(key: string, value: any, ops?: Options): Promise<any>; /** * 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"); */ push(key: string, value: any, ops?: Options): Promise<any>; /** * 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"); */ delete(key: string, ops?: Options): Promise<boolean>; /** * 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"); */ has(key: string, ops?: Options): Promise<boolean>; /** * 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"); */ includes(key: string, ops?: Options): Promise<boolean>; /** * 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(); */ clear(): Promise<number>; /** * Deletes the entire active table. * @returns {Promise<void>} void * @example await db.drop(); */ drop(): Promise<void>; /** * 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(); */ all(ops?: Options): Promise<any>; /** * 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(); */ fetchAll(ops?: Options): Promise<any>; /** * 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` */ type(key: string, ops?: Options): Promise<"bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined">; /** * @private Arbitrate * @param {PgClient} client * @param {Params} params * @param {Options} options */ private arbitrate; }