@fewer/adapter-postgres
Version:
A Fewer database adapter to connect to Postgres
110 lines • 3.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const squel_1 = __importDefault(require("squel"));
class CreateTableBlock extends squel_1.default.cls.Block {
constructor() {
super(...arguments);
this._name = '';
}
table(name) {
this._name = name;
}
_toParamString() {
return {
text: this._name,
values: [],
};
}
}
class CreateFieldBlock extends squel_1.default.cls.Block {
constructor() {
super(...arguments);
this._fields = [];
}
field(name, type, options) {
this._fields.push({
name,
type,
options,
});
}
_columnModifiers(options) {
if (!options)
return [];
const modifiers = [];
if (options.nonNull) {
modifiers.push('NOT NULL');
}
if (options.unique) {
modifiers.push('UNIQUE');
}
if (options.default) {
modifiers.push(`DEFAULT ${options.default}`);
}
return modifiers;
}
_toParamString() {
let str = this._fields
.map(f => {
return [
f.name,
f.type.toUpperCase(),
...this._columnModifiers(f.options),
].join(' ');
})
.join(', ');
return {
text: str,
values: [],
};
}
}
const BLOCK_OPTIONS = {};
class CreateTableQuery extends squel_1.default.cls.QueryBuilder {
constructor(options) {
const blocks = [
new squel.cls.StringBlock(BLOCK_OPTIONS, 'CREATE'),
];
if (options.temporary) {
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, 'TEMPORARY'));
}
if (options.unlogged) {
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, 'UNLOGGED'));
}
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, 'TABLE'));
blocks.push(new CreateTableBlock(BLOCK_OPTIONS), new squel.cls.StringBlock(BLOCK_OPTIONS, `(`), new CreateFieldBlock(BLOCK_OPTIONS));
if (options.primaryKey) {
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, `,`), new squel.cls.StringBlock(BLOCK_OPTIONS, `PRIMARY KEY (${options.primaryKey})`));
}
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, `)`));
if (options.inherits) {
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, `INHERITS (${options.inherits.join(', ')})`));
}
if (options.onCommit) {
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, `ON COMMIT ${options.onCommit}`));
}
if (options.tablespace) {
blocks.push(new squel.cls.StringBlock(BLOCK_OPTIONS, `TABLESPACE ${options.tablespace}`));
}
super(BLOCK_OPTIONS, blocks);
}
}
class DropTableQuery extends squel_1.default.cls.QueryBuilder {
constructor(tableName) {
super(BLOCK_OPTIONS, [
new squel.cls.StringBlock(BLOCK_OPTIONS, `DROP TABLE ${tableName}`),
]);
}
}
const squel = squel_1.default.useFlavour('postgres');
squel.create = function (options) {
return new CreateTableQuery(options);
};
squel.dropTable = function (tableName) {
return new DropTableQuery(tableName);
};
exports.default = squel;
//# sourceMappingURL=squel.js.map