UNPKG

@grandlinex/bundle-postgresql

Version:
69 lines (68 loc) 2.38 kB
import { CoreDBUpdate, XUtil } from '@grandlinex/core'; export default class PGUpdate extends 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 = 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 = 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); } }