1- import { appendFileSync , existsSync , mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
1+ import { appendFileSync , mkdirSync , writeFileSync } from "node:fs" ;
22import path from "node:path" ;
33
44import type { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
5-
6- type RawManifest = {
7- tag : { S : string } ;
8- path : { S : string } ;
9- revalidatedAt : { N : string } ;
10- } [ ] ;
5+ import type { TagCacheMetaFile } from "@opennextjs/aws/types/open-next.js" ;
116
127/**
138 * Generates SQL statements that can be used to initialise the cache assets manifest in an SQL data store.
149 */
15- export function compileCacheAssetsManifestSqlFile ( options : BuildOptions ) {
16- // TODO: Expose the function for getting this data as an adapter-agnostic utility in AWS.
17- const rawManifestPath = path . join ( options . outputDir , "dynamodb-provider/dynamodb-cache.json" ) ;
10+ export function compileCacheAssetsManifestSqlFile ( options : BuildOptions , metaFiles : TagCacheMetaFile [ ] ) {
1811 const outputPath = path . join ( options . outputDir , "cloudflare/cache-assets-manifest.sql" ) ;
1912
2013 const tagsTable = process . env . NEXT_CACHE_D1_TAGS_TABLE || "tags" ;
@@ -27,18 +20,12 @@ export function compileCacheAssetsManifestSqlFile(options: BuildOptions) {
2720 CREATE TABLE IF NOT EXISTS ${ JSON . stringify ( revalidationsTable ) } (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);\n`
2821 ) ;
2922
30- if ( existsSync ( rawManifestPath ) ) {
31- const rawManifest : RawManifest = JSON . parse ( readFileSync ( rawManifestPath , "utf-8" ) ) ;
23+ const values = metaFiles . map ( ( { tag, path } ) => `(${ JSON . stringify ( tag . S ) } , ${ JSON . stringify ( path . S ) } )` ) ;
3224
33- const values = rawManifest . map (
34- ( { tag, path } ) => `(${ JSON . stringify ( tag . S ) } , ${ JSON . stringify ( path . S ) } )`
25+ if ( values . length ) {
26+ appendFileSync (
27+ outputPath ,
28+ `INSERT INTO ${ JSON . stringify ( tagsTable ) } (tag, path) VALUES ${ values . join ( ", " ) } ;`
3529 ) ;
36-
37- if ( values . length ) {
38- appendFileSync (
39- outputPath ,
40- `INSERT INTO ${ JSON . stringify ( tagsTable ) } (tag, path) VALUES ${ values . join ( ", " ) } ;`
41- ) ;
42- }
4330 }
4431}
0 commit comments