mirror of
https://github.com/knex/knex.git
synced 2025-07-05 08:01:09 +00:00
61 lines
2.0 KiB
JavaScript
61 lines
2.0 KiB
JavaScript
![]() |
// PostgreSQL Column Builder & Compiler
|
||
|
// -------
|
||
|
module.exports = function(client) {
|
||
|
|
||
|
var _ = require('lodash');
|
||
|
var inherits = require('inherits');
|
||
|
var Schema = require('../../../schema');
|
||
|
|
||
|
// Column Builder
|
||
|
// ------
|
||
|
|
||
|
function ColumnBuilder_PG() {
|
||
|
this.client = client;
|
||
|
Schema.ColumnBuilder.apply(this, arguments);
|
||
|
}
|
||
|
inherits(ColumnBuilder_PG, Schema.ColumnBuilder);
|
||
|
|
||
|
function ColumnCompiler_PG() {
|
||
|
this.modifiers = ['nullable', 'defaultTo', 'comment'];
|
||
|
this.Formatter = client.Formatter;
|
||
|
Schema.ColumnCompiler.apply(this, arguments);
|
||
|
}
|
||
|
inherits(ColumnCompiler_PG, Schema.ColumnCompiler);
|
||
|
|
||
|
// Types
|
||
|
// ------
|
||
|
ColumnCompiler_PG.prototype.bigincrements = 'bigserial primary key';
|
||
|
ColumnCompiler_PG.prototype.bigint = 'bigint';
|
||
|
ColumnCompiler_PG.prototype.binary = 'bytea';
|
||
|
ColumnCompiler_PG.prototype.bit = function(column) {
|
||
|
return column.length !== false ? 'bit(' + column.length + ')' : 'bit';
|
||
|
};
|
||
|
ColumnCompiler_PG.prototype.bool = 'boolean',
|
||
|
ColumnCompiler_PG.prototype.datetime = 'timestamp',
|
||
|
|
||
|
// Create the column definition for an enum type.
|
||
|
// Using method "2" here: http://stackoverflow.com/a/10984951/525714
|
||
|
ColumnCompiler_PG.prototype.enu = function(allowed) {
|
||
|
return 'text check (' + this.args[0] + " in ('" + allowed.join("', '") + "'))";
|
||
|
};
|
||
|
|
||
|
ColumnCompiler_PG.prototype.double = 'double precision',
|
||
|
ColumnCompiler_PG.prototype.floating = 'real',
|
||
|
ColumnCompiler_PG.prototype.increments = 'serial primary key',
|
||
|
ColumnCompiler_PG.prototype.json = 'json';
|
||
|
ColumnCompiler_PG.prototype.smallint =
|
||
|
ColumnCompiler_PG.prototype.tinyint = 'smallint';
|
||
|
ColumnCompiler_PG.prototype.timestamp = 'timestamp';
|
||
|
ColumnCompiler_PG.prototype.uuid = 'uuid';
|
||
|
|
||
|
// Modifiers:
|
||
|
// ------
|
||
|
ColumnCompiler_PG.prototype.comment = function(comment) {
|
||
|
this.returnSql.pushQuery('comment on column ' + this.tableName + '.' +
|
||
|
this.formatter.wrap(this.currentColumn.args[0]) + " is " + (comment ? "'" + comment + "'" : 'NULL'));
|
||
|
};
|
||
|
|
||
|
client.ColumnBuilder = ColumnBuilder_PG;
|
||
|
client.ColumnCompiler = ColumnCompiler_PG;
|
||
|
|
||
|
};
|