@cheetah.js/orm 0.2.1 → 0.2.2

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.
@@ -16,6 +16,7 @@ class IdentityMapIntegration {
16
16
  return cached;
17
17
  }
18
18
  const instance = factory();
19
+ identityMap.setByKey(model, primaryKey, instance);
19
20
  return instance;
20
21
  }
21
22
  static registerEntity(entity) {
@@ -4,6 +4,7 @@ export declare class IdentityMap {
4
4
  constructor();
5
5
  get<T>(entityClass: Function, pk: any): T | undefined;
6
6
  set<T extends object>(entity: T): void;
7
+ setByKey<T extends object>(entityClass: Function, pk: any, entity: T): void;
7
8
  has(entityClass: Function, pk: any): boolean;
8
9
  remove(entityClass: Function, pk: any): void;
9
10
  clear(): void;
@@ -16,6 +16,10 @@ class IdentityMap {
16
16
  const key = this.keyGenerator.generateForEntity(entity);
17
17
  this.registry.set(key, entity);
18
18
  }
19
+ setByKey(entityClass, pk, entity) {
20
+ const key = this.keyGenerator.generate(entityClass, pk);
21
+ this.registry.set(key, entity);
22
+ }
19
23
  has(entityClass, pk) {
20
24
  const key = this.keyGenerator.generate(entityClass, pk);
21
25
  return this.registry.has(key);
@@ -9,41 +9,55 @@ class ModelTransformer {
9
9
  this.entityStorage = entityStorage;
10
10
  }
11
11
  transform(model, statement, data) {
12
- const instanceMap = this.createInstances(model, statement, data);
12
+ const { instanceMap, cachedAliases } = this.createInstances(model, statement, data);
13
13
  const optionsMap = this.buildOptionsMap(instanceMap);
14
- this.populateProperties(data, instanceMap, optionsMap);
14
+ this.populateProperties(data, instanceMap, optionsMap, cachedAliases);
15
15
  this.linkJoinedEntities(statement, instanceMap, optionsMap);
16
- this.resetChangedValues(instanceMap);
17
- this.registerInstancesInIdentityMap(instanceMap);
16
+ this.resetChangedValues(instanceMap, cachedAliases);
17
+ this.registerInstancesInIdentityMap(instanceMap, cachedAliases);
18
18
  return instanceMap[statement.alias];
19
19
  }
20
- registerInstancesInIdentityMap(instanceMap) {
21
- Object.values(instanceMap).forEach(instance => {
20
+ registerInstancesInIdentityMap(instanceMap, cachedAliases) {
21
+ Object.entries(instanceMap).forEach(([alias, instance]) => {
22
+ // Skip registering entities that were already in cache
23
+ if (cachedAliases.has(alias)) {
24
+ return;
25
+ }
22
26
  identity_map_1.IdentityMapIntegration.registerEntity(instance);
23
27
  });
24
28
  }
25
29
  createInstances(model, statement, data) {
30
+ const cachedAliases = new Set();
26
31
  const primaryKey = this.extractPrimaryKeyFromData(model, statement.alias, data);
27
- const instance = this.createInstance(model, primaryKey);
32
+ const { instance, wasCached } = this.createInstance(model, primaryKey);
33
+ if (wasCached) {
34
+ cachedAliases.add(statement.alias);
35
+ }
28
36
  const instanceMap = {
29
37
  [statement.alias]: instance,
30
38
  };
31
39
  if (statement.join) {
32
- this.addJoinedInstances(statement, instanceMap, data);
40
+ this.addJoinedInstances(statement, instanceMap, data, cachedAliases);
33
41
  }
34
- return instanceMap;
42
+ return { instanceMap, cachedAliases };
35
43
  }
36
44
  createInstance(model, primaryKey) {
37
- return identity_map_1.IdentityMapIntegration.getOrCreateInstance(model, primaryKey, () => {
38
- const instance = new model();
39
- instance.$_isPersisted = true;
40
- return instance;
41
- });
45
+ const cached = identity_map_1.IdentityMapIntegration.getEntity(model, primaryKey);
46
+ if (cached) {
47
+ return { instance: cached, wasCached: true };
48
+ }
49
+ const instance = new model();
50
+ instance.$_isPersisted = true;
51
+ // Note: Registration happens later in registerInstancesInIdentityMap after properties are populated
52
+ return { instance, wasCached: false };
42
53
  }
43
- addJoinedInstances(statement, instanceMap, data) {
54
+ addJoinedInstances(statement, instanceMap, data, cachedAliases) {
44
55
  statement.join.forEach(join => {
45
56
  const primaryKey = this.extractPrimaryKeyFromData(join.joinEntity, join.joinAlias, data);
46
- const joinInstance = this.createInstance(join.joinEntity, primaryKey);
57
+ const { instance: joinInstance, wasCached } = this.createInstance(join.joinEntity, primaryKey);
58
+ if (wasCached) {
59
+ cachedAliases.add(join.joinAlias);
60
+ }
47
61
  instanceMap[join.joinAlias] = joinInstance;
48
62
  });
49
63
  }
@@ -84,13 +98,17 @@ class ModelTransformer {
84
98
  }
85
99
  return optionsMap;
86
100
  }
87
- populateProperties(data, instanceMap, optionsMap) {
101
+ populateProperties(data, instanceMap, optionsMap, cachedAliases) {
88
102
  Object.entries(data).forEach(([key, value]) => {
89
103
  const { alias, propertyName } = this.parseColumnKey(key);
90
104
  const entity = instanceMap[alias];
91
105
  if (!entity) {
92
106
  return;
93
107
  }
108
+ // Skip populating properties for cached entities to preserve in-memory changes
109
+ if (cachedAliases.has(alias)) {
110
+ return;
111
+ }
94
112
  this.setPropertyValue(entity, propertyName, value, optionsMap.get(alias));
95
113
  });
96
114
  }
@@ -163,8 +181,12 @@ class ModelTransformer {
163
181
  appendToArray(existingArray, newItem) {
164
182
  return existingArray ? [...existingArray, newItem] : [newItem];
165
183
  }
166
- resetChangedValues(instanceMap) {
167
- Object.values(instanceMap).forEach(instance => {
184
+ resetChangedValues(instanceMap, cachedAliases) {
185
+ Object.entries(instanceMap).forEach(([alias, instance]) => {
186
+ // Skip resetting changed values for cached entities to preserve in-memory changes
187
+ if (cachedAliases.has(alias)) {
188
+ return;
189
+ }
168
190
  const currentValues = {};
169
191
  for (const key in instance) {
170
192
  if (!key.startsWith('_') && !key.startsWith('$')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "A simple ORM for Cheetah.js.",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",
@@ -55,5 +55,5 @@
55
55
  "bun",
56
56
  "value-object"
57
57
  ],
58
- "gitHead": "a46701aa7b6e513992090e6d8045918288eab16e"
58
+ "gitHead": "7e01e4a49e89040af0c81bd3e5d1bd257fbe3e2a"
59
59
  }