1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Example demonstrating the dependency injection system.
5
+ * Shows how to wire data-core with data-host-node adapters using DI.
6
+ *
7
+ * Run with: node packages/data-core/example-di.js
8
+ */
9
+
10
+ import { DIContainer } from './ports/DIContainer.js' ;
11
+ import { PortFactory , wireDataCore } from './ports/PortFactory.js' ;
12
+ import { DataCore } from './index.js' ;
13
+
14
+ // Import Node.js adapters
15
+ import {
16
+ FileSystemAdapter ,
17
+ CryptoAdapter ,
18
+ ProcessAdapter ,
19
+ EnvironmentAdapter
20
+ } from '../data-host-node/index.js' ;
21
+
22
+ console . log ( '🔗 Dependency Injection System Demo\n' ) ;
23
+
24
+ // === Method 1: Using DIContainer directly ===
25
+ console . log ( '📦 Method 1: Using DIContainer directly' ) ;
26
+
27
+ const container = new DIContainer ( ) ;
28
+
29
+ // Register all adapters as singletons
30
+ container
31
+ . registerSingleton ( 'fileSystem' , FileSystemAdapter , {
32
+ config : { encoding : 'utf8' }
33
+ } )
34
+ . registerSingleton ( 'crypto' , CryptoAdapter , {
35
+ config : { defaultAlgorithm : 'sha256' }
36
+ } )
37
+ . registerSingleton ( 'process' , ProcessAdapter , {
38
+ config : { timeout : 30000 , shell : '/bin/bash' }
39
+ } )
40
+ . registerSingleton ( 'environment' , EnvironmentAdapter , {
41
+ config : { prefix : 'DATA_' }
42
+ } ) ;
43
+
44
+ // Register DataCore with automatic dependency injection
45
+ container . register ( 'dataCore' , DataCore ) ;
46
+
47
+ // Resolve DataCore - all dependencies automatically injected
48
+ const dataCore1 = container . resolve ( 'dataCore' ) ;
49
+ console . log ( `✅ DataCore resolved with ports: ${ Object . keys ( dataCore1 ) . filter ( k => k . endsWith ( 'Port' ) ) . join ( ', ' ) } ` ) ;
50
+ console . log ( `📊 Container stats:` , container . getStats ( ) ) ;
51
+
52
+ console . log ( '\n---\n' ) ;
53
+
54
+ // === Method 2: Using PortFactory ===
55
+ console . log ( '🏭 Method 2: Using PortFactory' ) ;
56
+
57
+ const factory = new PortFactory ( ) ;
58
+
59
+ // Register adapters with factory
60
+ factory
61
+ . registerPort ( 'fileSystem' , FileSystemAdapter , FileSystemAdapter , { encoding : 'utf8' } )
62
+ . registerPort ( 'crypto' , CryptoAdapter , CryptoAdapter , { defaultAlgorithm : 'sha256' } )
63
+ . registerPort ( 'process' , ProcessAdapter , ProcessAdapter , { timeout : 30000 } )
64
+ . registerPort ( 'environment' , EnvironmentAdapter , EnvironmentAdapter , { prefix : 'DATA_' } ) ;
65
+
66
+ // Create all data-core compatible ports
67
+ const ports = factory . createDataCorePorts ( {
68
+ fileSystem : { encoding : 'utf8' , mode : 0o644 } ,
69
+ crypto : { defaultAlgorithm : 'sha256' } ,
70
+ process : { timeout : 30000 , shell : '/bin/bash' } ,
71
+ environment : { prefix : 'DATA_' , caseSensitive : true }
72
+ } ) ;
73
+
74
+ // Create DataCore with ports
75
+ const dataCore2 = new DataCore (
76
+ ports . fileSystem ,
77
+ ports . crypto ,
78
+ ports . process ,
79
+ ports . environment
80
+ ) ;
81
+
82
+ console . log ( `✅ DataCore created with factory-generated ports` ) ;
83
+ console . log ( `📊 Factory info:` , factory . getPortInfo ( ) ) ;
84
+
85
+ console . log ( '\n---\n' ) ;
86
+
87
+ // === Method 3: Using convenience wireDataCore function ===
88
+ console . log ( '⚡ Method 3: Using wireDataCore convenience function' ) ;
89
+
90
+ const { ports : wirePorts , dataCore : dataCore3 , factory : wireFactory } = wireDataCore (
91
+ DataCore ,
92
+ {
93
+ fileSystem : FileSystemAdapter ,
94
+ crypto : CryptoAdapter ,
95
+ process : ProcessAdapter ,
96
+ environment : EnvironmentAdapter
97
+ } ,
98
+ {
99
+ fileSystem : { encoding : 'utf8' } ,
100
+ crypto : { defaultAlgorithm : 'sha256' } ,
101
+ process : { timeout : 30000 } ,
102
+ environment : { prefix : 'DATA_' }
103
+ }
104
+ ) ;
105
+
106
+ console . log ( `✅ DataCore wired using convenience function` ) ;
107
+ console . log ( `🔌 Wired ports:` , Object . keys ( wirePorts ) ) ;
108
+
109
+ console . log ( '\n---\n' ) ;
110
+
111
+ // === Method 4: Factory + Container integration ===
112
+ console . log ( '🔄 Method 4: Factory + Container integration' ) ;
113
+
114
+ const integratedContainer = new DIContainer ( ) ;
115
+ const integratedFactory = new PortFactory ( ) ;
116
+
117
+ // Register adapters with factory
118
+ integratedFactory
119
+ . registerPort ( 'fileSystem' , FileSystemAdapter , FileSystemAdapter )
120
+ . registerPort ( 'crypto' , CryptoAdapter , CryptoAdapter )
121
+ . registerPort ( 'process' , ProcessAdapter , ProcessAdapter )
122
+ . registerPort ( 'environment' , EnvironmentAdapter , EnvironmentAdapter ) ;
123
+
124
+ // Register factory-created ports with container
125
+ integratedFactory . registerWithContainer ( integratedContainer , {
126
+ fileSystem : { encoding : 'utf8' } ,
127
+ crypto : { defaultAlgorithm : 'sha256' } ,
128
+ process : { timeout : 30000 } ,
129
+ environment : { prefix : 'DATA_' }
130
+ } ) ;
131
+
132
+ // Register DataCore
133
+ integratedContainer . registerSingleton ( 'dataCore' , DataCore ) ;
134
+
135
+ // Resolve everything
136
+ const integratedDataCore = integratedContainer . resolve ( 'dataCore' ) ;
137
+ console . log ( `✅ DataCore resolved from integrated Factory + Container` ) ;
138
+
139
+ console . log ( '\n---\n' ) ;
140
+
141
+ // === Demonstrate DataCore functionality ===
142
+ console . log ( '🚀 Testing DataCore functionality' ) ;
143
+
144
+ try {
145
+ // Test package info
146
+ const packageInfo = dataCore1 . getPackageInfo ( ) ;
147
+ console . log ( `📋 Package: ${ packageInfo . name } v${ packageInfo . version } ` ) ;
148
+ console . log ( `🔌 Port interfaces: ${ packageInfo . portInterfaces . join ( ', ' ) } ` ) ;
149
+ console . log ( `⚙️ Core engines: ${ packageInfo . coreEngines . join ( ', ' ) } ` ) ;
150
+
151
+ // Test sample schema creation
152
+ const sampleSchema = dataCore1 . createSampleSchema ( 'demo' ) ;
153
+ console . log ( `📊 Sample schema created with checksum capability` ) ;
154
+
155
+ console . log ( '\n✅ All dependency injection methods working correctly!' ) ;
156
+ console . log ( '\n🎯 Key Benefits:' ) ;
157
+ console . log ( ' • Automatic dependency resolution' ) ;
158
+ console . log ( ' • Circular dependency detection' ) ;
159
+ console . log ( ' • Singleton lifecycle management' ) ;
160
+ console . log ( ' • Configuration injection' ) ;
161
+ console . log ( ' • Factory pattern for reusability' ) ;
162
+ console . log ( ' • Multiple integration approaches' ) ;
163
+
164
+ } catch ( error ) {
165
+ console . error ( '❌ Error testing DataCore:' , error . message ) ;
166
+ process . exit ( 1 ) ;
167
+ }
0 commit comments