@@ -5,6 +5,7 @@ import { parseCommand } from '../utils/parseCommand';
55import { digitalOceanParserServiceName } from '../utils/digitalOceanParserServiceName' ;
66import { normalizeDigitalOceanImageInfo } from '../utils/normalizeDigitalOceanImageInfo' ;
77import { getDigitalOceanDatabaseType } from '../utils/getDigitalOceanDatabaseType' ;
8+ import { isDigitalOceanManagedDatabase } from '../utils/isDigitalOceanManagedDatabase' ;
89
910const defaultParserConfig : ParserConfig = {
1011 files : [
@@ -36,13 +37,39 @@ class DigitalOceanParser extends BaseParser {
3637
3738 parseFiles ( config : ApplicationConfig ) : { [ path : string ] : FileOutput } {
3839 const services : Array < any > = [ ] ;
40+ const databases : Array < any > = [ ] ;
41+ const databaseServiceMap = new Map < string , string > ( ) ;
3942 let isFirstService = true ;
4043
44+ // First pass: identify database services that should be managed databases
4145 for ( const [ serviceName , serviceConfig ] of Object . entries ( config . services ) ) {
46+ if ( isDigitalOceanManagedDatabase ( serviceConfig . image ) ) {
47+ // Create a database entry
48+ const dbName = digitalOceanParserServiceName ( `${ serviceName } -db` ) ;
49+
50+ // Track the mapping between service name and database name
51+ databaseServiceMap . set ( serviceName , dbName ) ;
52+
53+ // Get database config
54+ const dbConfig = getDigitalOceanDatabaseType ( serviceConfig . image ) ;
55+
56+ // Create database config object with proper typing
57+ const dbEntry : any = {
58+ name : dbName ,
59+ engine : dbConfig ?. engine || 'PG' // Default to PostgreSQL if unknown
60+ } ;
61+
62+ databases . push ( dbEntry ) ;
63+
64+ // Skip creating a service for this database
65+ continue ;
66+ }
67+
4268 const dockerImageInfo = serviceConfig . image ;
4369 const databaseConfig = getDigitalOceanDatabaseType ( dockerImageInfo ) ;
4470 const normalizedImage = normalizeDigitalOceanImageInfo ( dockerImageInfo ) ;
4571
72+ // Prepare base service configuration
4673 const baseService = {
4774 name : digitalOceanParserServiceName ( serviceName ) ,
4875 image : {
@@ -57,12 +84,58 @@ class DigitalOceanParser extends BaseParser {
5784 envs : Object . entries ( serviceConfig . environment )
5885 . map ( ( [ key , value ] ) => ( {
5986 key,
60- value : value . toString ( )
87+ value : value . toString ( ) ,
88+ scope : 'RUN_TIME'
6189 } ) )
6290 } ;
6391
64- if ( databaseConfig ) {
65- // Database/TCP service configuration
92+ // Process service connections - this is provider specific logic
93+ if ( config . serviceConnections ) {
94+ for ( const connection of config . serviceConnections ) {
95+ if ( connection . fromService === serviceName ) {
96+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
97+ for ( const [ varName , varInfo ] of Object . entries ( connection . variables ) ) {
98+ // Find the environment variable index
99+ const envIndex = baseService . envs . findIndex ( env => env . key === varName ) ;
100+
101+ // Check if target service is a managed database
102+ if ( databaseServiceMap . has ( connection . toService ) ) {
103+ const dbServiceName = databaseServiceMap . get ( connection . toService ) ! ;
104+ const transformedValue = `\${${ dbServiceName } .DATABASE_URL}` ;
105+
106+ if ( envIndex !== - 1 ) {
107+ // Update existing variable
108+ baseService . envs [ envIndex ] . value = transformedValue ;
109+ } else {
110+ // Add new variable
111+ baseService . envs . push ( {
112+ key : varName ,
113+ value : transformedValue ,
114+ scope : 'RUN_TIME'
115+ } ) ;
116+ }
117+ } else {
118+ // Regular service connection - not typically used in DigitalOcean but included for completeness
119+ const targetServiceName = digitalOceanParserServiceName ( connection . toService ) ;
120+ const transformedValue = `\${${ targetServiceName } .PRIVATE_URL}` ;
121+
122+ if ( envIndex !== - 1 ) {
123+ baseService . envs [ envIndex ] . value = transformedValue ;
124+ } else {
125+ baseService . envs . push ( {
126+ key : varName ,
127+ value : transformedValue ,
128+ scope : 'RUN_TIME'
129+ } ) ;
130+ }
131+ }
132+ }
133+ }
134+ }
135+ }
136+
137+ if ( databaseConfig && ! isDigitalOceanManagedDatabase ( dockerImageInfo ) ) {
138+ // Non-managed database/TCP service configuration
66139 services . push ( {
67140 ...baseService ,
68141 health_check : {
@@ -96,14 +169,19 @@ class DigitalOceanParser extends BaseParser {
96169 }
97170 }
98171
99- const digitalOceanConfig = {
172+ const digitalOceanConfig : any = {
100173 spec : {
101174 name : 'deploystack' ,
102175 region : defaultParserConfig . region ,
103176 services
104177 }
105178 } ;
106179
180+ // Add databases section if we have any
181+ if ( databases . length > 0 ) {
182+ digitalOceanConfig . spec . databases = databases ;
183+ }
184+
107185 return {
108186 '.do/deploy.template.yaml' : {
109187 content : this . formatFileContent ( digitalOceanConfig , TemplateFormat . yaml ) ,
0 commit comments