PostgreSQL administrative utilities such as creating and dropping tables, users etc.
import PgTestUtil from "../src/index";
let pgTestUtil: PgTestUtil;
beforeAll(async () => {
pgTestUtil = await PgTestUtil.new({ user: "user", password: "password" });
});
afterAll(async () => {
await pgTestUtil.cleanup();
});
describe("pg-test-util", () => {
it("should create database", async () => {
const sql = `CREATE TABLE public.member ( id SERIAL NOT NULL, name TEXT NOT NULL, PRIMARY KEY(id))`;
const database = await pgTestUtil.createDatabase({ sql });
expect(await database.query("SELECT * FROM member")).toEqual([]);
});
});
<%_ if (typedoc) { _%>
Ƭ EntityInfo: Object
Type to store entity details.
Name | Type | Description |
---|---|---|
name |
string |
Entity name |
schema |
string |
Schema name of the entity. |
Ƭ SequenceInfo: Object
Name | Type | Description |
---|---|---|
column |
string |
Column name which sequence is related to. |
name |
string |
Name of the sequence |
schema |
string |
Schema name of the table sequence is defined. |
table |
string |
Table name of the sequence. |
pg-test-util / Database
Execute tasks related to individual database such as connecting, querying, getting tables, getting sequences etc.
- connect
- disconnect
- getMaterializedViews
- getPartitionedTables
- getSequences
- getTables
- getViews
- query
- queryFile
- refresh
- syncSequences
- truncate
• Readonly
client: Client
• Readonly
drop: () => Promise
<void
>
▸ (): Promise
<void
>
Drops the database.
Promise
<void
>
• get
name(): string
Name of the database
string
▸ connect(): Promise
<void
>
Connects to database.
Promise
<void
>
▸ disconnect(): Promise
<void
>
Disconnects from database.
Promise
<void
>
▸ getMaterializedViews(): Promise
<EntityInfo
[]>
Returns materialized views from database. Uses cache for fast results. Use refresh()
method to refresh the cache.
Promise
<EntityInfo
[]>
▸ getPartitionedTables(): Promise
<EntityInfo
[]>
Returns partitioned tables from database. Uses cache for fast results. Use refresh()
method to refresh the cache.
Promise
<EntityInfo
[]>
▸ getSequences(): Promise
<SequenceInfo
[]>
Returns sequences from database. Uses cache for fast results. Use refresh()
method to refresh the cache.
Promise
<SequenceInfo
[]>
▸ getTables(): Promise
<EntityInfo
[]>
Returns tables from database. Uses cache for fast results. Use refresh()
method to refresh the cache.
Promise
<EntityInfo
[]>
▸ getViews(): Promise
<EntityInfo
[]>
Returns views from database. Uses cache for fast results. Use refresh()
method to refresh the cache.
Promise
<EntityInfo
[]>
▸ query<T
>(sql
, params?
): Promise
<T
[]>
Executes given SQL and returns result rows.
Name | Type | Description |
---|---|---|
T |
any |
is type for single row returned by SQL query. |
Name | Type | Description |
---|---|---|
sql |
string |
is sql query. |
params? |
any [] |
are array of parameters to pass query. |
Promise
<T
[]>
result rows of the SQL query.
▸ queryFile<T
>(file
, params?
): Promise
<T
[]>
Reads and executes SQL in given file and returns results.
Name | Type | Description |
---|---|---|
T |
any |
is type for single row returned by SQL query. |
Name | Type | Description |
---|---|---|
file |
string |
is file to read SQL from. |
params? |
any [] |
are array of parameters to pass query. |
Promise
<T
[]>
result rows of the SQL query.
▸ refresh(): Promise
<void
>
Fetches database objects (i.e. tables, sequences) from database and refreshes the cache of the object. If you create new tables etc., you should refresh.
Promise
<void
>
▸ syncSequences(): Promise
<void
>
Set current value of sequence for each column of all tables based on record with maximum number. If there are no record in the table, the value will be set to 1.
Promise
<void
>
▸ truncate(ignore?
): Promise
<void
>
Truncates all tables and resets their sequences in the database.
Name | Type | Description |
---|---|---|
ignore |
Object |
are the list of the tables to ignore. |
ignore.ignore? |
string [] |
- |
Promise
<void
>
pg-test-util / default
PgTestUtil class is used to perform PostgreSQL operations related to unit testing such as create database, truncate database and drop database etc.
- cleanup
- copyDatabase
- createDatabase
- createUser
- disconnect
- disconnectAll
- dropAll
- dropAllDatabases
- dropAllUsers
- dropConnections
- dropDatabase
- dropUser
- fetchAllDatabaseNames
- getDatabase
- getUserNames
- query
- new
▸ cleanup(): Promise
<void
>
Promise
<void
>
▸ copyDatabase(options
): Promise
<Database
>
Copies a given database with a new name.
Name | Type | Description |
---|---|---|
options |
Object |
is configuration. |
options.drop? |
boolean |
is whether to drop target database before copy. |
options.safe? |
boolean |
If true, only databases created by this instance is dropped. |
options.source? |
string | Database |
- |
options.target? |
string | Database |
- |
Promise
<Database
>
Database object.
▸ createDatabase(options?
): Promise
<Database
>
Creates a database. If name is not provided generates a name using baseName
from constructor and part of epoch time.
Name | Type | Description |
---|---|---|
options |
Object |
is configuration |
options.drop? |
boolean |
is whether to drop database before create command. |
options.encoding? |
string |
is database encoding |
options.file? |
string |
is SQL query file to execute on database after it is created. |
options.name? |
string |
is database name |
options.safe? |
boolean |
If true, only databases created by this instance is dropped. |
options.sql? |
string |
is SQL query to execute on database after it is created. |
options.template? |
string |
is database template to use. |
Promise
<Database
>
Database object representing created database.
▸ createUser(user
, password
): Promise
<void
>
Creates a new database user if it does not exist.
Name | Type | Description |
---|---|---|
user |
string |
is the name of the user. |
password |
string |
is the password for the user. |
Promise
<void
>
▸ disconnect(): Promise
<void
>
Disconnects admin client.
Promise
<void
>
▸ disconnectAll(options?
): Promise
<void
[]>
Disconnects all clients.
Name | Type | Description |
---|---|---|
options |
Object |
are options. |
options.admin |
undefined | boolean |
whether to disconnect admin client. |
Promise
<void
[]>
▸ dropAll(options?
): Promise
<void
>
Drops all items created by this instance.
Name | Type | Description |
---|---|---|
options |
Object |
are options. |
options.disconnect |
undefined | boolean |
is whether to disconnect admin client. |
Promise
<void
>
▸ dropAllDatabases(options?
): Promise
<void
>
Drops all databases created by this instance.
Name | Type | Description |
---|---|---|
options |
Object |
are options. |
options.disconnect |
undefined | boolean |
is whether to disconnect admin client. |
Promise
<void
>
▸ dropAllUsers(): Promise
<void
>
Drops all users created by this instance.
Promise
<void
>
▸ dropConnections(databaseName
): Promise
<void
>
Name | Type |
---|---|
databaseName |
string |
Promise
<void
>
▸ dropDatabase(database?
, options?
): Promise
<void
>
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.
Name | Type | Description |
---|---|---|
database |
string | Database |
is database name or Database instance to drop. |
options |
Object |
are options |
options.safe |
undefined | boolean |
If true, only databases created by this instance is dropped. |
Promise
<void
>
▸ dropUser(user
, options?
): Promise
<void
>
Drops database user.
Name | Type | Description |
---|---|---|
user |
string |
is user name to drop. |
options |
Object |
are options. |
options.safe |
undefined | boolean |
If true, only users created by this instance is dropped. |
Promise
<void
>
▸ fetchAllDatabaseNames(onlyCreated?
): Promise
<string
[]>
Fetches the list of all databases from server.
Name | Type |
---|---|
onlyCreated? |
boolean |
Promise
<string
[]>
▸ getDatabase(name?
): Promise
<Database
>
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.
Name | Type | Description |
---|---|---|
name |
string |
is database name to get instance for. defaultDatabaseName is used by default. |
Promise
<Database
>
Database instance for given database name.
▸ getUserNames(onlyCreated?
): Promise
<string
[]>
Fetches database users from database.
Name | Type | Default value | Description |
---|---|---|---|
onlyCreated |
boolean |
false |
is whether to fetch users only created by this utility instance. |
Promise
<string
[]>
array of usernames.
▸ query<T
>(sql
, params?
): Promise
<T
[]>
Executes given SQL in admin clinet and returns result rows. Admin client can be used fro administration queries such as creating databases etc.
Name | Type | Description |
---|---|---|
T |
any |
is type for single row returned by SQL query. |
Name | Type | Description |
---|---|---|
sql |
string |
is sql query. |
params? |
any [] |
are array of parameters to pass query. |
Promise
<T
[]>
result rows of the SQL query.
▸ Static
new(connection
, options?
): Promise
<default
>
Create an instance.
Name | Type | Description |
---|---|---|
connection |
string | Client | ClientConfig |
is the pg.client or connection parameters for pg.client . |
options |
Options |
are options. |
Promise
<default
>
pg-test-util / Options
Options
• Optional
baseName: string
Prefix to be used when creating new databases.
• Optional
cleanupOnError: boolean
Whether to drop all created objects if error is thorwn.
• Optional
database: string
Admin database name to connect. To create other databases we need to connect a database.
• Optional
safe: boolean
Drop only objects created by this instance.
<%_ } _%>