@stonyx/orm 0.3.2-alpha.3 → 0.3.2-alpha.30

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.
Files changed (44) hide show
  1. package/README.md +35 -2
  2. package/config/environment.js +8 -0
  3. package/dist/commands.js +34 -0
  4. package/dist/dynamodb/connection.d.ts +31 -0
  5. package/dist/dynamodb/connection.js +28 -0
  6. package/dist/dynamodb/dynamodb-db.d.ts +132 -0
  7. package/dist/dynamodb/dynamodb-db.js +586 -0
  8. package/dist/dynamodb/operation-builder.d.ts +76 -0
  9. package/dist/dynamodb/operation-builder.js +116 -0
  10. package/dist/dynamodb/type-map.d.ts +31 -0
  11. package/dist/dynamodb/type-map.js +48 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/main.d.ts +16 -0
  14. package/dist/main.js +36 -0
  15. package/dist/manage-record.js +51 -8
  16. package/dist/mysql/mysql-db.d.ts +1 -0
  17. package/dist/mysql/mysql-db.js +4 -2
  18. package/dist/orm-request.js +4 -3
  19. package/dist/postgres/connection.d.ts +1 -0
  20. package/dist/postgres/connection.js +8 -6
  21. package/dist/postgres/postgres-db.js +4 -2
  22. package/dist/relationships.js +1 -1
  23. package/dist/serializer.js +38 -2
  24. package/dist/store.d.ts +10 -0
  25. package/dist/store.js +66 -2
  26. package/dist/types/orm-types.d.ts +9 -0
  27. package/package.json +17 -7
  28. package/src/commands.ts +43 -0
  29. package/src/dynamodb/connection.ts +50 -0
  30. package/src/dynamodb/dynamodb-db.ts +801 -0
  31. package/src/dynamodb/operation-builder.ts +202 -0
  32. package/src/dynamodb/type-map.ts +54 -0
  33. package/src/index.ts +1 -0
  34. package/src/main.ts +45 -0
  35. package/src/manage-record.ts +54 -9
  36. package/src/mysql/mysql-db.ts +5 -2
  37. package/src/orm-request.ts +5 -2
  38. package/src/postgres/connection.ts +10 -6
  39. package/src/postgres/postgres-db.ts +4 -2
  40. package/src/relationships.ts +1 -1
  41. package/src/serializer.ts +39 -2
  42. package/src/store.ts +69 -2
  43. package/src/types/orm-types.ts +10 -0
  44. package/src/types/stonyx.d.ts +7 -1
package/src/store.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import Orm, { relationships } from '@stonyx/orm';
2
- import { TYPES, getHasManyRegistry, getBelongsToRegistry, getPendingRegistry } from './relationships.js';
2
+ import { TYPES, getHasManyRegistry, getBelongsToRegistry, getPendingRegistry, getPendingBelongsToRegistry } from './relationships.js';
3
3
  import ViewResolver from './view-resolver.js';
4
4
 
