@mikro-orm/core 7.2.9-dev.1 → 7.2.9-dev.11

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 CHANGED
@@ -1318,7 +1318,7 @@ export class EntityManager {
1318
1318
  // an empty condition identifies no row, it must not turn into a reload condition matching every row
1319
1319
  const getRowUniqueFields = (where) => Utils.isPlainObject(where) && !Utils.hasObjectKeys(where) ? uniqueFields : getUniqueFields(where);
1320
1320
  const platform = this.getPlatform();
1321
- const returning = getOnConflictReturningFields(meta, data[dataIndexes[0]], uniqueFields, options, platform);
1321
+ const returning = getOnConflictReturningFields(meta, allData[0], uniqueFields, options, platform);
1322
1322
  if (options.onConflictWhere && res.rows?.length !== allData.length) {
1323
1323
  returning.push(...meta.comparableProps.filter(p => !p.lazy && !p.embeddable).map(p => p.name));
1324
1324
  }
@@ -2399,7 +2399,17 @@ export class EntityManager {
2399
2399
  }
2400
2400
  let autoRefresh;
2401
2401
  if (options.fields) {
2402
- autoRefresh = options.fields.some(field => !helper(entity).__loadedProperties.has(field));
2402
+ // inline embeddables track only their leaf properties as loaded, not the parent
2403
+ const isLoaded = (prop) => helper(entity).__loadedProperties.has(prop.name) ||
2404
+ (prop.kind === ReferenceKind.EMBEDDED && !prop.object && Object.values(prop.embeddedProps).every(isLoaded));
2405
+ autoRefresh = options.fields.some(field => {
2406
+ const [first, ...rest] = field.split('.');
2407
+ let prop = meta.properties[first];
2408
+ for (const part of rest) {
2409
+ prop = prop?.kind === ReferenceKind.EMBEDDED && !prop.object ? prop.embeddedProps[part] : undefined;
2410
+ }
2411
+ return !prop || !isLoaded(prop);
2412
+ });
2403
2413
  }
2404
2414
  else {
2405
2415
  autoRefresh = meta.comparableProps.some(prop => {
@@ -501,6 +501,11 @@ export class EntityLoader {
501
501
  for (const entity of entities) {
502
502
  const ref = entity[prop.name];
503
503
  if (ref == null) {
504
+ // an inverse 1:1 with no matching row is resolved as empty, mark it so it is not queried again
505
+ if (isInverseOneToOne && !partial && childrenMap.has(getKey(entity))) {
506
+ entity[prop.name] = nullVal;
507
+ helper(entity).__loadedProperties.add(prop.name);
508
+ }
504
509
  continue;
505
510
  }
506
511
  const refKey = getKey(ref);
@@ -883,7 +888,11 @@ export class EntityLoader {
883
888
  if (value === null) {
884
889
  return false;
885
890
  }
886
- if (value === undefined || !Utils.isEntity(value, true)) {
891
+ // with `forceUndefined`, an empty relation that was already loaded is `undefined` instead of `null`
892
+ if (value === undefined) {
893
+ return !helper(e).__loadedProperties.has(prop.name);
894
+ }
895
+ if (!Utils.isEntity(value, true)) {
887
896
  return true;
888
897
  }
889
898
  const target = Reference.unwrapReference(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.2.9-dev.1",
3
+ "version": "7.2.9-dev.11",
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",
@@ -81,57 +81,66 @@ export class EntitySerializer {
81
81
  root = new SerializationContext();
82
82
  contextCreated = true;
83
83
  }
84
- SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== ReferenceKind.SCALAR);
85
84
  const ret = {};
86
- const props = new Set();
87
- if (meta.serializedPrimaryKey && !meta.compositePK) {
88
- props.add(meta.serializedPrimaryKey);
89
- }
90
- else {
91
- meta.primaryKeys.forEach(pk => props.add(pk));
92
- }
93
- if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
94
- const entityKeys = new Set(Object.keys(entity));
95
- for (const prop of meta.props) {
96
- if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
97
- props.add(prop.name);
98
- }
85
+ try {
86
+ SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== ReferenceKind.SCALAR);
87
+ const props = new Set();
88
+ if (meta.serializedPrimaryKey && !meta.compositePK) {
89
+ props.add(meta.serializedPrimaryKey);
99
90
  }
100
- for (const key of entityKeys) {
101
- if (!meta.properties[key]) {
102
- props.add(key);
103
- }
104
- }
105
- }
106
- const visited = root.visited.has(entity);
107
- if (!visited) {
108
- root.visited.add(entity);
109
- }
110
- for (const prop of props) {
111
- if (!isVisible(meta, prop, options)) {
112
- continue;
113
- }
114
- const cycle = root.visit(meta.class, prop);
115
- if (cycle && visited) {
116
- continue;
117
- }
118
- const val = this.processProperty(prop, entity, options);
119
- if (!cycle) {
120
- root.leave(meta.class, prop);
91
+ else {
92
+ meta.primaryKeys.forEach(pk => props.add(pk));
121
93
  }
122
- if (options.skipNull && Utils.isPlainObject(val)) {
123
- Utils.dropUndefinedProperties(val, null);
94
+ if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
95
+ const entityKeys = new Set(Object.keys(entity));
96
+ for (const prop of meta.props) {
97
+ if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
98
+ props.add(prop.name);
99
+ }
100
+ }
101
+ for (const key of entityKeys) {
102
+ if (!meta.properties[key]) {
103
+ props.add(key);
104
+ }
105
+ }
124
106
  }
125
- if (isRaw(val)) {
126
- throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
107
+ const visited = root.visited.has(entity);
108
+ if (!visited) {
109
+ root.visited.add(entity);
127
110
  }
128
- const visible = typeof val !== 'undefined' && !(val === null && options.skipNull);
129
- if (visible) {
130
- ret[this.propertyName(meta, prop)] = val;
111
+ for (const prop of props) {
112
+ if (!isVisible(meta, prop, options)) {
113
+ continue;
114
+ }
115
+ const cycle = root.visit(meta.class, prop);
116
+ if (cycle && visited) {
117
+ continue;
118
+ }
119
+ let val;
120
+ try {
121
+ val = this.processProperty(prop, entity, options);
122
+ }
123
+ finally {
124
+ if (!cycle) {
125
+ root.leave(meta.class, prop);
126
+ }
127
+ }
128
+ if (options.skipNull && Utils.isPlainObject(val)) {
129
+ Utils.dropUndefinedProperties(val, null);
130
+ }
131
+ if (isRaw(val)) {
132
+ throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
133
+ }
134
+ const visible = typeof val !== 'undefined' && !(val === null && options.skipNull);
135
+ if (visible) {
136
+ ret[this.propertyName(meta, prop)] = val;
137
+ }
131
138
  }
132
139
  }
133
- if (contextCreated) {
134
- root.close();
140
+ finally {
141
+ if (contextCreated) {
142
+ root.close();
143
+ }
135
144
  }
136
145
  if (!wrapped.isInitialized()) {
137
146
  return ret;
@@ -26,91 +26,100 @@ export class EntityTransformer {
26
26
  root = new SerializationContext(wrapped.__serializationContext.populate, wrapped.__serializationContext.fields, wrapped.__serializationContext.exclude);
27
27
  contextCreated = true;
28
28
  }
29
- SerializationContext.propagate(root, entity, isVisible);
30
- const meta = wrapped.__meta;
31
- const ret = {};
32
- const props = new Set();
33
- if (meta.serializedPrimaryKey && !meta.compositePK) {
34
- props.add(meta.serializedPrimaryKey);
35
- }
36
- else {
37
- meta.primaryKeys.forEach(pk => props.add(pk));
38
- }
39
- if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
40
- const entityKeys = new Set(Object.keys(entity));
41
- for (const prop of meta.props) {
42
- if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
43
- props.add(prop.name);
44
- }
29
+ try {
30
+ SerializationContext.propagate(root, entity, isVisible);
31
+ const meta = wrapped.__meta;
32
+ const ret = {};
33
+ const props = new Set();
34
+ if (meta.serializedPrimaryKey && !meta.compositePK) {
35
+ props.add(meta.serializedPrimaryKey);
36
+ }
37
+ else {
38
+ meta.primaryKeys.forEach(pk => props.add(pk));
45
39
  }
46
- for (const key of entityKeys) {
47
- if (!meta.properties[key]) {
48
- props.add(key);
40
+ if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
41
+ const entityKeys = new Set(Object.keys(entity));
42
+ for (const prop of meta.props) {
43
+ if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
44
+ props.add(prop.name);
45
+ }
46
+ }
47
+ for (const key of entityKeys) {
48
+ if (!meta.properties[key]) {
49
+ props.add(key);
50
+ }
49
51
  }
50
52
  }
51
- }
52
- const visited = root.visited.has(entity);
53
- const includePrimaryKeys = wrapped.__config.get('serialization').includePrimaryKeys;
54
- if (!visited) {
55
- root.visited.add(entity);
56
- }
57
- for (const prop of props) {
58
- const visible = raw ? meta.properties[prop] : isVisible(meta, prop, ignoreFields);
59
- if (!visible) {
60
- continue;
53
+ const visited = root.visited.has(entity);
54
+ const includePrimaryKeys = wrapped.__config.get('serialization').includePrimaryKeys;
55
+ if (!visited) {
56
+ root.visited.add(entity);
61
57
  }
62
- const populated = root.isMarkedAsPopulated(meta.class, prop);
63
- if (!raw) {
64
- const partiallyLoaded = root.isPartiallyLoaded(meta.class, prop);
65
- const isPrimary = includePrimaryKeys && meta.properties[prop].primary;
66
- if (!partiallyLoaded && !populated && !isPrimary) {
58
+ for (const prop of props) {
59
+ const visible = raw ? meta.properties[prop] : isVisible(meta, prop, ignoreFields);
60
+ if (!visible) {
67
61
  continue;
68
62
  }
69
- if (root.isExcluded(meta.class, prop) && !populated) {
63
+ const populated = root.isMarkedAsPopulated(meta.class, prop);
64
+ if (!raw) {
65
+ const partiallyLoaded = root.isPartiallyLoaded(meta.class, prop);
66
+ const isPrimary = includePrimaryKeys && meta.properties[prop].primary;
67
+ if (!partiallyLoaded && !populated && !isPrimary) {
68
+ continue;
69
+ }
70
+ if (root.isExcluded(meta.class, prop) && !populated) {
71
+ continue;
72
+ }
73
+ }
74
+ const cycle = root.visit(meta.class, prop);
75
+ if (cycle && visited) {
70
76
  continue;
71
77
  }
78
+ let val;
79
+ try {
80
+ val = EntityTransformer.processProperty(prop, entity, raw, populated);
81
+ }
82
+ finally {
83
+ if (!cycle) {
84
+ root.leave(meta.class, prop);
85
+ }
86
+ }
87
+ if (isRaw(val)) {
88
+ throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
89
+ }
90
+ if (typeof val === 'undefined') {
91
+ continue;
92
+ }
93
+ ret[this.propertyName(meta, prop, raw)] = val;
72
94
  }
73
- const cycle = root.visit(meta.class, prop);
74
- if (cycle && visited) {
75
- continue;
76
- }
77
- const val = EntityTransformer.processProperty(prop, entity, raw, populated);
78
- if (!cycle) {
79
- root.leave(meta.class, prop);
80
- }
81
- if (isRaw(val)) {
82
- throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
83
- }
84
- if (typeof val === 'undefined') {
85
- continue;
95
+ if (!wrapped.isInitialized() && wrapped.hasPrimaryKey()) {
96
+ return ret;
86
97
  }
87
- ret[this.propertyName(meta, prop, raw)] = val;
88
- }
89
- if (!wrapped.isInitialized() && wrapped.hasPrimaryKey()) {
90
- return ret;
91
- }
92
- for (const prop of meta.getterProps) {
93
- // decorated get methods
94
- if (prop.getterName != null) {
95
- const visible = !prop.hidden && entity[prop.getterName] instanceof Function;
96
- const populated = root.isMarkedAsPopulated(meta.class, prop.name);
97
- if (visible) {
98
- ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
98
+ for (const prop of meta.getterProps) {
99
+ // decorated get methods
100
+ if (prop.getterName != null) {
101
+ const visible = !prop.hidden && entity[prop.getterName] instanceof Function;
102
+ const populated = root.isMarkedAsPopulated(meta.class, prop.name);
103
+ if (visible) {
104
+ ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
105
+ }
99
106
  }
100
- }
101
- else {
102
- // decorated getters
103
- const visible = !prop.hidden && typeof entity[prop.name] !== 'undefined';
104
- const populated = root.isMarkedAsPopulated(meta.class, prop.name);
105
- if (visible) {
106
- ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.name, entity, raw, populated);
107
+ else {
108
+ // decorated getters
109
+ const visible = !prop.hidden && typeof entity[prop.name] !== 'undefined';
110
+ const populated = root.isMarkedAsPopulated(meta.class, prop.name);
111
+ if (visible) {
112
+ ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.name, entity, raw, populated);
113
+ }
107
114
  }
108
115
  }
116
+ return ret;
109
117
  }
110
- if (contextCreated) {
111
- root.close();
118
+ finally {
119
+ if (contextCreated) {
120
+ root.close();
121
+ }
112
122
  }
113
- return ret;
114
123
  }
115
124
  static propertyName(meta, prop, raw) {
116
125
  if (raw) {
@@ -141,7 +141,9 @@ export class UnitOfWork {
141
141
  wrapped.__loadedProperties.add(key);
142
142
  }
143
143
  }
144
- wrapped.__originalEntityData = data;
144
+ // a partial refresh must keep the snapshot of properties it did not reload
145
+ wrapped.__originalEntityData =
146
+ options?.refresh && wrapped.__originalEntityData ? { ...wrapped.__originalEntityData, ...data } : data;
145
147
  }
146
148
  return entity;
147
149
  }
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.9-dev.1';
156
+ static #ORM_VERSION = '7.2.9-dev.11';
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}`;