-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathdropColumns.ts
35 lines (27 loc) · 1.05 KB
/
dropColumns.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { MigrationOptions } from '../../types';
import { formatLines } from '../../utils';
import type { DropOptions, Name } from '../generalTypes';
export type DropColumnsOptions = DropOptions;
export type DropColumns = (
tableName: Name,
columns: string | string[] | { [name: string]: unknown },
dropOptions?: DropColumnsOptions
) => string;
export function dropColumns(mOptions: MigrationOptions): DropColumns {
const _drop: DropColumns = (tableName, columns, options = {}) => {
const { ifExists = false, cascade = false } = options;
if (typeof columns === 'string') {
columns = [columns];
} else if (!Array.isArray(columns) && typeof columns === 'object') {
columns = Object.keys(columns);
}
const ifExistsStr = ifExists ? 'IF EXISTS ' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const lines = columns
.map(mOptions.literal)
.map((column) => `DROP ${ifExistsStr}${column}${cascadeStr}`);
return `ALTER TABLE ${mOptions.literal(tableName)}
${formatLines(lines)};`;
};
return _drop;
}