-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathcreateIndex.ts
79 lines (64 loc) · 2.44 KB
/
createIndex.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { MigrationOptions } from '../../migrationOptions';
import { toArray } from '../../utils';
import type { IfNotExistsOption, Name, Reversible } from '../generalTypes';
import type { DropIndexOptions } from './dropIndex';
import { dropIndex } from './dropIndex';
import type { IndexColumn } from './shared';
import { generateColumnsString, generateIndexName } from './shared';
export interface CreateIndexOptions extends IfNotExistsOption {
name?: string;
unique?: boolean;
where?: string;
concurrently?: boolean;
method?: 'btree' | 'hash' | 'gist' | 'spgist' | 'gin';
include?: string | string[];
}
export type CreateIndexFn = (
tableName: Name,
columns: string | Array<string | IndexColumn>,
indexOptions?: CreateIndexOptions & DropIndexOptions
) => string;
export type CreateIndex = Reversible<CreateIndexFn>;
export function createIndex(mOptions: MigrationOptions): CreateIndex {
const _create: CreateIndex = (tableName, rawColumns, options = {}) => {
const {
unique = false,
concurrently = false,
ifNotExists = false,
method,
where,
include,
} = options;
/*
columns - the column, columns, or expression to create the index on
Options
name - explicitly specify the name for the index
unique - is this a unique index
where - where clause
concurrently -
ifNotExists - optionally create index
options.method - [ btree | hash | gist | spgist | gin ]
*/
const columns = toArray(rawColumns);
const indexName = generateIndexName(
typeof tableName === 'object' ? tableName.name : tableName,
columns,
options,
mOptions.schemalize
);
const columnsString = generateColumnsString(columns, mOptions);
const uniqueStr = unique ? ' UNIQUE' : '';
const concurrentlyStr = concurrently ? ' CONCURRENTLY' : '';
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const methodStr = method ? ` USING ${method}` : '';
const whereStr = where ? ` WHERE ${where}` : '';
const includeStr = include
? ` INCLUDE (${toArray(include).map(mOptions.literal).join(', ')})`
: '';
const indexNameStr = mOptions.literal(indexName);
const tableNameStr = mOptions.literal(tableName);
return `CREATE${uniqueStr} INDEX${concurrentlyStr}${ifNotExistsStr} ${indexNameStr} ON ${tableNameStr}${methodStr} (${columnsString})${includeStr}${whereStr};`;
};
_create.reverse = dropIndex(mOptions);
return _create;
}