@mikro-orm/knex 6.0.0-dev.10 → 6.0.0-dev.101
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 +8 -4
- package/AbstractSqlConnection.js +30 -18
- package/AbstractSqlDriver.d.ts +22 -22
- package/AbstractSqlDriver.js +233 -88
- package/AbstractSqlPlatform.d.ts +5 -4
- package/AbstractSqlPlatform.js +34 -5
- package/MonkeyPatchable.d.ts +1 -0
- package/MonkeyPatchable.js +3 -0
- package/README.md +64 -107
- package/SqlEntityManager.d.ts +2 -6
- package/SqlEntityManager.js +5 -9
- package/SqlEntityRepository.d.ts +7 -6
- package/SqlEntityRepository.js +11 -8
- package/index.mjs +22 -6
- package/package.json +8 -8
- package/query/ArrayCriteriaNode.d.ts +3 -3
- package/query/CriteriaNode.d.ts +8 -8
- package/query/CriteriaNode.js +16 -9
- package/query/CriteriaNodeFactory.d.ts +6 -6
- package/query/CriteriaNodeFactory.js +10 -13
- package/query/ObjectCriteriaNode.d.ts +3 -3
- package/query/ObjectCriteriaNode.js +12 -8
- package/query/QueryBuilder.d.ts +41 -19
- package/query/QueryBuilder.js +319 -110
- package/query/QueryBuilderHelper.d.ts +9 -13
- package/query/QueryBuilderHelper.js +123 -97
- package/query/ScalarCriteriaNode.d.ts +3 -2
- package/query/ScalarCriteriaNode.js +9 -6
- package/query/enums.js +1 -1
- package/schema/DatabaseSchema.d.ts +5 -2
- package/schema/DatabaseSchema.js +26 -8
- package/schema/DatabaseTable.d.ts +4 -2
- package/schema/DatabaseTable.js +53 -28
- package/schema/SchemaComparator.js +12 -2
- package/schema/SchemaHelper.d.ts +6 -4
- package/schema/SchemaHelper.js +11 -4
- package/schema/SqlSchemaGenerator.d.ts +3 -6
- package/schema/SqlSchemaGenerator.js +61 -28
- package/typings.d.ts +12 -7
package/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import mod from "./index.js";
|
|
2
2
|
|
|
3
3
|
export default mod;
|
|
4
|
+
export const ALIAS_REPLACEMENT = mod.ALIAS_REPLACEMENT;
|
|
5
|
+
export const ALIAS_REPLACEMENT_RE = mod.ALIAS_REPLACEMENT_RE;
|
|
4
6
|
export const ARRAY_OPERATORS = mod.ARRAY_OPERATORS;
|
|
5
7
|
export const AbstractNamingStrategy = mod.AbstractNamingStrategy;
|
|
6
8
|
export const AbstractSchemaGenerator = mod.AbstractSchemaGenerator;
|
|
@@ -10,6 +12,7 @@ export const AbstractSqlPlatform = mod.AbstractSqlPlatform;
|
|
|
10
12
|
export const AfterCreate = mod.AfterCreate;
|
|
11
13
|
export const AfterDelete = mod.AfterDelete;
|
|
12
14
|
export const AfterUpdate = mod.AfterUpdate;
|
|
15
|
+
export const AfterUpsert = mod.AfterUpsert;
|
|
13
16
|
export const ArrayCollection = mod.ArrayCollection;
|
|
14
17
|
export const ArrayCriteriaNode = mod.ArrayCriteriaNode;
|
|
15
18
|
export const ArrayType = mod.ArrayType;
|
|
@@ -17,6 +20,7 @@ export const BaseEntity = mod.BaseEntity;
|
|
|
17
20
|
export const BeforeCreate = mod.BeforeCreate;
|
|
18
21
|
export const BeforeDelete = mod.BeforeDelete;
|
|
19
22
|
export const BeforeUpdate = mod.BeforeUpdate;
|
|
23
|
+
export const BeforeUpsert = mod.BeforeUpsert;
|
|
20
24
|
export const BigIntType = mod.BigIntType;
|
|
21
25
|
export const BlobType = mod.BlobType;
|
|
22
26
|
export const BooleanType = mod.BooleanType;
|
|
@@ -34,8 +38,10 @@ export const ConfigurationLoader = mod.ConfigurationLoader;
|
|
|
34
38
|
export const Connection = mod.Connection;
|
|
35
39
|
export const ConnectionException = mod.ConnectionException;
|
|
36
40
|
export const ConstraintViolationException = mod.ConstraintViolationException;
|
|
41
|
+
export const CreateRequestContext = mod.CreateRequestContext;
|
|
37
42
|
export const CriteriaNode = mod.CriteriaNode;
|
|
38
43
|
export const CriteriaNodeFactory = mod.CriteriaNodeFactory;
|
|
44
|
+
export const Cursor = mod.Cursor;
|
|
39
45
|
export const DatabaseDriver = mod.DatabaseDriver;
|
|
40
46
|
export const DatabaseObjectExistsException = mod.DatabaseObjectExistsException;
|
|
41
47
|
export const DatabaseObjectNotFoundException = mod.DatabaseObjectNotFoundException;
|
|
@@ -48,8 +54,10 @@ export const DecimalType = mod.DecimalType;
|
|
|
48
54
|
export const DefaultLogger = mod.DefaultLogger;
|
|
49
55
|
export const DoubleType = mod.DoubleType;
|
|
50
56
|
export const DriverException = mod.DriverException;
|
|
57
|
+
export const EagerProps = mod.EagerProps;
|
|
51
58
|
export const Embeddable = mod.Embeddable;
|
|
52
59
|
export const Embedded = mod.Embedded;
|
|
60
|
+
export const EnsureRequestContext = mod.EnsureRequestContext;
|
|
53
61
|
export const Entity = mod.Entity;
|
|
54
62
|
export const EntityAssigner = mod.EntityAssigner;
|
|
55
63
|
export const EntityCaseNamingStrategy = mod.EntityCaseNamingStrategy;
|
|
@@ -72,6 +80,7 @@ export const EnumArrayType = mod.EnumArrayType;
|
|
|
72
80
|
export const EnumType = mod.EnumType;
|
|
73
81
|
export const EventManager = mod.EventManager;
|
|
74
82
|
export const EventType = mod.EventType;
|
|
83
|
+
export const EventTypeMap = mod.EventTypeMap;
|
|
75
84
|
export const ExceptionConverter = mod.ExceptionConverter;
|
|
76
85
|
export const FileCacheAdapter = mod.FileCacheAdapter;
|
|
77
86
|
export const Filter = mod.Filter;
|
|
@@ -79,14 +88,15 @@ export const FloatType = mod.FloatType;
|
|
|
79
88
|
export const FlushMode = mod.FlushMode;
|
|
80
89
|
export const ForeignKeyConstraintViolationException = mod.ForeignKeyConstraintViolationException;
|
|
81
90
|
export const Formula = mod.Formula;
|
|
91
|
+
export const GeneratedCacheAdapter = mod.GeneratedCacheAdapter;
|
|
82
92
|
export const GroupOperator = mod.GroupOperator;
|
|
93
|
+
export const HiddenProps = mod.HiddenProps;
|
|
83
94
|
export const Hydrator = mod.Hydrator;
|
|
84
95
|
export const IdentityMap = mod.IdentityMap;
|
|
85
96
|
export const Index = mod.Index;
|
|
86
97
|
export const IntegerType = mod.IntegerType;
|
|
87
98
|
export const InvalidFieldNameException = mod.InvalidFieldNameException;
|
|
88
99
|
export const IsolationLevel = mod.IsolationLevel;
|
|
89
|
-
export const JavaScriptMetadataProvider = mod.JavaScriptMetadataProvider;
|
|
90
100
|
export const JsonProperty = mod.JsonProperty;
|
|
91
101
|
export const JsonType = mod.JsonType;
|
|
92
102
|
export const Knex = mod.Knex;
|
|
@@ -125,7 +135,6 @@ export const Platform = mod.Platform;
|
|
|
125
135
|
export const PopulateHint = mod.PopulateHint;
|
|
126
136
|
export const PrimaryKey = mod.PrimaryKey;
|
|
127
137
|
export const PrimaryKeyProp = mod.PrimaryKeyProp;
|
|
128
|
-
export const PrimaryKeyType = mod.PrimaryKeyType;
|
|
129
138
|
export const Property = mod.Property;
|
|
130
139
|
export const QueryBuilder = mod.QueryBuilder;
|
|
131
140
|
export const QueryBuilderHelper = mod.QueryBuilderHelper;
|
|
@@ -135,13 +144,16 @@ export const QueryOperator = mod.QueryOperator;
|
|
|
135
144
|
export const QueryOrder = mod.QueryOrder;
|
|
136
145
|
export const QueryOrderNumeric = mod.QueryOrderNumeric;
|
|
137
146
|
export const QueryType = mod.QueryType;
|
|
147
|
+
export const RawQueryFragment = mod.RawQueryFragment;
|
|
138
148
|
export const ReadOnlyException = mod.ReadOnlyException;
|
|
149
|
+
export const Ref = mod.Ref;
|
|
139
150
|
export const Reference = mod.Reference;
|
|
140
|
-
export const
|
|
151
|
+
export const ReferenceKind = mod.ReferenceKind;
|
|
141
152
|
export const ReflectMetadataProvider = mod.ReflectMetadataProvider;
|
|
142
153
|
export const RequestContext = mod.RequestContext;
|
|
143
154
|
export const SCALAR_TYPES = mod.SCALAR_TYPES;
|
|
144
155
|
export const ScalarCriteriaNode = mod.ScalarCriteriaNode;
|
|
156
|
+
export const ScalarReference = mod.ScalarReference;
|
|
145
157
|
export const SchemaComparator = mod.SchemaComparator;
|
|
146
158
|
export const SchemaGenerator = mod.SchemaGenerator;
|
|
147
159
|
export const SchemaHelper = mod.SchemaHelper;
|
|
@@ -154,7 +166,6 @@ export const SqlEntityManager = mod.SqlEntityManager;
|
|
|
154
166
|
export const SqlEntityRepository = mod.SqlEntityRepository;
|
|
155
167
|
export const SqlSchemaGenerator = mod.SqlSchemaGenerator;
|
|
156
168
|
export const StringType = mod.StringType;
|
|
157
|
-
export const Subscriber = mod.Subscriber;
|
|
158
169
|
export const SyntaxErrorException = mod.SyntaxErrorException;
|
|
159
170
|
export const TableExistsException = mod.TableExistsException;
|
|
160
171
|
export const TableNotFoundException = mod.TableNotFoundException;
|
|
@@ -164,12 +175,12 @@ export const TinyIntType = mod.TinyIntType;
|
|
|
164
175
|
export const TransactionContext = mod.TransactionContext;
|
|
165
176
|
export const TransactionEventBroadcaster = mod.TransactionEventBroadcaster;
|
|
166
177
|
export const Type = mod.Type;
|
|
178
|
+
export const Uint8ArrayType = mod.Uint8ArrayType;
|
|
167
179
|
export const UnderscoreNamingStrategy = mod.UnderscoreNamingStrategy;
|
|
168
180
|
export const Unique = mod.Unique;
|
|
169
181
|
export const UniqueConstraintViolationException = mod.UniqueConstraintViolationException;
|
|
170
182
|
export const UnitOfWork = mod.UnitOfWork;
|
|
171
183
|
export const UnknownType = mod.UnknownType;
|
|
172
|
-
export const UseRequestContext = mod.UseRequestContext;
|
|
173
184
|
export const Utils = mod.Utils;
|
|
174
185
|
export const UuidType = mod.UuidType;
|
|
175
186
|
export const ValidationError = mod.ValidationError;
|
|
@@ -182,11 +193,16 @@ export const compareBuffers = mod.compareBuffers;
|
|
|
182
193
|
export const compareObjects = mod.compareObjects;
|
|
183
194
|
export const defineConfig = mod.defineConfig;
|
|
184
195
|
export const equals = mod.equals;
|
|
185
|
-
export const
|
|
196
|
+
export const getOnConflictFields = mod.getOnConflictFields;
|
|
197
|
+
export const getOnConflictReturningFields = mod.getOnConflictReturningFields;
|
|
186
198
|
export const helper = mod.helper;
|
|
187
199
|
export const knex = mod.knex;
|
|
200
|
+
export const parseJsonSafe = mod.parseJsonSafe;
|
|
201
|
+
export const raw = mod.raw;
|
|
188
202
|
export const ref = mod.ref;
|
|
203
|
+
export const rel = mod.rel;
|
|
189
204
|
export const serialize = mod.serialize;
|
|
205
|
+
export const sql = mod.sql;
|
|
190
206
|
export const t = mod.t;
|
|
191
207
|
export const types = mod.types;
|
|
192
208
|
export const wrap = mod.wrap;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "6.0.0-dev.
|
|
3
|
+
"version": "6.0.0-dev.101",
|
|
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",
|
|
@@ -46,26 +46,26 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://mikro-orm.io",
|
|
48
48
|
"engines": {
|
|
49
|
-
"node": ">=
|
|
49
|
+
"node": ">= 18.12.0"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "yarn clean && yarn compile && yarn copy && yarn run -T gen-esm-wrapper index.js index.mjs",
|
|
53
|
-
"clean": "rimraf ./dist",
|
|
54
|
-
"compile": "tsc -p tsconfig.build.json",
|
|
53
|
+
"clean": "yarn run -T rimraf ./dist",
|
|
54
|
+
"compile": "yarn run -T tsc -p tsconfig.build.json",
|
|
55
55
|
"copy": "node ../../scripts/copy.mjs"
|
|
56
56
|
},
|
|
57
57
|
"publishConfig": {
|
|
58
58
|
"access": "public"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"fs-extra": "11.1.
|
|
62
|
-
"knex": "2.
|
|
61
|
+
"fs-extra": "11.1.1",
|
|
62
|
+
"knex": "2.5.1",
|
|
63
63
|
"sqlstring": "2.3.3"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@mikro-orm/core": "^5.
|
|
66
|
+
"@mikro-orm/core": "^5.8.1"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "~6.0.0-dev.
|
|
69
|
+
"@mikro-orm/core": "~6.0.0-dev.101"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -3,7 +3,7 @@ import type { IQueryBuilder } from '../typings';
|
|
|
3
3
|
/**
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
|
-
export declare class ArrayCriteriaNode extends CriteriaNode {
|
|
7
|
-
process
|
|
8
|
-
willAutoJoin
|
|
6
|
+
export declare class ArrayCriteriaNode<T extends object> extends CriteriaNode<T> {
|
|
7
|
+
process(qb: IQueryBuilder<T>, alias?: string): any;
|
|
8
|
+
willAutoJoin(qb: IQueryBuilder<T>, alias?: string): any;
|
|
9
9
|
}
|
package/query/CriteriaNode.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { inspect } from 'util';
|
|
3
|
-
import type
|
|
3
|
+
import { type EntityProperty, type MetadataStorage, type EntityKey } from '@mikro-orm/core';
|
|
4
4
|
import type { ICriteriaNode, IQueryBuilder } from '../typings';
|
|
5
5
|
/**
|
|
6
6
|
* Helper for working with deeply nested where/orderBy/having criteria. Uses composite pattern to build tree from the payload.
|
|
7
7
|
* Auto-joins relations and converts payload from { books: { publisher: { name: '...' } } } to { 'publisher_alias.name': '...' }
|
|
8
8
|
* @internal
|
|
9
9
|
*/
|
|
10
|
-
export declare class CriteriaNode implements ICriteriaNode {
|
|
10
|
+
export declare class CriteriaNode<T extends object> implements ICriteriaNode<T> {
|
|
11
11
|
protected readonly metadata: MetadataStorage;
|
|
12
12
|
readonly entityName: string;
|
|
13
|
-
readonly parent?: ICriteriaNode | undefined;
|
|
14
|
-
readonly key?:
|
|
13
|
+
readonly parent?: ICriteriaNode<T> | undefined;
|
|
14
|
+
readonly key?: EntityKey<T> | undefined;
|
|
15
15
|
payload: any;
|
|
16
|
-
prop?: EntityProperty
|
|
16
|
+
prop?: EntityProperty<T>;
|
|
17
17
|
index?: number;
|
|
18
|
-
constructor(metadata: MetadataStorage, entityName: string, parent?: ICriteriaNode | undefined, key?:
|
|
19
|
-
process
|
|
18
|
+
constructor(metadata: MetadataStorage, entityName: string, parent?: ICriteriaNode<T> | undefined, key?: EntityKey<T> | undefined, validate?: boolean);
|
|
19
|
+
process(qb: IQueryBuilder<T>, alias?: string): any;
|
|
20
20
|
shouldInline(payload: any): boolean;
|
|
21
|
-
willAutoJoin
|
|
21
|
+
willAutoJoin(qb: IQueryBuilder<T>, alias?: string): boolean;
|
|
22
22
|
shouldRename(payload: any): boolean;
|
|
23
23
|
renameFieldToPK<T>(qb: IQueryBuilder<T>): string;
|
|
24
24
|
getPath(addIndex?: boolean): string;
|
package/query/CriteriaNode.js
CHANGED
|
@@ -9,6 +9,13 @@ const core_1 = require("@mikro-orm/core");
|
|
|
9
9
|
* @internal
|
|
10
10
|
*/
|
|
11
11
|
class CriteriaNode {
|
|
12
|
+
metadata;
|
|
13
|
+
entityName;
|
|
14
|
+
parent;
|
|
15
|
+
key;
|
|
16
|
+
payload;
|
|
17
|
+
prop;
|
|
18
|
+
index;
|
|
12
19
|
constructor(metadata, entityName, parent, key, validate = true) {
|
|
13
20
|
this.metadata = metadata;
|
|
14
21
|
this.entityName = entityName;
|
|
@@ -40,7 +47,7 @@ class CriteriaNode {
|
|
|
40
47
|
return false;
|
|
41
48
|
}
|
|
42
49
|
shouldRename(payload) {
|
|
43
|
-
const type = this.prop ? this.prop.
|
|
50
|
+
const type = this.prop ? this.prop.kind : null;
|
|
44
51
|
const composite = this.prop?.joinColumns ? this.prop.joinColumns.length > 1 : false;
|
|
45
52
|
const customExpression = CriteriaNode.isCustomExpression(this.key);
|
|
46
53
|
const scalar = payload === null || core_1.Utils.isPrimaryKey(payload) || payload instanceof RegExp || payload instanceof Date || customExpression;
|
|
@@ -49,17 +56,17 @@ class CriteriaNode {
|
|
|
49
56
|
return true;
|
|
50
57
|
}
|
|
51
58
|
switch (type) {
|
|
52
|
-
case core_1.
|
|
53
|
-
case core_1.
|
|
54
|
-
case core_1.
|
|
55
|
-
case core_1.
|
|
59
|
+
case core_1.ReferenceKind.MANY_TO_ONE: return false;
|
|
60
|
+
case core_1.ReferenceKind.ONE_TO_ONE: return !this.prop.owner;
|
|
61
|
+
case core_1.ReferenceKind.ONE_TO_MANY: return scalar || operator;
|
|
62
|
+
case core_1.ReferenceKind.MANY_TO_MANY: return scalar || operator;
|
|
56
63
|
default: return false;
|
|
57
64
|
}
|
|
58
65
|
}
|
|
59
66
|
renameFieldToPK(qb) {
|
|
60
67
|
const joinAlias = qb.getAliasForJoinPath(this.getPath());
|
|
61
68
|
const alias = joinAlias ?? qb.alias;
|
|
62
|
-
if (this.prop.
|
|
69
|
+
if (this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY) {
|
|
63
70
|
return core_1.Utils.getPrimaryKeyHash(this.prop.inverseJoinColumns.map(col => `${alias}.${col}`));
|
|
64
71
|
}
|
|
65
72
|
// if we found a matching join, we need to use the target table column names, as we use that alias instead of the root
|
|
@@ -70,7 +77,7 @@ class CriteriaNode {
|
|
|
70
77
|
}
|
|
71
78
|
getPath(addIndex = false) {
|
|
72
79
|
// use index on parent only if we are processing to-many relation
|
|
73
|
-
const addParentIndex = this.prop && [core_1.
|
|
80
|
+
const addParentIndex = this.prop && [core_1.ReferenceKind.ONE_TO_MANY, core_1.ReferenceKind.MANY_TO_MANY].includes(this.prop.kind);
|
|
74
81
|
const parentPath = this.parent?.getPath(addParentIndex) ?? this.entityName;
|
|
75
82
|
const index = addIndex && this.index != null ? `[${this.index}]` : '';
|
|
76
83
|
// ignore group operators to allow easier mapping (e.g. for orderBy)
|
|
@@ -89,7 +96,7 @@ class CriteriaNode {
|
|
|
89
96
|
const customExpression = CriteriaNode.isCustomExpression(this.key);
|
|
90
97
|
const scalar = this.payload === null || core_1.Utils.isPrimaryKey(this.payload) || this.payload instanceof RegExp || this.payload instanceof Date || customExpression;
|
|
91
98
|
const operator = core_1.Utils.isObject(this.payload) && Object.keys(this.payload).every(k => core_1.Utils.isOperator(k, false));
|
|
92
|
-
return this.prop.
|
|
99
|
+
return this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY && (scalar || operator);
|
|
93
100
|
}
|
|
94
101
|
getPivotPath(path) {
|
|
95
102
|
return `${path}[pivot]`;
|
|
@@ -97,7 +104,7 @@ class CriteriaNode {
|
|
|
97
104
|
[util_1.inspect.custom]() {
|
|
98
105
|
const o = {};
|
|
99
106
|
['entityName', 'key', 'index', 'payload']
|
|
100
|
-
.filter(k => this[k]
|
|
107
|
+
.filter(k => this[k] !== undefined)
|
|
101
108
|
.forEach(k => o[k] = this[k]);
|
|
102
109
|
return `${this.constructor.name} ${(0, util_1.inspect)(o)}`;
|
|
103
110
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Dictionary, type EntityMetadata, type MetadataStorage, type EntityKey } from '@mikro-orm/core';
|
|
2
2
|
import type { ICriteriaNode } from '../typings';
|
|
3
3
|
/**
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
6
|
export declare class CriteriaNodeFactory {
|
|
7
|
-
static createNode(metadata: MetadataStorage, entityName: string, payload: any, parent?: ICriteriaNode
|
|
8
|
-
static createScalarNode(metadata: MetadataStorage, entityName: string, payload: any, parent?: ICriteriaNode
|
|
9
|
-
static createArrayNode(metadata: MetadataStorage, entityName: string, payload: any[], parent?: ICriteriaNode
|
|
10
|
-
static createObjectNode(metadata: MetadataStorage, entityName: string, payload: Dictionary, parent?: ICriteriaNode
|
|
11
|
-
static createObjectItemNode(metadata: MetadataStorage, entityName: string, node: ICriteriaNode
|
|
7
|
+
static createNode<T extends object>(metadata: MetadataStorage, entityName: string, payload: any, parent?: ICriteriaNode<T>, key?: EntityKey<T>): ICriteriaNode<T>;
|
|
8
|
+
static createScalarNode<T extends object>(metadata: MetadataStorage, entityName: string, payload: any, parent?: ICriteriaNode<T>, key?: EntityKey<T>): ICriteriaNode<T>;
|
|
9
|
+
static createArrayNode<T extends object>(metadata: MetadataStorage, entityName: string, payload: any[], parent?: ICriteriaNode<T>, key?: EntityKey<T>): ICriteriaNode<T>;
|
|
10
|
+
static createObjectNode<T extends object>(metadata: MetadataStorage, entityName: string, payload: Dictionary, parent?: ICriteriaNode<T>, key?: EntityKey<T>): ICriteriaNode<T>;
|
|
11
|
+
static createObjectItemNode<T extends object>(metadata: MetadataStorage, entityName: string, node: ICriteriaNode<T>, payload: Dictionary, key: EntityKey<T>, meta?: EntityMetadata<T>): ICriteriaNode<T>;
|
|
12
12
|
}
|
|
@@ -12,7 +12,7 @@ const CriteriaNode_1 = require("./CriteriaNode");
|
|
|
12
12
|
class CriteriaNodeFactory {
|
|
13
13
|
static createNode(metadata, entityName, payload, parent, key) {
|
|
14
14
|
const customExpression = CriteriaNode_1.CriteriaNode.isCustomExpression(key || '');
|
|
15
|
-
const scalar = core_1.Utils.isPrimaryKey(payload) || payload instanceof RegExp || payload instanceof Date || customExpression;
|
|
15
|
+
const scalar = core_1.Utils.isPrimaryKey(payload) || core_1.Utils.isRawSql(payload) || payload instanceof RegExp || payload instanceof Date || customExpression;
|
|
16
16
|
if (Array.isArray(payload) && !scalar) {
|
|
17
17
|
return this.createArrayNode(metadata, entityName, payload, parent, key);
|
|
18
18
|
}
|
|
@@ -37,9 +37,6 @@ class CriteriaNodeFactory {
|
|
|
37
37
|
}
|
|
38
38
|
static createObjectNode(metadata, entityName, payload, parent, key) {
|
|
39
39
|
const meta = metadata.find(entityName);
|
|
40
|
-
if (!parent && Object.keys(payload).every(k => meta?.properties[k]?.reference === core_1.ReferenceType.SCALAR)) {
|
|
41
|
-
return this.createScalarNode(metadata, entityName, payload, parent, key);
|
|
42
|
-
}
|
|
43
40
|
const node = new ObjectCriteriaNode_1.ObjectCriteriaNode(metadata, entityName, parent, key);
|
|
44
41
|
node.payload = Object.keys(payload).reduce((o, item) => {
|
|
45
42
|
o[item] = this.createObjectItemNode(metadata, entityName, node, payload, item, meta);
|
|
@@ -47,24 +44,24 @@ class CriteriaNodeFactory {
|
|
|
47
44
|
}, {});
|
|
48
45
|
return node;
|
|
49
46
|
}
|
|
50
|
-
static createObjectItemNode(metadata, entityName, node, payload,
|
|
51
|
-
const prop = meta?.properties[
|
|
52
|
-
if (prop?.
|
|
53
|
-
const childEntity = prop && prop.
|
|
54
|
-
return this.createNode(metadata, childEntity, payload[
|
|
47
|
+
static createObjectItemNode(metadata, entityName, node, payload, key, meta) {
|
|
48
|
+
const prop = meta?.properties[key];
|
|
49
|
+
if (prop?.kind !== core_1.ReferenceKind.EMBEDDED) {
|
|
50
|
+
const childEntity = prop && prop.kind !== core_1.ReferenceKind.SCALAR ? prop.type : entityName;
|
|
51
|
+
return this.createNode(metadata, childEntity, payload[key], node, key);
|
|
55
52
|
}
|
|
56
|
-
const operator = Object.keys(payload[
|
|
53
|
+
const operator = Object.keys(payload[key]).some(f => core_1.Utils.isOperator(f));
|
|
57
54
|
if (operator) {
|
|
58
55
|
throw core_1.ValidationError.cannotUseOperatorsInsideEmbeddables(entityName, prop.name, payload);
|
|
59
56
|
}
|
|
60
|
-
const map = Object.keys(payload[
|
|
57
|
+
const map = Object.keys(payload[key]).reduce((oo, k) => {
|
|
61
58
|
if (!prop.embeddedProps[k]) {
|
|
62
59
|
throw core_1.ValidationError.invalidEmbeddableQuery(entityName, k, prop.type);
|
|
63
60
|
}
|
|
64
|
-
oo[prop.embeddedProps[k].name] = payload[
|
|
61
|
+
oo[prop.embeddedProps[k].name] = payload[key][k];
|
|
65
62
|
return oo;
|
|
66
63
|
}, {});
|
|
67
|
-
return this.createNode(metadata, entityName, map, node,
|
|
64
|
+
return this.createNode(metadata, entityName, map, node, key);
|
|
68
65
|
}
|
|
69
66
|
}
|
|
70
67
|
exports.CriteriaNodeFactory = CriteriaNodeFactory;
|
|
@@ -3,9 +3,9 @@ import type { IQueryBuilder } from '../typings';
|
|
|
3
3
|
/**
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
|
-
export declare class ObjectCriteriaNode extends CriteriaNode {
|
|
7
|
-
process
|
|
8
|
-
willAutoJoin
|
|
6
|
+
export declare class ObjectCriteriaNode<T extends object> extends CriteriaNode<T> {
|
|
7
|
+
process(qb: IQueryBuilder<T>, alias?: string): any;
|
|
8
|
+
willAutoJoin(qb: IQueryBuilder<T>, alias?: string): boolean;
|
|
9
9
|
shouldInline(payload: any): boolean;
|
|
10
10
|
private inlineChildPayload;
|
|
11
11
|
private shouldAutoJoin;
|
|
@@ -21,7 +21,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
|
|
|
21
21
|
const childNode = this.payload[field];
|
|
22
22
|
const payload = childNode.process(qb, this.prop ? alias : ownerAlias);
|
|
23
23
|
const operator = core_1.Utils.isOperator(field);
|
|
24
|
-
const
|
|
24
|
+
const isRawField = core_1.RawQueryFragment.isKnownFragment(field);
|
|
25
25
|
// we need to keep the prefixing for formulas otherwise we would lose aliasing context when nesting inside group operators
|
|
26
26
|
const virtual = childNode.prop?.persist === false && !childNode.prop?.formula;
|
|
27
27
|
// if key is missing, we are inside group operator and we need to prefix with alias
|
|
@@ -33,8 +33,12 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
|
|
|
33
33
|
else if (childNode.shouldRename(payload)) {
|
|
34
34
|
o[childNode.renameFieldToPK(qb)] = payload;
|
|
35
35
|
}
|
|
36
|
-
else if (
|
|
37
|
-
|
|
36
|
+
else if (isRawField) {
|
|
37
|
+
const rawField = core_1.RawQueryFragment.getKnownFragment(field);
|
|
38
|
+
o[(0, core_1.raw)(rawField.sql.replaceAll(core_1.ALIAS_REPLACEMENT, alias), rawField.params)] = payload;
|
|
39
|
+
}
|
|
40
|
+
else if (primaryKey || virtual || operator || field.includes('.') || ![enums_1.QueryType.SELECT, enums_1.QueryType.COUNT].includes(qb.type ?? enums_1.QueryType.SELECT)) {
|
|
41
|
+
o[field.replaceAll(core_1.ALIAS_REPLACEMENT, alias)] = payload;
|
|
38
42
|
}
|
|
39
43
|
else {
|
|
40
44
|
o[`${alias}.${field}`] = payload;
|
|
@@ -60,7 +64,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
|
|
|
60
64
|
const customExpression = ObjectCriteriaNode.isCustomExpression(this.key);
|
|
61
65
|
const scalar = core_1.Utils.isPrimaryKey(payload) || payload instanceof RegExp || payload instanceof Date || customExpression;
|
|
62
66
|
const operator = core_1.Utils.isObject(payload) && Object.keys(payload).every(k => core_1.Utils.isOperator(k, false));
|
|
63
|
-
return !!this.prop && this.prop.
|
|
67
|
+
return !!this.prop && this.prop.kind !== core_1.ReferenceKind.SCALAR && !scalar && !operator;
|
|
64
68
|
}
|
|
65
69
|
inlineChildPayload(o, payload, field, alias, childAlias) {
|
|
66
70
|
const prop = this.metadata.find(this.entityName).properties[field];
|
|
@@ -104,15 +108,15 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
|
|
|
104
108
|
if (!this.prop || !this.parent) {
|
|
105
109
|
return false;
|
|
106
110
|
}
|
|
107
|
-
const embeddable = this.prop.
|
|
108
|
-
const knownKey = [core_1.
|
|
111
|
+
const embeddable = this.prop.kind === core_1.ReferenceKind.EMBEDDED;
|
|
112
|
+
const knownKey = [core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.EMBEDDED].includes(this.prop.kind) || (this.prop.kind === core_1.ReferenceKind.ONE_TO_ONE && this.prop.owner);
|
|
109
113
|
const operatorKeys = knownKey && Object.keys(this.payload).every(key => core_1.Utils.isOperator(key, false));
|
|
110
114
|
const primaryKeys = knownKey && Object.keys(this.payload).every(key => {
|
|
111
115
|
const meta = this.metadata.find(this.entityName);
|
|
112
116
|
if (!meta.primaryKeys.includes(key)) {
|
|
113
117
|
return false;
|
|
114
118
|
}
|
|
115
|
-
if (!core_1.Utils.isPlainObject(this.payload[key].payload) || ![core_1.
|
|
119
|
+
if (!core_1.Utils.isPlainObject(this.payload[key].payload) || ![core_1.ReferenceKind.ONE_TO_ONE, core_1.ReferenceKind.MANY_TO_ONE].includes(meta.properties[key].kind)) {
|
|
116
120
|
return true;
|
|
117
121
|
}
|
|
118
122
|
return Object.keys(this.payload[key].payload).every(k => meta.properties[key].targetMeta.primaryKeys.includes(k));
|
|
@@ -125,7 +129,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
|
|
|
125
129
|
const scalar = core_1.Utils.isPrimaryKey(this.payload) || this.payload instanceof RegExp || this.payload instanceof Date || customExpression;
|
|
126
130
|
const operator = core_1.Utils.isPlainObject(this.payload) && Object.keys(this.payload).every(k => core_1.Utils.isOperator(k, false));
|
|
127
131
|
const field = `${alias}.${this.prop.name}`;
|
|
128
|
-
if (this.prop.
|
|
132
|
+
if (this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY && (scalar || operator)) {
|
|
129
133
|
qb.join(field, nestedAlias, undefined, 'pivotJoin', this.getPath());
|
|
130
134
|
}
|
|
131
135
|
else {
|
package/query/QueryBuilder.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { inspect } from 'util';
|
|
1
3
|
import type { Knex } from 'knex';
|
|
2
|
-
import type
|
|
3
|
-
import { LockMode, PopulateHint, QueryFlag } from '@mikro-orm/core';
|
|
4
|
+
import { LockMode, PopulateHint, QueryFlag, type AnyEntity, type ConnectionType, type Dictionary, type EntityData, type EntityName, type EntityProperty, type FlushMode, type GroupOperator, type MetadataStorage, type ObjectQuery, type PopulateOptions, type QBFilterQuery, type QBQueryOrderMap, type QueryResult, type RequiredEntityData, type LoggingOptions } from '@mikro-orm/core';
|
|
4
5
|
import { QueryType } from './enums';
|
|
5
6
|
import type { AbstractSqlDriver } from '../AbstractSqlDriver';
|
|
6
|
-
import { QueryBuilderHelper } from './QueryBuilderHelper';
|
|
7
|
-
import type { Alias } from './QueryBuilderHelper';
|
|
7
|
+
import { QueryBuilderHelper, type Alias } from './QueryBuilderHelper';
|
|
8
8
|
import type { SqlEntityManager } from '../SqlEntityManager';
|
|
9
9
|
import type { Field } from '../typings';
|
|
10
|
+
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
|
|
10
11
|
/**
|
|
11
12
|
* SQL query builder with fluent interface.
|
|
12
13
|
*
|
|
@@ -27,12 +28,14 @@ import type { Field } from '../typings';
|
|
|
27
28
|
* ```
|
|
28
29
|
*/
|
|
29
30
|
export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
31
|
+
#private;
|
|
30
32
|
private readonly metadata;
|
|
31
33
|
private readonly driver;
|
|
32
34
|
private readonly context?;
|
|
33
35
|
private connectionType?;
|
|
34
36
|
private readonly em?;
|
|
35
|
-
|
|
37
|
+
private readonly logging?;
|
|
38
|
+
get mainAlias(): Alias<T>;
|
|
36
39
|
get alias(): string;
|
|
37
40
|
get helper(): QueryBuilderHelper;
|
|
38
41
|
/** @internal */
|
|
@@ -56,6 +59,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
56
59
|
private _orderBy;
|
|
57
60
|
private _groupBy;
|
|
58
61
|
private _having;
|
|
62
|
+
private _returning?;
|
|
59
63
|
private _onConflict?;
|
|
60
64
|
private _limit?;
|
|
61
65
|
private _offset?;
|
|
@@ -63,6 +67,8 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
63
67
|
private _joinedProps;
|
|
64
68
|
private _cache?;
|
|
65
69
|
private _indexHint?;
|
|
70
|
+
private _comments;
|
|
71
|
+
private _hintComments;
|
|
66
72
|
private flushMode?;
|
|
67
73
|
private lockMode?;
|
|
68
74
|
private lockTables?;
|
|
@@ -75,7 +81,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
75
81
|
/**
|
|
76
82
|
* @internal
|
|
77
83
|
*/
|
|
78
|
-
constructor(entityName: EntityName<T> | QueryBuilder<T>, metadata: MetadataStorage, driver: AbstractSqlDriver, context?: Knex.Transaction<any, any[]> | undefined, alias?: string, connectionType?: ConnectionType | undefined, em?: SqlEntityManager<AbstractSqlDriver<import("
|
|
84
|
+
constructor(entityName: EntityName<T> | QueryBuilder<T>, metadata: MetadataStorage, driver: AbstractSqlDriver, context?: Knex.Transaction<any, any[]> | undefined, alias?: string, connectionType?: ConnectionType | undefined, em?: SqlEntityManager<AbstractSqlDriver<import("..").AbstractSqlConnection, AbstractSqlPlatform>> | undefined, logging?: LoggingOptions | undefined);
|
|
79
85
|
select(fields: Field<T> | Field<T>[], distinct?: boolean): SelectQueryBuilder<T>;
|
|
80
86
|
addSelect(fields: Field<T> | Field<T>[]): SelectQueryBuilder<T>;
|
|
81
87
|
distinct(): SelectQueryBuilder<T>;
|
|
@@ -86,11 +92,12 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
86
92
|
delete(cond?: QBFilterQuery): DeleteQueryBuilder<T>;
|
|
87
93
|
truncate(): TruncateQueryBuilder<T>;
|
|
88
94
|
count(field?: string | string[], distinct?: boolean): CountQueryBuilder<T>;
|
|
89
|
-
join(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): this;
|
|
90
|
-
leftJoin(field: string, alias: string, cond?: QBFilterQuery): this;
|
|
91
|
-
joinAndSelect(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): SelectQueryBuilder<T>;
|
|
92
|
-
leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery): SelectQueryBuilder<T>;
|
|
93
|
-
|
|
95
|
+
join(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string, schema?: string): this;
|
|
96
|
+
leftJoin(field: string, alias: string, cond?: QBFilterQuery, schema?: string): this;
|
|
97
|
+
joinAndSelect(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string, fields?: string[], schema?: string): SelectQueryBuilder<T>;
|
|
98
|
+
leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<T>;
|
|
99
|
+
innerJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<T>;
|
|
100
|
+
protected getFieldsForJoinedLoad(prop: EntityProperty<T>, alias: string, explicitFields?: string[]): Field<T>[];
|
|
94
101
|
withSubQuery(subQuery: Knex.QueryBuilder, alias: string): this;
|
|
95
102
|
where(cond: QBFilterQuery<T>, operator?: keyof typeof GroupOperator): this;
|
|
96
103
|
where(cond: string, params?: any[], operator?: keyof typeof GroupOperator): this;
|
|
@@ -104,17 +111,11 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
104
111
|
onConflict(fields?: Field<T> | Field<T>[]): this;
|
|
105
112
|
ignore(): this;
|
|
106
113
|
merge(data?: EntityData<T> | Field<T>[]): this;
|
|
114
|
+
returning(fields?: Field<T> | Field<T>[]): this;
|
|
107
115
|
/**
|
|
108
116
|
* @internal
|
|
109
117
|
*/
|
|
110
118
|
populate(populate: PopulateOptions<T>[], populateWhere?: ObjectQuery<T> | PopulateHint): this;
|
|
111
|
-
/**
|
|
112
|
-
* @internal
|
|
113
|
-
*/
|
|
114
|
-
ref(field: string): Knex.Ref<string, {
|
|
115
|
-
[x: string]: string;
|
|
116
|
-
}>;
|
|
117
|
-
raw<R = Knex.Raw>(sql: string, bindings?: Knex.RawBinding[] | Knex.ValueDict): R;
|
|
118
119
|
limit(limit?: number, offset?: number): this;
|
|
119
120
|
offset(offset?: number): this;
|
|
120
121
|
withSchema(schema?: string): this;
|
|
@@ -127,6 +128,16 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
127
128
|
* Adds index hint to the FROM clause.
|
|
128
129
|
*/
|
|
129
130
|
indexHint(sql: string): this;
|
|
131
|
+
/**
|
|
132
|
+
* Prepend comment to the sql query using the syntax `/* ... *‍/`. Some characters are forbidden such as `/*, *‍/` and `?`.
|
|
133
|
+
*/
|
|
134
|
+
comment(comment: string | string[]): this;
|
|
135
|
+
/**
|
|
136
|
+
* Add hints to the query using comment-like syntax `/*+ ... *‍/`. MySQL and Oracle use this syntax for optimizer hints.
|
|
137
|
+
* Also various DB proxies and routers use this syntax to pass hints to alter their behavior. In other dialects the hints
|
|
138
|
+
* are ignored as simple comments.
|
|
139
|
+
*/
|
|
140
|
+
hintComment(comment: string | string[]): this;
|
|
130
141
|
/**
|
|
131
142
|
* Specifies FROM which entity's table select/update/delete will be executed, removing all previously set FROM-s.
|
|
132
143
|
* Allows setting a main string alias of the selection data.
|
|
@@ -138,6 +149,11 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
138
149
|
* Returns the query with parameters as wildcards.
|
|
139
150
|
*/
|
|
140
151
|
getQuery(): string;
|
|
152
|
+
toQuery(): {
|
|
153
|
+
sql: string;
|
|
154
|
+
_sql: Knex.Sql;
|
|
155
|
+
params: readonly unknown[];
|
|
156
|
+
};
|
|
141
157
|
/**
|
|
142
158
|
* Returns the list of all parameters for this query.
|
|
143
159
|
*/
|
|
@@ -179,6 +195,10 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
179
195
|
* Executes count query (without offset and limit), returning total count of results
|
|
180
196
|
*/
|
|
181
197
|
getCount(field?: string | string[], distinct?: boolean): Promise<number>;
|
|
198
|
+
/**
|
|
199
|
+
* Executes the query, returning both array of results and total count query (without offset and limit).
|
|
200
|
+
*/
|
|
201
|
+
getResultAndCount(): Promise<[T[], number]>;
|
|
182
202
|
/**
|
|
183
203
|
* Provides promise-like interface so we can await the QB instance.
|
|
184
204
|
*/
|
|
@@ -194,11 +214,12 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
194
214
|
private prepareFields;
|
|
195
215
|
private init;
|
|
196
216
|
private getQueryBase;
|
|
217
|
+
private applyDiscriminatorCondition;
|
|
197
218
|
private finalize;
|
|
219
|
+
private processPopulateWhere;
|
|
198
220
|
private hasToManyJoins;
|
|
199
221
|
private wrapPaginateSubQuery;
|
|
200
222
|
private wrapModifySubQuery;
|
|
201
|
-
private autoJoinPivotTable;
|
|
202
223
|
private getSchema;
|
|
203
224
|
private createAlias;
|
|
204
225
|
private createMainAlias;
|
|
@@ -207,6 +228,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
|
|
|
207
228
|
private createQueryBuilderHelper;
|
|
208
229
|
private ensureFromClause;
|
|
209
230
|
private ensureNotFinalized;
|
|
231
|
+
[inspect.custom](depth: number): string;
|
|
210
232
|
}
|
|
211
233
|
export interface RunQueryBuilder<T extends object> extends Omit<QueryBuilder<T>, 'getResult' | 'getSingleResult' | 'getResultList' | 'where'> {
|
|
212
234
|
where(cond: QBFilterQuery<T> | string, params?: keyof typeof GroupOperator | any[], operator?: keyof typeof GroupOperator): this;
|