@stonyx/orm 0.3.2-beta.16 → 0.3.2-beta.160

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 (66) hide show
  1. package/README.md +1409 -11
  2. package/config/environment.js +99 -12
  3. package/dist/access-verdict.d.ts +85 -0
  4. package/dist/access-verdict.js +284 -0
  5. package/dist/commands.js +34 -0
  6. package/dist/dynamodb/connection.d.ts +31 -0
  7. package/dist/dynamodb/connection.js +28 -0
  8. package/dist/dynamodb/dynamodb-db.d.ts +142 -0
  9. package/dist/dynamodb/dynamodb-db.js +596 -0
  10. package/dist/dynamodb/operation-builder.d.ts +76 -0
  11. package/dist/dynamodb/operation-builder.js +116 -0
  12. package/dist/dynamodb/type-map.d.ts +31 -0
  13. package/dist/dynamodb/type-map.js +48 -0
  14. package/dist/hooks.d.ts +15 -1
  15. package/dist/index.d.ts +3 -0
  16. package/dist/index.js +8 -0
  17. package/dist/main.d.ts +116 -0
  18. package/dist/main.js +129 -0
  19. package/dist/manage-record.js +268 -12
  20. package/dist/mysql/connection.d.ts +1 -0
  21. package/dist/mysql/mysql-db.d.ts +8 -0
  22. package/dist/mysql/mysql-db.js +44 -10
  23. package/dist/orm-request.d.ts +274 -3
  24. package/dist/orm-request.js +1259 -65
  25. package/dist/postgres/connection.d.ts +1 -0
  26. package/dist/postgres/connection.js +8 -6
  27. package/dist/postgres/postgres-db.d.ts +8 -0
  28. package/dist/postgres/postgres-db.js +44 -10
  29. package/dist/record.d.ts +16 -0
  30. package/dist/record.js +154 -6
  31. package/dist/relationships.js +1 -1
  32. package/dist/serializer.js +38 -2
  33. package/dist/setup-rest-server.js +51 -5
  34. package/dist/standalone-db.js +17 -5
  35. package/dist/store.d.ts +13 -1
  36. package/dist/store.js +65 -6
  37. package/dist/types/orm-types.d.ts +260 -0
  38. package/dist/utils.d.ts +44 -0
  39. package/dist/utils.js +47 -0
  40. package/package.json +16 -7
  41. package/src/access-verdict.ts +312 -0
  42. package/src/commands.ts +43 -0
  43. package/src/dynamodb/connection.ts +50 -0
  44. package/src/dynamodb/dynamodb-db.ts +811 -0
  45. package/src/dynamodb/operation-builder.ts +202 -0
  46. package/src/dynamodb/type-map.ts +54 -0
  47. package/src/hooks.ts +15 -1
  48. package/src/index.ts +10 -0
  49. package/src/main.ts +133 -0
  50. package/src/manage-record.ts +294 -18
  51. package/src/mysql/connection.ts +1 -0
  52. package/src/mysql/mysql-db.ts +44 -12
  53. package/src/orm-request.ts +1281 -67
  54. package/src/postgres/connection.ts +10 -6
  55. package/src/postgres/postgres-db.ts +44 -12
  56. package/src/record.ts +182 -6
  57. package/src/relationships.ts +1 -1
  58. package/src/serializer.ts +39 -2
  59. package/src/setup-rest-server.ts +59 -6
  60. package/src/standalone-db.ts +17 -6
  61. package/src/store.ts +68 -6
  62. package/src/types/orm-types.ts +268 -1
  63. package/src/types/stonyx-rest-server.d.ts +14 -1
  64. package/src/types/stonyx.d.ts +7 -1
  65. package/src/utils.ts +50 -0
  66. package/config/environment.ts +0 -91
