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,54 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
// Get a free hosted Postgres database in seconds: `npx create-db`
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../lib/generated/prisma"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
}
|
||||
|
||||
enum Role {
|
||||
User
|
||||
Moderator
|
||||
Admin
|
||||
}
|
||||
|
||||
enum NewsletterStatus {
|
||||
Draft
|
||||
Schedule
|
||||
Sent
|
||||
}
|
||||
|
||||
model Users {
|
||||
id String @id @default(cuid()) // prsima generates cuid - generated text id
|
||||
email String @unique
|
||||
name String?
|
||||
passwordHash String? // g-accounts dont have pword
|
||||
googleId String? @unique // stable key for g-accounts
|
||||
role Role @default(User)
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
newsletters Newsletters[]
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
model Newsletters {
|
||||
id String @id @default(cuid())
|
||||
title String
|
||||
imageUrl String
|
||||
content String
|
||||
authorID String
|
||||
author Users @relation(fields: [authorID], references: [id], onDelete: Cascade) // when the associated user is deleted so is the newsletter
|
||||
status NewsletterStatus @default(Draft)
|
||||
likes Int @default(0)
|
||||
comments String[] @default([])
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
}
|
||||
@@ -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