forked from drizzle-team/drizzle-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes.ts
108 lines (83 loc) · 2.57 KB
/
indexes.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { entityKind } from '~/entity';
import type { SQL } from '~/sql';
import type { AnyMySqlColumn } from './columns';
import type { AnyMySqlTable } from './table';
interface IndexConfig {
name: string;
columns: IndexColumn[];
/**
* If true, the index will be created as `create unique index` instead of `create index`.
*/
unique?: boolean;
/**
* If set, the index will be created as `create index ... using { 'btree' | 'hash' }`.
*/
using?: 'btree' | 'hash';
/**
* If set, the index will be created as `create index ... algorythm { 'default' | 'inplace' | 'copy' }`.
*/
algorythm?: 'default' | 'inplace' | 'copy';
/**
* If set, adds locks to the index creation.
*/
lock?: 'default' | 'none' | 'shared' | 'exclusive';
}
export type IndexColumn = AnyMySqlColumn | SQL;
export class IndexBuilderOn {
static readonly [entityKind]: string = 'MySqlIndexBuilderOn';
constructor(private name: string, private unique: boolean) {}
on(...columns: [IndexColumn, ...IndexColumn[]]): IndexBuilder {
return new IndexBuilder(this.name, columns, this.unique);
}
}
export interface AnyIndexBuilder {
build(table: AnyMySqlTable): Index;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IndexBuilder extends AnyIndexBuilder {}
export class IndexBuilder implements AnyIndexBuilder {
static readonly [entityKind]: string = 'MySqlIndexBuilder';
/** @internal */
config: IndexConfig;
constructor(name: string, columns: IndexColumn[], unique: boolean) {
this.config = {
name,
columns,
unique,
};
}
using(using: IndexConfig['using']): this {
this.config.using = using;
return this;
}
algorythm(algorythm: IndexConfig['algorythm']): this {
this.config.algorythm = algorythm;
return this;
}
lock(lock: IndexConfig['lock']): this {
this.config.lock = lock;
return this;
}
/** @internal */
build(table: AnyMySqlTable): Index {
return new Index(this.config, table);
}
}
export class Index {
static readonly [entityKind]: string = 'MySqlIndex';
readonly config: IndexConfig & { table: AnyMySqlTable };
constructor(config: IndexConfig, table: AnyMySqlTable) {
this.config = { ...config, table };
}
}
export type GetColumnsTableName<TColumns> = TColumns extends
AnyMySqlColumn<{ tableName: infer TTableName extends string }> | AnyMySqlColumn<
{ tableName: infer TTableName extends string }
>[] ? TTableName
: never;
export function index(name: string): IndexBuilderOn {
return new IndexBuilderOn(name, false);
}
export function uniqueIndex(name: string): IndexBuilderOn {
return new IndexBuilderOn(name, true);
}