DbCsvPrinter - do-/node-doix-db GitHub Wiki

DbCsvPrinter is a Transform converting:

  • an input readable stream of objects representing records
  • into the corresponding binary (utf-8 encoded) CSV stream.

It's designed mainly to bulk write data into databases, but may be used for export into files etc.

const toCSV = new DbCsvPrinter ({
   table: db.model.find ('users'),
   columns: ['uuid', 'label'],
// columns: {uuid: 'UUID!', label: 'VARCHAR (255)'}, // ad hoc definition
// lang: db.lang, // needed unless `table` is set
// NULL: '\\N',
})

myReadableObjectStream.pipe (toCSV).pipe (myWritableBinaryStream)

Options

Name Type Description
table DbRelation The dataset description. May be omitted
columns [String] or {String: String} or {String: DbColumn} List of table's column names or a name keyed object of DbColumn definitions
lang DbLang Needed to construct the DbColumn array if table is not provided
NULL String How to represent NULL values in CSV text. Zero length string by default, may be overridden with '\\N' and other special values.

Encoding rules

Each record is printed as a text line terminated with '\n'.

Fields are separated by ','.

null and undefined (including missing) values are printed as the copy of NULL option. By default, it's a zero length string.

false and true are always printed as 0 and 1, respectively.

Infinity values are not supported.

For DbTypeCharacter columns, the content is enclosed in double quotes ("), internal double quotes are doubled.

For DbTypeArithmeticFixed columns, the values are formatted with toFixed ().

For DbTypeDate columns, Dates are formatted as YYYY-MM-DD (ISO), string values are truncated to 10 characters.

For DbTypeTimestamp columns, Dates are formatted as YYYY-MM-DD hh:mm:ss.iii (ISO). Without the scale set for the column to a positive number, the value is truncated to 19 characters. With a positive scale, up do 1 + scale more characters are printed (. + the fractional part).