@mikro-orm/knex 6.5.10-dev.14 → 6.5.10-dev.16
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/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +2 -2
- package/query/index.d.ts +1 -0
- package/query/index.js +3 -0
- package/query/raw.d.ts +59 -0
- package/query/raw.js +71 -0
package/index.d.ts
CHANGED
|
@@ -16,4 +16,5 @@ export { SqlEntityManager as EntityManager } from './SqlEntityManager';
|
|
|
16
16
|
export { SqlEntityRepository as EntityRepository } from './SqlEntityRepository';
|
|
17
17
|
/** @ignore */
|
|
18
18
|
export { Knex, knex } from 'knex';
|
|
19
|
+
/** @ts-ignore */
|
|
19
20
|
export * from '@mikro-orm/core';
|
package/index.js
CHANGED
|
@@ -38,4 +38,5 @@ Object.defineProperty(exports, "EntityRepository", { enumerable: true, get: func
|
|
|
38
38
|
var knex_1 = require("knex");
|
|
39
39
|
Object.defineProperty(exports, "Knex", { enumerable: true, get: function () { return knex_1.Knex; } });
|
|
40
40
|
Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return knex_1.knex; } });
|
|
41
|
+
/** @ts-ignore */
|
|
41
42
|
__exportStar(require("@mikro-orm/core"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "6.5.10-dev.
|
|
3
|
+
"version": "6.5.10-dev.16",
|
|
4
4
|
"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.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"@mikro-orm/core": "^6.5.9"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "6.5.10-dev.
|
|
69
|
+
"@mikro-orm/core": "6.5.10-dev.16",
|
|
70
70
|
"better-sqlite3": "*",
|
|
71
71
|
"libsql": "*",
|
|
72
72
|
"mariadb": "*"
|
package/query/index.d.ts
CHANGED
package/query/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.raw = void 0;
|
|
17
18
|
__exportStar(require("./enums"), exports);
|
|
18
19
|
__exportStar(require("./QueryBuilderHelper"), exports);
|
|
19
20
|
__exportStar(require("./QueryBuilder"), exports);
|
|
@@ -22,3 +23,5 @@ __exportStar(require("./ArrayCriteriaNode"), exports);
|
|
|
22
23
|
__exportStar(require("./ObjectCriteriaNode"), exports);
|
|
23
24
|
__exportStar(require("./ScalarCriteriaNode"), exports);
|
|
24
25
|
__exportStar(require("./CriteriaNodeFactory"), exports);
|
|
26
|
+
var raw_1 = require("./raw");
|
|
27
|
+
Object.defineProperty(exports, "raw", { enumerable: true, get: function () { return raw_1.rawKnex; } });
|
package/query/raw.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type AnyString, type Dictionary, type EntityKey, type RawQueryFragment } from '@mikro-orm/core';
|
|
2
|
+
import type { Knex } from 'knex';
|
|
3
|
+
import { QueryBuilder } from './QueryBuilder';
|
|
4
|
+
/**
|
|
5
|
+
* Creates raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented
|
|
6
|
+
* by `RawQueryFragment` class instance that can be serialized to a string, so it can be used both as an object value
|
|
7
|
+
* and key. When serialized, the fragment key gets cached and only such cached key will be recognized by the ORM.
|
|
8
|
+
* This adds a runtime safety to the raw query fragments.
|
|
9
|
+
*
|
|
10
|
+
* > **`raw()` helper is required since v6 to use a raw fragment in your query, both through EntityManager and QueryBuilder.**
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* // as a value
|
|
14
|
+
* await em.find(User, { time: raw('now()') });
|
|
15
|
+
*
|
|
16
|
+
* // as a key
|
|
17
|
+
* await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });
|
|
18
|
+
*
|
|
19
|
+
* // value can be empty array
|
|
20
|
+
* await em.find(User, { [raw('(select 1 = 1)')]: [] });
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* The `raw` helper supports several signatures, you can pass in a callback that receives the current property alias:
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* You can also use the `sql` tagged template function, which works the same, but supports only the simple string signature:
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* When using inside filters, you might have to use a callback signature to create new raw instance for every filter usage.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* @Filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* 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:
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("country")
|
|
45
|
+
* // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`country`)
|
|
46
|
+
* @Index({ name: 'custom_idx_on_name', expression: (table, columns) => raw(`create index ?? on ?? (??)`, ['custom_idx_on_name', table, columns.name]) })
|
|
47
|
+
* @Entity({ schema: 'library' })
|
|
48
|
+
* export class Author { ... }
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* 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:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
|
|
55
|
+
* @Entity({ schema: 'library' })
|
|
56
|
+
* export class Author { ... }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function rawKnex<T extends object = any, R = any>(sql: Knex.QueryBuilder | Knex.Raw | QueryBuilder<T> | EntityKey<T> | EntityKey<T>[] | AnyString | ((alias: string) => string) | RawQueryFragment, params?: readonly unknown[] | Dictionary<unknown>): NoInfer<R>;
|
package/query/raw.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rawKnex = rawKnex;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
const QueryBuilder_1 = require("./QueryBuilder");
|
|
6
|
+
/**
|
|
7
|
+
* Creates raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented
|
|
8
|
+
* by `RawQueryFragment` class instance that can be serialized to a string, so it can be used both as an object value
|
|
9
|
+
* and key. When serialized, the fragment key gets cached and only such cached key will be recognized by the ORM.
|
|
10
|
+
* This adds a runtime safety to the raw query fragments.
|
|
11
|
+
*
|
|
12
|
+
* > **`raw()` helper is required since v6 to use a raw fragment in your query, both through EntityManager and QueryBuilder.**
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* // as a value
|
|
16
|
+
* await em.find(User, { time: raw('now()') });
|
|
17
|
+
*
|
|
18
|
+
* // as a key
|
|
19
|
+
* await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });
|
|
20
|
+
*
|
|
21
|
+
* // value can be empty array
|
|
22
|
+
* await em.find(User, { [raw('(select 1 = 1)')]: [] });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* The `raw` helper supports several signatures, you can pass in a callback that receives the current property alias:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* You can also use the `sql` tagged template function, which works the same, but supports only the simple string signature:
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* When using inside filters, you might have to use a callback signature to create new raw instance for every filter usage.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* @Filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* 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:
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("country")
|
|
47
|
+
* // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`country`)
|
|
48
|
+
* @Index({ name: 'custom_idx_on_name', expression: (table, columns) => raw(`create index ?? on ?? (??)`, ['custom_idx_on_name', table, columns.name]) })
|
|
49
|
+
* @Entity({ schema: 'library' })
|
|
50
|
+
* export class Author { ... }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* 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:
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
|
|
57
|
+
* @Entity({ schema: 'library' })
|
|
58
|
+
* export class Author { ... }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
function rawKnex(sql, params) {
|
|
62
|
+
if (core_1.Utils.isObject(sql) && 'toSQL' in sql) {
|
|
63
|
+
const query = sql.toSQL();
|
|
64
|
+
return (0, core_1.raw)(query.sql, query.bindings);
|
|
65
|
+
}
|
|
66
|
+
if (sql instanceof QueryBuilder_1.QueryBuilder) {
|
|
67
|
+
const query = sql.toQuery()._sql;
|
|
68
|
+
return (0, core_1.raw)(query.sql, query.bindings);
|
|
69
|
+
}
|
|
70
|
+
return (0, core_1.raw)(sql, params);
|
|
71
|
+
}
|