@stonyx/orm 0.2.5-alpha.0 → 0.3.1

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 (166) hide show
  1. package/README.md +482 -15
  2. package/config/environment.js +63 -6
  3. package/dist/aggregates.d.ts +21 -0
  4. package/dist/aggregates.js +93 -0
  5. package/dist/attr.d.ts +2 -0
  6. package/dist/attr.js +22 -0
  7. package/dist/belongs-to.d.ts +11 -0
  8. package/dist/belongs-to.js +59 -0
  9. package/dist/cli.d.ts +22 -0
  10. package/dist/cli.js +148 -0
  11. package/dist/commands.d.ts +7 -0
  12. package/dist/commands.js +146 -0
  13. package/dist/db.d.ts +21 -0
  14. package/dist/db.js +180 -0
  15. package/dist/exports/db.d.ts +7 -0
  16. package/{src → dist}/exports/db.js +2 -4
  17. package/dist/has-many.d.ts +11 -0
  18. package/dist/has-many.js +58 -0
  19. package/dist/hooks.d.ts +75 -0
  20. package/dist/hooks.js +110 -0
  21. package/dist/index.d.ts +14 -0
  22. package/dist/index.js +34 -0
  23. package/dist/main.d.ts +46 -0
  24. package/dist/main.js +181 -0
  25. package/dist/manage-record.d.ts +13 -0
  26. package/dist/manage-record.js +123 -0
  27. package/dist/meta-request.d.ts +6 -0
  28. package/dist/meta-request.js +52 -0
  29. package/dist/migrate.d.ts +2 -0
  30. package/dist/migrate.js +57 -0
  31. package/dist/model-property.d.ts +9 -0
  32. package/dist/model-property.js +29 -0
  33. package/dist/model.d.ts +15 -0
  34. package/dist/model.js +18 -0
  35. package/dist/mysql/connection.d.ts +14 -0
  36. package/dist/mysql/connection.js +24 -0
  37. package/dist/mysql/migration-generator.d.ts +45 -0
  38. package/dist/mysql/migration-generator.js +254 -0
  39. package/dist/mysql/migration-runner.d.ts +12 -0
  40. package/dist/mysql/migration-runner.js +88 -0
  41. package/dist/mysql/mysql-db.d.ts +100 -0
  42. package/dist/mysql/mysql-db.js +425 -0
  43. package/dist/mysql/query-builder.d.ts +10 -0
  44. package/dist/mysql/query-builder.js +44 -0
  45. package/dist/mysql/schema-introspector.d.ts +19 -0
  46. package/dist/mysql/schema-introspector.js +257 -0
  47. package/dist/mysql/type-map.d.ts +21 -0
  48. package/dist/mysql/type-map.js +36 -0
  49. package/dist/orm-request.d.ts +38 -0
  50. package/dist/orm-request.js +475 -0
  51. package/dist/plural-registry.d.ts +4 -0
  52. package/dist/plural-registry.js +9 -0
  53. package/dist/postgres/connection.d.ts +15 -0
  54. package/dist/postgres/connection.js +32 -0
  55. package/dist/postgres/migration-generator.d.ts +45 -0
  56. package/dist/postgres/migration-generator.js +280 -0
  57. package/dist/postgres/migration-runner.d.ts +10 -0
  58. package/dist/postgres/migration-runner.js +87 -0
  59. package/dist/postgres/postgres-db.d.ts +119 -0
  60. package/dist/postgres/postgres-db.js +477 -0
  61. package/dist/postgres/query-builder.d.ts +27 -0
  62. package/dist/postgres/query-builder.js +98 -0
  63. package/dist/postgres/schema-introspector.d.ts +29 -0
  64. package/dist/postgres/schema-introspector.js +296 -0
  65. package/dist/postgres/type-map.d.ts +23 -0
  66. package/dist/postgres/type-map.js +56 -0
  67. package/dist/record.d.ts +75 -0
  68. package/dist/record.js +129 -0
  69. package/dist/relationships.d.ts +10 -0
  70. package/dist/relationships.js +41 -0
  71. package/dist/schema-helpers.d.ts +20 -0
  72. package/dist/schema-helpers.js +48 -0
  73. package/dist/serializer.d.ts +17 -0
  74. package/dist/serializer.js +136 -0
  75. package/dist/setup-rest-server.d.ts +1 -0
  76. package/dist/setup-rest-server.js +52 -0
  77. package/dist/standalone-db.d.ts +58 -0
  78. package/dist/standalone-db.js +142 -0
  79. package/dist/store.d.ts +62 -0
  80. package/dist/store.js +286 -0
  81. package/dist/timescale/query-builder.d.ts +43 -0
  82. package/dist/timescale/query-builder.js +115 -0
  83. package/dist/timescale/timescale-db.d.ts +45 -0
  84. package/dist/timescale/timescale-db.js +84 -0
  85. package/dist/transforms.d.ts +2 -0
  86. package/dist/transforms.js +17 -0
  87. package/dist/types/orm-types.d.ts +153 -0
  88. package/dist/types/orm-types.js +1 -0
  89. package/dist/utils.d.ts +7 -0
  90. package/dist/utils.js +17 -0
  91. package/dist/view-resolver.d.ts +8 -0
  92. package/dist/view-resolver.js +171 -0
  93. package/dist/view.d.ts +11 -0
  94. package/dist/view.js +18 -0
  95. package/package.json +64 -11
  96. package/src/aggregates.ts +109 -0
  97. package/src/{attr.js → attr.ts} +2 -2
  98. package/src/belongs-to.ts +90 -0
  99. package/src/cli.ts +183 -0
  100. package/src/commands.ts +179 -0
  101. package/src/db.ts +232 -0
  102. package/src/exports/db.ts +7 -0
  103. package/src/has-many.ts +92 -0
  104. package/src/hooks.ts +151 -0
  105. package/src/{index.js → index.ts} +12 -2
  106. package/src/main.ts +229 -0
  107. package/src/manage-record.ts +161 -0
  108. package/src/{meta-request.js → meta-request.ts} +17 -14
  109. package/src/migrate.ts +72 -0
  110. package/src/model-property.ts +35 -0
  111. package/src/model.ts +21 -0
  112. package/src/mysql/connection.ts +43 -0
  113. package/src/mysql/migration-generator.ts +337 -0
  114. package/src/mysql/migration-runner.ts +121 -0
  115. package/src/mysql/mysql-db.ts +543 -0
  116. package/src/mysql/query-builder.ts +69 -0
  117. package/src/mysql/schema-introspector.ts +310 -0
  118. package/src/mysql/type-map.ts +42 -0
  119. package/src/orm-request.ts +582 -0
  120. package/src/plural-registry.ts +12 -0
  121. package/src/postgres/connection.ts +48 -0
  122. package/src/postgres/migration-generator.ts +370 -0
  123. package/src/postgres/migration-runner.ts +115 -0
  124. package/src/postgres/postgres-db.ts +616 -0
  125. package/src/postgres/query-builder.ts +148 -0
  126. package/src/postgres/schema-introspector.ts +360 -0
  127. package/src/postgres/type-map.ts +61 -0
  128. package/src/record.ts +186 -0
  129. package/src/relationships.ts +54 -0
  130. package/src/schema-helpers.ts +59 -0
  131. package/src/serializer.ts +161 -0
  132. package/src/setup-rest-server.ts +62 -0
  133. package/src/standalone-db.ts +185 -0
  134. package/src/store.ts +373 -0
  135. package/src/timescale/query-builder.ts +174 -0
  136. package/src/timescale/timescale-db.ts +119 -0
  137. package/src/transforms.ts +20 -0
  138. package/src/types/mysql2.d.ts +49 -0
  139. package/src/types/orm-types.ts +158 -0
  140. package/src/types/pg.d.ts +32 -0
  141. package/src/types/stonyx-cron.d.ts +5 -0
  142. package/src/types/stonyx-events.d.ts +4 -0
  143. package/src/types/stonyx-rest-server.d.ts +16 -0
  144. package/src/types/stonyx-utils.d.ts +33 -0
  145. package/src/types/stonyx.d.ts +21 -0
  146. package/src/utils.ts +22 -0
  147. package/src/view-resolver.ts +211 -0
  148. package/src/view.ts +22 -0
  149. package/.claude/project-structure.md +0 -578
  150. package/.github/workflows/ci.yml +0 -36
  151. package/.github/workflows/publish.yml +0 -143
  152. package/src/belongs-to.js +0 -63
  153. package/src/db.js +0 -80
  154. package/src/has-many.js +0 -61
  155. package/src/main.js +0 -119
  156. package/src/manage-record.js +0 -103
  157. package/src/model-property.js +0 -29
  158. package/src/model.js +0 -9
  159. package/src/orm-request.js +0 -249
  160. package/src/record.js +0 -100
  161. package/src/relationships.js +0 -43
  162. package/src/serializer.js +0 -138
  163. package/src/setup-rest-server.js +0 -57
  164. package/src/store.js +0 -211
  165. package/src/transforms.js +0 -20
  166. package/stonyx-bootstrap.cjs +0 -30
