@mikro-orm/postgresql 7.0.0-dev.2 → 7.0.0-dev.200

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.
@@ -1,48 +1,9 @@
1
1
  import { Client } from 'pg';
2
+ import array from 'postgres-array';
2
3
  import parseDate from 'postgres-date';
3
4
  import PostgresInterval from 'postgres-interval';
4
- import { raw, ALIAS_REPLACEMENT, Utils, Type, RawQueryFragment, } from '@mikro-orm/core';
5
- import { AbstractSqlPlatform, PostgreSqlNativeQueryBuilder } from '@mikro-orm/knex';
6
- import { PostgreSqlSchemaHelper } from './PostgreSqlSchemaHelper.js';
7
- import { PostgreSqlExceptionConverter } from './PostgreSqlExceptionConverter.js';
8
- import { FullTextType } from './types/FullTextType.js';
9
- export class PostgreSqlPlatform extends AbstractSqlPlatform {
10
- schemaHelper = new PostgreSqlSchemaHelper(this);
11
- exceptionConverter = new PostgreSqlExceptionConverter();
12
- setConfig(config) {
13
- if (config.get('forceUtcTimezone') == null) {
14
- config.set('forceUtcTimezone', true);
15
- }
16
- super.setConfig(config);
17
- }
18
- createNativeQueryBuilder() {
19
- return new PostgreSqlNativeQueryBuilder(this);
20
- }
21
- usesReturningStatement() {
22
- return true;
23
- }
24
- usesCascadeStatement() {
25
- return true;
26
- }
27
- supportsNativeEnums() {
28
- return true;
29
- }
30
- usesEnumCheckConstraints() {
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
- }
5
+ import { BasePostgreSqlPlatform, Utils } from '@mikro-orm/sql';
6
+ export class PostgreSqlPlatform extends BasePostgreSqlPlatform {
46
7
  convertIntervalToJSValue(value) {
47
8
  return PostgresInterval(value);
48
9
  }
@@ -52,215 +13,13 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
52
13
  }
53
14
  return value;
54
15
  }
