1- import { ZModelCodeGenerator } from '@zenstackhq/language' ;
2- import { isDataSource } from '@zenstackhq/language/ast' ;
1+ import {
2+ ConfigExpr ,
3+ InvocationExpr ,
4+ isDataSource ,
5+ isInvocationExpr ,
6+ isLiteralExpr ,
7+ LiteralExpr ,
8+ } from '@zenstackhq/language/ast' ;
39import { getStringLiteral } from '@zenstackhq/language/utils' ;
410import { ZenStackClient , type ClientContract } from '@zenstackhq/orm' ;
511import { MysqlDialect } from '@zenstackhq/orm/dialects/mysql' ;
@@ -28,6 +34,10 @@ type Options = {
2834} ;
2935
3036export async function run ( options : Options ) {
37+ const allowedLogLevels = [ 'error' , 'query' ] as const ;
38+ const log = options . logLevel ?. filter ( ( level ) : level is ( typeof allowedLogLevels ) [ number ] =>
39+ allowedLogLevels . includes ( level as any ) ,
40+ ) ;
3141 const schemaFile = getSchemaFile ( options . schema ) ;
3242 console . log ( colors . gray ( `Loading ZModel schema from: ${ schemaFile } ` ) ) ;
3343
@@ -46,16 +56,12 @@ export async function run(options: Options) {
4656
4757 if ( ! databaseUrl ) {
4858 const schemaUrl = dataSource ?. fields . find ( ( f ) => f . name === 'url' ) ?. value ;
49-
5059 if ( ! schemaUrl ) {
5160 throw new CliError (
5261 `The schema's "datasource" does not have a "url" field, please provide it with -d option.` ,
5362 ) ;
5463 }
55- const zModelGenerator = new ZModelCodeGenerator ( ) ;
56- const url = zModelGenerator . generate ( schemaUrl ) ;
57-
58- databaseUrl = evaluateUrl ( url ) ;
64+ databaseUrl = evaluateUrl ( schemaUrl ) ;
5965 }
6066
6167 const provider = getStringLiteral ( dataSource ?. fields . find ( ( f ) => f . name === 'provider' ) ?. value ) ! ;
@@ -66,11 +72,6 @@ export async function run(options: Options) {
6672
6773 const schemaModule = ( await jiti . import ( path . join ( outputPath , 'schema' ) ) ) as any ;
6874
69- const allowedLogLevels = [ 'error' , 'query' ] as const ;
70- const log = options . logLevel ?. filter ( ( level ) : level is ( typeof allowedLogLevels ) [ number ] =>
71- allowedLogLevels . includes ( level as any ) ,
72- ) ;
73-
7475 const db = new ZenStackClient ( schemaModule . schema , {
7576 dialect : dialect ,
7677 log : log && log . length > 0 ? log : undefined ,
@@ -86,18 +87,20 @@ export async function run(options: Options) {
8687 startServer ( db , schemaModule . schema , options ) ;
8788}
8889
89- function evaluateUrl ( value : string ) : string {
90- // Check if it's an env() function call
91- const envMatch = value . trim ( ) . match ( / ^ e n v \s * \( \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] \s * \) $ / ) ;
92- if ( envMatch ) {
93- const varName = envMatch [ 1 ] ;
94- const envValue = process . env [ varName ! ] ;
90+ function evaluateUrl ( schemaUrl : ConfigExpr ) {
91+ if ( isLiteralExpr ( schemaUrl ) ) {
92+ // Handle string literal
93+ return getStringLiteral ( schemaUrl ) ;
94+ } else if ( isInvocationExpr ( schemaUrl ) ) {
95+ const envFunction = schemaUrl as InvocationExpr ;
96+ const envName = getStringLiteral ( envFunction . args [ 0 ] ?. value as LiteralExpr ) ! ;
97+ const envValue = process . env [ envName ] ;
9598 if ( ! envValue ) {
96- throw new CliError ( `Environment variable ${ varName } is not set` ) ;
99+ throw new CliError ( `Environment variable ${ envName } is not set` ) ;
97100 }
98101 return envValue ;
99102 } else {
100- return value ;
103+ throw new CliError ( `Unable to resolve the "url" field value.` ) ;
101104 }
102105}
103106
0 commit comments