openchs-models 1.33.6 → 1.33.8
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.
|
@@ -13,19 +13,20 @@ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object
|
|
|
13
13
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
14
14
|
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
15
15
|
/**
|
|
16
|
-
* Framework-level handler for
|
|
17
|
-
* This automatically processes
|
|
16
|
+
* Framework-level handler for nested object safety in Realm 12+
|
|
17
|
+
* This automatically processes nested objects (relationships) to prevent invalidation issues
|
|
18
|
+
* Note: This handles ALL nested objects, not just Realm's embedded object feature
|
|
18
19
|
*/
|
|
19
|
-
class
|
|
20
|
+
class RealmNestedObjectHandler {
|
|
20
21
|
/**
|
|
21
|
-
* Automatically processes
|
|
22
|
+
* Automatically processes nested objects in a data structure
|
|
22
23
|
* This should be called by RealmProxy.create() before creating objects
|
|
23
24
|
*
|
|
24
25
|
* @param {Object} data - The object data to process
|
|
25
26
|
* @param {Object} schema - The Realm schema for the object
|
|
26
|
-
* @returns {Object} Processed data with safe
|
|
27
|
+
* @returns {Object} Processed data with safe nested objects
|
|
27
28
|
*/
|
|
28
|
-
static
|
|
29
|
+
static processNestedObjects(data, schema) {
|
|
29
30
|
if (!data || !schema || !schema.properties) {
|
|
30
31
|
return data || {};
|
|
31
32
|
}
|
|
@@ -34,23 +35,23 @@ class RealmEmbeddedObjectHandler {
|
|
|
34
35
|
const propertySchema = schema.properties[propertyName];
|
|
35
36
|
const propertyValue = data[propertyName];
|
|
36
37
|
|
|
37
|
-
// Handle
|
|
38
|
-
if (this.
|
|
39
|
-
processedData[propertyName] = this.
|
|
38
|
+
// Handle nested objects (including optional ones)
|
|
39
|
+
if (this.isNestedObjectProperty(propertySchema) && propertyValue) {
|
|
40
|
+
processedData[propertyName] = this.safeCopyNestedObject(propertyValue);
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
// Handle lists with
|
|
43
|
-
if (this.
|
|
44
|
-
processedData[propertyName] = propertyValue.map(item => item ? this.
|
|
43
|
+
// Handle lists with nested objects
|
|
44
|
+
if (this.isListOfNestedObjects(propertySchema) && Array.isArray(propertyValue)) {
|
|
45
|
+
processedData[propertyName] = propertyValue.map(item => item ? this.safeCopyNestedObject(item) : item);
|
|
45
46
|
}
|
|
46
47
|
});
|
|
47
48
|
return processedData;
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
/**
|
|
51
|
-
* Checks if a property schema defines
|
|
52
|
+
* Checks if a property schema defines a nested object (relationship)
|
|
52
53
|
*/
|
|
53
|
-
static
|
|
54
|
+
static isNestedObjectProperty(propertySchema) {
|
|
54
55
|
if (!propertySchema || propertySchema.type !== 'object') {
|
|
55
56
|
return false;
|
|
56
57
|
}
|
|
@@ -58,53 +59,53 @@ class RealmEmbeddedObjectHandler {
|
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
/**
|
|
61
|
-
* Checks if a property schema defines a list of
|
|
62
|
+
* Checks if a property schema defines a list of nested objects
|
|
62
63
|
*/
|
|
63
|
-
static
|
|
64
|
+
static isListOfNestedObjects(propertySchema) {
|
|
64
65
|
return propertySchema && propertySchema.type === 'list' && propertySchema.objectType && propertySchema.objectType !== 'string';
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
/**
|
|
68
|
-
* Safely copies
|
|
69
|
+
* Safely copies a nested object using the best available method
|
|
69
70
|
*/
|
|
70
|
-
static
|
|
71
|
-
if (!
|
|
71
|
+
static safeCopyNestedObject(nestedObj) {
|
|
72
|
+
if (!nestedObj) {
|
|
72
73
|
return null;
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
// If it's not a Realm object, return as-is
|
|
76
|
-
if (!(0, _RealmCollectionHelper.isRealmObject)(
|
|
77
|
-
return
|
|
77
|
+
if (!(0, _RealmCollectionHelper.isRealmObject)(nestedObj)) {
|
|
78
|
+
return nestedObj;
|
|
78
79
|
}
|
|
79
80
|
try {
|
|
80
81
|
// Method 1: Try toJSON() (convenient)
|
|
81
|
-
if (typeof
|
|
82
|
-
return
|
|
82
|
+
if (typeof nestedObj.toJSON === 'function') {
|
|
83
|
+
return nestedObj.toJSON();
|
|
83
84
|
}
|
|
84
85
|
} catch (error) {
|
|
85
86
|
// Fall back to manual copy if toJSON fails
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
// Method 2: Manual deep copy (reliable)
|
|
89
|
-
return this.
|
|
90
|
+
return this.deepCopyNestedObject(nestedObj);
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
/**
|
|
93
|
-
* Manual deep copy of
|
|
94
|
+
* Manual deep copy of nested object
|
|
94
95
|
*/
|
|
95
|
-
static
|
|
96
|
-
if (!
|
|
96
|
+
static deepCopyNestedObject(nestedObj) {
|
|
97
|
+
if (!nestedObj) {
|
|
97
98
|
return null;
|
|
98
99
|
}
|
|
99
100
|
const plainCopy = {};
|
|
100
101
|
|
|
101
102
|
// Try to get schema properties
|
|
102
103
|
try {
|
|
103
|
-
if (
|
|
104
|
-
const schema =
|
|
104
|
+
if (nestedObj.objectSchema && typeof nestedObj.objectSchema === 'function') {
|
|
105
|
+
const schema = nestedObj.objectSchema();
|
|
105
106
|
if (schema && schema.properties) {
|
|
106
107
|
Object.keys(schema.properties).forEach(prop => {
|
|
107
|
-
plainCopy[prop] =
|
|
108
|
+
plainCopy[prop] = nestedObj[prop];
|
|
108
109
|
});
|
|
109
110
|
return plainCopy;
|
|
110
111
|
}
|
|
@@ -114,13 +115,13 @@ class RealmEmbeddedObjectHandler {
|
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
// Fallback: Copy all enumerable properties
|
|
117
|
-
Object.keys(
|
|
118
|
-
if (typeof
|
|
119
|
-
plainCopy[prop] =
|
|
118
|
+
Object.keys(nestedObj).forEach(prop => {
|
|
119
|
+
if (typeof nestedObj[prop] !== 'function') {
|
|
120
|
+
plainCopy[prop] = nestedObj[prop];
|
|
120
121
|
}
|
|
121
122
|
});
|
|
122
123
|
return plainCopy;
|
|
123
124
|
}
|
|
124
125
|
}
|
|
125
|
-
var _default =
|
|
126
|
+
var _default = RealmNestedObjectHandler;
|
|
126
127
|
exports.default = _default;
|
|
@@ -8,7 +8,7 @@ var _RealmResultsProxy = _interopRequireDefault(require("./RealmResultsProxy"));
|
|
|
8
8
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
9
9
|
var _RealmCollectionHelper = require("./RealmCollectionHelper");
|
|
10
10
|
var _General = _interopRequireDefault(require("../utility/General"));
|
|
11
|
-
var
|
|
11
|
+
var _RealmNestedObjectHandler = _interopRequireDefault(require("./RealmNestedObjectHandler"));
|
|
12
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
const isVanillaArray = function (object) {
|
|
14
14
|
return !_lodash.default.isNil(object) && "RealmListProxy" !== object.constructor.name && _lodash.default.isArrayLikeObject(object);
|
|
@@ -43,11 +43,12 @@ class RealmProxy {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @param
|
|
49
|
-
* @param
|
|
50
|
-
* @
|
|
46
|
+
* Creates a new entity in the database
|
|
47
|
+
*
|
|
48
|
+
* @param schemaName - The schema name of the entity to create
|
|
49
|
+
* @param object - The object data (can be entity wrapper or raw object)
|
|
50
|
+
* @param updateMode - Update mode: "never" (default), "modified", "all", or boolean equivalents
|
|
51
|
+
* @returns {*} - The created entity wrapped in its entity class
|
|
51
52
|
*/
|
|
52
53
|
create(schemaName, object, updateMode = "never") {
|
|
53
54
|
const entityClass = this.entityMappingConfig.getEntityClass(schemaName);
|
|
@@ -56,35 +57,11 @@ class RealmProxy {
|
|
|
56
57
|
// Extract the underlying data from entity wrapper if needed
|
|
57
58
|
const rawObject = (0, _RealmCollectionHelper.getUnderlyingRealmObject)(object) || object;
|
|
58
59
|
|
|
59
|
-
// 🚀 FRAMEWORK-LEVEL: Automatically process
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
const emptyMandatoryProperties = [];
|
|
63
|
-
const saveObjectKeys = Object.keys(processedObject);
|
|
64
|
-
if (updateMode === "never" || updateMode === false || _lodash.default.intersection(mandatoryObjectSchemaProperties, saveObjectKeys).length > 0) {
|
|
65
|
-
// Check for null/undefined mandatory properties that are present in the object
|
|
66
|
-
saveObjectKeys.forEach(x => {
|
|
67
|
-
const propertyValue = processedObject[x];
|
|
68
|
-
if (_lodash.default.isNil(propertyValue) && _lodash.default.some(mandatoryObjectSchemaProperties, y => y === x)) {
|
|
69
|
-
emptyMandatoryProperties.push(x);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
60
|
+
// 🚀 FRAMEWORK-LEVEL: Automatically process nested objects for Realm 12+ safety
|
|
61
|
+
// This is the only custom processing needed - Realm handles validation
|
|
62
|
+
const processedObject = _RealmNestedObjectHandler.default.processNestedObjects(rawObject, schema);
|
|
72
63
|
|
|
73
|
-
|
|
74
|
-
// or when we have some mandatory properties present (indicating intent to save a complete entity)
|
|
75
|
-
const isStrictUpdateMode = updateMode === "never" || updateMode === false;
|
|
76
|
-
const hasSomeMandatoryProperties = _lodash.default.intersection(mandatoryObjectSchemaProperties, saveObjectKeys).length > 0;
|
|
77
|
-
if (isStrictUpdateMode && hasSomeMandatoryProperties) {
|
|
78
|
-
mandatoryObjectSchemaProperties.forEach(mandatoryProp => {
|
|
79
|
-
if (!saveObjectKeys.includes(mandatoryProp)) {
|
|
80
|
-
emptyMandatoryProperties.push(mandatoryProp);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
if (emptyMandatoryProperties.length > 0) {
|
|
85
|
-
throw new Error(`${emptyMandatoryProperties.join(",")} are mandatory for ${schemaName}, Keys being saved - ${saveObjectKeys}. UUID: ${processedObject.uuid}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
64
|
+
// Let Realm handle all validation - it knows its schema better than we do
|
|
88
65
|
const dbEntity = this.realmDb.create(schemaName, processedObject, updateMode);
|
|
89
66
|
return new entityClass(dbEntity);
|
|
90
67
|
}
|
package/dist/index.js
CHANGED
|
@@ -315,10 +315,10 @@ Object.defineProperty(exports, "EntityMappingConfig", {
|
|
|
315
315
|
return _Schema.default;
|
|
316
316
|
}
|
|
317
317
|
});
|
|
318
|
-
Object.defineProperty(exports, "
|
|
318
|
+
Object.defineProperty(exports, "RealmNestedObjectHandler", {
|
|
319
319
|
enumerable: true,
|
|
320
320
|
get: function () {
|
|
321
|
-
return
|
|
321
|
+
return _RealmNestedObjectHandler.default;
|
|
322
322
|
}
|
|
323
323
|
});
|
|
324
324
|
Object.defineProperty(exports, "Settings", {
|
|
@@ -875,7 +875,7 @@ var _ReferenceEntity = _interopRequireDefault(require("./ReferenceEntity"));
|
|
|
875
875
|
var _RuleDependency = _interopRequireDefault(require("./RuleDependency"));
|
|
876
876
|
var _Rule = _interopRequireDefault(require("./Rule"));
|
|
877
877
|
var _Schema = _interopRequireDefault(require("./Schema"));
|
|
878
|
-
var
|
|
878
|
+
var _RealmNestedObjectHandler = _interopRequireDefault(require("./framework/RealmNestedObjectHandler"));
|
|
879
879
|
var _Settings = _interopRequireDefault(require("./Settings"));
|
|
880
880
|
var _SingleCodedValue = _interopRequireDefault(require("./observation/SingleCodedValue"));
|
|
881
881
|
var _SingleSelectFilter = _interopRequireDefault(require("./application/SingleSelectFilter"));
|