forked from drizzle-team/drizzle-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.ts
221 lines (195 loc) · 6.49 KB
/
view.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import type { BuildColumns } from '~/column-builder';
import { entityKind } from '~/entity';
import type { TypedQueryBuilder } from '~/query-builders/query-builder';
import type { AddAliasToSelection } from '~/query-builders/select.types';
import type { SQL } from '~/sql';
import { SelectionProxyHandler } from '~/subquery';
import { getTableColumns } from '~/utils';
import { type ColumnsSelection, View } from '~/view';
import { type MySqlColumn, type MySqlColumnBuilder } from './columns';
import { QueryBuilder } from './query-builders';
import type { SelectedFields } from './query-builders/select.types';
import { mysqlTable } from './table';
export interface ViewBuilderConfig {
algorithm?: 'undefined' | 'merge' | 'temptable';
definer?: string;
sqlSecurity?: 'definer' | 'invoker';
withCheckOption?: 'cascaded' | 'local';
}
export class ViewBuilderCore<TConfig extends { name: string; columns?: unknown }> {
static readonly [entityKind]: string = 'MySqlViewBuilder';
declare readonly _: {
readonly name: TConfig['name'];
readonly columns: TConfig['columns'];
};
constructor(
protected name: TConfig['name'],
protected schema: string | undefined,
) {}
protected config: ViewBuilderConfig = {};
algorithm(
algorithm: Exclude<ViewBuilderConfig['algorithm'], undefined>,
): this {
this.config.algorithm = algorithm;
return this;
}
definer(
definer: Exclude<ViewBuilderConfig['definer'], undefined>,
): this {
this.config.definer = definer;
return this;
}
sqlSecurity(
sqlSecurity: Exclude<ViewBuilderConfig['sqlSecurity'], undefined>,
): this {
this.config.sqlSecurity = sqlSecurity;
return this;
}
withCheckOption(
withCheckOption?: Exclude<ViewBuilderConfig['withCheckOption'], undefined>,
): this {
this.config.withCheckOption = withCheckOption ?? 'cascaded';
return this;
}
}
export class ViewBuilder<TName extends string = string> extends ViewBuilderCore<{ name: TName }> {
static readonly [entityKind]: string = 'MySqlViewBuilder';
as<TSelectedFields extends SelectedFields>(
qb: TypedQueryBuilder<TSelectedFields> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelectedFields>),
): MySqlViewWithSelection<TName, false, AddAliasToSelection<TSelectedFields, TName, 'mysql'>> {
if (typeof qb === 'function') {
qb = qb(new QueryBuilder());
}
const selectionProxy = new SelectionProxyHandler<TSelectedFields>({
alias: this.name,
sqlBehavior: 'error',
sqlAliasedBehavior: 'alias',
replaceOriginalName: true,
});
const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
return new Proxy(
new MySqlView({
mysqlConfig: this.config,
config: {
name: this.name,
schema: this.schema,
selectedFields: aliasedSelection,
query: qb.getSQL().inlineParams(),
},
}),
selectionProxy as any,
) as MySqlViewWithSelection<TName, false, AddAliasToSelection<TSelectedFields, TName, 'mysql'>>;
}
}
export class ManualViewBuilder<
TName extends string = string,
TColumns extends Record<string, MySqlColumnBuilder> = Record<string, MySqlColumnBuilder>,
> extends ViewBuilderCore<{ name: TName; columns: TColumns }> {
static readonly [entityKind]: string = 'MySqlManualViewBuilder';
private columns: Record<string, MySqlColumn>;
constructor(
name: TName,
columns: TColumns,
schema: string | undefined,
) {
super(name, schema);
this.columns = getTableColumns(mysqlTable(name, columns)) as BuildColumns<TName, TColumns, 'mysql'>;
}
existing(): MySqlViewWithSelection<TName, true, BuildColumns<TName, TColumns, 'mysql'>> {
return new Proxy(
new MySqlView({
mysqlConfig: undefined,
config: {
name: this.name,
schema: this.schema,
selectedFields: this.columns,
query: undefined,
},
}),
new SelectionProxyHandler({
alias: this.name,
sqlBehavior: 'error',
sqlAliasedBehavior: 'alias',
replaceOriginalName: true,
}),
) as MySqlViewWithSelection<TName, true, BuildColumns<TName, TColumns, 'mysql'>>;
}
as(query: SQL): MySqlViewWithSelection<TName, false, BuildColumns<TName, TColumns, 'mysql'>> {
return new Proxy(
new MySqlView({
mysqlConfig: this.config,
config: {
name: this.name,
schema: this.schema,
selectedFields: this.columns,
query: query.inlineParams(),
},
}),
new SelectionProxyHandler({
alias: this.name,
sqlBehavior: 'error',
sqlAliasedBehavior: 'alias',
replaceOriginalName: true,
}),
) as MySqlViewWithSelection<TName, false, BuildColumns<TName, TColumns, 'mysql'>>;
}
}
export abstract class MySqlViewBase<
TName extends string = string,
TExisting extends boolean = boolean,
TSelectedFields extends ColumnsSelection = ColumnsSelection,
> extends View<TName, TExisting, TSelectedFields> {
static readonly [entityKind]: string = 'MySqlViewBase';
declare readonly _: View<TName, TExisting, TSelectedFields>['_'] & {
readonly viewBrand: 'MySqlViewBase';
};
}
export const MySqlViewConfig = Symbol.for('drizzle:MySqlViewConfig');
export class MySqlView<
TName extends string = string,
TExisting extends boolean = boolean,
TSelectedFields extends ColumnsSelection = ColumnsSelection,
> extends MySqlViewBase<TName, TExisting, TSelectedFields> {
static readonly [entityKind]: string = 'MySqlView';
declare protected $MySqlViewBrand: 'MySqlView';
[MySqlViewConfig]: ViewBuilderConfig | undefined;
constructor({ mysqlConfig, config }: {
mysqlConfig: ViewBuilderConfig | undefined;
config: {
name: TName;
schema: string | undefined;
selectedFields: SelectedFields;
query: SQL | undefined;
};
}) {
super(config);
this[MySqlViewConfig] = mysqlConfig;
}
}
export type MySqlViewWithSelection<
TName extends string,
TExisting extends boolean,
TSelectedFields extends ColumnsSelection,
> = MySqlView<TName, TExisting, TSelectedFields> & TSelectedFields;
/** @internal */
export function mysqlViewWithSchema(
name: string,
selection: Record<string, MySqlColumnBuilder> | undefined,
schema: string | undefined,
): ViewBuilder | ManualViewBuilder {
if (selection) {
return new ManualViewBuilder(name, selection, schema);
}
return new ViewBuilder(name, schema);
}
export function mysqlView<TName extends string>(name: TName): ViewBuilder<TName>;
export function mysqlView<TName extends string, TColumns extends Record<string, MySqlColumnBuilder>>(
name: TName,
columns: TColumns,
): ManualViewBuilder<TName, TColumns>;
export function mysqlView(
name: string,
selection?: Record<string, MySqlColumnBuilder>,
): ViewBuilder | ManualViewBuilder {
return mysqlViewWithSchema(name, selection, undefined);
}