package/src/store.js DELETED
@@ -1,211 +0,0 @@
1
- import { relationships } from '@stonyx/orm';
2
- import { TYPES } from './relationships.js';
3
-
4
- export default class Store {
5
- constructor() {
6
- if (Store.instance) return Store.instance;
7
- Store.instance = this;
8
-
9
- this.data = new Map();
10
- }
11
-
12
- get(key, id) {
13
- if (!id) return this.data.get(key);
14
-
15
- return this.data.get(key)?.get(id);
16
- }
17
-
18
- set(key, value) {
19
- this.data.set(key, value);
20
- }
21
-
22
- remove(key, id) {
23
- if (id) return this.unloadRecord(key, id);
24
-
25
- this.unloadAllRecords(key);
26
- }
27
-
28
- unloadRecord(model, id, options={}) {
29
- const modelStore = this.data.get(model);
30
-
31
- if (!modelStore) {
32
- console.warn(`[Store] Cannot unload record: model "${model}" not found in store`);
33
- return;
34
- }
35
-
36
- const record = modelStore.get(id);
37
-
38
- if (!record) {
39
- console.warn(`[Store] Cannot unload record: ${model}:${id} not found in store`);
40
- return;
41
- }
42
-
43
- const { toUnload, visited } = options.includeChildren
44
- ? this._buildUnloadQueue(record, options)
45
- : { toUnload: [{ record, modelName: model, recordId: id }], visited: new Set([`${model}:${id}`]) };
46
-
47
- for (const item of toUnload.reverse()) {
48
- const { record: recordToUnload, modelName, recordId } = item;
49
-
50
- this._removeFromHasManyArrays(modelName, recordId, visited);
51
- this._nullifyBelongsToReferences(modelName, recordId, visited);
52
- this._cleanupRelationshipRegistries(modelName, recordId);
53
- recordToUnload.clean();
54
-
55
- this.data.get(modelName).delete(recordId);
56
- }
57
- }
58
-
59
- unloadAllRecords(model, options={}) {
60
- const modelStore = this.data.get(model);
61
-
62
- if (!modelStore) {
63
- console.warn(`[Store] Cannot unload all records: model "${model}" not found in store`);
64
- return;
65
- }
66
-
67
- const recordIds = Array.from(modelStore.keys());
68
-
69
- for (const id of recordIds) {
70
- if (modelStore.has(id)) {
71
- this.unloadRecord(model, id, options);
72
- }
73
- }
74
-
75
- for (const relationshipType of TYPES) relationships.get(relationshipType).delete(model);
76
- }
77
-
78
- _removeFromHasManyArrays(modelName, recordId, visited) {
79
- const hasManyRegistry = relationships.get('hasMany');
80
-
81
- for (const [sourceModel, targetModels] of hasManyRegistry) {
82
- const targetModelMap = targetModels.get(modelName);
83
- if (!targetModelMap) continue;
84
-
85
- for (const [sourceRecordId, hasManyArray] of targetModelMap) {
86
- const sourceKey = `${sourceModel}:${sourceRecordId}`;
87
-
88
- // Don't modify arrays of records being deleted
89
- if (visited.has(sourceKey)) continue;
90
-
91
- const index = hasManyArray.findIndex(r => r && r.id === recordId);
92
- if (index !== -1) hasManyArray.splice(index, 1);
93
- }
94
- }
95
- }
96
-
97
- _nullifyBelongsToReferences(modelName, recordId, visited) {
98
- const belongsToRegistry = relationships.get('belongsTo');
99
-
100
- for (const [sourceModel, targetModels] of belongsToRegistry) {
101
- const targetModelMap = targetModels.get(modelName);
102
- if (!targetModelMap) continue;
103
-
104
- for (const [sourceRecordId, belongsToRecord] of targetModelMap) {
105
- if (belongsToRecord && belongsToRecord.id === recordId) {
106
- const sourceKey = `${sourceModel}:${sourceRecordId}`;
107
-
108
- if (visited.has(sourceKey)) continue;
109
- targetModelMap.set(sourceRecordId, null);
110
-
111
- const sourceRecord = this.get(sourceModel, sourceRecordId);
112
- if (sourceRecord && sourceRecord.__relationships) {
113
- for (const [key, value] of Object.entries(sourceRecord.__relationships)) {
114
- if (value && value.id === recordId) {
115
- sourceRecord.__relationships[key] = null;
116
- }
117
- }
118
- }
119
- }
120
- }
121
- }
122
- }
123
-
124
- _cleanupRelationshipRegistries(modelName, recordId) {
125
- const hasManyMap = relationships.get('hasMany').get(modelName);
126
- if (hasManyMap) {
127
- for (const [, recordMap] of hasManyMap) recordMap.delete(recordId);
128
- }
129
-
130
- const belongsToMap = relationships.get('belongsTo').get(modelName);
131
- if (belongsToMap) {
132
- for (const [, recordMap] of belongsToMap) recordMap.delete(recordId);
133
- }
134
-
135
- const pendingMap = relationships.get('pending').get(modelName);
136
- if (pendingMap) pendingMap.delete(recordId);
137
- }
138
-
139
- /**
140
- * Extracts hasMany and non-bidirectional belongsTo children from a record
141
- * @private
142
- */
143
- _getChildren(record) {
144
- const children = [];
145
-
146
- if (!record.__relationships) return children;
147
-
148
- for (const [key, value] of Object.entries(record.__relationships)) {
149
- // hasMany children - always include
150
- if (Array.isArray(value)) {
151
- for (const childRecord of value) {
152
- if (childRecord) children.push({ childRecord, relationshipKey: key, type: 'hasMany' });
153
- }
154
- } else if (value && !this._isBidirectionalRelationship(
155
- record.__model.__name,
156
- value.__model.__name
157
- )) {
158
- children.push({ childRecord: value, relationshipKey: key, type: 'belongsTo' });
159
- }
160
- }
161
-
162
- return children;
163
- }
164
-
165
- _isBidirectionalRelationship(sourceModel, targetModel) {
166
- const hasManyRegistry = relationships.get('hasMany');
167
- const inverseMap = hasManyRegistry.get(targetModel)?.get(sourceModel);
168
-
169
- return inverseMap && inverseMap.size > 0;
170
- }
171
-
172
- _buildUnloadQueue(record, options) {
173
- const visited = new Set();
174
- const toUnload = [];
175
- const queue = [{
176
- record,
177
- modelName: record.__model.__name,
178
- recordId: record.id,
179
- isRoot: true,
180
- depth: 0
181
- }];
182
-
183
- while (queue.length > 0) {
184
- const item = queue.shift();
185
- const key = `${item.modelName}:${item.recordId}`;
186
-
187
- if (visited.has(key)) continue;
188
- visited.add(key);
189
-
190
- toUnload.push(item);
191
-
192
- // Add children to queue if includeChildren is enabled
193
- if (options.includeChildren) {
194
- const children = this._getChildren(item.record);
195
- for (const { childRecord } of children) {
196
- if (childRecord) {
197
- queue.push({
198
- record: childRecord,
199
- modelName: childRecord.__model.__name,
200
- recordId: childRecord.id,
201
- isRoot: false,
202
- depth: item.depth + 1
203
- });
204
- }
205
- }
206
- }
207
- }
208
-
209
- return { toUnload, visited };
210
- }
211
- }
package/src/transforms.js DELETED
@@ -1,20 +0,0 @@
1
- import { getTimestamp } from "@stonyx/utils/date";
2
-
3
- const transforms = {
4
- boolean: value => typeof value === 'string' ? value.trim().toLowerCase() === 'true' : !!value,
5
- date: value => value ? new Date(value) : null,
6
- float: value => parseFloat(value),
7
- number: value => parseInt(value),
8
- passthrough: value => value,
9
- string: value => String(value),
10
- timestamp: value => getTimestamp(value),
11
- trim: value => value?.trim(),
12
- uppercase: value => value?.toUpperCase(),
13
- };
14
-
15
- // Math Proxies
16
- ['ceil', 'floor', 'round'].forEach(method => {
17
- transforms[method] = value => Math[method](value);
18
- });
19
-
20
- export default transforms;
@@ -1,30 +0,0 @@
1
- /**
2
- * commonJS Bootstrap loading - Stonyx must be loaded first, prior to the rest of the application
3
- */
4
- const { default:Stonyx } = require('stonyx');
5
- const { default:config } = require('./config/environment.js');
6
-
7
- // Override paths for tests
8
- Object.assign(config.paths, {
9
- access: './test/sample/access',
10
- model: './test/sample/models',
11
- serializer: './test/sample/serializers',
12
- transform: './test/sample/transforms'
13
- })
14
-
15
- // Override db settings for tests
16
- Object.assign(config.db, {
17
- file: './test/sample/db.json',
18
- schema: './test/sample/db-schema.js'
19
- });
20
-
21
- // Create restServer module path for tests
22
- config.modules = {
23
- restServer: {
24
- dir: './test/sample/requests'
25
- }
26
- }
27
-
28
- new Stonyx(config, __dirname);
29
-
30
- module.exports = Stonyx;