5
5
  interface UnloadOptions {
@@ -179,7 +179,12 @@ export default class Store {
179
179
  // Auto-persist delete to SQL
180
180
  if (id && Orm.instance?.sqlDb) {
181
181
  Orm.instance.sqlDb.persist('delete', key, { recordId: id }, {}).catch((err: unknown) => {
182
- console.error(`[ORM] Failed to persist delete for ${key}:${id}`, err);
182
+ Orm.instance.emitPersistError({
183
+ operation: 'delete',
184
+ modelName: key,
185
+ recordId: id,
186
+ error: err instanceof Error ? err : new Error(String(err)),
187
+ });
183
188
  });
184
189
  }
185
190
 
@@ -188,6 +193,44 @@ export default class Store {
188
193
  this.unloadAllRecords(key);
189
194
  }
190
195
 
196
+ /**
197
+ * Evict a record from the store with full relationship registry cleanup,
198
+ * WITHOUT calling record.clean(). This preserves the caller's reference
199
+ * to the returned record (used by memory:false post-persist eviction).
200
+ *
201
+ * @param registryId - The ID used when the record's relationships were
202
+ * registered. For SQL models with pending IDs, this is the original
203
+ * negative pending ID (before the adapter re-keyed to the real DB ID).
204
+ */
205
+ evictRecord(modelName: string, id: unknown, registryId?: unknown): void {
206
+ const modelStore = this.data.get(modelName);
207
+ if (!modelStore) return;
208
+
209
+ if (typeof id !== 'string' && typeof id !== 'number') return;
210
+ const raw = modelStore.get(id);
211
+ if (!raw || !isStoreRecord(raw)) return;
212
+
213
+ const visited = new Set([`${modelName}:${id}`]);
214
+
215
+ // Remove from hasMany arrays and nullify belongsTo references using current ID
216
+ // (the adapter updates record.id, so value-based matches need the current ID)
217
+ this._removeFromHasManyArrays(modelName, id, visited);
218
+ this._nullifyBelongsToReferences(modelName, id, visited);
219
+
220
+ // Clean up relationship registry entries using the registry key
221
+ // (belongsTo/hasMany registries were keyed by the ID at registration time,
222
+ // which may differ from the current ID if SQL persist re-keyed the record)
223
+ const cleanupId = registryId ?? id;
224
+ this._cleanupRelationshipRegistries(modelName, cleanupId);
225
+
226
+ // If registryId differs from id, also clean with current id as safety net
227
+ if (registryId !== undefined && registryId !== id) {
228
+ this._cleanupRelationshipRegistries(modelName, id);
229
+ }
230
+
231
+ modelStore.delete(id);
232
+ }
233
+
191
234
  unloadRecord(model: string, id: unknown, options: UnloadOptions = {}): void {
192
235
  const modelStore = this.data.get(model);
193
236
 
@@ -303,6 +346,30 @@ export default class Store {
303
346
 
304
347
  const pendingMap = getPendingRegistry().get(modelName);
305
348
  if (pendingMap) pendingMap.delete(recordId);
349
+
350
+ // Clean pendingBelongsTo entries in both directions
351
+ const pendingBelongsToMap = getPendingBelongsToRegistry();
352
+ if (pendingBelongsToMap) {
353
+ // Direction 1: evicted record was the TARGET others were waiting for
354
+ const targetEntries = pendingBelongsToMap.get(modelName);
355
+ if (targetEntries) targetEntries.delete(recordId);
356
+
357
+ // Direction 2: evicted record was the SOURCE with unresolved forward-references
358
+ for (const [, targetIdMap] of pendingBelongsToMap) {
359
+ for (const [targetId, entries] of targetIdMap) {
360
+ if (!Array.isArray(entries)) continue;
361
+ const filtered = entries.filter((e: unknown) => {
362
+ const entry = e as { sourceModelName?: string; relationshipId?: unknown };
363
+ return !(entry.sourceModelName === modelName && entry.relationshipId === recordId);
364
+ });
365
+ if (filtered.length === 0) {
366
+ targetIdMap.delete(targetId);
367
+ } else if (filtered.length < entries.length) {
368
+ targetIdMap.set(targetId, filtered);
369
+ }
370
+ }
371
+ }
372
+ }
306
373
  }
307
374
 
308
375
  /**
@@ -48,6 +48,13 @@ export interface OrmRestServerConfig {
48
48
  metaRoute: boolean;
49
49
  }
50
50
 
51
+ export interface OrmDynamoDBConfig {
52
+ region?: string;
53
+ endpoint?: string;
54
+ tablePrefix?: string;
55
+ [key: string]: unknown;
56
+ }
57
+
51
58
  export interface OrmSection {
52
59
  db: OrmDbConfig;
53
60
  paths: OrmPaths;
@@ -55,6 +62,9 @@ export interface OrmSection {
55
62
  mysql?: OrmMysqlConfig;
56
63
  postgres?: OrmPostgresConfig;
57
64
  timescale?: OrmPostgresConfig;
65
+ dynamodb?: OrmDynamoDBConfig;
66
+ logColor?: string;
67
+ logMethod?: string;
58
68
  [key: string]: unknown;
59
69
  }
60
70
 
@@ -5,7 +5,13 @@ declare module 'stonyx/config' {
5
5
  }
6
6
 
7
7
  declare module 'stonyx/log' {
8
- const log: Record<string, ((...args: unknown[]) => void) | undefined>;
8
+ interface Log {
9
+ db(message: string): void;
10
+ error(message: string, ...args: unknown[]): void;
11
+ defineType(type: string, setting: string, options?: Record<string, unknown> | null): void;
12
+ [key: string]: ((...args: unknown[]) => void) | undefined;
13
+ }
14
+ const log: Log;
9
15
  export default log;
10
16
  }
11
17