@@ -38,6 +38,7 @@ export type CreateTestClientOptions<Schema extends SchemaDef> = Omit<ClientOptio
3838 extraSourceFiles ?: Record < string , string > ;
3939 workDir ?: string ;
4040 debug ?: boolean ;
41+ dbFile ?: string ;
4142} ;
4243
4344export async function createTestClient < Schema extends SchemaDef > (
@@ -57,7 +58,6 @@ export async function createTestClient<Schema extends SchemaDef>(
5758 let workDir = options ?. workDir ;
5859 let _schema : Schema ;
5960 const provider = options ?. provider ?? getTestDbProvider ( ) ?? 'sqlite' ;
60-
6161 const dbName = options ?. dbName ?? getTestDbName ( provider ) ;
6262
6363 const dbUrl =
@@ -108,35 +108,48 @@ export async function createTestClient<Schema extends SchemaDef>(
108108 console . log ( `Work directory: ${ workDir } ` ) ;
109109 }
110110
111+ // copy db file to workDir if specified
112+ if ( options ?. dbFile ) {
113+ if ( provider !== 'sqlite' ) {
114+ throw new Error ( 'dbFile option is only supported for sqlite provider' ) ;
115+ }
116+ fs . copyFileSync ( options . dbFile , path . join ( workDir , dbName ) ) ;
117+ }
118+
111119 const { plugins, ...rest } = options ?? { } ;
112120 const _options : ClientOptions < Schema > = {
113121 ...rest ,
114122 } as ClientOptions < Schema > ;
115123
116- if ( options ?. usePrismaPush ) {
117- invariant ( typeof schema === 'string' || schemaFile , 'a schema file must be provided when using prisma db push' ) ;
118- if ( ! model ) {
119- const r = await loadDocumentWithPlugins ( path . join ( workDir , 'schema.zmodel' ) ) ;
120- if ( ! r . success ) {
121- throw new Error ( r . errors . join ( '\n' ) ) ;
124+ if ( ! options ?. dbFile ) {
125+ if ( options ?. usePrismaPush ) {
126+ invariant (
127+ typeof schema === 'string' || schemaFile ,
128+ 'a schema file must be provided when using prisma db push' ,
129+ ) ;
130+ if ( ! model ) {
131+ const r = await loadDocumentWithPlugins ( path . join ( workDir , 'schema.zmodel' ) ) ;
132+ if ( ! r . success ) {
133+ throw new Error ( r . errors . join ( '\n' ) ) ;
134+ }
135+ model = r . model ;
136+ }
137+ const prismaSchema = new PrismaSchemaGenerator ( model ) ;
138+ const prismaSchemaText = await prismaSchema . generate ( ) ;
139+ fs . writeFileSync ( path . resolve ( workDir ! , 'schema.prisma' ) , prismaSchemaText ) ;
140+ execSync ( 'npx prisma db push --schema ./schema.prisma --skip-generate --force-reset' , {
141+ cwd : workDir ,
142+ stdio : 'ignore' ,
143+ } ) ;
144+ } else {
145+ if ( provider === 'postgresql' ) {
146+ invariant ( dbName , 'dbName is required' ) ;
147+ const pgClient = new PGClient ( TEST_PG_CONFIG ) ;
148+ await pgClient . connect ( ) ;
149+ await pgClient . query ( `DROP DATABASE IF EXISTS "${ dbName } "` ) ;
150+ await pgClient . query ( `CREATE DATABASE "${ dbName } "` ) ;
151+ await pgClient . end ( ) ;
122152 }
123- model = r . model ;
124- }
125- const prismaSchema = new PrismaSchemaGenerator ( model ) ;
126- const prismaSchemaText = await prismaSchema . generate ( ) ;
127- fs . writeFileSync ( path . resolve ( workDir ! , 'schema.prisma' ) , prismaSchemaText ) ;
128- execSync ( 'npx prisma db push --schema ./schema.prisma --skip-generate --force-reset' , {
129- cwd : workDir ,
130- stdio : 'ignore' ,
131- } ) ;
132- } else {
133- if ( provider === 'postgresql' ) {
134- invariant ( dbName , 'dbName is required' ) ;
135- const pgClient = new PGClient ( TEST_PG_CONFIG ) ;
136- await pgClient . connect ( ) ;
137- await pgClient . query ( `DROP DATABASE IF EXISTS "${ dbName } "` ) ;
138- await pgClient . query ( `CREATE DATABASE "${ dbName } "` ) ;
139- await pgClient . end ( ) ;
140153 }
141154 }
142155
@@ -155,7 +168,7 @@ export async function createTestClient<Schema extends SchemaDef>(
155168
156169 let client = new ZenStackClient ( _schema , _options ) ;
157170
158- if ( ! options ?. usePrismaPush ) {
171+ if ( ! options ?. usePrismaPush && ! options ?. dbFile ) {
159172 await client . $pushSchema ( ) ;
160173 }
161174
0 commit comments