pg-test-util
Version:
PostgreSQL administrative utilities such as creating and dropping tables, users etc.
153 lines • 6.22 kB
TypeScript
import { Client, ClientConfig } from "pg";
import Database from "./database";
/** Options */
export interface Options {
/** Drop only objects created by this instance. */
safe?: boolean;
/** Prefix to be used when creating new databases. */
baseName?: string;
/** Whether to drop all created objects if error is thorwn. */
cleanupOnError?: boolean;
/** Admin database name to connect. To create other databases we need to connect a database. */
database?: string;
}
/** PgTestUtil class is used to perform PostgreSQL operations related to unit testing such as create database, truncate database and drop database etc. */
export default class PgTestUtil {
#private;
private constructor();
/**
* Create an instance.
*
* @param connection is the `pg.client` or connection parameters for `pg.client`.
* @param options are options.
*/
static new(connection: Client | ClientConfig | string, options?: Options): Promise<PgTestUtil>;
/**
* Executes given SQL in admin clinet and returns result rows.
* Admin client can be used fro administration queries such as creating databases etc.
*
* @typeparam T is type for single row returned by SQL query.
*
* @param sql is sql query.
* @param params are array of parameters to pass query.
* @returns result rows of the SQL query.
*/
query<T = any>(sql: string, params?: any[]): Promise<T[]>;
/**
* Returns `Database` instance object for given database name. Also connects to database if it is not connected.
* If no connection details are provided, default database is returned using same connection parameters as master database.
*
* @param name is database name to get instance for. `defaultDatabaseName` is used by default.
* @returns [[Database]] instance for given database name.
*/
getDatabase(name?: string): Promise<Database>;
/** Disconnects admin client. */
disconnect(): Promise<void>;
/**
* Disconnects all clients.
*
* @param options are options.
* @param options.admin whether to disconnect admin client.
*/
disconnectAll({ admin }?: {
admin?: boolean | undefined;
}): Promise<void[]>;
/** Fetches the list of all databases from server. */
fetchAllDatabaseNames(onlyCreated?: boolean): Promise<Array<string>>;
/**
* Creates a database. If name is not provided generates a name using `baseName` from constructor and part of epoch time.
*
* @param options is configuration
* @param options.name is database name
* @param options.encoding is database encoding
* @param options.template is database template to use.
* @param options.sql is SQL query to execute on database after it is created.
* @param options.file is SQL query file to execute on database after it is created.
* @param options.drop is whether to drop database before create command.
* @param options.safe If true, only databases created by this instance is dropped.
* @returns [[Database]] object representing created database.
*/
createDatabase({ name, encoding, template, sql, file, drop, safe, }?: {
name?: string;
encoding?: string;
template?: string;
sql?: string;
file?: string;
drop?: boolean;
safe?: boolean;
}): Promise<Database>;
/**
* Copies a given database with a new name.
*
* @param options is configuration.
* @param options.from is source database name or [[Database]] instance to copy from.
* @param options.to is target database name or [[Database]] instance to copy to.
* @param options.drop is whether to drop target database before copy.
* @param options.safe If true, only databases created by this instance is dropped.
* @returns [[Database]] object.
*/
copyDatabase({ source, target, drop, safe, }: {
source?: string | Database;
target?: string | Database;
drop?: boolean;
safe?: boolean;
}): Promise<Database>;
/**
* Drops given database. To ensure the task, drops all connections to the database beforehand.
* If `dropOnlyCreated` is true and database is not created by this instance, throws error.
*
* @param database is database name or [[Database]] instance to drop.
* @param options are options
* @param options.safe If true, only databases created by this instance is dropped.
*/
dropDatabase(database?: string | Database, { safe }?: {
safe?: boolean | undefined;
}): Promise<void>;
dropConnections(databaseName: string): Promise<void>;
/**
* Drops all databases created by this instance.
*
* @param options are options.
* @param options.disconnect is whether to disconnect admin client.
*/
dropAllDatabases({ disconnect }?: {
disconnect?: boolean | undefined;
}): Promise<void>;
/**
* Creates a new database user if it does not exist.
*
* @param user is the name of the user.
* @param password is the password for the user.
*/
createUser(user: string, password: string): Promise<void>;
/**
* Fetches database users from database.
*
* @param onlyCreated is whether to fetch users only created by this utility instance.
* @returns array of usernames.
*/
getUserNames(onlyCreated?: boolean): Promise<Array<string>>;
/**
* Drops database user.
*
* @param user is user name to drop.
* @param options are options.
* @param options.safe If true, only users created by this instance is dropped.
*/
dropUser(user: string, { safe }?: {
safe?: boolean | undefined;
}): Promise<void>;
/** Drops all users created by this instance. */
dropAllUsers(): Promise<void>;
/**
* Drops all items created by this instance.
*
* @param options are options.
* @param options.disconnect is whether to disconnect admin client.
*/
dropAll({ disconnect }?: {
disconnect?: boolean | undefined;
}): Promise<void>;
cleanup(): Promise<void>;
}
//# sourceMappingURL=pg-test-util.d.ts.map