@stonyx/orm 0.2.1-beta.9 → 0.2.1-beta.91

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 (175) hide show
  1. package/README.md +64 -6
  2. package/config/environment.js +37 -1
  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 +261 -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 +28 -0
  64. package/dist/postgres/schema-introspector.js +280 -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 +142 -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 +57 -15
  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.js → commands.ts} +179 -170
  101. package/src/{db.js → db.ts} +55 -29
  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} +11 -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.js → migrate.ts} +9 -9
  110. package/src/model-property.ts +35 -0
  111. package/src/model.ts +21 -0
  112. package/src/mysql/{connection.js → connection.ts} +43 -28
  113. package/src/mysql/migration-generator.ts +337 -0
  114. package/src/mysql/{migration-runner.js → migration-runner.ts} +121 -110
  115. package/src/mysql/mysql-db.ts +543 -0
  116. package/src/mysql/{query-builder.js → query-builder.ts} +69 -64
  117. package/src/mysql/schema-introspector.ts +310 -0
  118. package/src/mysql/{type-map.js → type-map.ts} +42 -37
  119. package/src/{orm-request.js → orm-request.ts} +187 -108
  120. package/src/plural-registry.ts +12 -0
  121. package/src/postgres/connection.ts +48 -0
  122. package/src/postgres/migration-generator.ts +348 -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 +343 -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.js → setup-rest-server.ts} +18 -16
  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 +146 -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/code-style-rules.md +0 -44
  150. package/.claude/hooks.md +0 -250
  151. package/.claude/index.md +0 -279
  152. package/.claude/usage-patterns.md +0 -217
  153. package/.github/workflows/ci.yml +0 -16
  154. package/.github/workflows/publish.yml +0 -51
  155. package/improvements.md +0 -139
  156. package/project-structure.md +0 -343
  157. package/src/belongs-to.js +0 -63
  158. package/src/has-many.js +0 -61
  159. package/src/hooks.js +0 -124
  160. package/src/main.js +0 -148
  161. package/src/manage-record.js +0 -118
  162. package/src/model-property.js +0 -29
  163. package/src/model.js +0 -9
  164. package/src/mysql/migration-generator.js +0 -188
  165. package/src/mysql/mysql-db.js +0 -320
  166. package/src/mysql/schema-introspector.js +0 -158
  167. package/src/record.js +0 -127
  168. package/src/relationships.js +0 -43
  169. package/src/serializer.js +0 -138
  170. package/src/store.js +0 -211
  171. package/src/transforms.js +0 -20
  172. package/src/utils.js +0 -12
  173. package/test-events-setup.js +0 -41
  174. package/test-hooks-manual.js +0 -54
  175. package/test-hooks-with-logging.js +0 -52
