The createTable
and addColumns
methods both take a columns
argument that specifies column names and options. It is a object (key/value) where each key is the name of the column, and the value is another object that defines the options for the column.
type
[string] - data type (use normal postgres types)collation
[string] - collation of data typeunique
[boolean] - set to true to add a unique constraint on this columnprimaryKey
[boolean] - set to true to make this column the primary keynotNull
[boolean] - set to true to make this column not nulldefault
[string] - adds DEFAULT clause for column. Accepts null, a literal value, or apgm.func()
expression.check
[string] - sql for a check constraint for this columnreferences
[Name or string] - a table name that this column is a foreign key toreferencesConstraintName
[string] - name of the created constraintreferencesConstraintComment
[string] - comment on the created constraintonDelete
[string] - adds ON DELETE constraint for a reference columnonUpdate
[string] - adds ON UPDATE constraint for a reference columnmatch
[string] -FULL
orSIMPLE
deferrable
[boolean] - flag for deferrable column constraintdeferred
[boolean] - flag for initially deferred deferrable column constraintcomment
[string] - adds comment on columngenerated
[object] - creates identity column- sequence options -- see sequence options section
precedence
[string] -ALWAYS
orBY DEFAULT
Data type strings will be passed through directly to postgres, so write types as you would if you were writing the queries by hand.
There are some aliases on types to make things more foolproof: (int, string, float, double, datetime, bool)
There is a shorthand to pass only the type instead of an options object:
pgm.addColumns('myTable', { age: 'integer' });
is equivalent to
pgm.addColumns('myTable', { age: { type: 'integer' } });
There is a shorthand for normal auto-increment IDs:
pgm.addColumns('myTable', { id: 'id' });
is equivalent to
pgm.addColumns('myTable', { id: { type: 'serial', primaryKey: true } });
Add columns to an existing table - postgres docs
Arguments:
tablename
[Name] - name of the table to alternew_columns
[object] - column names / options -- see column definitions sectionoptions
[object] - options:ifNotExists
[boolean] adds column only if it does not exist
Aliases: addColumn
Reverse Operation: dropColumns
Drop columns from a table - postgres docs
Arguments:
tablename
[Name] - name of the table to altercolumns
[array of strings or object] - columns to drop (if object, uses keys)options
[object] - options:ifExists
[boolean] - drops column only if it existscascade
[boolean] - drops also dependent objects
Aliases: dropColumn
Rename a column - postgres docs
Arguments:
tablename
[Name] - name of the table to alterold_column_name
[string] - current column namenew_column_name
[string] - new column name
Reverse Operation: same operation in opposite direction
Alter a column (default value, type, allow null) - postgres docs
Arguments:
tablename
[Name] - name of the table to altercolumn_name
[string] - column to altercolumn_options
[object] - optional new column optionsdefault
[string or null] - null, stringtype
[string] - new datatypenotNull
[boolean] - sets NOT NULL if true or NULL if falseallowNull
[boolean] - sets NULL if true (alternative tonotNull
)using
[string] - adds USING clause to change values in columncollation
[string] - adds COLLATE clause to change values in columncomment
[string] - adds comment on columngenerated
[object or null] - sets or drops identity column- sequence options -- see sequence options section
precedence
[string] -ALWAYS
orBY DEFAULT