@@ -13,14 +13,14 @@ import { PostgresDialect } from '@zenstackhq/orm/dialects/postgres';
1313import { SqliteDialect } from '@zenstackhq/orm/dialects/sqlite' ;
1414import { RPCApiHandler } from '@zenstackhq/server/api' ;
1515import { ZenStackMiddleware } from '@zenstackhq/server/express' ;
16- import SQLite from 'better-sqlite3' ;
16+ import type BetterSqlite3 from 'better-sqlite3' ;
1717import colors from 'colors' ;
1818import cors from 'cors' ;
1919import express from 'express' ;
2020import { createJiti } from 'jiti' ;
21- import { createPool as createMysqlPool } from 'mysql2' ;
21+ import type { createPool as MysqlCreatePool } from 'mysql2' ;
2222import path from 'node:path' ;
23- import { Pool as PgPool } from 'pg' ;
23+ import type { Pool as PgPoolType } from 'pg' ;
2424import { CliError } from '../cli-error' ;
2525import { getVersion } from '../utils/version-utils' ;
2626import { getOutputPath , getSchemaFile , loadSchemaDocument } from './action-utils' ;
@@ -67,7 +67,7 @@ export async function run(options: Options) {
6767
6868 const provider = getStringLiteral ( dataSource ?. fields . find ( ( f ) => f . name === 'provider' ) ?. value ) ! ;
6969
70- const dialect = createDialect ( provider , databaseUrl ! , outputPath ) ;
70+ const dialect = await createDialect ( provider , databaseUrl ! , outputPath ) ;
7171
7272 const jiti = createJiti ( import . meta. url ) ;
7373
@@ -137,9 +137,17 @@ function redactDatabaseUrl(url: string): string {
137137 }
138138}
139139
140- function createDialect ( provider : string , databaseUrl : string , outputPath : string ) {
140+ async function createDialect ( provider : string , databaseUrl : string , outputPath : string ) {
141141 switch ( provider ) {
142142 case 'sqlite' : {
143+ let SQLite : typeof BetterSqlite3 ;
144+ try {
145+ SQLite = ( await import ( 'better-sqlite3' ) ) . default ;
146+ } catch {
147+ throw new CliError (
148+ `Package "better-sqlite3" is required for SQLite support. Please install it with: npm install better-sqlite3` ,
149+ ) ;
150+ }
143151 let resolvedUrl = databaseUrl . trim ( ) ;
144152 if ( resolvedUrl . startsWith ( 'file:' ) ) {
145153 const filePath = resolvedUrl . substring ( 'file:' . length ) ;
@@ -152,20 +160,36 @@ function createDialect(provider: string, databaseUrl: string, outputPath: string
152160 database : new SQLite ( resolvedUrl ) ,
153161 } ) ;
154162 }
155- case 'postgresql' :
163+ case 'postgresql' : {
164+ let PgPool : typeof PgPoolType ;
165+ try {
166+ PgPool = ( await import ( 'pg' ) ) . Pool ;
167+ } catch {
168+ throw new CliError (
169+ `Package "pg" is required for PostgreSQL support. Please install it with: npm install pg` ,
170+ ) ;
171+ }
156172 console . log ( colors . gray ( `Connecting to PostgreSQL database at: ${ redactDatabaseUrl ( databaseUrl ) } ` ) ) ;
157173 return new PostgresDialect ( {
158174 pool : new PgPool ( {
159175 connectionString : databaseUrl ,
160176 } ) ,
161177 } ) ;
162-
163- case 'mysql' :
178+ }
179+ case 'mysql' : {
180+ let createMysqlPool : typeof MysqlCreatePool ;
181+ try {
182+ createMysqlPool = ( await import ( 'mysql2' ) ) . createPool ;
183+ } catch {
184+ throw new CliError (
185+ `Package "mysql2" is required for MySQL support. Please install it with: npm install mysql2` ,
186+ ) ;
187+ }
164188 console . log ( colors . gray ( `Connecting to MySQL database at: ${ redactDatabaseUrl ( databaseUrl ) } ` ) ) ;
165189 return new MysqlDialect ( {
166190 pool : createMysqlPool ( databaseUrl ) ,
167191 } ) ;
168-
192+ }
169193 default :
170194 throw new CliError ( `Unsupported database provider: ${ provider } ` ) ;
171195 }
0 commit comments