Skip to content

Commit d63edc4

Browse files
committed
use a single insert statement
1 parent ea7e409 commit d63edc4

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

packages/cloudflare/src/cli/build/open-next/compile-cache-assets-manifest.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
1+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
22
import path from "node:path";
33

44
import 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

Comments
 (0)