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 ( If the string doesn't adhere to the above requirements, the method will throw TypeError = When |
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Destination column name + source property name (if |
|
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 |
mod |
string |
<optional> |
Formatting modifier, as supported by method as.format: |
cast |
string |
<optional> |
Server-side type casting, without |
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 It can be set from a string initialization, by adding |
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 |
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 |
- 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