Takes a specifically-named directory structure of CSV files and conjures bulk insert, update and delete statements and applies them to a PostgreSQL database.
$ npm install supercopy --save
const pg =const supercopy =// Make a new Postgres clientconst client = 'postgres://postgres:postgres@localhost:5432/my_test_db'
||An absolute path pointing to a directory containing action folders. See the File Structure section for more details.|
||When conjuring an
||An array of strings, where each string is a table name. Table inserts will occur in this order and deletes in reverse - use to avoid integrity-constraint errors. If no schema prefix is supplied to a table name, then it's inferred from
||Either a pg client or pool (something with a
||Identifies a PostgreSQL schema where the tables that are to be affected by this copy be found.|
||A flag to indicate whether or not to truncate tables before supercopying into them|
||Show debugging information on the console|
||Enables 'sourceDir' to house many typical Supercopy 'sourceDir' shaped directories. Defaults to false.|
The directory identified by the
sourceDir option should be structured in the following way:
/someDir /inserts table1.csv table2.csv /updates table1.csv table2.csv /upserts table1.csv table2.csv /deletes table1.csv OR IF USING MULTICOPY /manyDirs /someDir /inserts table1.csv table2.csv /someDir /inserts table1.csv table2.csv
- The sub-directories here refer to the type of action that should be performed using CSV data files contained in it. Supported directory names are
upsert(try to update, failing that insert) and
- The filename of each file should refer to a table name in the schema identified by the
- The expected format of the .csv files is:
- One line per record
- The first line to be a comma delimited list of column names (i.e. a header record)
- For update and upsert files, ensure columns-names in the header record that are part of the primary key are identified with a
- All records to be comma delimited, and any text columns containing a
,should be quoted with a
". The csv-string package might help.
- Note that only primary key values should be provided in a 'delete' file.
Before running these tests, you'll need a test PostgreSQL database available and set a
PG_CONNECTION_STRING environment variable to point to it, for example:
$ npm test