1
- import { existsSync , readFileSync , writeFileSync } from "node:fs" ;
1
+ import { appendFileSync , existsSync , mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
2
2
import path from "node:path" ;
3
3
4
4
import type { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
@@ -19,19 +19,22 @@ export function compileCacheAssetsManifestSqlFile(options: BuildOptions) {
19
19
20
20
const table = process . env . NEXT_CACHE_D1 || "tags" ;
21
21
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
+ ) ;
25
27
26
28
if ( existsSync ( rawManifestPath ) ) {
27
29
const rawManifest : RawManifest = JSON . parse ( readFileSync ( rawManifestPath , "utf-8" ) ) ;
28
30
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
+ ) ;
35
35
36
- writeFileSync ( outputPath , stmts . join ( "\n" ) ) ;
36
+ if ( values . length ) {
37
+ appendFileSync ( outputPath , `INSERT INTO tags (tag, path, revalidatedAt) VALUES ${ values . join ( ", " ) } ;` ) ;
38
+ }
39
+ }
37
40
}
0 commit comments