@mikro-orm/core 6.6.20-dev.3 → 6.6.20
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/EntityManager.js +2 -2
- package/entity/EntityAssigner.js +7 -4
- package/errors.d.ts +1 -0
- package/errors.js +3 -0
- package/package.json +2 -2
- package/utils/upsert-utils.d.ts +2 -1
- package/utils/upsert-utils.js +20 -1
package/EntityManager.js
CHANGED
|
@@ -702,7 +702,7 @@ class EntityManager {
|
|
|
702
702
|
}
|
|
703
703
|
}
|
|
704
704
|
}
|
|
705
|
-
where = (0, utils_1.getWhereCondition)(meta, options.onConflictFields, data, where).where;
|
|
705
|
+
where = (0, utils_1.getWhereCondition)(meta, options.onConflictFields, data, where, this.getPlatform()).where;
|
|
706
706
|
data = utils_1.QueryHelper.processObjectParams(data);
|
|
707
707
|
em.validator.validateParams(data, 'insert data');
|
|
708
708
|
if (em.eventManager.hasListeners(enums_1.EventType.beforeUpsert, meta)) {
|
|
@@ -839,7 +839,7 @@ class EntityManager {
|
|
|
839
839
|
}
|
|
840
840
|
const unique = options.onConflictFields ?? meta.props.filter(p => p.unique).map(p => p.name);
|
|
841
841
|
propIndex = !utils_1.Utils.isRawSql(unique) && unique.findIndex(p => data[p] ?? data[p.substring(0, p.indexOf('.'))] != null);
|
|
842
|
-
const tmp = (0, utils_1.getWhereCondition)(meta, options.onConflictFields, row, where);
|
|
842
|
+
const tmp = (0, utils_1.getWhereCondition)(meta, options.onConflictFields, row, where, this.getPlatform());
|
|
843
843
|
propIndex = tmp.propIndex;
|
|
844
844
|
where = utils_1.QueryHelper.processWhere({
|
|
845
845
|
where: tmp.where,
|
package/entity/EntityAssigner.js
CHANGED
|
@@ -56,12 +56,12 @@ class EntityAssigner {
|
|
|
56
56
|
if ([enums_1.ReferenceKind.ONE_TO_MANY].includes(prop.kind)) {
|
|
57
57
|
return;
|
|
58
58
|
}
|
|
59
|
-
if (
|
|
59
|
+
if (prop.kind === enums_1.ReferenceKind.MANY_TO_MANY) {
|
|
60
60
|
if (!prop.owner) {
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
|
-
else if (value
|
|
64
|
-
value = value.
|
|
63
|
+
else if (Utils_1.Utils.isCollection(value)) {
|
|
64
|
+
value = value.getItems();
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
if ([enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
@@ -169,6 +169,9 @@ class EntityAssigner {
|
|
|
169
169
|
static assignCollection(entity, collection, value, prop, em, options) {
|
|
170
170
|
const invalid = [];
|
|
171
171
|
const items = Utils_1.Utils.asArray(value).map((item, idx) => {
|
|
172
|
+
if (options.onlyOwnProperties && (Array.isArray(value) ? !Array.isArray(item) : Utils_1.Utils.isPlainObject(item))) {
|
|
173
|
+
item = Utils_1.Utils.extractPK(item, prop.targetMeta);
|
|
174
|
+
}
|
|
172
175
|
// try to propagate missing owning side reference to the payload first
|
|
173
176
|
const prop2 = prop.targetMeta?.properties[prop.mappedBy];
|
|
174
177
|
if (Utils_1.Utils.isPlainObject(item) && prop2 && item[prop2.name] == null) {
|
|
@@ -232,7 +235,7 @@ class EntityAssigner {
|
|
|
232
235
|
if (Utils_1.Utils.isEntity(item)) {
|
|
233
236
|
return item;
|
|
234
237
|
}
|
|
235
|
-
if (Utils_1.Utils.isPrimaryKey(item) && EntityAssigner.validateEM(em)) {
|
|
238
|
+
if (Utils_1.Utils.isPrimaryKey(item, prop.targetMeta.compositePK) && EntityAssigner.validateEM(em)) {
|
|
236
239
|
return em.getReference(prop.type, item, options);
|
|
237
240
|
}
|
|
238
241
|
if (Utils_1.Utils.isPlainObject(item) && options.merge && EntityAssigner.validateEM(em)) {
|
package/errors.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export declare class ValidationError<T extends AnyEntity = AnyEntity> extends Er
|
|
|
25
25
|
static cannotUseGlobalContext(): ValidationError;
|
|
26
26
|
static cannotUseOperatorsInsideEmbeddables(className: string, propName: string, payload: unknown): ValidationError;
|
|
27
27
|
static cannotUseGroupOperatorsInsideScalars(className: string, propName: string, payload: unknown): ValidationError;
|
|
28
|
+
static cannotUseOperatorsInUpsertKeys(className: string, propName: string, payload: unknown): ValidationError;
|
|
28
29
|
static invalidEmbeddableQuery(className: string, propName: string, embeddableType: string): ValidationError;
|
|
29
30
|
static invalidQueryCondition(cond: unknown): ValidationError;
|
|
30
31
|
}
|
package/errors.js
CHANGED
|
@@ -101,6 +101,9 @@ class ValidationError extends Error {
|
|
|
101
101
|
static cannotUseGroupOperatorsInsideScalars(className, propName, payload) {
|
|
102
102
|
return new ValidationError(`Using group operators ($and/$or) inside scalar properties is not allowed, move the operator above. (property: ${className}.${propName}, payload: ${(0, node_util_1.inspect)(payload)})`);
|
|
103
103
|
}
|
|
104
|
+
static cannotUseOperatorsInUpsertKeys(className, propName, payload) {
|
|
105
|
+
return new ValidationError(`Invalid value of ${className}.${propName}, operators are not allowed in the unique keys of upsert data: ${(0, node_util_1.inspect)(payload)}`);
|
|
106
|
+
}
|
|
104
107
|
static invalidEmbeddableQuery(className, propName, embeddableType) {
|
|
105
108
|
return new ValidationError(`Invalid query for entity '${className}', property '${propName}' does not exist in embeddable '${embeddableType}'`);
|
|
106
109
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "6.6.20
|
|
3
|
+
"version": "6.6.20",
|
|
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",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"esprima": "4.0.1",
|
|
65
65
|
"fs-extra": "11.3.3",
|
|
66
66
|
"globby": "11.1.0",
|
|
67
|
-
"mikro-orm": "6.6.20
|
|
67
|
+
"mikro-orm": "6.6.20",
|
|
68
68
|
"reflect-metadata": "0.2.2"
|
|
69
69
|
}
|
|
70
70
|
}
|
package/utils/upsert-utils.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { EntityData, EntityMetadata, FilterQuery } from '../typings';
|
|
2
2
|
import type { UpsertOptions } from '../drivers/IDatabaseDriver';
|
|
3
3
|
import type { RawQueryFragment } from '../utils/RawQueryFragment';
|
|
4
|
+
import type { Platform } from '../platforms/Platform';
|
|
4
5
|
/** @internal */
|
|
5
6
|
export declare function getOnConflictFields<T>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | RawQueryFragment, options: UpsertOptions<T>): (keyof T)[];
|
|
6
7
|
/** @internal */
|
|
7
8
|
export declare function getOnConflictReturningFields<T, P extends string>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | RawQueryFragment, options: UpsertOptions<T, P>): (keyof T)[] | '*';
|
|
8
9
|
/** @internal */
|
|
9
|
-
export declare function getWhereCondition<T extends object>(meta: EntityMetadata<T>, onConflictFields: (keyof T)[] | RawQueryFragment | undefined, data: EntityData<T>, where: FilterQuery<T
|
|
10
|
+
export declare function getWhereCondition<T extends object>(meta: EntityMetadata<T>, onConflictFields: (keyof T)[] | RawQueryFragment | undefined, data: EntityData<T>, where: FilterQuery<T>, platform: Platform): {
|
|
10
11
|
where: FilterQuery<T>;
|
|
11
12
|
propIndex: number | false;
|
|
12
13
|
};
|
package/utils/upsert-utils.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.getOnConflictFields = getOnConflictFields;
|
|
|
4
4
|
exports.getOnConflictReturningFields = getOnConflictReturningFields;
|
|
5
5
|
exports.getWhereCondition = getWhereCondition;
|
|
6
6
|
const Utils_1 = require("./Utils");
|
|
7
|
+
const errors_1 = require("../errors");
|
|
7
8
|
function expandEmbeddedProperties(prop, key) {
|
|
8
9
|
if (prop.object) {
|
|
9
10
|
return [prop.name];
|
|
@@ -114,8 +115,25 @@ function getPropertyValue(obj, key) {
|
|
|
114
115
|
}
|
|
115
116
|
return curr[parts[parts.length - 1]];
|
|
116
117
|
}
|
|
118
|
+
/** The condition is built from the upsert data, so its values must never be interpreted as operators. */
|
|
119
|
+
function validateWhereCondition(meta, platform, value, propName) {
|
|
120
|
+
if (Array.isArray(value)) {
|
|
121
|
+
// a top-level array is a composite primary key
|
|
122
|
+
return value.forEach((item, idx) => validateWhereCondition(meta, platform, item, propName ?? meta.primaryKeys[idx]));
|
|
123
|
+
}
|
|
124
|
+
if (!Utils_1.Utils.isPlainObject(value)) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
for (const key of Object.keys(value)) {
|
|
128
|
+
// SQL drivers interpret only the known operators, other `$` keys are valid in JSON values, mongo passes all of them to the server
|
|
129
|
+
if (platform.usesPivotTable() ? Utils_1.Utils.isOperator(key) : key.startsWith('$')) {
|
|
130
|
+
throw errors_1.ValidationError.cannotUseOperatorsInUpsertKeys(meta.className, propName ?? meta.primaryKeys[0], value);
|
|
131
|
+
}
|
|
132
|
+
validateWhereCondition(meta, platform, value[key], propName ?? key);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
117
135
|
/** @internal */
|
|
118
|
-
function getWhereCondition(meta, onConflictFields, data, where) {
|
|
136
|
+
function getWhereCondition(meta, onConflictFields, data, where, platform) {
|
|
119
137
|
const unique = onConflictFields ?? meta.props.filter(p => p.unique).map(p => p.name);
|
|
120
138
|
const propIndex = !Utils_1.Utils.isRawSql(unique) && unique.findIndex(p => data[p] ?? data[p.substring(0, p.indexOf('.'))] != null);
|
|
121
139
|
if (onConflictFields || where == null) {
|
|
@@ -141,5 +159,6 @@ function getWhereCondition(meta, onConflictFields, data, where) {
|
|
|
141
159
|
}
|
|
142
160
|
}
|
|
143
161
|
}
|
|
162
|
+
validateWhereCondition(meta, platform, where);
|
|
144
163
|
return { where, propIndex };
|
|
145
164
|
}
|