@stonyx/orm 0.2.1-beta.2 → 0.2.1-beta.21

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.
@@ -0,0 +1,41 @@
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
+ }
@@ -0,0 +1,54 @@
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!');
@@ -0,0 +1,52 @@
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');