2.1.0 • Published 5 months ago

@voxpelli/pg-utils v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@voxpelli/pg-utils

My personal database utils / helpers for Postgres

npm version npm downloads neostandard javascript style Module type: ESM Types in JS Follow @voxpelli@mastodon.social

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

PgTestHelpersOptions

  • connectionString`string | – a connection string for the postgres database
  • fixtureFolder[string | URL]optional – the path to a folder of .csv-file fixtures named by their respective table
  • schemastring | URL | Umzug – an umzug instance that can be used to initialize tables or the schema itself or a URL to a text file containing the schema
  • tablesWithDependencies[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 tables
  • insertFixtures() => Promise<void> – inserts all the fixtures data into the tables (only usable if fixtureFolder has been set)
  • removeTables() => Promise<void> – removes all of the tables (starting with tablesWithDependencies)

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

  • poolstring | pg.Pool – a postgres pool to use for the queries or a connection string that will be used to create one
  • pathstring | URL – the path to the folder that contains the CSV:s named by their table names
  • tablesWithDependencies[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

2.1.0

5 months ago

2.0.0

5 months ago

1.2.0

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.0

7 months ago