@stonyx/orm 0.2.1-beta.85 → 0.2.1-beta.86

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.
@@ -23,8 +23,11 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
23
23
  throw new Error(`Model store for '${modelName}' is not registered. Ensure the model is defined before creating records.`);
24
24
  assignRecordId(modelName, rawData);
25
25
  const existingRecord = modelStore.get(rawData.id);
26
- if (existingRecord)
26
+ if (existingRecord) {
27
+ // Update the existing record with new data so the last entry wins
28
+ updateRecord(existingRecord, rawData, { ...options, update: true });
27
29
  return existingRecord;
30
+ }
28
31
  const recordClasses = orm.getRecordClasses(modelName);
29
32
  const modelClass = recordClasses.modelClass;
30
33
  const serializerClass = recordClasses.serializerClass;
package/dist/record.js CHANGED
@@ -39,9 +39,23 @@ export default class Record {
39
39
  const { __data: data } = this;
40
40
  const records = {};
41
41
  for (const [key, childRecord] of Object.entries(this.__relationships)) {
42
- records[key] = Array.isArray(childRecord)
43
- ? childRecord.map((r) => r.serialize())
44
- : childRecord?.serialize() ?? null;
42
+ if (Array.isArray(childRecord)) {
43
+ // Deduplicate by record ID — keep last occurrence (latest data wins)
44
+ const seen = new Set();
45
+ const unique = [];
46
+ for (let i = childRecord.length - 1; i >= 0; i--) {
47
+ const r = childRecord[i];
48
+ if (!seen.has(r.id)) {
49
+ seen.add(r.id);
50
+ unique.push(r);
51
+ }
52
+ }
53
+ unique.reverse();
54
+ records[key] = unique.map((r) => r.serialize());
55
+ }
56
+ else {
57
+ records[key] = childRecord?.serialize() ?? null;
58
+ }
45
59
  }
46
60
  return { ...data, ...records };
47
61
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.2.1-beta.85",
7
+ "version": "0.2.1-beta.86",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
@@ -44,7 +44,12 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
44
44
 
45
45
  assignRecordId(modelName, rawData);
46
46
  const existingRecord = modelStore.get(rawData.id as number | string);
47
- if (existingRecord) return existingRecord as OrmRecord;
47
+
48
+ if (existingRecord) {
49
+ // Update the existing record with new data so the last entry wins
50
+ updateRecord(existingRecord as OrmRecord, rawData, { ...options, update: true });
51
+ return existingRecord as OrmRecord;
52
+ }
48
53
 
49
54
  const recordClasses = orm.getRecordClasses(modelName);
50
55
  const modelClass = recordClasses.modelClass as (new (name: string) => { __name: string; [key: string]: unknown }) | undefined;
package/src/record.ts CHANGED
@@ -86,9 +86,24 @@ export default class Record {
86
86
  const records: { [key: string]: unknown } = {};
87
87
 
88
88
  for (const [key, childRecord] of Object.entries(this.__relationships)) {
89
- records[key] = Array.isArray(childRecord)
90
- ? childRecord.map((r: Record) => r.serialize())
91
- : (childRecord as Record)?.serialize() ?? null;
89
+ if (Array.isArray(childRecord)) {
90
+ // Deduplicate by record ID — keep last occurrence (latest data wins)
91
+ const seen = new Set<unknown>();
92
+ const unique: Record[] = [];
93
+
94
+ for (let i = childRecord.length - 1; i >= 0; i--) {
95
+ const r = childRecord[i] as Record;
96
+ if (!seen.has(r.id)) {
97
+ seen.add(r.id);
98
+ unique.push(r);
99
+ }
100
+ }
101
+
102
+ unique.reverse();
103
+ records[key] = unique.map((r: Record) => r.serialize());
104
+ } else {
105
+ records[key] = (childRecord as Record)?.serialize() ?? null;
106
+ }
92
107
  }
93
108
 
94
109
  return { ...data, ...records };