@@ -0,0 +1,142 @@
1
+ /**
2
+ * DynamoDB driver implementing the SqlDb PAL contract.
3
+ *
4
+ * Drop-in replacement for PostgresDB / MysqlDB — zero ORM core changes.
5
+ * Selected via config.orm.dynamodb.
6
+ */
7
+ import { createDocumentClient, destroyDocumentClient } from './connection.js';
8
+ import type { DocumentClient, DynamoDBConfig } from './connection.js';
9
+ import { buildPutItem, buildGetItem, buildUpdateItem, buildDeleteItem, buildScan, buildQuery } from './operation-builder.js';
10
+ import { introspectModels, getTopologicalOrder } from '../postgres/schema-introspector.js';
11
+ import { getDynamoKeyType } from './type-map.js';
12
+ import { store } from '@stonyx/orm';
13
+ import { createRecord } from '../manage-record.js';
14
+ import { getPluralName } from '../plural-registry.js';
15
+ import config from 'stonyx/config';
16
+ import log from 'stonyx/log';
17
+ import type { OrmRecord } from '../types/orm-types.js';
18
+ /**
19
+ * Load the DynamoDB DocumentClient command constructors via dynamic import.
20
+ * Returns a frozen object so it can be cached in deps.
21
+ */
22
+ export declare function loadDocClientCommands(): Promise<{
23
+ PutCommand: new (params: unknown) => unknown;
24
+ GetCommand: new (params: unknown) => unknown;
25
+ UpdateCommand: new (params: unknown) => unknown;
26
+ DeleteCommand: new (params: unknown) => unknown;
27
+ ScanCommand: new (params: unknown) => unknown;
28
+ QueryCommand: new (params: unknown) => unknown;
29
+ }>;
30
+ export declare function loadTableCommands(): Promise<{
31
+ DynamoDBClient: new (opts: unknown) => {
32
+ send(cmd: unknown): Promise<unknown>;
33
+ };
34
+ DescribeTableCommand: new (params: unknown) => unknown;
35
+ CreateTableCommand: new (params: unknown) => unknown;
36
+ UpdateTableCommand: new (params: unknown) => unknown;
37
+ }>;
38
+ interface PersistContext {
39
+ record?: OrmRecord;
40
+ recordId?: unknown;
41
+ oldState?: Record<string, unknown>;
42
+ rawData?: Record<string, unknown>;
43
+ }
44
+ interface PersistResponse {
45
+ data?: {
46
+ id?: unknown;
47
+ };
48
+ }
49
+ /** Minimal Orm module shape needed at runtime — avoids circular import at top-level. */
50
+ interface OrmModule {
51
+ default: {
52
+ instance: {
53
+ getRecordClasses(name: string): {
54
+ modelClass: {
55
+ memory?: boolean;
56
+ };
57
+ };
58
+ isView?(name: string): boolean;
59
+ };
60
+ };
61
+ }
62
+ export interface DynamoDBDeps {
63
+ createDocumentClient: typeof createDocumentClient;
64
+ destroyDocumentClient: typeof destroyDocumentClient;
65
+ loadDocClientCommands: typeof loadDocClientCommands;
66
+ loadTableCommands: typeof loadTableCommands;
67
+ buildPutItem: typeof buildPutItem;
68
+ buildGetItem: typeof buildGetItem;
69
+ buildUpdateItem: typeof buildUpdateItem;
70
+ buildDeleteItem: typeof buildDeleteItem;
71
+ buildScan: typeof buildScan;
72
+ buildQuery: typeof buildQuery;
73
+ introspectModels: typeof introspectModels;
74
+ getTopologicalOrder: typeof getTopologicalOrder;
75
+ getDynamoKeyType: typeof getDynamoKeyType;
76
+ createRecord: typeof createRecord;
77
+ store: typeof store;
78
+ getPluralName: typeof getPluralName;
79
+ config: typeof config;
80
+ log: typeof log;
81
+ /** Injected for testing — import('@stonyx/orm') replacement */
82
+ _importOrm?: () => Promise<OrmModule>;
83
+ [key: string]: unknown;
84
+ }
85
+ export default class DynamoDBDB {
86
+ static instance: DynamoDBDB | undefined;
87
+ deps: DynamoDBDeps;
88
+ client: DocumentClient | null;
89
+ dbConfig: DynamoDBConfig;
90
+ /** GSI registry built during init from model introspection. */
91
+ private _gsiRegistry;
92
+ constructor(deps?: Partial<DynamoDBDeps>);
93
+ private requireClient;
94
+ private _resolveTableName;
95
+ /** Resolve Orm singleton — falls back to real import in production. */
96
+ private _getOrm;
97
+ init(): Promise<void>;
98
+ /**
99
+ * For each model, DescribeTable — CreateTable if missing (with GSIs, PAY_PER_REQUEST).
100
+ * For existing tables, check for missing GSIs and UpdateTable + poll for ACTIVE.
101
+ */
102
+ startup(): Promise<void>;
103
+ shutdown(): Promise<void>;
104
+ /**
105
+ * DynamoDB does NOT use write serialization (#156).
106
+ *
107
+ * Unlike MySQL/PostgreSQL, DynamoDB has no server-side foreign key
108
+ * constraints and no multi-row transactions in standard single-item
109
+ * operations (PutItem, UpdateItem, DeleteItem). Each operation is
110
+ * atomic at the item level and cannot deadlock against other items.
111
+ * Concurrent fire-and-forget writes therefore cannot produce the
112
+ * cross-row lock contention that causes InnoDB/PG deadlocks.
113
+ */
114
+ persist(operation: string, modelName: string, context: PersistContext, response: PersistResponse): Promise<void>;
115
+ findRecord(modelName: string, id: unknown): Promise<OrmRecord | undefined>;
116
+ findAll(modelName: string, conditions?: Record<string, unknown>): Promise<OrmRecord[]>;
117
+ loadMemoryRecords(): Promise<void>;
118
+ private _persistCreate;
119
+ private _persistUpdate;
120
+ private _persistDelete;
121
+ private _paginatedScan;
122
+ private _paginatedQuery;
123
+ /**
124
+ * Build the GSI registry from model introspection.
125
+ * Registry: modelName → attrName → gsiName
126
+ *
127
+ * FK columns (belonging to belongsTo relationships) get a GSI automatically.
128
+ */
129
+ private _buildGsiRegistry;
130
+ /**
131
+ * Find a GSI that can serve the given conditions.
132
+ */
133
+ private _findGsiMatch;
134
+ private _buildAttributeDefinitions;
135
+ private _buildGsiDefinitions;
136
+ private _waitForTableActive;
137
+ private _itemToRawData;
138
+ private _recordToItem;
139
+ private _evictIfNotMemory;
140
+ loadAllRecords(): Promise<void>;
141
+ }
142
+ export {};