UNPKG

@grandlinex/bundle-postgresql

Version:
72 lines (71 loc) 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@grandlinex/core"); class PGUpdate extends core_1.CoreDBUpdate { /** * Initialize a new table in the database. * This method is used to create a new table in the database. * @param className */ initNewTable(className) { return this.getDb().getEntityWrapper(className).init(); } /** * Alter a table to add a new column. * @param className * @param columName * @param type * @param notNull * @param defaultValue * @param deleteDefault * @protected */ async alterTableAddColumn(className, columName, type, notNull, defaultValue, deleteDefault) { const db = this.getDb(); const tableName = core_1.XUtil.camelToSnakeCase(className); const query = []; if (notNull && defaultValue !== undefined) { query.push({ exec: `ALTER TABLE ${db.schemaName}.${tableName} ADD ${columName} ${type} NOT NULL DEFAULT ${defaultValue};`, param: [], }); } else if (notNull) { query.push({ exec: `ALTER TABLE ${db.schemaName}.${tableName} ADD ${columName} ${type} NOT NULL;`, param: [], }); } else { query.push({ exec: `ALTER TABLE ${db.schemaName}.${tableName} ADD ${columName} ${type};`, param: [], }); } if (defaultValue !== undefined && deleteDefault) { query.push({ exec: `ALTER TABLE ${db.schemaName}.${tableName} ALTER COLUMN ${columName} DROP DEFAULT ;`, param: [], }); } return (await db.execScripts(query)).every((e) => e !== null); } /** * Alter a table to delete a column. * This method is used to remove a column from an existing table in the database. * @param className * @param columName * @protected */ async alterTableDeleteColumn(className, columName) { const db = this.getDb(); const tableName = core_1.XUtil.camelToSnakeCase(className); const query = []; query.push({ exec: `ALTER TABLE ${db.schemaName}.${tableName} DROP COLUMN ${columName};`, param: [], }); return (await db.execScripts(query)).every((e) => e !== null); } } exports.default = PGUpdate;