@mikro-orm/sql 7.0.8-dev.8 → 7.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/AbstractSqlConnection.d.ts +94 -58
  2. package/AbstractSqlConnection.js +235 -238
  3. package/AbstractSqlDriver.d.ts +410 -155
  4. package/AbstractSqlDriver.js +2072 -1950
  5. package/AbstractSqlPlatform.d.ts +85 -75
  6. package/AbstractSqlPlatform.js +166 -162
  7. package/PivotCollectionPersister.d.ts +33 -15
  8. package/PivotCollectionPersister.js +158 -160
  9. package/README.md +1 -1
  10. package/SqlEntityManager.d.ts +67 -22
  11. package/SqlEntityManager.js +54 -38
  12. package/SqlEntityRepository.d.ts +14 -14
  13. package/SqlEntityRepository.js +23 -23
  14. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -12
  15. package/dialects/mssql/MsSqlNativeQueryBuilder.js +199 -201
  16. package/dialects/mysql/BaseMySqlPlatform.d.ts +65 -46
  17. package/dialects/mysql/BaseMySqlPlatform.js +137 -134
  18. package/dialects/mysql/MySqlExceptionConverter.d.ts +6 -6
  19. package/dialects/mysql/MySqlExceptionConverter.js +91 -77
  20. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +3 -3
  21. package/dialects/mysql/MySqlNativeQueryBuilder.js +66 -69
  22. package/dialects/mysql/MySqlSchemaHelper.d.ts +58 -39
  23. package/dialects/mysql/MySqlSchemaHelper.js +327 -319
  24. package/dialects/oracledb/OracleDialect.d.ts +81 -52
  25. package/dialects/oracledb/OracleDialect.js +155 -149
  26. package/dialects/oracledb/OracleNativeQueryBuilder.d.ts +12 -12
  27. package/dialects/oracledb/OracleNativeQueryBuilder.js +239 -243
  28. package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +109 -106
  29. package/dialects/postgresql/BasePostgreSqlPlatform.js +354 -353
  30. package/dialects/postgresql/FullTextType.d.ts +10 -6
  31. package/dialects/postgresql/FullTextType.js +51 -51
  32. package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +5 -5
  33. package/dialects/postgresql/PostgreSqlExceptionConverter.js +55 -43
  34. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
  35. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +4 -4
  36. package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +117 -82
  37. package/dialects/postgresql/PostgreSqlSchemaHelper.js +747 -711
  38. package/dialects/sqlite/BaseSqliteConnection.d.ts +3 -5
  39. package/dialects/sqlite/BaseSqliteConnection.js +21 -19
  40. package/dialects/sqlite/NodeSqliteDialect.d.ts +1 -1
  41. package/dialects/sqlite/NodeSqliteDialect.js +23 -23
  42. package/dialects/sqlite/SqliteDriver.d.ts +1 -1
  43. package/dialects/sqlite/SqliteDriver.js +3 -3
  44. package/dialects/sqlite/SqliteExceptionConverter.d.ts +6 -6
  45. package/dialects/sqlite/SqliteExceptionConverter.js +67 -51
  46. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +2 -2
  47. package/dialects/sqlite/SqliteNativeQueryBuilder.js +7 -7
  48. package/dialects/sqlite/SqlitePlatform.d.ts +63 -72
  49. package/dialects/sqlite/SqlitePlatform.js +139 -139
  50. package/dialects/sqlite/SqliteSchemaHelper.d.ts +77 -60
  51. package/dialects/sqlite/SqliteSchemaHelper.js +541 -522
  52. package/package.json +3 -3
  53. package/plugin/index.d.ts +42 -35
  54. package/plugin/index.js +43 -36
  55. package/plugin/transformer.d.ts +117 -94
  56. package/plugin/transformer.js +890 -881
  57. package/query/ArrayCriteriaNode.d.ts +4 -4
  58. package/query/ArrayCriteriaNode.js +18 -18
  59. package/query/CriteriaNode.d.ts +35 -25
  60. package/query/CriteriaNode.js +133 -123
  61. package/query/CriteriaNodeFactory.d.ts +49 -6
  62. package/query/CriteriaNodeFactory.js +97 -94
  63. package/query/NativeQueryBuilder.d.ts +120 -120
  64. package/query/NativeQueryBuilder.js +507 -501
  65. package/query/ObjectCriteriaNode.d.ts +12 -12
  66. package/query/ObjectCriteriaNode.js +298 -282
  67. package/query/QueryBuilder.d.ts +1557 -905
  68. package/query/QueryBuilder.js +2322 -2192
  69. package/query/QueryBuilderHelper.d.ts +153 -72
  70. package/query/QueryBuilderHelper.js +1079 -1028
  71. package/query/ScalarCriteriaNode.d.ts +3 -3
  72. package/query/ScalarCriteriaNode.js +53 -46
  73. package/query/enums.d.ts +14 -14
  74. package/query/enums.js +14 -14
  75. package/query/raw.d.ts +16 -6
  76. package/query/raw.js +10 -10
  77. package/schema/DatabaseSchema.d.ts +74 -50
  78. package/schema/DatabaseSchema.js +355 -327
  79. package/schema/DatabaseTable.d.ts +96 -73
  80. package/schema/DatabaseTable.js +1012 -927
  81. package/schema/SchemaComparator.d.ts +70 -66
  82. package/schema/SchemaComparator.js +790 -764
  83. package/schema/SchemaHelper.d.ts +121 -96
  84. package/schema/SchemaHelper.js +683 -668
  85. package/schema/SqlSchemaGenerator.d.ts +79 -59
  86. package/schema/SqlSchemaGenerator.js +525 -495
  87. package/typings.d.ts +405 -275
