@@ -12,6 +12,7 @@ import type {
1212 GetTestPostgresDatabase ,
1313 GetTestPostgresDatabaseFactoryOptions ,
1414 GetTestPostgresDatabaseOptions ,
15+ GetTestPostgresDatabaseResult ,
1516} from "./public-types"
1617import { Pool } from "pg"
1718import type { Jsonifiable } from "type-fest"
@@ -104,7 +105,7 @@ export const getTestPostgresDatabaseFactory = <
104105 Params extends Jsonifiable = never
105106> (
106107 options ?: GetTestPostgresDatabaseFactoryOptions < Params >
107- ) => {
108+ ) : GetTestPostgresDatabase < Params > => {
108109 const initialData : InitialWorkerData = {
109110 postgresVersion : options ?. postgresVersion ?? "14" ,
110111 containerOptions : options ?. container ,
@@ -157,41 +158,28 @@ export const getTestPostgresDatabaseFactory = <
157158 throw error
158159 } )
159160
160- const createdNestedConnections : ConnectionDetails [ ] = [ ]
161161 const hookResult = await options . beforeTemplateIsBaked ( {
162162 params : params as any ,
163163 connection : connectionDetails ,
164164 containerExec : async ( command ) : Promise < ExecResult > =>
165165 rpc . execCommandInContainer ( command ) ,
166166 // This is what allows a consumer to get a "nested" database from within their beforeTemplateIsBaked hook
167- beforeTemplateIsBaked : async ( options ) => {
168- const { connectionDetails, beforeTemplateIsBakedResult } =
169- await rpc . getTestDatabase ( {
170- params : options . params ,
171- databaseDedupeKey : options . databaseDedupeKey ,
172- } )
173-
174- const mappedConnection =
167+ manuallyBuildAdditionalTemplate : async ( ) => {
168+ const connection =
175169 mapWorkerConnectionDetailsToConnectionDetails (
176- connectionDetails
170+ await rpc . createEmptyDatabase ( )
177171 )
178172
179- createdNestedConnections . push ( mappedConnection )
180-
181173 return {
182- ...mappedConnection ,
183- beforeTemplateIsBakedResult,
174+ connection,
175+ finish : async ( ) => {
176+ await teardownConnection ( connection )
177+ return rpc . convertDatabaseToTemplate ( connection . database )
178+ } ,
184179 }
185180 } ,
186181 } )
187182
188- await Promise . all (
189- createdNestedConnections . map ( async ( connection ) => {
190- await teardownConnection ( connection )
191- await rpc . dropDatabase ( connection . database )
192- } )
193- )
194-
195183 await teardownConnection ( connectionDetails )
196184
197185 if ( hookResult && ! isSerializable ( hookResult ) ) {
@@ -226,11 +214,11 @@ export const getTestPostgresDatabaseFactory = <
226214 }
227215 } ) ( )
228216
229- const getTestPostgresDatabase : GetTestPostgresDatabase < Params > = async (
217+ const getTestPostgresDatabase = async (
230218 t : ExecutionContext ,
231219 params : any ,
232220 getTestDatabaseOptions ?: GetTestPostgresDatabaseOptions
233- ) => {
221+ ) : Promise < GetTestPostgresDatabaseResult > => {
234222 const testDatabaseConnection = await rpc . getTestDatabase ( {
235223 databaseDedupeKey : getTestDatabaseOptions ?. databaseDedupeKey ,
236224 params,
@@ -251,7 +239,22 @@ export const getTestPostgresDatabaseFactory = <
251239 }
252240 }
253241
254- return getTestPostgresDatabase
242+ getTestPostgresDatabase . fromTemplate = async (
243+ t : ExecutionContext ,
244+ templateName : string
245+ ) => {
246+ const connection = mapWorkerConnectionDetailsToConnectionDetails (
247+ await rpc . createDatabaseFromTemplate ( templateName )
248+ )
249+
250+ t . teardown ( async ( ) => {
251+ await teardownConnection ( connection )
252+ } )
253+
254+ return connection
255+ }
256+
257+ return getTestPostgresDatabase as any
255258}
256259
257260export * from "./public-types"
0 commit comments