@voxpelli/pg-utils
v1.2.0
Published
My personal database utils / helpers for Postgres
Downloads
295
Readme
@voxpelli/pg-utils
My personal database utils / helpers for Postgres
Usage
import {
csvFromFolderToDb,
PgTestHelpers,
} from '@voxpelli/pg-utils';
const pgHelpers = new PgTestHelpers({
connectionString: 'postgres://user:pass@localhost/example',
fixtureFolder: new URL('./fixtures', import.meta.url),
schema: new URL('./create-tables.sql', import.meta.url),,
tablesWithDependencies: [
'abc',
['foo', 'bar'],
]
});
PgTestHelpers
Class that creates a helpers instance
Syntax
new PgTestHelpers({
connectionString: 'postgres://user:pass@localhost/example',
fixtureFolder: new URL('./fixtures', import.meta.url),
schema: new URL('./create-tables.sql', import.meta.url),,
tablesWithDependencies: [
// ...
]
});
Arguments
options
–PgTestHelpersOptions
PgTestHelpersOptions
connectionString
– _`string | _ – a connection string for the postgres databasefixtureFolder
–[string | URL]
– optional – the path to a folder of.csv
-file fixtures named by their respective tableschema
–string | URL | Umzug
– an umzug instance that can be used to initialize tables or the schema itself or aURL
to a text file containing the schematablesWithDependencies
–[Array<string[] | string>]
– optional – names of tables that depend on other tables. If some of these tables depend on each other, then use nested arrays to ensure that within the same array no two tables depend on each other
Methods
initTables() => Promise<void>
– sets up all of the tablesinsertFixtures() => Promise<void>
– inserts all the fixtures data into the tables (only usable iffixtureFolder
has been set)removeTables() => Promise<void>
– removes all of the tables (starting withtablesWithDependencies
)
csvFromFolderToDb()
Imports data into tables from a folder of CSV files. All files will be imported and they should named by their table names + .csv
.
Syntax
csvFromFolderToDb(pool, path, [tablesWithDependencies]) => Promise<void>
Arguments
pool
–string | pg.Pool
– a postgres pool to use for the queries or a connection string that will be used to create onepath
–string | URL
– the path to the folder that contains the CSV:s named by their table namestablesWithDependencies
–[string[]]
– optional – names of tables that depend on other tables. The first name in this list will have its fixtures inserted last
Returns
Promise
that resolves on completion