helpers.Column(col) → {helpers.Column}

new Column(col) → {helpers.Column}

Read-only structure with details for a single column. Used primarily by ColumnSet.

The class parses details into a template, to be used for query generation.

Parameters:
Name Type Description
col string | helpers.ColumnConfig

Column details, depending on the type.

When it is a string, it is expected to contain a name for both the column and the source property, assuming that the two are the same. The name must adhere to JavaScript syntax for variable names. The name can be appended with any format modifier as supported by as.format (^, ~, #, :csv, :list, :json, :alias, :name, :raw, :value), which is then removed from the name and put into property mod. If the name starts with ?, it is removed, while setting flag cnd = true.

If the string doesn't adhere to the above requirements, the method will throw TypeError = Invalid column syntax.

When col is a simple ColumnConfig-like object, it is used as an input configurator to set all the properties of the class.

Properties:
Name Type Attributes Description
name string

Destination column name + source property name (if prop is skipped). The name must adhere to JavaScript syntax for variables, unless prop is specified, in which case name represents only the column name, and therefore can be any non-empty string.

prop string <optional>

Source property name, if different from the column's name. It must adhere to JavaScript syntax for variables.

It is ignored when it is the same as name.

mod string <optional>

Formatting modifier, as supported by method as.format: ^, ~, #, :csv, :list, :json, :alias, :name, :raw, :value.

cast string <optional>

Server-side type casting, without :: in front.

cnd boolean <optional>

Conditional column flag.

Used by methods update and sets, ignored by methods insert and values. It indicates that the column is reserved for a WHERE condition, not to be set or updated.

It can be set from a string initialization, by adding ? in front of the name.

def * <optional>

Default value for the property, to be used only when the source object doesn't have the property. It is ignored when property init is set.

init helpers.initCB <optional>

Override callback for the value.

skip helpers.skipCB <optional>

An override for skipping columns dynamically.

Used by methods update (for a single object) and sets, ignored by methods insert and values.

It is also ignored when conditional flag cnd is set.

Source:
See:
Returns:
Type
helpers.Column
Example
const pgp = require('pg-promise')({
    capSQL: true // if you want all generated SQL capitalized
});

const {Column} = pgp.helpers;

// creating a column from just a name:
const col1 = new Column('colName');
console.log(col1);
//=>
// Column {
//    name: "colName"
// }

// creating a column from a name + modifier:
const col2 = new Column('colName:csv');
console.log(col2);
//=>
// Column {
//    name: "colName"
//    mod: ":csv"
// }

// creating a column from a configurator:
const col3 = new Column({
    name: 'colName', // required
    prop: 'propName', // optional
    mod: '^', // optional
    def: 123 // optional
});
console.log(col3);
//=>
// Column {
//    name: "colName"
//    prop: "propName"
//    mod: "^"
//    def: 123
// }

Members

(readonly) castText :string

Full-syntax sql type casting, if there is any, or else an empty string.

Type:
  • string
Source:

(readonly) escapedName :string

Escaped name of the column, ready to be injected into queries directly.

Type:
  • string
Source:

(readonly) variable :string

Full-syntax formatting variable, ready for direct use in query templates.

Type:
  • string
Source:
Example
const cs = new ColumnSet([
    'id',
    'coordinate:json',
    {
        name: 'places',
        mod: ':csv',
        cast: 'int[]'
    }
]);

// cs.columns[0].variable = ${id}
// cs.columns[1].variable = ${coordinate:json}
// cs.columns[2].variable = ${places:csv}::int[]

Methods

toString(levelopt) → {string}

Creates a well-formatted multi-line string that represents the object.

It is called automatically when writing the object into the console.

Parameters:
Name Type Attributes Default Description
level number <optional>
0

Nested output level, to provide visual offset.

Source:
Returns:
Type
string