1- import { existsSync , readFileSync , writeFileSync } from "node:fs" ;
1+ import { appendFileSync , existsSync , mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
22import path from "node:path" ;
33
44import type { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
@@ -19,19 +19,22 @@ export function compileCacheAssetsManifestSqlFile(options: BuildOptions) {
1919
2020 const table = process . env . NEXT_CACHE_D1 || "tags" ;
2121
22- const stmts = [
23- `CREATE TABLE IF NOT EXISTS ${ table } (tag TEXT NOT NULL, path TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag, path) ON CONFLICT REPLACE);` ,
24- ] ;
22+ mkdirSync ( path . dirname ( outputPath ) , { recursive : true } ) ;
23+ writeFileSync (
24+ outputPath ,
25+ `CREATE TABLE IF NOT EXISTS ${ table } (tag TEXT NOT NULL, path TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag, path) ON CONFLICT REPLACE);\n`
26+ ) ;
2527
2628 if ( existsSync ( rawManifestPath ) ) {
2729 const rawManifest : RawManifest = JSON . parse ( readFileSync ( rawManifestPath , "utf-8" ) ) ;
2830
29- rawManifest . forEach ( ( { tag : { S : tag } , path : { S : path } , revalidatedAt : { N : revalidatedAt } } ) => {
30- stmts . push (
31- `INSERT INTO ${ table } (tag, path, revalidatedAt) VALUES ('${ tag } ', '${ path } ', ${ revalidatedAt } );`
32- ) ;
33- } ) ;
34- }
31+ const values = rawManifest . map (
32+ ( { tag, path, revalidatedAt } ) =>
33+ `(${ JSON . stringify ( tag . S ) } , ${ JSON . stringify ( path . S ) } , ${ revalidatedAt . N } )`
34+ ) ;
3535
36- writeFileSync ( outputPath , stmts . join ( "\n" ) ) ;
36+ if ( values . length ) {
37+ appendFileSync ( outputPath , `INSERT INTO tags (tag, path, revalidatedAt) VALUES ${ values . join ( ", " ) } ;` ) ;
38+ }
39+ }
3740}
0 commit comments