@mikro-orm/sql 7.0.0-dev.98 → 7.0.0-rc.0
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.
- package/AbstractSqlConnection.d.ts +6 -7
- package/AbstractSqlConnection.js +27 -24
- package/AbstractSqlDriver.d.ts +82 -23
- package/AbstractSqlDriver.js +584 -184
- package/AbstractSqlPlatform.d.ts +3 -4
- package/AbstractSqlPlatform.js +0 -4
- package/PivotCollectionPersister.d.ts +5 -0
- package/PivotCollectionPersister.js +30 -12
- package/SqlEntityManager.d.ts +2 -2
- package/dialects/mysql/{MySqlPlatform.d.ts → BaseMySqlPlatform.d.ts} +3 -2
- package/dialects/mysql/{MySqlPlatform.js → BaseMySqlPlatform.js} +5 -1
- package/dialects/mysql/MySqlSchemaHelper.d.ts +12 -1
- package/dialects/mysql/MySqlSchemaHelper.js +97 -6
- package/dialects/mysql/index.d.ts +1 -2
- package/dialects/mysql/index.js +1 -2
- package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +106 -0
- package/dialects/postgresql/BasePostgreSqlPlatform.js +350 -0
- package/dialects/postgresql/FullTextType.d.ts +14 -0
- package/dialects/postgresql/FullTextType.js +59 -0
- package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +8 -0
- package/dialects/postgresql/PostgreSqlExceptionConverter.js +47 -0
- package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +90 -0
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +732 -0
- package/dialects/postgresql/index.d.ts +3 -0
- package/dialects/postgresql/index.js +3 -0
- package/dialects/sqlite/BaseSqliteConnection.d.ts +1 -0
- package/dialects/sqlite/BaseSqliteConnection.js +14 -1
- package/dialects/sqlite/BaseSqlitePlatform.d.ts +6 -0
- package/dialects/sqlite/BaseSqlitePlatform.js +12 -0
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +25 -0
- package/dialects/sqlite/SqliteSchemaHelper.js +145 -19
- package/dialects/sqlite/index.d.ts +0 -1
- package/dialects/sqlite/index.js +0 -1
- package/package.json +5 -6
- package/plugin/transformer.d.ts +1 -1
- package/plugin/transformer.js +1 -1
- package/query/CriteriaNode.d.ts +9 -5
- package/query/CriteriaNode.js +16 -15
- package/query/CriteriaNodeFactory.d.ts +6 -6
- package/query/CriteriaNodeFactory.js +33 -31
- package/query/NativeQueryBuilder.d.ts +3 -2
- package/query/NativeQueryBuilder.js +1 -2
- package/query/ObjectCriteriaNode.js +50 -35
- package/query/QueryBuilder.d.ts +548 -79
- package/query/QueryBuilder.js +537 -159
- package/query/QueryBuilderHelper.d.ts +22 -14
- package/query/QueryBuilderHelper.js +158 -69
- package/query/ScalarCriteriaNode.js +2 -2
- package/query/raw.d.ts +11 -3
- package/query/raw.js +1 -2
- package/schema/DatabaseSchema.d.ts +15 -2
- package/schema/DatabaseSchema.js +143 -15
- package/schema/DatabaseTable.d.ts +12 -0
- package/schema/DatabaseTable.js +91 -31
- package/schema/SchemaComparator.d.ts +8 -0
- package/schema/SchemaComparator.js +126 -3
- package/schema/SchemaHelper.d.ts +26 -3
- package/schema/SchemaHelper.js +98 -11
- package/schema/SqlSchemaGenerator.d.ts +10 -0
- package/schema/SqlSchemaGenerator.js +137 -9
- package/tsconfig.build.tsbuildinfo +1 -0
- package/typings.d.ts +74 -36
- package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +0 -1
- package/dialects/postgresql/PostgreSqlTableCompiler.js +0 -1
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { ALIAS_REPLACEMENT, ARRAY_OPERATORS, raw, RawQueryFragment, Type, Utils, } from '@mikro-orm/core';
|
|
2
|
+
import { AbstractSqlPlatform } from '../../AbstractSqlPlatform.js';
|
|
3
|
+
import { PostgreSqlNativeQueryBuilder } from './PostgreSqlNativeQueryBuilder.js';
|
|
4
|
+
import { PostgreSqlSchemaHelper } from './PostgreSqlSchemaHelper.js';
|
|
5
|
+
import { PostgreSqlExceptionConverter } from './PostgreSqlExceptionConverter.js';
|
|
6
|
+
import { FullTextType } from './FullTextType.js';
|
|
7
|
+
export class BasePostgreSqlPlatform extends AbstractSqlPlatform {
|
|
8
|
+
schemaHelper = new PostgreSqlSchemaHelper(this);
|
|
9
|
+
exceptionConverter = new PostgreSqlExceptionConverter();
|
|
10
|
+
createNativeQueryBuilder() {
|
|
11
|
+
return new PostgreSqlNativeQueryBuilder(this);
|
|
12
|
+
}
|
|
13
|
+
usesReturningStatement() {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
usesCascadeStatement() {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
supportsNativeEnums() {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
usesEnumCheckConstraints() {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
supportsMaterializedViews() {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
supportsCustomPrimaryKeyNames() {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
getCurrentTimestampSQL(length) {
|
|
32
|
+
return `current_timestamp(${length})`;
|
|
33
|
+
}
|
|
34
|
+
getDateTimeTypeDeclarationSQL(column) {
|
|
35
|
+
/* v8 ignore next */
|
|
36
|
+
return 'timestamptz' + (column.length != null ? `(${column.length})` : '');
|
|
37
|
+
}
|
|
38
|
+
getDefaultDateTimeLength() {
|
|
39
|
+
return 6;
|
|
40
|
+
}
|
|
41
|
+
getTimeTypeDeclarationSQL() {
|
|
42
|
+
return 'time(0)';
|
|
43
|
+
}
|
|
44
|
+
getIntegerTypeDeclarationSQL(column) {
|
|
45
|
+
if (column.autoincrement && !column.generated) {
|
|
46
|
+
return 'serial';
|
|
47
|
+
}
|
|
48
|
+
return 'int';
|
|
49
|
+
}
|
|
50
|
+
getBigIntTypeDeclarationSQL(column) {
|
|
51
|
+
/* v8 ignore next */
|
|
52
|
+
if (column.autoincrement) {
|
|
53
|
+
return `bigserial`;
|
|
54
|
+
}
|
|
55
|
+
return 'bigint';
|
|
56
|
+
}
|
|
57
|
+
getTinyIntTypeDeclarationSQL(column) {
|
|
58
|
+
return 'smallint';
|
|
59
|
+
}
|
|
60
|
+
getUuidTypeDeclarationSQL(column) {
|
|
61
|
+
return `uuid`;
|
|
62
|
+
}
|
|
63
|
+
getFullTextWhereClause(prop) {
|
|
64
|
+
if (prop.customType instanceof FullTextType) {
|
|
65
|
+
return `:column: @@ plainto_tsquery('${prop.customType.regconfig}', :query)`;
|
|
66
|
+
}
|
|
67
|
+
/* v8 ignore next */
|
|
68
|
+
if (prop.columnTypes[0] === 'tsvector') {
|
|
69
|
+
return `:column: @@ plainto_tsquery('simple', :query)`;
|
|
70
|
+
}
|
|
71
|
+
return `to_tsvector('simple', :column:) @@ plainto_tsquery('simple', :query)`;
|
|
72
|
+
}
|
|
73
|
+
supportsCreatingFullTextIndex() {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
|
|
77
|
+
/* v8 ignore next */
|
|
78
|
+
const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
|
|
79
|
+
const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
|
|
80
|
+
const quotedIndexName = this.quoteIdentifier(indexName);
|
|
81
|
+
if (columns.length === 1 && columns[0].type === 'tsvector') {
|
|
82
|
+
return `create index ${quotedIndexName} on ${quotedTableName} using gin(${quotedColumnNames[0]})`;
|
|
83
|
+
}
|
|
84
|
+
return `create index ${quotedIndexName} on ${quotedTableName} using gin(to_tsvector('simple', ${quotedColumnNames.join(` || ' ' || `)}))`;
|
|
85
|
+
}
|
|
86
|
+
normalizeColumnType(type, options) {
|
|
87
|
+
const simpleType = this.extractSimpleType(type);
|
|
88
|
+
if (['int', 'int4', 'integer'].includes(simpleType)) {
|
|
89
|
+
return this.getIntegerTypeDeclarationSQL({});
|
|
90
|
+
}
|
|
91
|
+
if (['bigint', 'int8'].includes(simpleType)) {
|
|
92
|
+
return this.getBigIntTypeDeclarationSQL({});
|
|
93
|
+
}
|
|
94
|
+
if (['smallint', 'int2'].includes(simpleType)) {
|
|
95
|
+
return this.getSmallIntTypeDeclarationSQL({});
|
|
96
|
+
}
|
|
97
|
+
if (['boolean', 'bool'].includes(simpleType)) {
|
|
98
|
+
return this.getBooleanTypeDeclarationSQL();
|
|
99
|
+
}
|
|
100
|
+
if (['varchar', 'character varying'].includes(simpleType)) {
|
|
101
|
+
return this.getVarcharTypeDeclarationSQL(options);
|
|
102
|
+
}
|
|
103
|
+
if (['char', 'bpchar'].includes(simpleType)) {
|
|
104
|
+
return this.getCharTypeDeclarationSQL(options);
|
|
105
|
+
}
|
|
106
|
+
if (['decimal', 'numeric'].includes(simpleType)) {
|
|
107
|
+
return this.getDecimalTypeDeclarationSQL(options);
|
|
108
|
+
}
|
|
109
|
+
if (['interval'].includes(simpleType)) {
|
|
110
|
+
return this.getIntervalTypeDeclarationSQL(options);
|
|
111
|
+
}
|
|
112
|
+
return super.normalizeColumnType(type, options);
|
|
113
|
+
}
|
|
114
|
+
getMappedType(type) {
|
|
115
|
+
switch (this.extractSimpleType(type)) {
|
|
116
|
+
case 'tsvector': return Type.getType(FullTextType);
|
|
117
|
+
default: return super.getMappedType(type);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
getRegExpOperator(val, flags) {
|
|
121
|
+
/* v8 ignore next */
|
|
122
|
+
if ((val instanceof RegExp && val.flags.includes('i')) || flags?.includes('i')) {
|
|
123
|
+
return '~*';
|
|
124
|
+
}
|
|
125
|
+
return '~';
|
|
126
|
+
}
|
|
127
|
+
/* v8 ignore next */
|
|
128
|
+
getRegExpValue(val) {
|
|
129
|
+
if (val.flags.includes('i')) {
|
|
130
|
+
return { $re: val.source, $flags: val.flags };
|
|
131
|
+
}
|
|
132
|
+
return { $re: val.source };
|
|
133
|
+
}
|
|
134
|
+
isBigIntProperty(prop) {
|
|
135
|
+
return super.isBigIntProperty(prop) || (['bigserial', 'int8'].includes(prop.columnTypes?.[0]));
|
|
136
|
+
}
|
|
137
|
+
getArrayDeclarationSQL() {
|
|
138
|
+
return 'text[]';
|
|
139
|
+
}
|
|
140
|
+
getFloatDeclarationSQL() {
|
|
141
|
+
return 'real';
|
|
142
|
+
}
|
|
143
|
+
getDoubleDeclarationSQL() {
|
|
144
|
+
return 'double precision';
|
|
145
|
+
}
|
|
146
|
+
getEnumTypeDeclarationSQL(column) {
|
|
147
|
+
/* v8 ignore next */
|
|
148
|
+
if (column.nativeEnumName) {
|
|
149
|
+
return column.nativeEnumName;
|
|
150
|
+
}
|
|
151
|
+
if (column.items?.every(item => typeof item === 'string')) {
|
|
152
|
+
return 'text';
|
|
153
|
+
}
|
|
154
|
+
return `smallint`;
|
|
155
|
+
}
|
|
156
|
+
supportsMultipleStatements() {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
getBeginTransactionSQL(options) {
|
|
160
|
+
if (options?.isolationLevel || options?.readOnly) {
|
|
161
|
+
let sql = 'start transaction';
|
|
162
|
+
sql += options.isolationLevel ? ` isolation level ${options.isolationLevel}` : '';
|
|
163
|
+
sql += options.readOnly ? ` read only` : '';
|
|
164
|
+
return [sql];
|
|
165
|
+
}
|
|
166
|
+
return ['begin'];
|
|
167
|
+
}
|
|
168
|
+
marshallArray(values) {
|
|
169
|
+
const quote = (v) => v === '' || v.match(/["{},\\]/) ? JSON.stringify(v) : v;
|
|
170
|
+
return `{${values.map(v => quote('' + v)).join(',')}}`;
|
|
171
|
+
}
|
|
172
|
+
/* v8 ignore next */
|
|
173
|
+
unmarshallArray(value) {
|
|
174
|
+
if (value === '{}') {
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
return value.substring(1, value.length - 1).split(',').map(v => {
|
|
178
|
+
if (v === `""`) {
|
|
179
|
+
return '';
|
|
180
|
+
}
|
|
181
|
+
if (v.match(/"(.*)"/)) {
|
|
182
|
+
return v.substring(1, v.length - 1).replaceAll('\\"', '"');
|
|
183
|
+
}
|
|
184
|
+
return v;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
getVarcharTypeDeclarationSQL(column) {
|
|
188
|
+
if (column.length === -1) {
|
|
189
|
+
return 'varchar';
|
|
190
|
+
}
|
|
191
|
+
return super.getVarcharTypeDeclarationSQL(column);
|
|
192
|
+
}
|
|
193
|
+
getCharTypeDeclarationSQL(column) {
|
|
194
|
+
if (column.length === -1) {
|
|
195
|
+
return 'char';
|
|
196
|
+
}
|
|
197
|
+
return super.getCharTypeDeclarationSQL(column);
|
|
198
|
+
}
|
|
199
|
+
getIntervalTypeDeclarationSQL(column) {
|
|
200
|
+
return 'interval' + (column.length != null ? `(${column.length})` : '');
|
|
201
|
+
}
|
|
202
|
+
getBlobDeclarationSQL() {
|
|
203
|
+
return 'bytea';
|
|
204
|
+
}
|
|
205
|
+
getJsonDeclarationSQL() {
|
|
206
|
+
return 'jsonb';
|
|
207
|
+
}
|
|
208
|
+
getSearchJsonPropertyKey(path, type, aliased, value) {
|
|
209
|
+
const first = path.shift();
|
|
210
|
+
const last = path.pop();
|
|
211
|
+
const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${first}` : first);
|
|
212
|
+
type = typeof type === 'string' ? this.getMappedType(type).runtimeType : String(type);
|
|
213
|
+
const types = {
|
|
214
|
+
number: 'float8',
|
|
215
|
+
bigint: 'int8',
|
|
216
|
+
boolean: 'bool',
|
|
217
|
+
};
|
|
218
|
+
const cast = (key) => raw(type in types ? `(${key})::${types[type]}` : key);
|
|
219
|
+
let lastOperator = '->>';
|
|
220
|
+
// force `->` for operator payloads with array values
|
|
221
|
+
if (Utils.isPlainObject(value) && Object.keys(value).every(key => ARRAY_OPERATORS.includes(key) && Array.isArray(value[key]))) {
|
|
222
|
+
lastOperator = '->';
|
|
223
|
+
}
|
|
224
|
+
if (path.length === 0) {
|
|
225
|
+
return cast(`${root}${lastOperator}'${last}'`);
|
|
226
|
+
}
|
|
227
|
+
return cast(`${root}->${path.map(a => this.quoteValue(a)).join('->')}${lastOperator}'${last}'`);
|
|
228
|
+
}
|
|
229
|
+
getJsonIndexDefinition(index) {
|
|
230
|
+
return index.columnNames
|
|
231
|
+
.map(column => {
|
|
232
|
+
if (!column.includes('.')) {
|
|
233
|
+
return column;
|
|
234
|
+
}
|
|
235
|
+
const path = column.split('.');
|
|
236
|
+
const first = path.shift();
|
|
237
|
+
const last = path.pop();
|
|
238
|
+
if (path.length === 0) {
|
|
239
|
+
return `(${this.quoteIdentifier(first)}->>${this.quoteValue(last)})`;
|
|
240
|
+
}
|
|
241
|
+
return `(${this.quoteIdentifier(first)}->${path.map(c => this.quoteValue(c)).join('->')}->>${this.quoteValue(last)})`;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
quoteIdentifier(id, quote = '"') {
|
|
245
|
+
if (RawQueryFragment.isKnownFragment(id)) {
|
|
246
|
+
return super.quoteIdentifier(id);
|
|
247
|
+
}
|
|
248
|
+
return `${quote}${id.toString().replace('.', `${quote}.${quote}`)}${quote}`;
|
|
249
|
+
}
|
|
250
|
+
pad(number, digits) {
|
|
251
|
+
return String(number).padStart(digits, '0');
|
|
252
|
+
}
|
|
253
|
+
/** @internal */
|
|
254
|
+
formatDate(date) {
|
|
255
|
+
if (this.timezone === 'Z') {
|
|
256
|
+
return date.toISOString();
|
|
257
|
+
}
|
|
258
|
+
let offset = -date.getTimezoneOffset();
|
|
259
|
+
let year = date.getFullYear();
|
|
260
|
+
const isBCYear = year < 1;
|
|
261
|
+
/* v8 ignore next */
|
|
262
|
+
if (isBCYear) {
|
|
263
|
+
year = Math.abs(year) + 1;
|
|
264
|
+
}
|
|
265
|
+
const datePart = `${this.pad(year, 4)}-${this.pad(date.getMonth() + 1, 2)}-${this.pad(date.getDate(), 2)}`;
|
|
266
|
+
const timePart = `${this.pad(date.getHours(), 2)}:${this.pad(date.getMinutes(), 2)}:${this.pad(date.getSeconds(), 2)}.${this.pad(date.getMilliseconds(), 3)}`;
|
|
267
|
+
let ret = `${datePart}T${timePart}`;
|
|
268
|
+
/* v8 ignore next */
|
|
269
|
+
if (offset < 0) {
|
|
270
|
+
ret += '-';
|
|
271
|
+
offset *= -1;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
ret += '+';
|
|
275
|
+
}
|
|
276
|
+
ret += this.pad(Math.floor(offset / 60), 2) + ':' + this.pad(offset % 60, 2);
|
|
277
|
+
/* v8 ignore next */
|
|
278
|
+
if (isBCYear) {
|
|
279
|
+
ret += ' BC';
|
|
280
|
+
}
|
|
281
|
+
return ret;
|
|
282
|
+
}
|
|
283
|
+
indexForeignKeys() {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
getDefaultMappedType(type) {
|
|
287
|
+
const normalizedType = this.extractSimpleType(type);
|
|
288
|
+
const map = {
|
|
289
|
+
'int2': 'smallint',
|
|
290
|
+
'smallserial': 'smallint',
|
|
291
|
+
'int': 'integer',
|
|
292
|
+
'int4': 'integer',
|
|
293
|
+
'serial': 'integer',
|
|
294
|
+
'serial4': 'integer',
|
|
295
|
+
'int8': 'bigint',
|
|
296
|
+
'bigserial': 'bigint',
|
|
297
|
+
'serial8': 'bigint',
|
|
298
|
+
'numeric': 'decimal',
|
|
299
|
+
'bool': 'boolean',
|
|
300
|
+
'real': 'float',
|
|
301
|
+
'float4': 'float',
|
|
302
|
+
'float8': 'double',
|
|
303
|
+
'timestamp': 'datetime',
|
|
304
|
+
'timestamptz': 'datetime',
|
|
305
|
+
'bytea': 'blob',
|
|
306
|
+
'jsonb': 'json',
|
|
307
|
+
'character varying': 'varchar',
|
|
308
|
+
'bpchar': 'character',
|
|
309
|
+
};
|
|
310
|
+
return super.getDefaultMappedType(map[normalizedType] ?? type);
|
|
311
|
+
}
|
|
312
|
+
supportsSchemas() {
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
getDefaultSchemaName() {
|
|
316
|
+
return 'public';
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Returns the default name of index for the given columns
|
|
320
|
+
* cannot go past 63 character length for identifiers in MySQL
|
|
321
|
+
*/
|
|
322
|
+
getIndexName(tableName, columns, type) {
|
|
323
|
+
const indexName = super.getIndexName(tableName, columns, type);
|
|
324
|
+
if (indexName.length > 63) {
|
|
325
|
+
const suffix = type === 'primary' ? 'pkey' : type;
|
|
326
|
+
return `${indexName.substring(0, 55 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
|
|
327
|
+
}
|
|
328
|
+
return indexName;
|
|
329
|
+
}
|
|
330
|
+
getDefaultPrimaryName(tableName, columns) {
|
|
331
|
+
const indexName = `${tableName}_pkey`;
|
|
332
|
+
if (indexName.length > 63) {
|
|
333
|
+
return `${indexName.substring(0, 55 - 'pkey'.length)}_${Utils.hash(indexName, 5)}_pkey`;
|
|
334
|
+
}
|
|
335
|
+
return indexName;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* @inheritDoc
|
|
339
|
+
*/
|
|
340
|
+
castColumn(prop) {
|
|
341
|
+
switch (prop?.columnTypes?.[0]) {
|
|
342
|
+
case this.getUuidTypeDeclarationSQL({}): return '::text';
|
|
343
|
+
case this.getBooleanTypeDeclarationSQL(): return '::int';
|
|
344
|
+
default: return '';
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
getDefaultClientUrl() {
|
|
348
|
+
return 'postgresql://postgres@127.0.0.1:5432';
|
|
349
|
+
}
|
|
350
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Type, type TransformContext, type RawQueryFragment } from '@mikro-orm/core';
|
|
2
|
+
import type { BasePostgreSqlPlatform } from './BasePostgreSqlPlatform.js';
|
|
3
|
+
type FullTextWeight = 'A' | 'B' | 'C' | 'D';
|
|
4
|
+
export type WeightedFullTextValue = {
|
|
5
|
+
[K in FullTextWeight]?: string | null;
|
|
6
|
+
};
|
|
7
|
+
export declare class FullTextType extends Type<string | WeightedFullTextValue, string | null | RawQueryFragment> {
|
|
8
|
+
regconfig: string;
|
|
9
|
+
constructor(regconfig?: string);
|
|
10
|
+
compareAsType(): string;
|
|
11
|
+
getColumnType(): string;
|
|
12
|
+
convertToDatabaseValue(value: string | WeightedFullTextValue, platform: BasePostgreSqlPlatform, context?: TransformContext | boolean): string | null | RawQueryFragment;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { raw, Type } from '@mikro-orm/core';
|
|
2
|
+
export class FullTextType extends Type {
|
|
3
|
+
regconfig;
|
|
4
|
+
constructor(regconfig = 'simple') {
|
|
5
|
+
super();
|
|
6
|
+
this.regconfig = regconfig;
|
|
7
|
+
}
|
|
8
|
+
compareAsType() {
|
|
9
|
+
return 'any';
|
|
10
|
+
}
|
|
11
|
+
getColumnType() {
|
|
12
|
+
return 'tsvector';
|
|
13
|
+
}
|
|
14
|
+
// Use convertToDatabaseValue to prepare insert queries as this method has
|
|
15
|
+
// access to the raw JS value. Return Knex#raw to prevent QueryBuilderHelper#mapData
|
|
16
|
+
// from sanitizing the returned chaing of SQL functions.
|
|
17
|
+
convertToDatabaseValue(value, platform, context) {
|
|
18
|
+
// Don't convert to values from select queries to the to_tsvector notation
|
|
19
|
+
// these should be compared as string using a special oparator or function
|
|
20
|
+
// this behaviour is defined in Platform#getFullTextWhereClause.
|
|
21
|
+
// This is always a string.
|
|
22
|
+
if (typeof context === 'object' && context.fromQuery) {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
// Null values should not be processed
|
|
26
|
+
if (!value) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
// the object from that looks like { A: 'test data', B: 'test data2' ... }
|
|
30
|
+
// must be converted to
|
|
31
|
+
// setweight(to_tsvector(regconfig, value), A) || setweight(to_tsvector(regconfig, value), B)... etc
|
|
32
|
+
// use Knex#raw to do binding of the values sanitization of the boundvalues
|
|
33
|
+
// as we return a raw string which should not be sanitzed anymore
|
|
34
|
+
if (typeof value === 'object') {
|
|
35
|
+
const bindings = [];
|
|
36
|
+
const sqlParts = [];
|
|
37
|
+
for (const [weight, data] of Object.entries(value)) {
|
|
38
|
+
// Check whether the weight is valid according to Postgres,
|
|
39
|
+
// Postgres allows the weight to be upper and lowercase.
|
|
40
|
+
if (!['A', 'B', 'C', 'D'].includes(weight.toUpperCase())) {
|
|
41
|
+
throw new Error('Weight should be one of A, B, C, D.');
|
|
42
|
+
}
|
|
43
|
+
// Ignore all values that are not a string
|
|
44
|
+
if (typeof data === 'string') {
|
|
45
|
+
sqlParts.push('setweight(to_tsvector(?, ?), ?)');
|
|
46
|
+
bindings.push(this.regconfig, data, weight);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Return null if the object has no valid strings
|
|
50
|
+
if (sqlParts.length === 0) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
// Join all the `setweight` parts using the PostgreSQL tsvector `||` concatenation operator
|
|
54
|
+
return raw(sqlParts.join(' || '), bindings);
|
|
55
|
+
}
|
|
56
|
+
// if it's not an object, it is expected to be string which does not have to be wrapped in setweight.
|
|
57
|
+
return raw('to_tsvector(?, ?)', [this.regconfig, value]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ExceptionConverter, type Dictionary, type DriverException } from '@mikro-orm/core';
|
|
2
|
+
export declare class PostgreSqlExceptionConverter extends ExceptionConverter {
|
|
3
|
+
/**
|
|
4
|
+
* @see http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
|
|
5
|
+
* @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractPostgreSQLDriver.php
|
|
6
|
+
*/
|
|
7
|
+
convertException(exception: Error & Dictionary): DriverException;
|
|
8
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { DeadlockException, ExceptionConverter, ForeignKeyConstraintViolationException, InvalidFieldNameException, NonUniqueFieldNameException, NotNullConstraintViolationException, SyntaxErrorException, TableExistsException, TableNotFoundException, UniqueConstraintViolationException, CheckConstraintViolationException, } from '@mikro-orm/core';
|
|
2
|
+
export class PostgreSqlExceptionConverter extends ExceptionConverter {
|
|
3
|
+
/**
|
|
4
|
+
* @see http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
|
|
5
|
+
* @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractPostgreSQLDriver.php
|
|
6
|
+
*/
|
|
7
|
+
convertException(exception) {
|
|
8
|
+
if (exception.detail?.toString().trim()) {
|
|
9
|
+
exception.message += '\n - detail: ' + exception.detail;
|
|
10
|
+
}
|
|
11
|
+
if (exception.hint?.toString().trim()) {
|
|
12
|
+
exception.message += '\n - hint: ' + exception.hint;
|
|
13
|
+
}
|
|
14
|
+
/* v8 ignore next */
|
|
15
|
+
switch (exception.code) {
|
|
16
|
+
case '40001':
|
|
17
|
+
case '40P01':
|
|
18
|
+
return new DeadlockException(exception);
|
|
19
|
+
case '0A000':
|
|
20
|
+
// Foreign key constraint violations during a TRUNCATE operation
|
|
21
|
+
// are considered "feature not supported" in PostgreSQL.
|
|
22
|
+
if (exception.message.includes('truncate')) {
|
|
23
|
+
return new ForeignKeyConstraintViolationException(exception);
|
|
24
|
+
}
|
|
25
|
+
break;
|
|
26
|
+
case '23502':
|
|
27
|
+
return new NotNullConstraintViolationException(exception);
|
|
28
|
+
case '23503':
|
|
29
|
+
return new ForeignKeyConstraintViolationException(exception);
|
|
30
|
+
case '23505':
|
|
31
|
+
return new UniqueConstraintViolationException(exception);
|
|
32
|
+
case '23514':
|
|
33
|
+
return new CheckConstraintViolationException(exception);
|
|
34
|
+
case '42601':
|
|
35
|
+
return new SyntaxErrorException(exception);
|
|
36
|
+
case '42702':
|
|
37
|
+
return new NonUniqueFieldNameException(exception);
|
|
38
|
+
case '42703':
|
|
39
|
+
return new InvalidFieldNameException(exception);
|
|
40
|
+
case '42P01':
|
|
41
|
+
return new TableNotFoundException(exception);
|
|
42
|
+
case '42P07':
|
|
43
|
+
return new TableExistsException(exception);
|
|
44
|
+
}
|
|
45
|
+
return super.convertException(exception);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type Dictionary } from '@mikro-orm/core';
|
|
2
|
+
import { SchemaHelper } from '../../schema/SchemaHelper.js';
|
|
3
|
+
import type { AbstractSqlConnection } from '../../AbstractSqlConnection.js';
|
|
4
|
+
import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../../typings.js';
|
|
5
|
+
import type { DatabaseSchema } from '../../schema/DatabaseSchema.js';
|
|
6
|
+
import type { DatabaseTable } from '../../schema/DatabaseTable.js';
|
|
7
|
+
export declare class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
8
|
+
static readonly DEFAULT_VALUES: {
|
|
9
|
+
'now()': string[];
|
|
10
|
+
'current_timestamp(?)': string[];
|
|
11
|
+
"('now'::text)::timestamp(?) with time zone": string[];
|
|
12
|
+
"('now'::text)::timestamp(?) without time zone": string[];
|
|
13
|
+
'null::character varying': string[];
|
|
14
|
+
'null::timestamp with time zone': string[];
|
|
15
|
+
'null::timestamp without time zone': string[];
|
|
16
|
+
};
|
|
17
|
+
getSchemaBeginning(charset: string, disableForeignKeys?: boolean): string;
|
|
18
|
+
getCreateDatabaseSQL(name: string): string;
|
|
19
|
+
getListTablesSQL(): string;
|
|
20
|
+
private getIgnoredViewsCondition;
|
|
21
|
+
getListViewsSQL(): string;
|
|
22
|
+
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection): Promise<void>;
|
|
23
|
+
getListMaterializedViewsSQL(): string;
|
|
24
|
+
loadMaterializedViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
|
|
25
|
+
createMaterializedView(name: string, schema: string | undefined, definition: string, withData?: boolean): string;
|
|
26
|
+
dropMaterializedViewIfExists(name: string, schema?: string): string;
|
|
27
|
+
refreshMaterializedView(name: string, schema?: string, concurrently?: boolean): string;
|
|
28
|
+
getNamespaces(connection: AbstractSqlConnection): Promise<string[]>;
|
|
29
|
+
private getIgnoredNamespacesConditionSQL;
|
|
30
|
+
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[]): Promise<void>;
|
|
31
|
+
getAllIndexes(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<IndexDef[]>>;
|
|
32
|
+
/**
|
|
33
|
+
* Parses column definitions from the full CREATE INDEX expression.
|
|
34
|
+
* Since pg_get_indexdef(oid, col_num, true) doesn't include sort modifiers,
|
|
35
|
+
* we extract them from the full expression instead.
|
|
36
|
+
*
|
|
37
|
+
* We use columnDefs (from individual pg_get_indexdef calls) as the source
|
|
38
|
+
* of column names, and find their modifiers in the expression.
|
|
39
|
+
*/
|
|
40
|
+
private parseIndexColumnsFromExpression;
|
|
41
|
+
/**
|
|
42
|
+
* Extracts the content inside parentheses starting at the given position.
|
|
43
|
+
* Handles nested parentheses correctly.
|
|
44
|
+
*/
|
|
45
|
+
private extractParenthesizedContent;
|
|
46
|
+
getAllColumns(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]>, nativeEnums?: Dictionary<{
|
|
47
|
+
name: string;
|
|
48
|
+
schema?: string;
|
|
49
|
+
items: string[];
|
|
50
|
+
}>): Promise<Dictionary<Column[]>>;
|
|
51
|
+
getAllChecks(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]>): Promise<Dictionary<CheckDef[]>>;
|
|
52
|
+
getAllForeignKeys(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]>): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
53
|
+
getNativeEnumDefinitions(connection: AbstractSqlConnection, schemas: string[]): Promise<Dictionary<{
|
|
54
|
+
name: string;
|
|
55
|
+
schema?: string;
|
|
56
|
+
items: string[];
|
|
57
|
+
}>>;
|
|
58
|
+
getCreateNativeEnumSQL(name: string, values: unknown[], schema?: string): string;
|
|
59
|
+
getDropNativeEnumSQL(name: string, schema?: string): string;
|
|
60
|
+
getAlterNativeEnumSQL(name: string, schema?: string, value?: string, items?: string[], oldItems?: string[]): string;
|
|
61
|
+
private getEnumDefinitions;
|
|
62
|
+
createTableColumn(column: Column, table: DatabaseTable): string | undefined;
|
|
63
|
+
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string[];
|
|
64
|
+
castColumn(name: string, type: string): string;
|
|
65
|
+
dropForeignKey(tableName: string, constraintName: string): string;
|
|
66
|
+
getPostAlterTable(tableDiff: TableDifference, safe: boolean): string[];
|
|
67
|
+
private getAlterColumnAutoincrement;
|
|
68
|
+
getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
|
|
69
|
+
alterTableComment(table: DatabaseTable, comment?: string): string;
|
|
70
|
+
normalizeDefaultValue(defaultValue: string, length: number): string | number;
|
|
71
|
+
appendComments(table: DatabaseTable): string[];
|
|
72
|
+
getDatabaseExistsSQL(name: string): string;
|
|
73
|
+
getDatabaseNotExistsError(dbName: string): string;
|
|
74
|
+
getManagementDbName(): string;
|
|
75
|
+
disableForeignKeysSQL(): string;
|
|
76
|
+
enableForeignKeysSQL(): string;
|
|
77
|
+
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string[];
|
|
78
|
+
dropIndex(table: string, index: IndexDef, oldIndexName?: string): string;
|
|
79
|
+
/**
|
|
80
|
+
* Build the column list for a PostgreSQL index.
|
|
81
|
+
*/
|
|
82
|
+
protected getIndexColumns(index: IndexDef): string;
|
|
83
|
+
/**
|
|
84
|
+
* PostgreSQL-specific index options like fill factor.
|
|
85
|
+
*/
|
|
86
|
+
protected getCreateIndexSuffix(index: IndexDef): string;
|
|
87
|
+
private getIndexesSQL;
|
|
88
|
+
private getChecksSQL;
|
|
89
|
+
inferLengthFromColumnType(type: string): number | undefined;
|
|
90
|
+
}
|