Skip to content

Commit 05f210e

Browse files
committed
Add dump script
1 parent 4d0f244 commit 05f210e

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

src/prisma/dump.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable no-console */
2+
const { PrismaClient } = require("@prisma/client");
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
const prisma = new PrismaClient();
7+
8+
async function main() {
9+
const pages = await prisma.page.findMany({
10+
include: {
11+
finalDraft: true,
12+
},
13+
});
14+
15+
const output = {};
16+
17+
for (const page of pages) {
18+
if (!page.finalDraft) {
19+
console.warn(`Skipping "${page.name}" (${page.path}) — no published draft.`);
20+
continue;
21+
}
22+
23+
output[page.path] = page.finalDraft.content;
24+
}
25+
26+
const count = Object.keys(output).length;
27+
28+
if (count === 0) {
29+
console.warn("No published pages found.");
30+
return;
31+
}
32+
33+
const json = JSON.stringify(output, null, 4) + "\n";
34+
const outputPath = process.argv[2];
35+
36+
if (outputPath) {
37+
fs.writeFileSync(path.resolve(outputPath), json, "utf-8");
38+
console.info(`Dumped ${count} page(s) to ${outputPath}`);
39+
} else {
40+
process.stdout.write(json);
41+
}
42+
}
43+
44+
main()
45+
.catch((error) => {
46+
console.error("Failed to dump database", error);
47+
process.exit(1);
48+
})
49+
.finally(async () => {
50+
await prisma.$disconnect();
51+
});

0 commit comments

Comments
 (0)