1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Complete example showing dependency injection system integration.
5
+ * Demonstrates wiring data-core with data-host-node adapters.
6
+ */
7
+
8
+ import { DIContainer , PortFactory , wireDataCore , DataCore } from './index.js' ;
9
+
10
+ // Import Node.js adapters
11
+ import { FileSystemAdapter } from '../data-host-node/adapters/FileSystemAdapter.js' ;
12
+ import { CryptoAdapter } from '../data-host-node/adapters/CryptoAdapter.js' ;
13
+ import { ProcessAdapter } from '../data-host-node/adapters/ProcessAdapter.js' ;
14
+ import { EnvironmentAdapter } from '../data-host-node/adapters/EnvironmentAdapter.js' ;
15
+
16
+ console . log ( '🚀 Complete Dependency Injection Integration Demo\n' ) ;
17
+
18
+ // === Method 1: Manual DI Container Setup ===
19
+ console . log ( '📦 Method 1: Manual DIContainer Setup' ) ;
20
+
21
+ const container = new DIContainer ( ) ;
22
+
23
+ // Register all Node.js adapters as singletons with no dependencies (they only take config objects)
24
+ container
25
+ . registerSingleton ( 'fileSystem' , FileSystemAdapter , {
26
+ dependencies : [ ] , // No DI dependencies, just config
27
+ config : { encoding : 'utf8' , mode : 0o644 }
28
+ } )
29
+ . registerSingleton ( 'crypto' , CryptoAdapter , {
30
+ dependencies : [ ] ,
31
+ config : { defaultAlgorithm : 'sha256' , encoding : 'hex' }
32
+ } )
33
+ . registerSingleton ( 'process' , ProcessAdapter , {
34
+ dependencies : [ ] ,
35
+ config : { timeout : 30000 , shell : '/bin/bash' }
36
+ } )
37
+ . registerSingleton ( 'environment' , EnvironmentAdapter , {
38
+ dependencies : [ ] ,
39
+ config : { prefix : 'DATA_' }
40
+ } ) ;
41
+
42
+ // Register DataCore with automatic dependency injection
43
+ container . register ( 'dataCore' , DataCore , {
44
+ dependencies : [ 'fileSystem' , 'crypto' , 'process' , 'environment' ]
45
+ } ) ;
46
+
47
+ // Resolve DataCore with all dependencies wired
48
+ const dataCore1 = container . resolve ( 'dataCore' ) ;
49
+ console . log ( '✅ DataCore resolved from DIContainer' ) ;
50
+ console . log ( ` Ports injected: fileSystem, crypto, process, environment` ) ;
51
+
52
+ // Test functionality
53
+ const packageInfo1 = dataCore1 . getPackageInfo ( ) ;
54
+ console . log ( ` Package: ${ packageInfo1 . name } v${ packageInfo1 . version } ` ) ;
55
+ console . log ( ` Capabilities: ${ Object . keys ( packageInfo1 . capabilities ) . length } features` ) ;
56
+
57
+ console . log ( '\n---\n' ) ;
58
+
59
+ // === Method 2: PortFactory Approach ===
60
+ console . log ( '🏭 Method 2: PortFactory Approach' ) ;
61
+
62
+ const factory = new PortFactory ( ) ;
63
+
64
+ // Register adapters with factory (note: using the base classes for validation)
65
+ factory
66
+ . registerPort ( 'fileSystem' , FileSystemAdapter , FileSystemAdapter )
67
+ . registerPort ( 'crypto' , CryptoAdapter , CryptoAdapter )
68
+ . registerPort ( 'process' , ProcessAdapter , ProcessAdapter )
69
+ . registerPort ( 'environment' , EnvironmentAdapter , EnvironmentAdapter ) ;
70
+
71
+ // Create configured ports
72
+ const ports = factory . createDataCorePorts ( {
73
+ fileSystem : { encoding : 'utf8' , mode : 0o644 } ,
74
+ crypto : { defaultAlgorithm : 'sha256' } ,
75
+ process : { timeout : 30000 , shell : '/bin/bash' } ,
76
+ environment : { prefix : 'DATA_' }
77
+ } ) ;
78
+
79
+ // Wire DataCore manually
80
+ const dataCore2 = new DataCore (
81
+ ports . fileSystem ,
82
+ ports . crypto ,
83
+ ports . process ,
84
+ ports . environment
85
+ ) ;
86
+
87
+ console . log ( '✅ DataCore created with PortFactory' ) ;
88
+ console . log ( ` Generated ports: ${ Object . keys ( ports ) . join ( ', ' ) } ` ) ;
89
+
90
+ // Test functionality
91
+ const sampleSchema = dataCore2 . createSampleSchema ( 'factory-test' ) ;
92
+ console . log ( ` Sample schema created successfully` ) ;
93
+
94
+ console . log ( '\n---\n' ) ;
95
+
96
+ // === Method 3: wireDataCore Convenience Function ===
97
+ console . log ( '⚡ Method 3: wireDataCore Convenience Function' ) ;
98
+
99
+ const { ports : wireports , dataCore : dataCore3 , factory : wirefactory } = wireDataCore (
100
+ DataCore ,
101
+ {
102
+ fileSystem : FileSystemAdapter ,
103
+ crypto : CryptoAdapter ,
104
+ process : ProcessAdapter ,
105
+ environment : EnvironmentAdapter
106
+ } ,
107
+ {
108
+ fileSystem : { encoding : 'utf8' } ,
109
+ crypto : { defaultAlgorithm : 'sha256' } ,
110
+ process : { timeout : 30000 } ,
111
+ environment : { prefix : 'DATA_' }
112
+ }
113
+ ) ;
114
+
115
+ console . log ( '✅ DataCore wired with convenience function' ) ;
116
+ console . log ( ` Auto-wired ports: ${ Object . keys ( wireports ) . join ( ', ' ) } ` ) ;
117
+
118
+ console . log ( '\n---\n' ) ;
119
+
120
+ // === Method 4: Factory + Container Integration ===
121
+ console . log ( '🔄 Method 4: Factory + Container Integration' ) ;
122
+
123
+ const integrationContainer = new DIContainer ( ) ;
124
+ const integrationFactory = new PortFactory ( ) ;
125
+
126
+ // Register adapters with factory
127
+ integrationFactory
128
+ . registerPort ( 'fileSystem' , FileSystemAdapter , FileSystemAdapter )
129
+ . registerPort ( 'crypto' , CryptoAdapter , CryptoAdapter )
130
+ . registerPort ( 'process' , ProcessAdapter , ProcessAdapter )
131
+ . registerPort ( 'environment' , EnvironmentAdapter , EnvironmentAdapter ) ;
132
+
133
+ // Register factory ports with container as singletons
134
+ integrationFactory . registerWithContainer ( integrationContainer , {
135
+ fileSystem : { encoding : 'utf8' } ,
136
+ crypto : { defaultAlgorithm : 'sha256' } ,
137
+ process : { timeout : 30000 } ,
138
+ environment : { prefix : 'DATA_' }
139
+ } ) ;
140
+
141
+ // Register DataCore for automatic resolution
142
+ integrationContainer . register ( 'dataCore' , DataCore , {
143
+ dependencies : [ 'fileSystem' , 'crypto' , 'process' , 'environment' ]
144
+ } ) ;
145
+
146
+ // Resolve everything
147
+ const dataCore4 = integrationContainer . resolve ( 'dataCore' ) ;
148
+ console . log ( '✅ DataCore resolved from integrated Factory + Container' ) ;
149
+
150
+ // Show container statistics
151
+ const stats = integrationContainer . getStats ( ) ;
152
+ console . log ( ` Container: ${ stats . totalServices } services, ${ stats . singletonInstances } singletons` ) ;
153
+
154
+ console . log ( '\n---\n' ) ;
155
+
156
+ // === Demonstrate DataCore Functionality ===
157
+ console . log ( '🎯 Testing DataCore Functionality' ) ;
158
+
159
+ try {
160
+ // Test with one of our DataCore instances
161
+ const testDataCore = dataCore1 ;
162
+
163
+ // Get package information
164
+ const info = testDataCore . getPackageInfo ( ) ;
165
+ console . log ( `📋 Package: ${ info . name } v${ info . version } ` ) ;
166
+ console . log ( `🔌 Port interfaces: ${ info . portInterfaces . join ( ', ' ) } ` ) ;
167
+ console . log ( `⚙️ Core engines: ${ info . coreEngines . join ( ', ' ) } ` ) ;
168
+
169
+ // Create sample schema
170
+ const schema = testDataCore . createSampleSchema ( 'integration-test' ) ;
171
+ console . log ( `📊 Sample schema created` ) ;
172
+
173
+ // Show capabilities
174
+ console . log ( `🎪 Capabilities:` ) ;
175
+ for ( const [ capability , enabled ] of Object . entries ( info . capabilities ) ) {
176
+ console . log ( ` • ${ capability } : ${ enabled ? '✅' : '❌' } ` ) ;
177
+ }
178
+
179
+ console . log ( '\n🎉 All integration methods working successfully!' ) ;
180
+
181
+ console . log ( '\n📋 Summary:' ) ;
182
+ console . log ( ' 1. DIContainer: Manual registration with full control' ) ;
183
+ console . log ( ' 2. PortFactory: Type-safe port creation with validation' ) ;
184
+ console . log ( ' 3. wireDataCore: One-liner convenience for simple cases' ) ;
185
+ console . log ( ' 4. Factory+Container: Best of both worlds for complex apps' ) ;
186
+
187
+ console . log ( '\n🔑 Key Benefits:' ) ;
188
+ console . log ( ' • Constructor injection with automatic dependency resolution' ) ;
189
+ console . log ( ' • Singleton lifecycle management for shared resources' ) ;
190
+ console . log ( ' • Configuration injection for customizable behavior' ) ;
191
+ console . log ( ' • Circular dependency detection prevents infinite loops' ) ;
192
+ console . log ( ' • Port interface validation ensures contract compliance' ) ;
193
+ console . log ( ' • Factory pattern enables reusable, configured instances' ) ;
194
+ console . log ( ' • Multiple integration approaches for different use cases' ) ;
195
+
196
+ } catch ( error ) {
197
+ console . error ( '❌ Error testing DataCore functionality:' , error . message ) ;
198
+ console . error ( error . stack ) ;
199
+ process . exit ( 1 ) ;
200
+ }
0 commit comments