-
Notifications
You must be signed in to change notification settings - Fork 180
/
createSequence.ts
40 lines (32 loc) · 1.28 KB
/
createSequence.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
import type { MigrationOptions } from '../../types';
import type { IfNotExistsOption, Name, Reversible } from '../generalTypes';
import type { DropSequenceOptions } from './dropSequence';
import { dropSequence } from './dropSequence';
import type { SequenceOptions } from './shared';
import { parseSequenceOptions } from './shared';
export interface CreateSequenceOptions
extends SequenceOptions,
IfNotExistsOption {
temporary?: boolean;
}
export type CreateSequenceFn = (
sequenceName: Name,
sequenceOptions?: CreateSequenceOptions & DropSequenceOptions
) => string;
export type CreateSequence = Reversible<CreateSequenceFn>;
export function createSequence(mOptions: MigrationOptions): CreateSequence {
const _create: CreateSequence = (sequenceName, options = {}) => {
const { temporary = false, ifNotExists = false } = options;
const temporaryStr = temporary ? ' TEMPORARY' : '';
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const sequenceNameStr = mOptions.literal(sequenceName);
const clausesStr = parseSequenceOptions(
mOptions.typeShorthands,
options
).join('\n ');
return `CREATE${temporaryStr} SEQUENCE${ifNotExistsStr} ${sequenceNameStr}
${clausesStr};`;
};
_create.reverse = dropSequence(mOptions);
return _create;
}