forked from drizzle-team/drizzle-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexes.ts
78 lines (61 loc) · 1.63 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
import { entityKind } from '~/entity';
import type { SQL } from '~/sql';
import { type SQLiteColumn } from './columns';
import type { AnySQLiteTable } from './table';
export interface IndexConfig {
name: string;
columns: IndexColumn[];
unique: boolean;
where: SQL | undefined;
}
export type IndexColumn = SQLiteColumn | SQL;
export class IndexBuilderOn {
static readonly [entityKind]: string = 'SQLiteIndexBuilderOn';
constructor(private name: string, private unique: boolean) {}
on(...columns: [IndexColumn, ...IndexColumn[]]): IndexBuilder {
return new IndexBuilder(this.name, columns, this.unique);
}
}
export class IndexBuilder {
static readonly [entityKind]: string = 'SQLiteIndexBuilder';
declare _: {
brand: 'SQLiteIndexBuilder';
};
/** @internal */
config: IndexConfig;
constructor(name: string, columns: IndexColumn[], unique: boolean) {
this.config = {
name,
columns,
unique,
where: undefined,
};
}
/**
* Condition for partial index.
*/
where(condition: SQL): this {
this.config.where = condition;
return this;
}
/** @internal */
build(table: AnySQLiteTable): Index {
return new Index(this.config, table);
}
}
export class Index {
static readonly [entityKind]: string = 'SQLiteIndex';
declare _: {
brand: 'SQLiteIndex';
};
readonly config: IndexConfig & { table: AnySQLiteTable };
constructor(config: IndexConfig, table: AnySQLiteTable) {
this.config = { ...config, table };
}
}
export function index(name: string): IndexBuilderOn {
return new IndexBuilderOn(name, false);
}
export function uniqueIndex(name: string): IndexBuilderOn {
return new IndexBuilderOn(name, true);
}