Iterate over rows in a csv file and easily insert them into a postgres database.
Note
This will break up insert commands to limit the maximum number of bytes per statement. This is to allow usage with the Aurora Data API. To remove this limitation, set the
maxChars
setting to NaN
.
First, create an instance:
import { CsvInsert } from "pg-insert-csv";
const insert = CsvInsert((statement: string) => pg.runSql(statement), {
numericColumns: ["total_orders"],
maxChars: NaN
});
Now, open a read stream to a CSV file, and pass it into the new instance:
const reader = fs.createReadStream("some/file/path.csv");
await insert(reader, "some_table");
If you want to display the current progress outside of the default std.err
output
you can specify a callback that accepts the current progress and table name.
const insert = CsvInsert((statement: string) => pg.runSql(statement), {
progressCallback: (progress: number, tableName: string) =>
console.info(`Current progress: ${progress.toFixed(2)}%`)
});
If a specific column needs to be modified before insert, you can do that by defining column transformers.
const insert = CsvInsert((statement: string) => pg.runSql(statement), {
columnTransformers: {
some_column: (value: string) => value.toUpperCase()
}
});
Sometimes non-printable characters can get added to a file when its edited using
an application like Excel. To avoid running into problems, you can set the filterInput
argument to either true
or a regular expression to select the characters to be removed.
The default selector is /[^\000-\031]+/gi
, this should remove all non-printable
characters.
const insert = CsvInsert((statement: string) => pg.runSql(statement), {
filterInput: true
});