@@ -1,365 +1,366 @@
1
- import { ALIAS_REPLACEMENT, ARRAY_OPERATORS, raw, RawQueryFragment, Type, Utils, } from '@mikro-orm/core';
1
+ import { ALIAS_REPLACEMENT, ARRAY_OPERATORS, raw, RawQueryFragment, Type, Utils } from '@mikro-orm/core';
2
2
  import { AbstractSqlPlatform } from '../../AbstractSqlPlatform.js';
3
3
  import { PostgreSqlNativeQueryBuilder } from './PostgreSqlNativeQueryBuilder.js';
4
4
  import { PostgreSqlSchemaHelper } from './PostgreSqlSchemaHelper.js';
5
5
  import { PostgreSqlExceptionConverter } from './PostgreSqlExceptionConverter.js';
6
6
  import { FullTextType } from './FullTextType.js';
7
7
  export class BasePostgreSqlPlatform extends AbstractSqlPlatform {
8
- schemaHelper = new PostgreSqlSchemaHelper(this);
9
- exceptionConverter = new PostgreSqlExceptionConverter();
10
- /** Maps JS runtime type names to PostgreSQL cast types for JSON property access. @internal */
11
- #jsonTypeCasts = { number: 'float8', bigint: 'int8', boolean: 'bool' };
12
- createNativeQueryBuilder() {
13
- return new PostgreSqlNativeQueryBuilder(this);
14
- }
15
- usesReturningStatement() {
16
- return true;
17
- }
18
- usesCascadeStatement() {
19
- return true;
20
- }
21
- supportsNativeEnums() {
22
- return true;
23
- }
24
- usesEnumCheckConstraints() {
25
- return true;
26
- }
27
- getEnumArrayCheckConstraintExpression(column, items) {
28
- return `${this.quoteIdentifier(column)} <@ array[${items.map(item => `${this.quoteValue(item)}::text`).join(', ')}]`;
29
- }
30
- supportsMaterializedViews() {
31
- return true;
32
- }
33
- supportsCustomPrimaryKeyNames() {
34
- return true;
35
- }
36
- getCurrentTimestampSQL(length) {
37
- return `current_timestamp(${length})`;
38
- }
39
- getDateTimeTypeDeclarationSQL(column) {
40
- /* v8 ignore next */
41
- return 'timestamptz' + (column.length != null ? `(${column.length})` : '');
42
- }
43
- getDefaultDateTimeLength() {
44
- return 6;
45
- }
46
- getTimeTypeDeclarationSQL() {
47
- return 'time(0)';
48
- }
49
- getIntegerTypeDeclarationSQL(column) {
50
- if (column.autoincrement && !column.generated) {
51
- return 'serial';
52
- }
53
- return 'int';
54
- }
55
- getBigIntTypeDeclarationSQL(column) {
56
- /* v8 ignore next */
57
- if (column.autoincrement) {
58
- return `bigserial`;
59
- }
60
- return 'bigint';
61
- }
62
- getTinyIntTypeDeclarationSQL(column) {
63
- return 'smallint';
64
- }
65
- getUuidTypeDeclarationSQL(column) {
66
- return `uuid`;
67
- }
68
- getFullTextWhereClause(prop) {
69
- if (prop.customType instanceof FullTextType) {
70
- return `:column: @@ plainto_tsquery('${prop.customType.regconfig}', :query)`;
71
- }
72
- /* v8 ignore next */
73
- if (prop.columnTypes[0] === 'tsvector') {
74
- return `:column: @@ plainto_tsquery('simple', :query)`;
75
- }
76
- return `to_tsvector('simple', :column:) @@ plainto_tsquery('simple', :query)`;
77
- }
78
- supportsCreatingFullTextIndex() {
79
- return true;
80
- }
81
- getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
82
- /* v8 ignore next */
83
- const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
84
- const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
85
- const quotedIndexName = this.quoteIdentifier(indexName);
86
- if (columns.length === 1 && columns[0].type === 'tsvector') {
87
- return `create index ${quotedIndexName} on ${quotedTableName} using gin(${quotedColumnNames[0]})`;
88
- }
89
- return `create index ${quotedIndexName} on ${quotedTableName} using gin(to_tsvector('simple', ${quotedColumnNames.join(` || ' ' || `)}))`;
90
- }
91
- normalizeColumnType(type, options) {
92
- const simpleType = this.extractSimpleType(type);
93
- if (['int', 'int4', 'integer'].includes(simpleType)) {
94
- return this.getIntegerTypeDeclarationSQL({});
95
- }
96
- if (['bigint', 'int8'].includes(simpleType)) {
97
- return this.getBigIntTypeDeclarationSQL({});
98
- }
99
- if (['smallint', 'int2'].includes(simpleType)) {
100
- return this.getSmallIntTypeDeclarationSQL({});
101
- }
102
- if (['boolean', 'bool'].includes(simpleType)) {
103
- return this.getBooleanTypeDeclarationSQL();
104
- }
105
- if (['varchar', 'character varying'].includes(simpleType)) {
106
- return this.getVarcharTypeDeclarationSQL(options);
107
- }
108
- if (['char', 'bpchar'].includes(simpleType)) {
109
- return this.getCharTypeDeclarationSQL(options);
110
- }
111
- if (['decimal', 'numeric'].includes(simpleType)) {
112
- return this.getDecimalTypeDeclarationSQL(options);
113
- }
114
- if (['interval'].includes(simpleType)) {
115
- return this.getIntervalTypeDeclarationSQL(options);
116
- }
117
- return super.normalizeColumnType(type, options);
118
- }
119
- getMappedType(type) {
120
- switch (this.extractSimpleType(type)) {
121
- case 'tsvector':
122
- return Type.getType(FullTextType);
123
- default:
124
- return super.getMappedType(type);
125
- }
126
- }
127
- getRegExpOperator(val, flags) {
128
- /* v8 ignore next */
129
- if ((val instanceof RegExp && val.flags.includes('i')) || flags?.includes('i')) {
130
- return '~*';
131
- }
132
- return '~';
133
- }
8
+ schemaHelper = new PostgreSqlSchemaHelper(this);
9
+ exceptionConverter = new PostgreSqlExceptionConverter();
10
+ /** Maps JS runtime type names to PostgreSQL cast types for JSON property access. @internal */
11
+ #jsonTypeCasts = { number: 'float8', bigint: 'int8', boolean: 'bool' };
12
+ createNativeQueryBuilder() {
13
+ return new PostgreSqlNativeQueryBuilder(this);
14
+ }
15
+ usesReturningStatement() {
16
+ return true;
17
+ }
18
+ usesCascadeStatement() {
19
+ return true;
20
+ }
21
+ supportsNativeEnums() {
22
+ return true;
23
+ }
24
+ usesEnumCheckConstraints() {
25
+ return true;
26
+ }
27
+ getEnumArrayCheckConstraintExpression(column, items) {
28
+ return `${this.quoteIdentifier(column)} <@ array[${items.map(item => `${this.quoteValue(item)}::text`).join(', ')}]`;
29
+ }
30
+ supportsMaterializedViews() {
31
+ return true;
32
+ }
33
+ supportsCustomPrimaryKeyNames() {
34
+ return true;
35
+ }
36
+ getCurrentTimestampSQL(length) {
37
+ return `current_timestamp(${length})`;
38
+ }
39
+ getDateTimeTypeDeclarationSQL(column) {
134
40
  /* v8 ignore next */
135
- getRegExpValue(val) {
136
- if (val.flags.includes('i')) {
137
- return { $re: val.source, $flags: val.flags };
138
- }
139
- return { $re: val.source };
140
- }
141
- isBigIntProperty(prop) {
142
- return super.isBigIntProperty(prop) || ['bigserial', 'int8'].includes(prop.columnTypes?.[0]);
143
- }
144
- getArrayDeclarationSQL() {
145
- return 'text[]';
146
- }
147
- getFloatDeclarationSQL() {
148
- return 'real';
149
- }
150
- getDoubleDeclarationSQL() {
151
- return 'double precision';
152
- }
153
- getEnumTypeDeclarationSQL(column) {
154
- /* v8 ignore next */
155
- if (column.nativeEnumName) {
156
- return column.nativeEnumName;
157
- }
158
- if (column.items?.every(item => typeof item === 'string')) {
159
- return 'text';
160
- }
161
- return `smallint`;
162
- }
163
- supportsMultipleStatements() {
164
- return true;
165
- }
166
- getBeginTransactionSQL(options) {
167
- if (options?.isolationLevel || options?.readOnly) {
168
- let sql = 'start transaction';
169
- sql += options.isolationLevel ? ` isolation level ${options.isolationLevel}` : '';
170
- sql += options.readOnly ? ` read only` : '';
171
- return [sql];
172
- }
173
- return ['begin'];
174
- }
175
- marshallArray(values) {
176
- const quote = (v) => (v === '' || /["{},\\]/.exec(v) ? JSON.stringify(v) : v);
177
- return `{${values.map(v => quote('' + v)).join(',')}}`;
178
- }
41
+ return 'timestamptz' + (column.length != null ? `(${column.length})` : '');
42
+ }
43
+ getDefaultDateTimeLength() {
44
+ return 6;
45
+ }
46
+ getTimeTypeDeclarationSQL() {
47
+ return 'time(0)';
48
+ }
49
+ getIntegerTypeDeclarationSQL(column) {
50
+ if (column.autoincrement && !column.generated) {
51
+ return 'serial';
52
+ }
53
+ return 'int';
54
+ }
55
+ getBigIntTypeDeclarationSQL(column) {
179
56
  /* v8 ignore next */
180
- unmarshallArray(value) {
181
- if (value === '{}') {
182
- return [];
183
- }
184
- return value
185
- .substring(1, value.length - 1)
186
- .split(',')
187
- .map(v => {
188
- if (v === `""`) {
189
- return '';
190
- }
191
- if (/"(.*)"/.exec(v)) {
192
- return v.substring(1, v.length - 1).replaceAll('\\"', '"');
193
- }
194
- return v;
195
- });
196
- }
197
- getVarcharTypeDeclarationSQL(column) {
198
- if (column.length === -1) {
199
- return 'varchar';
200
- }
201
- return super.getVarcharTypeDeclarationSQL(column);
202
- }
203
- getCharTypeDeclarationSQL(column) {
204
- if (column.length === -1) {
205
- return 'char';
206
- }
207
- return super.getCharTypeDeclarationSQL(column);
208
- }
209
- getIntervalTypeDeclarationSQL(column) {
210
- return 'interval' + (column.length != null ? `(${column.length})` : '');
211
- }
212
- getBlobDeclarationSQL() {
213
- return 'bytea';
214
- }
215
- getJsonDeclarationSQL() {
216
- return 'jsonb';
217
- }
218
- getSearchJsonPropertyKey(path, type, aliased, value) {
219
- const first = path.shift();
220
- const last = path.pop();
221
- const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${first}` : first);
222
- type = typeof type === 'string' ? this.getMappedType(type).runtimeType : String(type);
223
- const cast = (key) => raw(type in this.#jsonTypeCasts ? `(${key})::${this.#jsonTypeCasts[type]}` : key);
224
- let lastOperator = '->>';
225
- // force `->` for operator payloads with array values
226
- if (Utils.isPlainObject(value) &&
227
- Object.keys(value).every(key => ARRAY_OPERATORS.includes(key) && Array.isArray(value[key]))) {
228
- lastOperator = '->';
229
- }
230
- if (path.length === 0) {
231
- return cast(`${root}${lastOperator}'${last}'`);
232
- }
233
- return cast(`${root}->${path.map(a => this.quoteValue(a)).join('->')}${lastOperator}'${last}'`);
234
- }
235
- getJsonIndexDefinition(index) {
236
- return index.columnNames.map(column => {
237
- if (!column.includes('.')) {
238
- return column;
239
- }
240
- const path = column.split('.');
241
- const first = path.shift();
242
- const last = path.pop();
243
- if (path.length === 0) {
244
- return `(${this.quoteIdentifier(first)}->>${this.quoteValue(last)})`;
245
- }
246
- return `(${this.quoteIdentifier(first)}->${path.map(c => this.quoteValue(c)).join('->')}->>${this.quoteValue(last)})`;
247
- });
248
- }
249
- quoteIdentifier(id, quote = '"') {
250
- if (RawQueryFragment.isKnownFragment(id)) {
251
- return super.quoteIdentifier(id);
252
- }
253
- return `${quote}${id.toString().replace('.', `${quote}.${quote}`)}${quote}`;
254
- }
255
- pad(number, digits) {
256
- return String(number).padStart(digits, '0');
57
+ if (column.autoincrement) {
58
+ return `bigserial`;
59
+ }
60
+ return 'bigint';
61
+ }
62
+ getTinyIntTypeDeclarationSQL(column) {
63
+ return 'smallint';
64
+ }
65
+ getUuidTypeDeclarationSQL(column) {
66
+ return `uuid`;
67
+ }
68
+ getFullTextWhereClause(prop) {
69
+ if (prop.customType instanceof FullTextType) {
70
+ return `:column: @@ plainto_tsquery('${prop.customType.regconfig}', :query)`;
257
71
  }
258
- /** @internal */
259
- formatDate(date) {
260
- if (this.timezone === 'Z') {
261
- return date.toISOString();
262
- }
263
- let offset = -date.getTimezoneOffset();
264
- let year = date.getFullYear();
265
- const isBCYear = year < 1;
266
- /* v8 ignore next */
267
- if (isBCYear) {
268
- year = Math.abs(year) + 1;
269
- }
270
- const datePart = `${this.pad(year, 4)}-${this.pad(date.getMonth() + 1, 2)}-${this.pad(date.getDate(), 2)}`;
271
- const timePart = `${this.pad(date.getHours(), 2)}:${this.pad(date.getMinutes(), 2)}:${this.pad(date.getSeconds(), 2)}.${this.pad(date.getMilliseconds(), 3)}`;
272
- let ret = `${datePart}T${timePart}`;
273
- /* v8 ignore next */
274
- if (offset < 0) {
275
- ret += '-';
276
- offset *= -1;
277
- }
278
- else {
279
- ret += '+';
280
- }
281
- ret += this.pad(Math.floor(offset / 60), 2) + ':' + this.pad(offset % 60, 2);
282
- /* v8 ignore next */
283
- if (isBCYear) {
284
- ret += ' BC';
285
- }
286
- return ret;
287
- }
288
- indexForeignKeys() {
289
- return false;
290
- }
291
- getDefaultMappedType(type) {
292
- const normalizedType = this.extractSimpleType(type);
293
- const map = {
294
- int2: 'smallint',
295
- smallserial: 'smallint',
296
- int: 'integer',
297
- int4: 'integer',
298
- serial: 'integer',
299
- serial4: 'integer',
300
- int8: 'bigint',
301
- bigserial: 'bigint',
302
- serial8: 'bigint',
303
- numeric: 'decimal',
304
- bool: 'boolean',
305
- real: 'float',
306
- float4: 'float',
307
- float8: 'double',
308
- timestamp: 'datetime',
309
- timestamptz: 'datetime',
310
- bytea: 'blob',
311
- jsonb: 'json',
312
- 'character varying': 'varchar',
313
- bpchar: 'character',
314
- };
315
- return super.getDefaultMappedType(map[normalizedType] ?? type);
316
- }
317
- supportsSchemas() {
318
- return true;
319
- }
320
- getDefaultSchemaName() {
321
- return 'public';
322
- }
323
- /**
324
- * Returns the default name of index for the given columns
325
- * cannot go past 63 character length for identifiers in MySQL
326
- */
327
- getIndexName(tableName, columns, type) {
328
- const indexName = super.getIndexName(tableName, columns, type);
329
- if (indexName.length > 63) {
330
- const suffix = type === 'primary' ? 'pkey' : type;
331
- return `${indexName.substring(0, 55 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
332
- }
333
- return indexName;
334
- }
335
- getDefaultPrimaryName(tableName, columns) {
336
- const indexName = `${tableName}_pkey`;
337
- if (indexName.length > 63) {
338
- return `${indexName.substring(0, 55 - 'pkey'.length)}_${Utils.hash(indexName, 5)}_pkey`;
339
- }
340
- return indexName;
341
- }
342
- /**
343
- * @inheritDoc
344
- */
345
- castColumn(prop) {
346
- switch (prop?.columnTypes?.[0]) {
347
- case this.getUuidTypeDeclarationSQL({}):
348
- return '::text';
349
- case this.getBooleanTypeDeclarationSQL():
350
- return '::int';
351
- default:
352
- return '';
353
- }
354
- }
355
- getJsonArrayFromSQL(column, alias, _properties) {
356
- return `jsonb_array_elements(${column}) as ${this.quoteIdentifier(alias)}`;
357
- }
358
- getJsonArrayElementPropertySQL(alias, property, type) {
359
- const expr = `${this.quoteIdentifier(alias)}->>${this.quoteValue(property)}`;
360
- return type in this.#jsonTypeCasts ? `(${expr})::${this.#jsonTypeCasts[type]}` : expr;
72
+ /* v8 ignore next */
73
+ if (prop.columnTypes[0] === 'tsvector') {
74
+ return `:column: @@ plainto_tsquery('simple', :query)`;
75
+ }
76
+ return `to_tsvector('simple', :column:) @@ plainto_tsquery('simple', :query)`;
77
+ }
78
+ supportsCreatingFullTextIndex() {
79
+ return true;
80
+ }
81
+ getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
82
+ /* v8 ignore next */
83
+ const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
84
+ const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
85
+ const quotedIndexName = this.quoteIdentifier(indexName);
86
+ if (columns.length === 1 && columns[0].type === 'tsvector') {
87
+ return `create index ${quotedIndexName} on ${quotedTableName} using gin(${quotedColumnNames[0]})`;
88
+ }
89
+ return `create index ${quotedIndexName} on ${quotedTableName} using gin(to_tsvector('simple', ${quotedColumnNames.join(` || ' ' || `)}))`;
90
+ }
91
+ normalizeColumnType(type, options) {
92
+ const simpleType = this.extractSimpleType(type);
93
+ if (['int', 'int4', 'integer'].includes(simpleType)) {
94
+ return this.getIntegerTypeDeclarationSQL({});
95
+ }
96
+ if (['bigint', 'int8'].includes(simpleType)) {
97
+ return this.getBigIntTypeDeclarationSQL({});
98
+ }
99
+ if (['smallint', 'int2'].includes(simpleType)) {
100
+ return this.getSmallIntTypeDeclarationSQL({});
101
+ }
102
+ if (['boolean', 'bool'].includes(simpleType)) {
103
+ return this.getBooleanTypeDeclarationSQL();
104
+ }
105
+ if (['varchar', 'character varying'].includes(simpleType)) {
106
+ return this.getVarcharTypeDeclarationSQL(options);
107
+ }
108
+ if (['char', 'bpchar'].includes(simpleType)) {
109
+ return this.getCharTypeDeclarationSQL(options);
110
+ }
111
+ if (['decimal', 'numeric'].includes(simpleType)) {
112
+ return this.getDecimalTypeDeclarationSQL(options);
113
+ }
114
+ if (['interval'].includes(simpleType)) {
115
+ return this.getIntervalTypeDeclarationSQL(options);
116
+ }
117
+ return super.normalizeColumnType(type, options);
118
+ }
119
+ getMappedType(type) {
120
+ switch (this.extractSimpleType(type)) {
121
+ case 'tsvector':
122
+ return Type.getType(FullTextType);
123
+ default:
124
+ return super.getMappedType(type);
125
+ }
126
+ }
127
+ getRegExpOperator(val, flags) {
128
+ /* v8 ignore next */
129
+ if ((val instanceof RegExp && val.flags.includes('i')) || flags?.includes('i')) {
130
+ return '~*';
131
+ }
132
+ return '~';
133
+ }
134
+ /* v8 ignore next */
135
+ getRegExpValue(val) {
136
+ if (val.flags.includes('i')) {
137
+ return { $re: val.source, $flags: val.flags };
138
+ }
139
+ return { $re: val.source };
140
+ }
141
+ isBigIntProperty(prop) {
142
+ return super.isBigIntProperty(prop) || ['bigserial', 'int8'].includes(prop.columnTypes?.[0]);
143
+ }
144
+ getArrayDeclarationSQL() {
145
+ return 'text[]';
146
+ }
147
+ getFloatDeclarationSQL() {
148
+ return 'real';
149
+ }
150
+ getDoubleDeclarationSQL() {
151
+ return 'double precision';
152
+ }
153
+ getEnumTypeDeclarationSQL(column) {
154
+ /* v8 ignore next */
155
+ if (column.nativeEnumName) {
156
+ return column.nativeEnumName;
157
+ }
158
+ if (column.items?.every(item => typeof item === 'string')) {
159
+ return 'text';
160
+ }
161
+ return `smallint`;
162
+ }
163
+ supportsMultipleStatements() {
164
+ return true;
165
+ }
166
+ getBeginTransactionSQL(options) {
167
+ if (options?.isolationLevel || options?.readOnly) {
168
+ let sql = 'start transaction';
169
+ sql += options.isolationLevel ? ` isolation level ${options.isolationLevel}` : '';
170
+ sql += options.readOnly ? ` read only` : '';
171
+ return [sql];
172
+ }
173
+ return ['begin'];
174
+ }
175
+ marshallArray(values) {
176
+ const quote = v => (v === '' || /["{},\\]/.exec(v) ? JSON.stringify(v) : v);
177
+ return `{${values.map(v => quote('' + v)).join(',')}}`;
178
+ }
179
+ /* v8 ignore next */
180
+ unmarshallArray(value) {
181
+ if (value === '{}') {
182
+ return [];
183
+ }
184
+ return value
185
+ .substring(1, value.length - 1)
186
+ .split(',')
187
+ .map(v => {
188
+ if (v === `""`) {
189
+ return '';
190
+ }
191
+ if (/"(.*)"/.exec(v)) {
192
+ return v.substring(1, v.length - 1).replaceAll('\\"', '"');
193
+ }
194
+ return v;
195
+ });
196
+ }
197
+ getVarcharTypeDeclarationSQL(column) {
198
+ if (column.length === -1) {
199
+ return 'varchar';
200
+ }
201
+ return super.getVarcharTypeDeclarationSQL(column);
202
+ }
203
+ getCharTypeDeclarationSQL(column) {
204
+ if (column.length === -1) {
205
+ return 'char';
206
+ }
207
+ return super.getCharTypeDeclarationSQL(column);
208
+ }
209
+ getIntervalTypeDeclarationSQL(column) {
210
+ return 'interval' + (column.length != null ? `(${column.length})` : '');
211
+ }
212
+ getBlobDeclarationSQL() {
213
+ return 'bytea';
214
+ }
215
+ getJsonDeclarationSQL() {
216
+ return 'jsonb';
217
+ }
218
+ getSearchJsonPropertyKey(path, type, aliased, value) {
219
+ const first = path.shift();
220
+ const last = path.pop();
221
+ const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${first}` : first);
222
+ type = typeof type === 'string' ? this.getMappedType(type).runtimeType : String(type);
223
+ const cast = key => raw(type in this.#jsonTypeCasts ? `(${key})::${this.#jsonTypeCasts[type]}` : key);
224
+ let lastOperator = '->>';
225
+ // force `->` for operator payloads with array values
226
+ if (
227
+ Utils.isPlainObject(value) &&
228
+ Object.keys(value).every(key => ARRAY_OPERATORS.includes(key) && Array.isArray(value[key]))
229
+ ) {
230
+ lastOperator = '->';
231
+ }
232
+ if (path.length === 0) {
233
+ return cast(`${root}${lastOperator}'${last}'`);
234
+ }
235
+ return cast(`${root}->${path.map(a => this.quoteValue(a)).join('->')}${lastOperator}'${last}'`);
236
+ }
237
+ getJsonIndexDefinition(index) {
238
+ return index.columnNames.map(column => {
239
+ if (!column.includes('.')) {
240
+ return column;
241
+ }
242
+ const path = column.split('.');
243
+ const first = path.shift();
244
+ const last = path.pop();
245
+ if (path.length === 0) {
246
+ return `(${this.quoteIdentifier(first)}->>${this.quoteValue(last)})`;
247
+ }
248
+ return `(${this.quoteIdentifier(first)}->${path.map(c => this.quoteValue(c)).join('->')}->>${this.quoteValue(last)})`;
249
+ });
250
+ }
251
+ quoteIdentifier(id, quote = '"') {
252
+ if (RawQueryFragment.isKnownFragment(id)) {
253
+ return super.quoteIdentifier(id);
254
+ }
255
+ return `${quote}${id.toString().replace('.', `${quote}.${quote}`)}${quote}`;
256
+ }
257
+ pad(number, digits) {
258
+ return String(number).padStart(digits, '0');
259
+ }
260
+ /** @internal */
261
+ formatDate(date) {
262
+ if (this.timezone === 'Z') {
263
+ return date.toISOString();
264
+ }
265
+ let offset = -date.getTimezoneOffset();
266
+ let year = date.getFullYear();
267
+ const isBCYear = year < 1;
268
+ /* v8 ignore next */
269
+ if (isBCYear) {
270
+ year = Math.abs(year) + 1;
361
271
  }
362
- getDefaultClientUrl() {
363
- return 'postgresql://postgres@127.0.0.1:5432';
272
+ const datePart = `${this.pad(year, 4)}-${this.pad(date.getMonth() + 1, 2)}-${this.pad(date.getDate(), 2)}`;
273
+ const timePart = `${this.pad(date.getHours(), 2)}:${this.pad(date.getMinutes(), 2)}:${this.pad(date.getSeconds(), 2)}.${this.pad(date.getMilliseconds(), 3)}`;
274
+ let ret = `${datePart}T${timePart}`;
275
+ /* v8 ignore next */
276
+ if (offset < 0) {
277
+ ret += '-';
278
+ offset *= -1;
279
+ } else {
280
+ ret += '+';
364
281
  }
282
+ ret += this.pad(Math.floor(offset / 60), 2) + ':' + this.pad(offset % 60, 2);
283
+ /* v8 ignore next */
284
+ if (isBCYear) {
285
+ ret += ' BC';
286
+ }
287
+ return ret;
288
+ }
289
+ indexForeignKeys() {
290
+ return false;
291
+ }
292
+ getDefaultMappedType(type) {
293
+ const normalizedType = this.extractSimpleType(type);
294
+ const map = {
295
+ int2: 'smallint',
296
+ smallserial: 'smallint',
297
+ int: 'integer',
298
+ int4: 'integer',
299
+ serial: 'integer',
300
+ serial4: 'integer',
301
+ int8: 'bigint',
302
+ bigserial: 'bigint',
303
+ serial8: 'bigint',
304
+ numeric: 'decimal',
305
+ bool: 'boolean',
306
+ real: 'float',
307
+ float4: 'float',
308
+ float8: 'double',
309
+ timestamp: 'datetime',
310
+ timestamptz: 'datetime',
311
+ bytea: 'blob',
312
+ jsonb: 'json',
313
+ 'character varying': 'varchar',
314
+ bpchar: 'character',
315
+ };
316
+ return super.getDefaultMappedType(map[normalizedType] ?? type);
317
+ }
318
+ supportsSchemas() {
319
+ return true;
320
+ }
321
+ getDefaultSchemaName() {
322
+ return 'public';
323
+ }
324
+ /**
325
+ * Returns the default name of index for the given columns
326
+ * cannot go past 63 character length for identifiers in MySQL
327
+ */
328
+ getIndexName(tableName, columns, type) {
329
+ const indexName = super.getIndexName(tableName, columns, type);
330
+ if (indexName.length > 63) {
331
+ const suffix = type === 'primary' ? 'pkey' : type;
332
+ return `${indexName.substring(0, 55 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
333
+ }
334
+ return indexName;
335
+ }
336
+ getDefaultPrimaryName(tableName, columns) {
337
+ const indexName = `${tableName}_pkey`;
338
+ if (indexName.length > 63) {
339
+ return `${indexName.substring(0, 55 - 'pkey'.length)}_${Utils.hash(indexName, 5)}_pkey`;
340
+ }
341
+ return indexName;
342
+ }
343
+ /**
344
+ * @inheritDoc
345
+ */
346
+ castColumn(prop) {
347
+ switch (prop?.columnTypes?.[0]) {
348
+ case this.getUuidTypeDeclarationSQL({}):
349
+ return '::text';
350
+ case this.getBooleanTypeDeclarationSQL():
351
+ return '::int';
352
+ default:
353
+ return '';
354
+ }
355
+ }
356
+ getJsonArrayFromSQL(column, alias, _properties) {
357
+ return `jsonb_array_elements(${column}) as ${this.quoteIdentifier(alias)}`;
358
+ }
359
+ getJsonArrayElementPropertySQL(alias, property, type) {
360
+ const expr = `${this.quoteIdentifier(alias)}->>${this.quoteValue(property)}`;
361
+ return type in this.#jsonTypeCasts ? `(${expr})::${this.#jsonTypeCasts[type]}` : expr;
362
+ }
363
+ getDefaultClientUrl() {
364
+ return 'postgresql://postgres@127.0.0.1:5432';
365
+ }
365
366
  }