@@ -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,9 +58,14 @@ 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
63+ if ( options ?. dbFile ) {
64+ if ( provider !== 'sqlite' ) {
65+ throw new Error ( 'dbFile option is only supported for sqlite provider' ) ;
66+ }
67+ }
68+
6369 const dbUrl =
6470 provider === 'sqlite'
6571 ? `file:${ dbName } `
@@ -108,35 +114,48 @@ export async function createTestClient<Schema extends SchemaDef>(
108114 console . log ( `Work directory: ${ workDir } ` ) ;
109115 }
110116
117+ // copy db file to workDir if specified
118+ if ( options ?. dbFile ) {
119+ if ( provider !== 'sqlite' ) {
120+ throw new Error ( 'dbFile option is only supported for sqlite provider' ) ;
121+ }
122+ fs . copyFileSync ( options . dbFile , path . join ( workDir , dbName ) ) ;
123+ }
124+
111125 const { plugins, ...rest } = options ?? { } ;
112126 const _options : ClientOptions < Schema > = {
113127 ...rest ,
114128 } as ClientOptions < Schema > ;
115129
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' ) ) ;
130+ if ( ! options ?. dbFile ) {
131+ if ( options ?. usePrismaPush ) {
132+ invariant (
133+ typeof schema === 'string' || schemaFile ,
134+ 'a schema file must be provided when using prisma db push' ,
135+ ) ;
136+ if ( ! model ) {
137+ const r = await loadDocumentWithPlugins ( path . join ( workDir , 'schema.zmodel' ) ) ;
138+ if ( ! r . success ) {
139+ throw new Error ( r . errors . join ( '\n' ) ) ;
140+ }
141+ model = r . model ;
142+ }
143+ const prismaSchema = new PrismaSchemaGenerator ( model ) ;
144+ const prismaSchemaText = await prismaSchema . generate ( ) ;
145+ fs . writeFileSync ( path . resolve ( workDir ! , 'schema.prisma' ) , prismaSchemaText ) ;
146+ execSync ( 'npx prisma db push --schema ./schema.prisma --skip-generate --force-reset' , {
147+ cwd : workDir ,
148+ stdio : 'ignore' ,
149+ } ) ;
150+ } else {
151+ if ( provider === 'postgresql' ) {
152+ invariant ( dbName , 'dbName is required' ) ;
153+ const pgClient = new PGClient ( TEST_PG_CONFIG ) ;
154+ await pgClient . connect ( ) ;
155+ await pgClient . query ( `DROP DATABASE IF EXISTS "${ dbName } "` ) ;
156+ await pgClient . query ( `CREATE DATABASE "${ dbName } "` ) ;
157+ await pgClient . end ( ) ;
122158 }
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 ( ) ;
140159 }
141160 }
142161
@@ -155,7 +174,7 @@ export async function createTestClient<Schema extends SchemaDef>(
155174
156175 let client = new ZenStackClient ( _schema , _options ) ;
157176
158- if ( ! options ?. usePrismaPush ) {
177+ if ( ! options ?. usePrismaPush && ! options ?. dbFile ) {
159178 await client . $pushSchema ( ) ;
160179 }
161180
0 commit comments