package/src/serializer.js DELETED
@@ -1,138 +0,0 @@
1
- import config from 'stonyx/config';
2
- import { get, makeArray } from '@stonyx/utils/object';
3
-
4
- const RESERVED_KEYS = ['__name'];
5
-
6
- function searchQuery(query, array, key) {
7
- const result = makeArray(array).find(item => {
8
- for (const [ prop, value ] of Object.entries(query)) {
9
- if (item[prop] !== value) return false;
10
-
11
- return true;
12
- }
13
- });
14
-
15
- if (!result) return null;
16
- if (key) return result[key];
17
-
18
- return result;
19
- }
20
-
21
- function query(rawData, pathPrefix, subPath) {
22
- if (!rawData) return null;
23
-
24
- const [ path, getter, pointer ] = makeArray(subPath);
25
- const fullPath = `${pathPrefix}${path}`;
26
- const value = get(rawData, fullPath);
27
-
28
- if (getter === undefined || getter === null) return value;
29
-
30
- try {
31
- switch(typeof getter) {
32
- case 'object':
33
- return searchQuery(getter, value, pointer);
34
-
35
- case 'function':
36
- return getter(value);
37
-
38
- case 'number':
39
- const element = value[getter];
40
- return pointer ? element[pointer] : element;
41
-
42
- default:
43
- return value[getter];
44
- }
45
- } catch (error) {
46
- if (config.debug) console.error(`Cannot parse value for ${fullPath}.`, { getter, query }, error);
47
- }
48
- }
49
-
50
- export default class Serializer {
51
- map = {};
52
- path = '';
53
-
54
- constructor(model) {
55
- this.model = model;
56
- }
57
-
58
- /**
59
- * This method populates the record's instance with instances of
60
- * the ModelProperty object, while setting parsed values to the record's
61
- * __data property, which represents the serialized version of the data
62
- */
63
- setProperties(rawData, record, options) {
64
- const { path, model } = this;
65
- const keys = Object.keys(model).filter(key => !RESERVED_KEYS.includes(key));
66
- const pathPrefix = path ? `${path}.` : '';
67
- const { __data:parsedData, __relationships:relatedRecords } = record;
68
-
69
- for (const key of keys) {
70
- const subPath = options.serialize ? (this.map[key] || key) : key;
71
- const handler = model[key];
72
- const data = query(rawData, pathPrefix, subPath);
73
-
74
- // Ignore null/undefined values on updates (TODO: What if we want it set to null?)
75
- if ((data === null || data === undefined) && options.update) continue;
76
-
77
- // Relationship handling
78
- if (typeof handler === 'function') {
79
- // Pass relationship key name to handler for pending fulfillment
80
- const handlerOptions = { ...options, _relationshipKey: key };
81
- const childRecord = handler(record, data, handlerOptions);
82
-
83
- record[key] = childRecord
84
- relatedRecords[key] = childRecord;
85
-
86
- continue;
87
- }
88
-
89
- // Direct assignment handling
90
- if (handler?.constructor?.name !== 'ModelProperty') {
91
- parsedData[key] = handler;
92
- record[key] = handler;
93
- continue;
94
- }
95
-
96
- Object.defineProperty(record, key, {
97
- enumerable: true,
98
- configurable: true,
99
- get: () => handler.value,
100
- set(newValue) {
101
- handler.ignoreFirstTransform = !options.transform;
102
- handler.value = newValue;
103
- parsedData[key] = handler.value;
104
- }
105
- });
106
-
107
- record[key] = data;
108
- }
109
-
110
- if (options.update) return;
111
-
112
- // Serialize computed properties
113
- for (const [key, getter] of getComputedProperties(this.model)) {
114
- Object.defineProperty(record, key, {
115
- enumerable: true,
116
- get: () => getter.call(record)
117
- });
118
- }
119
-
120
- record.__serialized = true;
121
- }
122
-
123
- /**
124
- * OVERRIDE: This hook allows for data manipulation prior to serialization logic
125
- */
126
- normalize(data) {
127
- return data;
128
- }
129
- }
130
-
131
- export function getComputedProperties(classInstance) {
132
- const proto = Object.getPrototypeOf(classInstance);
133
- if (!proto || proto === Object.prototype) return [];
134
-
135
- return Object.entries(Object.getOwnPropertyDescriptors(proto))
136
- .filter(([key, descriptor]) => key !== 'constructor' && descriptor.get)
137
- .map(([key, descriptor]) => [key, descriptor.get]);
138
- }
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;
package/src/utils.js DELETED
@@ -1,12 +0,0 @@
1
- import { pluralize as basePluralize } from '@stonyx/utils/string';
2
-
3
- // Wrapper to handle dasherized model names (e.g., "access-link" → "access-links")
4
- export function pluralize(word) {
5
- if (word.includes('-')) {
6
- const parts = word.split('-');
7
- const pluralizedLast = basePluralize(parts.pop());
8
- return [...parts, pluralizedLast].join('-');
9
- }
10
-
11
- return basePluralize(word);
12
- }
@@ -1,41 +0,0 @@
1
- /**
2
- * Debug script to verify event setup
3
- */
4
-
5
- import Stonyx from 'stonyx';
6
- import config from './config/environment.js';
7
- import Orm from './src/main.js';
8
- import { subscribe } from '@stonyx/events';
9
-
10
- // Override paths for tests
11
- Object.assign(config.paths, {
12
- access: './test/sample/access',
13
- model: './test/sample/models',
14
- serializer: './test/sample/serializers',
15
- transform: './test/sample/transforms'
16
- });
17
-
18
- // Override db settings for tests
19
- Object.assign(config.db, {
20
- file: './test/sample/db.json',
21
- schema: './test/sample/db-schema.js'
22
- });
23
-
24
- new Stonyx(config, import.meta.dirname);
25
-
26
- const orm = new Orm();
27
- await orm.init();
28
-
29
- console.log('ORM initialized');
30
- console.log('Store keys:', Array.from(Orm.store.data.keys()));
31
-
32
- // Try subscribing to an event
33
- try {
34
- const unsubscribe = subscribe('before:create:animal', (context) => {
35
- console.log('Hook called!', context);
36
- });
37
- console.log('✓ Successfully subscribed to before:create:animal');
38
- unsubscribe();
39
- } catch (error) {
40
- console.error('✗ Failed to subscribe:', error.message);
41
- }
@@ -1,54 +0,0 @@
1
- /**
2
- * Manual test script for hooks functionality
3
- * Run with: node test-hooks-manual.js
4
- */
5
-
6
- import { setup, subscribe, emit } from '@stonyx/events';
7
-
8
- console.log('Testing hooks system...\n');
9
-
10
- // Setup events
11
- const eventNames = ['before:create:animal', 'after:create:animal'];
12
- setup(eventNames);
13
-
14
- let beforeCalled = false;
15
- let afterCalled = false;
16
- let contextReceived = null;
17
-
18
- // Subscribe to hooks
19
- const unsubscribe1 = subscribe('before:create:animal', async (context) => {
20
- console.log('✓ before:create:animal hook called');
21
- console.log(' Context:', JSON.stringify(context, null, 2));
22
- beforeCalled = true;
23
- contextReceived = context;
24
- });
25
-
26
- const unsubscribe2 = subscribe('after:create:animal', async (context) => {
27
- console.log('✓ after:create:animal hook called');
28
- console.log(' Context:', JSON.stringify(context, null, 2));
29
- afterCalled = true;
30
- });
31
-
32
- // Simulate hook execution
33
- const testContext = {
34
- model: 'animal',
35
- operation: 'create',
36
- body: { data: { type: 'animals', attributes: { name: 'Test' } } }
37
- };
38
-
39
- console.log('Emitting before:create:animal...');
40
- await emit('before:create:animal', testContext);
41
-
42
- console.log('\nEmitting after:create:animal...');
43
- await emit('after:create:animal', { ...testContext, record: { id: 1, name: 'Test' } });
44
-
45
- console.log('\n--- Test Results ---');
46
- console.log('Before hook called:', beforeCalled ? '✓ PASS' : '✗ FAIL');
47
- console.log('After hook called:', afterCalled ? '✓ PASS' : '✗ FAIL');
48
- console.log('Context passed correctly:', contextReceived?.model === 'animal' ? '✓ PASS' : '✗ FAIL');
49
-
50
- // Cleanup
51
- unsubscribe1();
52
- unsubscribe2();
53
-
54
- console.log('\n✓ Hooks system working correctly!');
@@ -1,52 +0,0 @@
1
- /**
2
- * Test to verify hooks wrapper is being called
3
- */
4
-
5
- import { emit } from '@stonyx/events';
6
-
7
- // Simulate the _withHooks wrapper
8
- function _withHooks(operation, handler, model) {
9
- console.log(`Creating wrapper for ${operation} on ${model}`);
10
-
11
- return async (request, state) => {
12
- console.log(`Wrapper called for ${operation} on ${model}`);
13
-
14
- const context = {
15
- model,
16
- operation,
17
- request,
18
- };
19
-
20
- console.log(`About to emit before:${operation}:${model}`);
21
- await emit(`before:${operation}:${model}`, context);
22
- console.log(`Emitted before hook`);
23
-
24
- const response = await handler(request, state);
25
- console.log(`Handler completed`);
26
-
27
- context.response = response;
28
- await emit(`after:${operation}:${model}`, context);
29
- console.log(`Emitted after hook`);
30
-
31
- return response;
32
- };
33
- }
34
-
35
- // Simulate a handler
36
- const createHandler = ({ body }) => {
37
- console.log('Original handler called');
38
- return { data: { id: 1, ...body } };
39
- };
40
-
41
- // Create wrapped handler
42
- const wrappedHandler = _withHooks('create', createHandler, 'animal');
43
-
44
- // Simulate a request
45
- const mockRequest = {
46
- body: { name: 'Test' }
47
- };
48
-
49
- console.log('\n=== Testing wrapper ===');
50
- const result = await wrappedHandler(mockRequest, {});
51
- console.log('Result:', result);
52
- console.log('\n✓ Wrapper test completed');