3.2.1 • Published 1 year ago

pg-test-util v3.2.1

Weekly downloads
171
License
MIT
Repository
github
Last release
1 year ago

pg-test-util

PostgreSQL administrative utilities such as creating and dropping tables, users etc.

Synopsis

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([]);
  });
});

Details

<% if (typedoc) { %>

API

Table of contents

Classes

Interfaces

Type aliases

Type aliases

EntityInfo

Ƭ EntityInfo: Object

Type to store entity details.

Type declaration

NameTypeDescription
namestringEntity name
schemastringSchema name of the entity.

Defined in

types.ts:2


SequenceInfo

Ƭ SequenceInfo: Object

Type declaration

NameTypeDescription
columnstringColumn name which sequence is related to.
namestringName of the sequence
schemastringSchema name of the table sequence is defined.
tablestringTable name of the sequence.

Defined in

types.ts:9

Classes

pg-test-util / Database

Class: Database

Execute tasks related to individual database such as connecting, querying, getting tables, getting sequences etc.

Table of contents

Properties

Accessors

Methods

Properties

client

Readonly client: Client

node-postgres client

Defined in

database.ts:35


drop

Readonly drop: () => Promise<void>

Type declaration

▸ (): Promise<void>

Drops the database.

Returns

Promise<void>

Defined in

database.ts:38

Accessors

name

get name(): string

Name of the database

Returns

string

Defined in

database.ts:62

Methods

connect

connect(): Promise<void>

Connects to database.

Returns

Promise<void>

Defined in

database.ts:67


disconnect

disconnect(): Promise<void>

Disconnects from database.

Returns

Promise<void>

Defined in

database.ts:78


getMaterializedViews

getMaterializedViews(): Promise<EntityInfo[]>

Returns materialized views from database. Uses cache for fast results. Use refresh() method to refresh the cache.

Returns

Promise<EntityInfo[]>

Defined in

database.ts:136


getPartitionedTables

getPartitionedTables(): Promise<EntityInfo[]>

Returns partitioned tables from database. Uses cache for fast results. Use refresh() method to refresh the cache.

Returns

Promise<EntityInfo[]>

Defined in

database.ts:142


getSequences

getSequences(): Promise<SequenceInfo[]>

Returns sequences from database. Uses cache for fast results. Use refresh() method to refresh the cache.

Returns

Promise<SequenceInfo[]>

Defined in

database.ts:148


getTables

getTables(): Promise<EntityInfo[]>

Returns tables from database. Uses cache for fast results. Use refresh() method to refresh the cache.

Returns

Promise<EntityInfo[]>

Defined in

database.ts:124


getViews

getViews(): Promise<EntityInfo[]>

Returns views from database. Uses cache for fast results. Use refresh() method to refresh the cache.

Returns

Promise<EntityInfo[]>

Defined in

database.ts:130


query

query<T>(sql, params?): Promise<T[]>

Executes given SQL and returns result rows.

Type parameters

NameTypeDescription
Tanyis type for single row returned by SQL query.

Parameters

NameTypeDescription
sqlstringis sql query.
params?any[]are array of parameters to pass query.

Returns

Promise<T[]>

result rows of the SQL query.

Defined in

database.ts:207


queryFile

queryFile<T>(file, params?): Promise<T[]>

Reads and executes SQL in given file and returns results.

Type parameters

NameTypeDescription
Tanyis type for single row returned by SQL query.

Parameters

NameTypeDescription
filestringis file to read SQL from.
params?any[]are array of parameters to pass query.

Returns

Promise<T[]>

result rows of the SQL query.

Defined in

database.ts:228


refresh

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.

Returns

Promise<void>

Defined in

database.ts:89


syncSequences

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.

Returns

Promise<void>

Defined in

database.ts:154


truncate

truncate(ignore?): Promise<void>

Truncates all tables and resets their sequences in the database.

Parameters

NameTypeDescription
ignoreObjectare the list of the tables to ignore.
ignore.ignore?string[]-

Returns

Promise<void>

Defined in

database.ts:180

pg-test-util / default

Class: default

PgTestUtil class is used to perform PostgreSQL operations related to unit testing such as create database, truncate database and drop database etc.

Table of contents

Methods

Methods

cleanup

cleanup(): Promise<void>

Returns

Promise<void>

Defined in

pg-test-util.ts:310


copyDatabase

copyDatabase(options): Promise<Database>

Copies a given database with a new name.

Parameters

NameTypeDescription
optionsObjectis configuration.
options.drop?booleanis whether to drop target database before copy.
options.safe?booleanIf true, only databases created by this instance is dropped.
options.source?string | Database-
options.target?string | Database-

Returns

Promise<Database>

Database object.

Defined in

pg-test-util.ts:189


createDatabase

createDatabase(options?): Promise<Database>

Creates a database. If name is not provided generates a name using baseName from constructor and part of epoch time.

Parameters

NameTypeDescription
optionsObjectis configuration
options.drop?booleanis whether to drop database before create command.
options.encoding?stringis database encoding
options.file?stringis SQL query file to execute on database after it is created.
options.name?stringis database name
options.safe?booleanIf true, only databases created by this instance is dropped.
options.sql?stringis SQL query to execute on database after it is created.
options.template?stringis database template to use.

Returns

Promise<Database>

Database object representing created database.

Defined in

pg-test-util.ts:148


createUser

createUser(user, password): Promise<void>

Creates a new database user if it does not exist.

Parameters

NameTypeDescription
userstringis the name of the user.
passwordstringis the password for the user.

Returns

Promise<void>

Defined in

pg-test-util.ts:256


disconnect

disconnect(): Promise<void>

Disconnects admin client.

Returns

Promise<void>

Defined in

pg-test-util.ts:109


disconnectAll

disconnectAll(options?): Promise<void[]>

Disconnects all clients.

Parameters

NameTypeDescription
optionsObjectare options.
options.adminundefined | booleanwhether to disconnect admin client.

Returns

Promise<void[]>

Defined in

pg-test-util.ts:123


dropAll

dropAll(options?): Promise<void>

Drops all items created by this instance.

Parameters

NameTypeDescription
optionsObjectare options.
options.disconnectundefined | booleanis whether to disconnect admin client.

Returns

Promise<void>

Defined in

pg-test-util.ts:306


dropAllDatabases

dropAllDatabases(options?): Promise<void>

Drops all databases created by this instance.

Parameters

NameTypeDescription
optionsObjectare options.
options.disconnectundefined | booleanis whether to disconnect admin client.

Returns

Promise<void>

Defined in

pg-test-util.ts:245


dropAllUsers

dropAllUsers(): Promise<void>

Drops all users created by this instance.

Returns

Promise<void>

Defined in

pg-test-util.ts:295


dropConnections

dropConnections(databaseName): Promise<void>

Parameters

NameType
databaseNamestring

Returns

Promise<void>

Defined in

pg-test-util.ts:235


dropDatabase

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.

Parameters

NameTypeDescription
databasestring | Databaseis database name or Database instance to drop.
optionsObjectare options
options.safeundefined | booleanIf true, only databases created by this instance is dropped.

Returns

Promise<void>

Defined in

pg-test-util.ts:217


dropUser

dropUser(user, options?): Promise<void>

Drops database user.

Parameters

NameTypeDescription
userstringis user name to drop.
optionsObjectare options.
options.safeundefined | booleanIf true, only users created by this instance is dropped.

Returns

Promise<void>

Defined in

pg-test-util.ts:286


fetchAllDatabaseNames

fetchAllDatabaseNames(onlyCreated?): Promise<string[]>

Fetches the list of all databases from server.

Parameters

NameType
onlyCreated?boolean

Returns

Promise<string[]>

Defined in

pg-test-util.ts:130


getDatabase

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.

Parameters

NameTypeDescription
namestringis database name to get instance for. defaultDatabaseName is used by default.

Returns

Promise<Database>

Database instance for given database name.

Defined in

pg-test-util.ts:96


getUserNames

getUserNames(onlyCreated?): Promise<string[]>

Fetches database users from database.

Parameters

NameTypeDefault valueDescription
onlyCreatedbooleanfalseis whether to fetch users only created by this utility instance.

Returns

Promise<string[]>

array of usernames.

Defined in

pg-test-util.ts:273


query

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.

Type parameters

NameTypeDescription
Tanyis type for single row returned by SQL query.

Parameters

NameTypeDescription
sqlstringis sql query.
params?any[]are array of parameters to pass query.

Returns

Promise<T[]>

result rows of the SQL query.

Defined in

pg-test-util.ts:81


new

Static new(connection, options?): Promise<default>

Create an instance.

Parameters

NameTypeDescription
connectionstring | Client | ClientConfigis the pg.client or connection parameters for pg.client.
optionsOptionsare options.

Returns

Promise<default>

Defined in

pg-test-util.ts:45

Interfaces

pg-test-util / Options

Interface: Options

Options

Table of contents

Properties

Properties

baseName

Optional baseName: string

Prefix to be used when creating new databases.

Defined in

pg-test-util.ts:12


cleanupOnError

Optional cleanupOnError: boolean

Whether to drop all created objects if error is thorwn.

Defined in

pg-test-util.ts:14


database

Optional database: string

Admin database name to connect. To create other databases we need to connect a database.

Defined in

pg-test-util.ts:16


safe

Optional safe: boolean

Drop only objects created by this instance.

Defined in

pg-test-util.ts:10

<% } %>

3.2.1

1 year ago

3.2.0

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.9

4 years ago

2.0.10

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

5 years ago

2.0.4

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.3.4

7 years ago

1.3.3

8 years ago

1.3.2

8 years ago

1.2.3

9 years ago

1.2.2

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago