/
Platform.ts
485 lines (385 loc) · 13.2 KB
/
Platform.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { clone } from '../utils/clone';
import { EntityRepository } from '../entity';
import type { NamingStrategy } from '../naming-strategy';
import { UnderscoreNamingStrategy } from '../naming-strategy';
import type { Constructor, EntityProperty, IEntityGenerator, IMigrator, IPrimaryKey, ISchemaGenerator, PopulateOptions, Primary, EntityMetadata, SimpleColumnMeta } from '../typings';
import { ExceptionConverter } from './ExceptionConverter';
import type { EntityManager } from '../EntityManager';
import type { Configuration } from '../utils/Configuration';
import type { IDatabaseDriver } from '../drivers/IDatabaseDriver';
import {
ArrayType, BigIntType, BlobType, BooleanType, DateType, DecimalType, DoubleType, JsonType, SmallIntType, TimeType,
TinyIntType, Type, UuidType, StringType, IntegerType, FloatType, DateTimeType, TextType, EnumType, UnknownType, MediumIntType,
} from '../types';
import { parseJsonSafe, Utils } from '../utils/Utils';
import { ReferenceKind } from '../enums';
import type { MikroORM } from '../MikroORM';
import type { TransformContext } from '../types/Type';
export const JsonProperty = Symbol('JsonProperty');
export abstract class Platform {
protected readonly exceptionConverter = new ExceptionConverter();
protected config!: Configuration;
protected namingStrategy!: NamingStrategy;
protected timezone?: string;
usesPivotTable(): boolean {
return false;
}
supportsTransactions(): boolean {
return !this.config.get('disableTransactions');
}
usesImplicitTransactions(): boolean {
return true;
}
getNamingStrategy(): { new(): NamingStrategy } {
return UnderscoreNamingStrategy;
}
usesReturningStatement(): boolean {
return false;
}
usesCascadeStatement(): boolean {
return false;
}
getSchemaHelper(): unknown {
return undefined;
}
indexForeignKeys() {
return false;
}
allowsMultiInsert() {
return true;
}
/**
* Whether or not the driver supports retuning list of created PKs back when multi-inserting
*/
usesBatchInserts(): boolean {
return true;
}
/**
* Whether or not the driver supports updating many records at once
*/
usesBatchUpdates(): boolean {
return true;
}
usesDefaultKeyword(): boolean {
return true;
}
/**
* Normalizes primary key wrapper to scalar value (e.g. mongodb's ObjectId to string)
*/
normalizePrimaryKey<T extends number | string = number | string>(data: Primary<T> | IPrimaryKey): T {
return data as T;
}
/**
* Converts scalar primary key representation to native driver wrapper (e.g. string to mongodb's ObjectId)
*/
denormalizePrimaryKey(data: IPrimaryKey): IPrimaryKey {
return data;
}
/**
* Used when serializing via toObject and toJSON methods, allows to use different PK field name (like `id` instead of `_id`)
*/
getSerializedPrimaryKeyField(field: string): string {
return field;
}
usesDifferentSerializedPrimaryKey(): boolean {
return false;
}
/**
* Returns the SQL specific for the platform to get the current timestamp
*/
getCurrentTimestampSQL(length?: number): string {
return 'current_timestamp' + (length ? `(${length})` : '');
}
getDateTimeTypeDeclarationSQL(column: { length?: number } = { length: 0 }): string {
return 'datetime' + (column.length ? `(${column.length})` : '');
}
getDefaultDateTimeLength(): number {
return 0;
}
getDateTypeDeclarationSQL(length?: number): string {
return 'date' + (length ? `(${length})` : '');
}
getTimeTypeDeclarationSQL(length?: number): string {
return 'time' + (length ? `(${length})` : '');
}
getRegExpOperator(val?: unknown, flags?: string): string {
return 'regexp';
}
getRegExpValue(val: RegExp): { $re: string; $flags?: string } {
if (val.flags.includes('i')) {
return { $re: `(?i)${val.source}` };
}
return { $re: val.source };
}
isAllowedTopLevelOperator(operator: string) {
return operator === '$not';
}
quoteVersionValue(value: Date | number, prop: EntityProperty): Date | string | number {
return value;
}
getDefaultVersionLength(): number {
return 3;
}
allowsComparingTuples() {
return true;
}
allowsUniqueBatchUpdates() {
return true;
}
isBigIntProperty(prop: EntityProperty): boolean {
return prop.columnTypes && prop.columnTypes[0] === 'bigint';
}
isRaw(value: any): boolean {
return typeof value === 'object' && value !== null && '__raw' in value;
}
getDefaultSchemaName(): string | undefined {
return undefined;
}
getBooleanTypeDeclarationSQL(): string {
return 'boolean';
}
getIntegerTypeDeclarationSQL(column: { length?: number; unsigned?: boolean; autoincrement?: boolean }): string {
return 'int';
}
getSmallIntTypeDeclarationSQL(column: { length?: number; unsigned?: boolean; autoincrement?: boolean }): string {
return 'smallint';
}
getMediumIntTypeDeclarationSQL(column: { length?: number; unsigned?: boolean; autoincrement?: boolean }): string {
return 'mediumint';
}
getTinyIntTypeDeclarationSQL(column: { length?: number; unsigned?: boolean; autoincrement?: boolean }): string {
return 'tinyint';
}
getBigIntTypeDeclarationSQL(column: { length?: number; unsigned?: boolean; autoincrement?: boolean }): string {
return 'bigint';
}
getVarcharTypeDeclarationSQL(column: { length?: number }): string {
return `varchar(${column.length ?? 255})`;
}
getTextTypeDeclarationSQL(_column: { length?: number }): string {
return `text`;
}
getEnumTypeDeclarationSQL(column: { items?: unknown[]; fieldNames: string[]; length?: number; unsigned?: boolean; autoincrement?: boolean }): string {
if (column.items?.every(item => Utils.isString(item))) {
return `enum('${column.items.join("', '")}')`;
}
return this.getTinyIntTypeDeclarationSQL(column);
}
getFloatDeclarationSQL(): string {
return 'float';
}
getDoubleDeclarationSQL(): string {
return 'double';
}
getDecimalTypeDeclarationSQL(column: { precision?: number; scale?: number }): string {
const precision = column.precision ?? 10;
const scale = column.scale ?? 0;
return `numeric(${precision},${scale})`;
}
getUuidTypeDeclarationSQL(column: { length?: number }): string {
column.length ??= 36;
return this.getVarcharTypeDeclarationSQL(column);
}
extractSimpleType(type: string): string {
return type.toLowerCase().match(/[^(), ]+/)![0];
}
getMappedType(type: string): Type<unknown> {
const mappedType = this.config.get('discovery').getMappedType?.(type, this);
return mappedType ?? this.getDefaultMappedType(type);
}
getDefaultMappedType(type: string): Type<unknown> {
if (type.endsWith('[]')) {
return Type.getType(ArrayType);
}
switch (this.extractSimpleType(type)) {
case 'string': return Type.getType(StringType);
case 'varchar': return Type.getType(StringType);
case 'text': return Type.getType(TextType);
case 'number': return Type.getType(IntegerType);
case 'bigint': return Type.getType(BigIntType);
case 'smallint': return Type.getType(SmallIntType);
case 'tinyint': return Type.getType(TinyIntType);
case 'mediumint': return Type.getType(MediumIntType);
case 'float': return Type.getType(FloatType);
case 'double': return Type.getType(DoubleType);
case 'integer': return Type.getType(IntegerType);
case 'decimal':
case 'numeric': return Type.getType(DecimalType);
case 'boolean': return Type.getType(BooleanType);
case 'blob':
case 'uint8array':
case 'buffer': return Type.getType(BlobType);
case 'uuid': return Type.getType(UuidType);
case 'date': return Type.getType(DateType);
case 'datetime': return Type.getType(DateTimeType);
case 'time': return Type.getType(TimeType);
case 'object':
case 'json': return Type.getType(JsonType);
case 'enum': return Type.getType(EnumType);
default: return Type.getType(UnknownType);
}
}
supportsMultipleStatements(): boolean {
return this.config.get('multipleStatements');
}
getArrayDeclarationSQL(): string {
return 'text';
}
marshallArray(values: string[]): string {
return values.join(',');
}
unmarshallArray(value: string): string[] {
if (value === '') {
return [];
}
return value.split(',') as string[];
}
getBlobDeclarationSQL(): string {
return 'blob';
}
getJsonDeclarationSQL(): string {
return 'json';
}
getSearchJsonPropertySQL(path: string, type: string, aliased: boolean): string {
return path;
}
getSearchJsonPropertyKey(path: string[], type: string, aliased: boolean): string {
return path.join('.');
}
getFullTextWhereClause(prop: EntityProperty): string {
throw new Error('Full text searching is not supported by this driver.');
}
supportsCreatingFullTextIndex(): boolean {
throw new Error('Full text searching is not supported by this driver.');
}
getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string {
throw new Error('Full text searching is not supported by this driver.');
}
// TODO v6: remove the `marshall` parameter
convertsJsonAutomatically(marshall = false): boolean {
return true;
}
convertJsonToDatabaseValue(value: unknown, context?: TransformContext): unknown {
return JSON.stringify(value);
}
convertJsonToJSValue(value: unknown): unknown {
return parseJsonSafe(value);
}
getRepositoryClass<T extends object>(): Constructor<EntityRepository<T>> {
return EntityRepository;
}
getDefaultCharset(): string {
return 'utf8';
}
getExceptionConverter(): ExceptionConverter {
return this.exceptionConverter;
}
/**
* Allows to register extensions of the driver automatically (e.g. `SchemaGenerator` extension in SQL drivers).
*/
lookupExtensions(orm: MikroORM): void {
// no extensions by default
}
/* istanbul ignore next: kept for type inference only */
getSchemaGenerator(driver: IDatabaseDriver, em?: EntityManager): ISchemaGenerator {
throw new Error(`${driver.constructor.name} does not support SchemaGenerator`);
}
processDateProperty(value: unknown): string | number | Date {
return value as string;
}
quoteIdentifier(id: string, quote = '`'): string {
return `${quote}${id.replace('.', `${quote}.${quote}`)}${quote}`;
}
quoteValue(value: any): string {
return value;
}
cloneEmbeddable<T>(data: T): T {
const copy = clone(data);
// tag the copy so we know it should be stringified when quoting (so we know how to treat JSON arrays)
Object.defineProperty(copy, JsonProperty, { enumerable: false, value: true });
return copy;
}
setConfig(config: Configuration): void {
this.config = config;
this.namingStrategy = config.getNamingStrategy();
if (this.config.get('forceUtcTimezone')) {
this.timezone = 'Z';
} else {
this.timezone = this.config.get('timezone');
}
}
getConfig(): Configuration {
return this.config;
}
isNumericColumn(mappedType: Type<unknown>): boolean {
return [IntegerType, SmallIntType, BigIntType].some(t => mappedType instanceof t);
}
supportsUnsigned(): boolean {
return false;
}
/**
* Returns the default name of index for the given columns
*/
getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string {
return this.namingStrategy.indexName(tableName, columns, type);
}
/* istanbul ignore next */
getDefaultPrimaryName(tableName: string, columns: string[]): string {
return this.namingStrategy.indexName(tableName, columns, 'primary');
}
supportsCustomPrimaryKeyNames(): boolean {
return false;
}
shouldHaveColumn<T>(prop: EntityProperty<T>, populate: PopulateOptions<T>[] | boolean, includeFormulas = true): boolean {
if (prop.formula) {
return includeFormulas && (!prop.lazy || populate === true || (populate !== false && populate.some(p => p.field === prop.name)));
}
if (prop.persist === false) {
return false;
}
if (prop.lazy && (populate === false || (populate !== true && !populate.some(p => p.field === prop.name)))) {
return false;
}
if ([ReferenceKind.SCALAR, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
return true;
}
if (prop.kind === ReferenceKind.EMBEDDED) {
return !!prop.object;
}
return prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner;
}
/**
* Currently not supported due to how knex does complex sqlite diffing (always based on current schema)
*/
supportsDownMigrations(): boolean {
return true;
}
validateMetadata(meta: EntityMetadata): void {
return;
}
/**
* Generates a custom order by statement given a set of in order values, eg.
* ORDER BY (CASE WHEN priority = 'low' THEN 1 WHEN priority = 'medium' THEN 2 ELSE NULL END)
*/
generateCustomOrder(escapedColumn: string, values: unknown[]) {
throw new Error('Not supported');
}
/**
* @internal
*/
castColumn(prop?: { columnTypes?: string[] }): string {
return '';
}
/**
* @internal
*/
castJsonValue(prop?: { columnTypes?: string[] }): string {
return '';
}
/**
* @internal
*/
clone() {
return this;
}
}