55
- getTimeTypeDeclarationSQL() {
56
- return 'time(0)';
57
- }
58
- getIntegerTypeDeclarationSQL(column) {
59
- if (column.autoincrement && !column.generated) {
60
- return 'serial';
61
- }
62
- return 'int';
63
- }
64
- getBigIntTypeDeclarationSQL(column) {
65
- /* v8 ignore next 3 */
66
- if (column.autoincrement) {
67
- return `bigserial`;
68
- }
69
- return 'bigint';
70
- }
71
- getTinyIntTypeDeclarationSQL(column) {
72
- return 'smallint';
73
- }
74
- getUuidTypeDeclarationSQL(column) {
75
- return `uuid`;
76
- }
77
- getFullTextWhereClause(prop) {
78
- if (prop.customType instanceof FullTextType) {
79
- return `:column: @@ plainto_tsquery('${prop.customType.regconfig}', :query)`;
80
- }
81
- /* v8 ignore next 3 */
82
- if (prop.columnTypes[0] === 'tsvector') {
83
- return `:column: @@ plainto_tsquery('simple', :query)`;
84
- }
85
- return `to_tsvector('simple', :column:) @@ plainto_tsquery('simple', :query)`;
86
- }
87
- supportsCreatingFullTextIndex() {
88
- return true;
89
- }
90
- getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
91
- /* v8 ignore next */
92
- const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
93
- const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
94
- const quotedIndexName = this.quoteIdentifier(indexName);
95
- if (columns.length === 1 && columns[0].type === 'tsvector') {
96
- return `create index ${quotedIndexName} on ${quotedTableName} using gin(${quotedColumnNames[0]})`;
97
- }
98
- return `create index ${quotedIndexName} on ${quotedTableName} using gin(to_tsvector('simple', ${quotedColumnNames.join(` || ' ' || `)}))`;
99
- }
100
- normalizeColumnType(type, options) {
101
- const simpleType = this.extractSimpleType(type);
102
- if (['int', 'int4', 'integer'].includes(simpleType)) {
103
- return this.getIntegerTypeDeclarationSQL({});
104
- }
105
- if (['bigint', 'int8'].includes(simpleType)) {
106
- return this.getBigIntTypeDeclarationSQL({});
107
- }
108
- if (['smallint', 'int2'].includes(simpleType)) {
109
- return this.getSmallIntTypeDeclarationSQL({});
110
- }
111
- if (['boolean', 'bool'].includes(simpleType)) {
112
- return this.getBooleanTypeDeclarationSQL();
113
- }
114
- if (['varchar', 'character varying'].includes(simpleType)) {
115
- return this.getVarcharTypeDeclarationSQL(options);
116
- }
117
- if (['char', 'bpchar'].includes(simpleType)) {
118
- return this.getCharTypeDeclarationSQL(options);
119
- }
120
- if (['decimal', 'numeric'].includes(simpleType)) {
121
- return this.getDecimalTypeDeclarationSQL(options);
122
- }
123
- if (['interval'].includes(simpleType)) {
124
- return this.getIntervalTypeDeclarationSQL(options);
125
- }
126
- return super.normalizeColumnType(type, options);
127
- }
128
- getMappedType(type) {
129
- switch (this.extractSimpleType(type)) {
130
- case 'tsvector': return Type.getType(FullTextType);
131
- default: return super.getMappedType(type);
132
- }
133
- }
134
- getRegExpOperator(val, flags) {
135
- /* v8 ignore next 3 */
136
- if ((val instanceof RegExp && val.flags.includes('i')) || flags?.includes('i')) {
137
- return '~*';
138
- }
139
- return '~';
140
- }
141
- /* v8 ignore next 8 */
142
- getRegExpValue(val) {
143
- if (val.flags.includes('i')) {
144
- return { $re: val.source, $flags: val.flags };
145
- }
146
- return { $re: val.source };
147
- }
148
- isBigIntProperty(prop) {
149
- return super.isBigIntProperty(prop) || (['bigserial', 'int8'].includes(prop.columnTypes?.[0]));
150
- }
151
- getArrayDeclarationSQL() {
152
- return 'text[]';
153
- }
154
- getFloatDeclarationSQL() {
155
- return 'real';
156
- }
157
- getDoubleDeclarationSQL() {
158
- return 'double precision';
159
- }
160
- getEnumTypeDeclarationSQL(column) {
161
- /* v8 ignore next 3 */
162
- if (column.nativeEnumName) {
163
- return column.nativeEnumName;
164
- }
165
- if (column.items?.every(item => Utils.isString(item))) {
166
- return 'text';
167
- }
168
- return `smallint`;
169
- }
170
- supportsMultipleStatements() {
171
- return true;
172
- }
173
- getBeginTransactionSQL(options) {
174
- if (options?.isolationLevel || options?.readOnly) {
175
- let sql = 'start transaction';
176
- sql += options.isolationLevel ? ` isolation level ${options.isolationLevel}` : '';
177
- sql += options.readOnly ? ` read only` : '';
178
- return [sql];
179
- }
180
- return ['begin'];
181
- }
182
- marshallArray(values) {
183
- const quote = (v) => v === '' || v.match(/["{},\\]/) ? JSON.stringify(v) : v;
184
- return `{${values.map(v => quote('' + v)).join(',')}}`;
185
- }
186
16
  unmarshallArray(value) {
187
- if (value === '{}') {
188
- return [];
189
- }
190
- return value.substring(1, value.length - 1).split(',').map(v => {
191
- if (v === `""`) {
192
- return '';
193
- }
194
- if (v.match(/"(.*)"/)) {
195
- return v.substring(1, v.length - 1).replaceAll('\\"', '"');
196
- }
197
- return v;
198
- });
199
- }
200
- getVarcharTypeDeclarationSQL(column) {
201
- if (column.length === -1) {
202
- return 'varchar';
203
- }
204
- return super.getVarcharTypeDeclarationSQL(column);
205
- }
206
- getCharTypeDeclarationSQL(column) {
207
- if (column.length === -1) {
208
- return 'char';
209
- }
210
- return super.getCharTypeDeclarationSQL(column);
211
- }
212
- getIntervalTypeDeclarationSQL(column) {
213
- return 'interval' + (column.length != null ? `(${column.length})` : '');
214
- }
215
- getBlobDeclarationSQL() {
216
- return 'bytea';
217
- }
218
- getJsonDeclarationSQL() {
219
- return 'jsonb';
220
- }
221
- getSearchJsonPropertyKey(path, type, aliased, value) {
222
- const first = path.shift();
223
- const last = path.pop();
224
- const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${first}` : first);
225
- type = typeof type === 'string' ? this.getMappedType(type).runtimeType : String(type);
226
- const types = {
227
- number: 'float8',
228
- bigint: 'int8',
229
- boolean: 'bool',
230
- };
231
- const cast = (key) => raw(type in types ? `(${key})::${types[type]}` : key);
232
- let lastOperator = '->>';
233
- // force `->` for operator payloads with array values
234
- if (Utils.isPlainObject(value) && Object.keys(value).every(key => Utils.isArrayOperator(key) && Array.isArray(value[key]))) {
235
- lastOperator = '->';
236
- }
237
- if (path.length === 0) {
238
- return cast(`${root}${lastOperator}'${last}'`);
239
- }
240
- return cast(`${root}->${path.map(a => this.quoteValue(a)).join('->')}${lastOperator}'${last}'`);
241
- }
242
- getJsonIndexDefinition(index) {
243
- return index.columnNames
244
- .map(column => {
245
- if (!column.includes('.')) {
246
- return column;
247
- }
248
- const path = column.split('.');
249
- const first = path.shift();
250
- const last = path.pop();
251
- if (path.length === 0) {
252
- return `(${this.quoteIdentifier(first)}->>${this.quoteValue(last)})`;
253
- }
254
- return `(${this.quoteIdentifier(first)}->${path.map(c => this.quoteValue(c)).join('->')}->>${this.quoteValue(last)})`;
255
- });
256
- }
257
- quoteIdentifier(id, quote = '"') {
258
- if (RawQueryFragment.isKnownFragment(id)) {
259
- return super.quoteIdentifier(id);
260
- }
261
- return `${quote}${id.replace('.', `${quote}.${quote}`)}${quote}`;
17
+ return array.parse(value);
262
18
  }
263
19
  escape(value) {
20
+ if (typeof value === 'bigint') {
21
+ value = value.toString();
22
+ }
264
23
  if (typeof value === 'string') {
265
24
  return Client.prototype.escapeLiteral(value);
266
25
  }
@@ -270,104 +29,10 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
270
29
  if (ArrayBuffer.isView(value)) {
271
30
  return `E'\\\\x${value.toString('hex')}'`;
272
31
  }
273
- return super.escape(value);
274
- }
275
- pad(number, digits) {
276
- return String(number).padStart(digits, '0');
277
- }
278
- /** @internal */
279
- formatDate(date) {
280
- if (this.timezone === 'Z') {
281
- return date.toISOString();
282
- }
283
- let offset = -date.getTimezoneOffset();
284
- let year = date.getFullYear();
285
- const isBCYear = year < 1;
286
- /* v8 ignore next 3 */
287
- if (isBCYear) {
288
- year = Math.abs(year) + 1;
289
- }
290
- const datePart = `${this.pad(year, 4)}-${this.pad(date.getMonth() + 1, 2)}-${this.pad(date.getDate(), 2)}`;
291
- const timePart = `${this.pad(date.getHours(), 2)}:${this.pad(date.getMinutes(), 2)}:${this.pad(date.getSeconds(), 2)}.${this.pad(date.getMilliseconds(), 3)}`;
292
- let ret = `${datePart}T${timePart}`;
293
- /* v8 ignore next 4 */
294
- if (offset < 0) {
295
- ret += '-';
296
- offset *= -1;
297
- }
298
- else {
299
- ret += '+';
300
- }
301
- ret += this.pad(Math.floor(offset / 60), 2) + ':' + this.pad(offset % 60, 2);
302
- /* v8 ignore next 3 */
303
- if (isBCYear) {
304
- ret += ' BC';
305
- }
306
- return ret;
307
- }
308
- indexForeignKeys() {
309
- return false;
310
- }
311
- getDefaultMappedType(type) {
312
- const normalizedType = this.extractSimpleType(type);
313
- const map = {
314
- 'int2': 'smallint',
315
- 'smallserial': 'smallint',
316
- 'int': 'integer',
317
- 'int4': 'integer',
318
- 'serial': 'integer',
319
- 'serial4': 'integer',
320
- 'int8': 'bigint',
321
- 'bigserial': 'bigint',
322
- 'serial8': 'bigint',
323
- 'numeric': 'decimal',
324
- 'bool': 'boolean',
325
- 'real': 'float',
326
- 'float4': 'float',
327
- 'float8': 'double',
328
- 'timestamp': 'datetime',
329
- 'timestamptz': 'datetime',
330
- 'bytea': 'blob',
331
- 'jsonb': 'json',
332
- 'character varying': 'varchar',
333
- 'bpchar': 'character',
334
- };
335
- return super.getDefaultMappedType(map[normalizedType] ?? type);
336
- }
337
- supportsSchemas() {
338
- return true;
339
- }
340
- getDefaultSchemaName() {
341
- return 'public';
342
- }
343
- /**
344
- * Returns the default name of index for the given columns
345
- * cannot go past 63 character length for identifiers in MySQL
346
- */
347
- getIndexName(tableName, columns, type) {
348
- const indexName = super.getIndexName(tableName, columns, type);
349
- if (indexName.length > 63) {
350
- const suffix = type === 'primary' ? 'pkey' : type;
351
- return `${indexName.substring(0, 55 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
352
- }
353
- return indexName;
354
- }
355
- getDefaultPrimaryName(tableName, columns) {
356
- const indexName = `${tableName}_pkey`;
357
- if (indexName.length > 63) {
358
- return `${indexName.substring(0, 55 - 'pkey'.length)}_${Utils.hash(indexName, 5)}_pkey`;
359
- }
360
- return indexName;
361
- }
362
- /**
363
- * @inheritDoc
364
- */
365
- castColumn(prop) {
366
- switch (prop?.columnTypes?.[0]) {
367
- case this.getUuidTypeDeclarationSQL({}): return '::text';
368
- case this.getBooleanTypeDeclarationSQL(): return '::int';
369
- default: return '';
32
+ if (Array.isArray(value)) {
33
+ return value.map(v => this.escape(v)).join(', ');
370
34
  }
35
+ return value;
371
36
  }
372
37
  /**
373
38
  * @inheritDoc
@@ -377,19 +42,16 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
377
42
  if (typeof value === 'string' && value.charAt(10) === 'T') {
378
43
  return new Date(value);
379
44
  }
380
- /* v8 ignore next 3 */
45
+ /* v8 ignore next */
381
46
  if (typeof value === 'number') {
382
47
  return new Date(value);
383
48
  }
384
49
  // @ts-ignore fix wrong type resolution during build
385
50
  const parsed = parseDate(value);
386
- /* v8 ignore next 3 */
51
+ /* v8 ignore next */
387
52
  if (parsed === null) {
388
53
  return value;
389
54
  }
390
55
  return parsed;
391
56
  }
392
- getDefaultClientUrl() {
393
- return 'postgresql://postgres@127.0.0.1:5432';
394
- }
395
57
  }
package/README.md CHANGED
@@ -11,7 +11,6 @@ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-or
11
11
  [![Chat on discord](https://img.shields.io/discord/1214904142443839538?label=discord&color=blue)](https://discord.gg/w8bjxFHS7X)
12
12
  [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
13
13
  [![Coverage Status](https://img.shields.io/coveralls/mikro-orm/mikro-orm.svg)](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
14
- [![Maintainability](https://api.codeclimate.com/v1/badges/27999651d3adc47cfa40/maintainability)](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
15
14
  [![Build Status](https://github.com/mikro-orm/mikro-orm/workflows/tests/badge.svg?branch=master)](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
16
15
 
17
16
  ## 🤔 Unit of What?
@@ -141,7 +140,7 @@ There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit m
141
140
  - [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
142
141
  - [Filters](https://mikro-orm.io/docs/filters)
143
142
  - [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
144
- - [Preloading Deeply Nested Structures via populate](https://mikro-orm.io/docs/nested-populate)
143
+ - [Populating relations](https://mikro-orm.io/docs/populating-relations)
145
144
  - [Property Validation](https://mikro-orm.io/docs/property-validation)
146
145
  - [Lifecycle Hooks](https://mikro-orm.io/docs/events#hooks)
147
146
  - [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
@@ -382,6 +381,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
382
381
 
383
382
  Please ⭐️ this repository if this project helped you!
384
383
 
384
+ > If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
385
+
385
386
  ## 📝 License
386
387
 
387
388
  Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
package/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- export * from '@mikro-orm/knex';
1
+ export * from '@mikro-orm/sql';
2
2
  export * from './PostgreSqlConnection.js';
3
3
  export * from './PostgreSqlDriver.js';
4
4
  export * from './PostgreSqlPlatform.js';
5
- export * from './PostgreSqlSchemaHelper.js';
6
- export * from './PostgreSqlExceptionConverter.js';
7
- export * from './types/index.js';
8
- export { PostgreSqlMikroORM as MikroORM, PostgreSqlOptions as Options, definePostgreSqlConfig as defineConfig, } from './PostgreSqlMikroORM.js';
5
+ export { PostgreSqlEntityManager as EntityManager } from './PostgreSqlEntityManager.js';
6
+ export { PostgreSqlMikroORM as MikroORM, type PostgreSqlOptions as Options, definePostgreSqlConfig as defineConfig, } from './PostgreSqlMikroORM.js';
7
+ export { raw } from './raw.js';
package/index.js CHANGED
@@ -1,8 +1,7 @@
1
- export * from '@mikro-orm/knex';
1
+ export * from '@mikro-orm/sql';
2
2
  export * from './PostgreSqlConnection.js';
3
3
  export * from './PostgreSqlDriver.js';
4
4
  export * from './PostgreSqlPlatform.js';
5
- export * from './PostgreSqlSchemaHelper.js';
6
- export * from './PostgreSqlExceptionConverter.js';
7
- export * from './types/index.js';
5
+ export { PostgreSqlEntityManager as EntityManager } from './PostgreSqlEntityManager.js';
8
6
  export { PostgreSqlMikroORM as MikroORM, definePostgreSqlConfig as defineConfig, } from './PostgreSqlMikroORM.js';
7
+ export { raw } from './raw.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/postgresql",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.2",
4
+ "version": "7.0.0-dev.200",
5
5
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -38,10 +38,10 @@
38
38
  },
39
39
  "homepage": "https://mikro-orm.io",
40
40
  "engines": {
41
- "node": ">= 22.11.0"
41
+ "node": ">= 22.17.0"
42
42
  },
43
43
  "scripts": {
44
- "build": "yarn clean && yarn compile && yarn copy",
44
+ "build": "yarn compile && yarn copy",
45
45
  "clean": "yarn run -T rimraf ./dist",
46
46
  "compile": "yarn run -T tsc -p tsconfig.build.json",
47
47
  "copy": "node ../../scripts/copy.mjs"
@@ -50,18 +50,18 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "@mikro-orm/knex": "7.0.0-dev.2",
54
- "pg": "8.13.1",
55
- "postgres-array": "3.0.2",
53
+ "@mikro-orm/sql": "7.0.0-dev.200",
54
+ "kysely": "0.28.10",
55
+ "pg": "8.18.0",
56
+ "pg-cursor": "2.17.0",
57
+ "postgres-array": "3.0.4",
56
58
  "postgres-date": "2.1.0",
57
59
  "postgres-interval": "4.0.2"
58
60
  },
59
61
  "devDependencies": {
60
- "@mikro-orm/core": "^6.4.5",
61
- "kysely": "https://pkg.pr.new/kysely-org/kysely/kysely@2b7007e"
62
+ "@mikro-orm/core": "^6.6.4"
62
63
  },
63
64
  "peerDependencies": {
64
- "@mikro-orm/core": "7.0.0-dev.2",
65
- "kysely": "*"
65
+ "@mikro-orm/core": "7.0.0-dev.200"
66
66
  }
67
67
  }
package/raw.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ import { type AnyString, type Dictionary, type EntityKey, type RawQueryFragment, type QueryBuilder } from '@mikro-orm/sql';
2
+ import type { SelectQueryBuilder } from 'kysely';
3
+ /**
4
+ * Creates raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented
5
+ * by `RawQueryFragment` class instance that can be serialized to a string, so it can be used both as an object value
6
+ * and key. When serialized, the fragment key gets cached and only such cached key will be recognized by the ORM.
7
+ * This adds a runtime safety to the raw query fragments.
8
+ *
9
+ * > **`raw()` helper is required since v6 to use a raw fragment in your query, both through EntityManager and QueryBuilder.**
10
+ *
11
+ * ```ts
12
+ * // as a value
13
+ * await em.find(User, { time: raw('now()') });
14
+ *
15
+ * // as a key
16
+ * await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });
17
+ *
18
+ * // value can be empty array
19
+ * await em.find(User, { [raw('(select 1 = 1)')]: [] });
20
+ * ```
21
+ *
22
+ * The `raw` helper supports several signatures, you can pass in a callback that receives the current property alias:
23
+ *
24
+ * ```ts
25
+ * await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
26
+ * ```
27
+ *
28
+ * You can also use the `sql` tagged template function, which works the same, but supports only the simple string signature:
29
+ *
30
+ * ```ts
31
+ * await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
32
+ * ```
33
+ *
34
+ * When using inside filters, you might have to use a callback signature to create new raw instance for every filter usage.
35
+ *
36
+ * ```ts
37
+ * @Filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
38
+ * ```
39
+ *
40
+ * The `raw` helper can be used within indexes and uniques to write database-agnostic SQL expressions. In that case, you can use `'??'` to tag your database identifiers (table name, column names, index name, ...) inside your expression, and pass those identifiers as a second parameter to the `raw` helper. Internally, those will automatically be quoted according to the database in use:
41
+ *
42
+ * ```ts
43
+ * // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("country")
44
+ * // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`country`)
45
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => raw(`create index ?? on ?? (??)`, ['custom_idx_on_name', table, columns.name]) })
46
+ * @Entity({ schema: 'library' })
47
+ * export class Author { ... }
48
+ * ```
49
+ *
50
+ * You can also use the `quote` tag function to write database-agnostic SQL expressions. The end-result is the same as using the `raw` function regarding database identifiers quoting, only to have a more elegant expression syntax:
51
+ *
52
+ * ```ts
53
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
54
+ * @Entity({ schema: 'library' })
55
+ * export class Author { ... }
56
+ * ```
57
+ */
58
+ export declare function raw<T extends object = any, R = any>(sql: SelectQueryBuilder<any, any, any> | QueryBuilder<T> | EntityKey<T> | EntityKey<T>[] | AnyString | ((alias: string) => string) | RawQueryFragment, params?: readonly unknown[] | Dictionary<unknown>): NoInfer<R>;
package/raw.js ADDED
@@ -0,0 +1,64 @@
1
+ import { raw as raw_, Utils } from '@mikro-orm/sql';
2
+ /**
3
+ * Creates raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented
4
+ * by `RawQueryFragment` class instance that can be serialized to a string, so it can be used both as an object value
5
+ * and key. When serialized, the fragment key gets cached and only such cached key will be recognized by the ORM.
6
+ * This adds a runtime safety to the raw query fragments.
7
+ *
8
+ * > **`raw()` helper is required since v6 to use a raw fragment in your query, both through EntityManager and QueryBuilder.**
9
+ *
10
+ * ```ts
11
+ * // as a value
12
+ * await em.find(User, { time: raw('now()') });
13
+ *
14
+ * // as a key
15
+ * await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });
16
+ *
17
+ * // value can be empty array
18
+ * await em.find(User, { [raw('(select 1 = 1)')]: [] });
19
+ * ```
20
+ *
21
+ * The `raw` helper supports several signatures, you can pass in a callback that receives the current property alias:
22
+ *
23
+ * ```ts
24
+ * await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
25
+ * ```
26
+ *
27
+ * You can also use the `sql` tagged template function, which works the same, but supports only the simple string signature:
28
+ *
29
+ * ```ts
30
+ * await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
31
+ * ```
32
+ *
33
+ * When using inside filters, you might have to use a callback signature to create new raw instance for every filter usage.
34
+ *
35
+ * ```ts
36
+ * @Filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
37
+ * ```
38
+ *
39
+ * The `raw` helper can be used within indexes and uniques to write database-agnostic SQL expressions. In that case, you can use `'??'` to tag your database identifiers (table name, column names, index name, ...) inside your expression, and pass those identifiers as a second parameter to the `raw` helper. Internally, those will automatically be quoted according to the database in use:
40
+ *
41
+ * ```ts
42
+ * // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("country")
43
+ * // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`country`)
44
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => raw(`create index ?? on ?? (??)`, ['custom_idx_on_name', table, columns.name]) })
45
+ * @Entity({ schema: 'library' })
46
+ * export class Author { ... }
47
+ * ```
48
+ *
49
+ * You can also use the `quote` tag function to write database-agnostic SQL expressions. The end-result is the same as using the `raw` function regarding database identifiers quoting, only to have a more elegant expression syntax:
50
+ *
51
+ * ```ts
52
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
53
+ * @Entity({ schema: 'library' })
54
+ * export class Author { ... }
55
+ * ```
56
+ */
57
+ export function raw(sql, params) {
58
+ if (Utils.isObject(sql) && 'compile' in sql) {
59
+ const query = sql.compile();
60
+ const processed = query.sql.replaceAll(/\$\d+/g, '?');
61
+ return raw_(processed, query.parameters);
62
+ }
63
+ return raw_(sql, params);
64
+ }