@mikro-orm/core 7.2.7-dev.10 → 7.2.7-dev.12
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/package.json +1 -1
- package/utils/QueryHelper.js +3 -2
- package/utils/RawQueryFragment.d.ts +5 -1
- package/utils/RawQueryFragment.js +27 -19
- package/utils/Utils.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "7.2.7-dev.
|
|
3
|
+
"version": "7.2.7-dev.12",
|
|
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
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
package/utils/QueryHelper.js
CHANGED
|
@@ -3,7 +3,7 @@ import { DANGEROUS_PROPERTY_NAMES, Utils } from './Utils.js';
|
|
|
3
3
|
import { ARRAY_OPERATORS, GroupOperator, JSON_KEY_OPERATORS, ReferenceKind } from '../enums.js';
|
|
4
4
|
import { JsonType } from '../types/JsonType.js';
|
|
5
5
|
import { helper } from '../entity/wrap.js';
|
|
6
|
-
import { isRaw, Raw } from './RawQueryFragment.js';
|
|
6
|
+
import { isRaw, isSubquery, Raw } from './RawQueryFragment.js';
|
|
7
7
|
import { MetadataError, ValidationError } from '../errors.js';
|
|
8
8
|
/** @internal */
|
|
9
9
|
export class QueryHelper {
|
|
@@ -458,7 +458,8 @@ export class QueryHelper {
|
|
|
458
458
|
if (Array.isArray(cond) && !(key && ARRAY_OPERATORS.includes(key))) {
|
|
459
459
|
return cond.map(v => QueryHelper.processCustomType(prop, v, platform, key, fromQuery));
|
|
460
460
|
}
|
|
461
|
-
|
|
461
|
+
// a query builder renders as a sub-query, not as a value of the property type
|
|
462
|
+
if (isRaw(cond) || isSubquery(cond)) {
|
|
462
463
|
return cond;
|
|
463
464
|
}
|
|
464
465
|
return prop.customType.convertToDatabaseValue(cond, platform, { fromQuery, key, mode: 'query' });
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import type { AnyString, Dictionary, EntityKey } from '../typings.js';
|
|
1
|
+
import type { AnyString, Dictionary, EntityKey, Subquery } from '../typings.js';
|
|
2
2
|
declare const rawFragmentSymbolBrand: unique symbol;
|
|
3
3
|
/** Branded symbol type used as a unique key for tracking raw SQL fragments in object properties. */
|
|
4
4
|
export type RawQueryFragmentSymbol = symbol & {
|
|
5
5
|
readonly [rawFragmentSymbolBrand]: true;
|
|
6
6
|
};
|
|
7
|
+
/** Prototype brand of query builders usable as sub-queries; a symbol so JSON payloads cannot carry it, `Symbol.for` so package copies agree. */
|
|
8
|
+
export declare const SUBQUERY_BRAND: unique symbol;
|
|
7
9
|
/** Checks whether the given value is a `RawQueryFragment` instance. */
|
|
8
10
|
export declare function isRaw(value: unknown): value is RawQueryFragment;
|
|
11
|
+
/** Checks whether the given value is a query builder usable as a sub-query (its prototype carries the `SUBQUERY_BRAND`). */
|
|
12
|
+
export declare function isSubquery(value: unknown): value is Subquery;
|
|
9
13
|
/** Represents a raw SQL fragment with optional parameters, usable as both a value and an object key via Symbol coercion. */
|
|
10
14
|
export declare class RawQueryFragment<Alias extends string = string> {
|
|
11
15
|
#private;
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { Utils } from './Utils.js';
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
// so each CJS/ESM copy of this module independently installs it on its own
|
|
5
|
-
// prototype without publishing a global key for the marker that controls raw
|
|
6
|
-
// SQL assembly. The string is namespaced so it does not collide with property
|
|
7
|
-
// names users might independently install on their own prototypes.
|
|
8
|
-
const RAW_FRAGMENT_BRAND = '__mikroOrmRawFragment';
|
|
2
|
+
// Prototype brand of raw fragments; `Symbol.for` so package copies agree.
|
|
3
|
+
const RAW_FRAGMENT_BRAND = Symbol.for('mikro-orm.raw-fragment');
|
|
9
4
|
// Back-references from a fragment's symbol key to the fragment itself, shared
|
|
10
5
|
// across CJS/ESM module copies via globalThis: when one copy creates a fragment
|
|
11
6
|
// via `raw('…')` and stores its symbol in a where-clause object key, the other
|
|
@@ -13,6 +8,20 @@ const RAW_FRAGMENT_BRAND = '__mikroOrmRawFragment';
|
|
|
13
8
|
const REGISTRY_KEY = Symbol.for('@mikro-orm/core/RawQueryFragment.references');
|
|
14
9
|
const rawQueryReferences = (globalThis[REGISTRY_KEY] ??=
|
|
15
10
|
new WeakMap());
|
|
11
|
+
/** Prototype brand of query builders usable as sub-queries; a symbol so JSON payloads cannot carry it, `Symbol.for` so package copies agree. */
|
|
12
|
+
export const SUBQUERY_BRAND = Symbol.for('mikro-orm.subquery');
|
|
13
|
+
function hasPrototypeBrand(value, brand) {
|
|
14
|
+
// Walk the prototype chain starting from the *prototype* (not the value) so
|
|
15
|
+
// own-property spoofing from JSON payloads cannot forge the brand. Stop at
|
|
16
|
+
// `Object.prototype`, which is never branded — that bails plain objects,
|
|
17
|
+
// JSON payloads, and built-ins like `Date`/`Array`/`Map` at depth 1.
|
|
18
|
+
for (let p = Object.getPrototypeOf(value); p != null && p !== Object.prototype; p = Object.getPrototypeOf(p)) {
|
|
19
|
+
if (Object.hasOwn(p, brand)) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
16
25
|
/** Checks whether the given value is a `RawQueryFragment` instance. */
|
|
17
26
|
export function isRaw(value) {
|
|
18
27
|
if (value == null || typeof value !== 'object') {
|
|
@@ -23,16 +32,11 @@ export function isRaw(value) {
|
|
|
23
32
|
if (value instanceof RawQueryFragment) {
|
|
24
33
|
return true;
|
|
25
34
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (Object.hasOwn(p, RAW_FRAGMENT_BRAND)) {
|
|
32
|
-
return true;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return false;
|
|
35
|
+
return hasPrototypeBrand(value, RAW_FRAGMENT_BRAND);
|
|
36
|
+
}
|
|
37
|
+
/** Checks whether the given value is a query builder usable as a sub-query (its prototype carries the `SUBQUERY_BRAND`). */
|
|
38
|
+
export function isSubquery(value) {
|
|
39
|
+
return value != null && typeof value === 'object' && hasPrototypeBrand(value, SUBQUERY_BRAND);
|
|
36
40
|
}
|
|
37
41
|
/** Represents a raw SQL fragment with optional parameters, usable as both a value and an object key via Symbol coercion. */
|
|
38
42
|
export class RawQueryFragment {
|
|
@@ -121,6 +125,10 @@ export { RawQueryFragment as Raw };
|
|
|
121
125
|
export const ALIAS_REPLACEMENT = '[::alias::]';
|
|
122
126
|
/** @internal */
|
|
123
127
|
export const ALIAS_REPLACEMENT_RE = '\\[::alias::\\]';
|
|
128
|
+
// a query builder param is embedded as a sub-query instead of being bound as a value
|
|
129
|
+
function toRawParam(param) {
|
|
130
|
+
return isSubquery(param) ? param.toRaw() : param;
|
|
131
|
+
}
|
|
124
132
|
/**
|
|
125
133
|
* Creates raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented
|
|
126
134
|
* by `RawQueryFragment` class instance that can be serialized to a string, so it can be used both as an object value
|
|
@@ -204,12 +212,12 @@ export function raw(sql, params) {
|
|
|
204
212
|
if (!Object.hasOwn(dict, key)) {
|
|
205
213
|
return match;
|
|
206
214
|
}
|
|
207
|
-
objectParams.push(dict[key]);
|
|
215
|
+
objectParams.push(toRawParam(dict[key]));
|
|
208
216
|
return identifier ? '??' : '?';
|
|
209
217
|
});
|
|
210
218
|
return new RawQueryFragment(sql, objectParams);
|
|
211
219
|
}
|
|
212
|
-
return new RawQueryFragment(sql, params);
|
|
220
|
+
return new RawQueryFragment(sql, params?.map(toRawParam));
|
|
213
221
|
}
|
|
214
222
|
/**
|
|
215
223
|
* Alternative to the `raw()` helper allowing to use it as a tagged template function for the simple cases.
|
package/utils/Utils.js
CHANGED
|
@@ -153,7 +153,7 @@ export function parseJsonSafe(value) {
|
|
|
153
153
|
/** Collection of general-purpose utility methods used throughout the ORM. */
|
|
154
154
|
export class Utils {
|
|
155
155
|
static PK_SEPARATOR = '~~~';
|
|
156
|
-
static #ORM_VERSION = '7.2.7-dev.
|
|
156
|
+
static #ORM_VERSION = '7.2.7-dev.12';
|
|
157
157
|
/** Default session variable name backing an RLS filter argument (`current_setting('mikro.<filter>.<arg>')`). */
|
|
158
158
|
static getRlsSettingName(filterName, argName) {
|
|
159
159
|
return `mikro.${filterName}.${argName}`;
|