@@ -19,14 +19,17 @@ interface Movie {
1919
2020const pool = new Pool ( {
2121 host : process . env . PARADEDB_HOST || "paradedb.localhost.localstack.cloud" ,
22- port : parseInt ( process . env . PARADEDB_PORT || "5432 " ) ,
23- database : process . env . PARADEDB_DATABASE || "postgres " ,
24- user : process . env . PARADEDB_USER || "postgres " ,
25- password : process . env . PARADEDB_PASSWORD || "postgres " ,
22+ port : parseInt ( process . env . PARADEDB_PORT || "4566 " ) ,
23+ database : process . env . PARADEDB_DATABASE || "mydatabase " ,
24+ user : process . env . PARADEDB_USER || "myuser " ,
25+ password : process . env . PARADEDB_PASSWORD || "mypassword " ,
2626} ) ;
2727
28+ const S3_ENDPOINT = process . env . S3_ENDPOINT || "http://s3.localhost.localstack.cloud:4566" ;
29+ const DATA_BUCKET = process . env . DATA_BUCKET || "movie-search-data" ;
30+
2831const s3Client = new S3Client ( {
29- endpoint : "http://s3.localhost.localstack.cloud:4566" ,
32+ endpoint : S3_ENDPOINT ,
3033 region : "us-east-1" ,
3134 forcePathStyle : true ,
3235 credentials : {
@@ -35,8 +38,6 @@ const s3Client = new S3Client({
3538 } ,
3639} ) ;
3740
38- const DATA_BUCKET = process . env . DATA_BUCKET || "movie-search-data" ;
39-
4041function successResponse ( data : unknown ) : APIGatewayProxyResult {
4142 return {
4243 statusCode : 200 ,
@@ -266,34 +267,26 @@ export async function seedHandler(): Promise<APIGatewayProxyResult> {
266267
267268 console . log ( `Parsed ${ movies . length } movies from bulk file` ) ;
268269
269- await client . query ( "DELETE FROM movies " ) ;
270+ await client . query ( "BEGIN " ) ;
270271
271- let inserted = 0 ;
272- const batchSize = 100 ;
272+ try {
273+ await client . query ( "DELETE FROM movies" ) ;
273274
274- for ( let i = 0 ; i < movies . length ; i += batchSize ) {
275- const batch = movies . slice ( i , i + batchSize ) ;
275+ const batchSize = 100 ;
276+ let inserted = 0 ;
276277
277- for ( const movie of batch ) {
278- await client . query (
279- `
280- INSERT INTO movies (id, title, year, genres, rating, directors, actors, plot,
281- image_url, release_date, rank, running_time_secs)
282- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
283- ON CONFLICT (id) DO UPDATE SET
284- title = EXCLUDED.title,
285- year = EXCLUDED.year,
286- genres = EXCLUDED.genres,
287- rating = EXCLUDED.rating,
288- directors = EXCLUDED.directors,
289- actors = EXCLUDED.actors,
290- plot = EXCLUDED.plot,
291- image_url = EXCLUDED.image_url,
292- release_date = EXCLUDED.release_date,
293- rank = EXCLUDED.rank,
294- running_time_secs = EXCLUDED.running_time_secs
295- ` ,
296- [
278+ for ( let i = 0 ; i < movies . length ; i += batchSize ) {
279+ const batch = movies . slice ( i , i + batchSize ) ;
280+
281+ const values : unknown [ ] = [ ] ;
282+ const placeholders : string [ ] = [ ] ;
283+
284+ batch . forEach ( ( movie , idx ) => {
285+ const offset = idx * 12 ;
286+ placeholders . push (
287+ `($${ offset + 1 } , $${ offset + 2 } , $${ offset + 3 } , $${ offset + 4 } , $${ offset + 5 } , $${ offset + 6 } , $${ offset + 7 } , $${ offset + 8 } , $${ offset + 9 } , $${ offset + 10 } , $${ offset + 11 } , $${ offset + 12 } )`
288+ ) ;
289+ values . push (
297290 movie . id ,
298291 movie . title ,
299292 movie . year || null ,
@@ -305,21 +298,45 @@ export async function seedHandler(): Promise<APIGatewayProxyResult> {
305298 movie . image_url || null ,
306299 movie . release_date || null ,
307300 movie . rank || null ,
308- movie . running_time_secs || null ,
309- ]
301+ movie . running_time_secs || null
302+ ) ;
303+ } ) ;
304+
305+ await client . query (
306+ `INSERT INTO movies (id, title, year, genres, rating, directors, actors, plot,
307+ image_url, release_date, rank, running_time_secs)
308+ VALUES ${ placeholders . join ( ", " ) }
309+ ON CONFLICT (id) DO UPDATE SET
310+ title = EXCLUDED.title,
311+ year = EXCLUDED.year,
312+ genres = EXCLUDED.genres,
313+ rating = EXCLUDED.rating,
314+ directors = EXCLUDED.directors,
315+ actors = EXCLUDED.actors,
316+ plot = EXCLUDED.plot,
317+ image_url = EXCLUDED.image_url,
318+ release_date = EXCLUDED.release_date,
319+ rank = EXCLUDED.rank,
320+ running_time_secs = EXCLUDED.running_time_secs` ,
321+ values
310322 ) ;
311- inserted ++ ;
323+
324+ inserted += batch . length ;
325+ console . log ( `Inserted ${ inserted } /${ movies . length } movies...` ) ;
312326 }
313327
314- console . log ( `Inserted ${ inserted } /${ movies . length } movies...` ) ;
315- }
328+ await client . query ( "COMMIT" ) ;
316329
317- console . log ( `Seeding complete: ${ inserted } movies` ) ;
330+ console . log ( `Seeding complete: ${ inserted } movies` ) ;
318331
319- return successResponse ( {
320- message : "Data seeded successfully" ,
321- count : inserted ,
322- } ) ;
332+ return successResponse ( {
333+ message : "Data seeded successfully" ,
334+ count : inserted ,
335+ } ) ;
336+ } catch ( error ) {
337+ await client . query ( "ROLLBACK" ) ;
338+ throw error ;
339+ }
323340 } catch ( error ) {
324341 console . error ( "Seed error:" , error ) ;
325342 return errorResponse ( 500 , "Seeding failed" ) ;
0 commit comments