committing summative
This commit is contained in:
1 parent
73ac8e85be
commit
95b138f29e
44 files changed
+5319
-755
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import { PrismaClient } from "../lib/generated/prisma/client"
|
||||
import "dotenv/config"; // can access .env when run directly
|
||||
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
|
||||
if (!databaseUrl) {
|
||||
throw new Error("Database URL required in .env");
|
||||
}
|
||||
|
||||
const prisma = new PrismaClient({
|
||||
adapter: new PrismaPg({ connectionString:databaseUrl }),
|
||||
});
|
||||
|
||||
async function main() {
|
||||
const password = process.env.SEED_ACCOUNT_PASSWORD;
|
||||
|
||||
if (!password) {
|
||||
throw new Error("A seed account password is required");
|
||||
}
|
||||
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
|
||||
const user = await prisma.users.upsert({
|
||||
where: {
|
||||
email: "seededTest@test.test",
|
||||
},
|
||||
update: {}, // if found do nothing
|
||||
create: {
|
||||
name: "seededTest",
|
||||
email: "seededTest@test.test",
|
||||
passwordHash,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.newsletters.upsert({
|
||||
where: {
|
||||
id: "seed-newsletter-1",
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
id: "seed-newsletter-1",
|
||||
title: "Example newsletter",
|
||||
imageUrl: "https://images.unsplash.com/photo-1793645915308-bbb4be832480?q=80&w=1675&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=NHwxMjA3fDA%3D",
|
||||
content: "This is a sample newsletter for testing the CMS.",
|
||||
authorID: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Seeded database with a user and newsletter");
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((error) => { // needed to use .catch to handle error from promise which we cant await as outside async func
|
||||
console.error("Seeding failed:", error);
|
||||
process.exitCode = 1;
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in new issue
Block a user