64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { PrismaClient } from "@prisma/client";
|
|
|
|
const prisma = new PrismaClient();
|
|
const id = (value: string) => createHash("sha256").update(value).digest("hex");
|
|
|
|
async function seed(): Promise<void> {
|
|
const accounts = await Promise.all(
|
|
[
|
|
{ key: "demo-a", nickname: "海风" },
|
|
{ key: "demo-b", nickname: "星河" },
|
|
].map(({ key, nickname }) =>
|
|
prisma.account.upsert({
|
|
where: { phoneHmac: id(`drift-bottle:${key}:phone-hmac`) },
|
|
update: {
|
|
anonymousProfile: {
|
|
upsert: { create: { nickname }, update: { nickname } },
|
|
},
|
|
},
|
|
create: {
|
|
phoneCiphertext: Buffer.from(
|
|
id(`drift-bottle:${key}:ciphertext`),
|
|
"hex",
|
|
),
|
|
phoneHmac: id(`drift-bottle:${key}:phone-hmac`),
|
|
anonymousProfile: { create: { nickname } },
|
|
},
|
|
}),
|
|
),
|
|
);
|
|
|
|
const author = accounts[0];
|
|
if (!author) throw new Error("Seed author was not created");
|
|
|
|
await prisma.bottle.upsert({
|
|
where: { id: "00000000-0000-4000-8000-000000000101" },
|
|
update: {
|
|
authorId: author.id,
|
|
contentText: "愿你今天遇见温柔。",
|
|
status: "IN_POOL",
|
|
},
|
|
create: {
|
|
id: "00000000-0000-4000-8000-000000000101",
|
|
authorId: author.id,
|
|
contentText: "愿你今天遇见温柔。",
|
|
status: "IN_POOL",
|
|
},
|
|
});
|
|
|
|
console.info(
|
|
"Seed complete: 2 demo accounts, 2 anonymous profiles, 1 in-pool bottle.",
|
|
);
|
|
}
|
|
|
|
seed()
|
|
.catch((error: unknown) => {
|
|
console.error(
|
|
"Seed failed",
|
|
error instanceof Error ? error.message : "unknown error",
|
|
);
|
|
process.exitCode = 1;
|
|
})
|
|
.finally(async () => prisma.$disconnect());
|