Initial commit
This commit is contained in:
22
packages/db/src/client.ts
Normal file
22
packages/db/src/client.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { PrismaPg } from "@prisma/adapter-pg"
|
||||
import { PrismaClient } from "@prisma/client"
|
||||
import { Pool } from "pg"
|
||||
|
||||
const connectionString = process.env.DATABASE_URL
|
||||
|
||||
if (!connectionString) {
|
||||
throw new Error("DATABASE_URL is not set")
|
||||
}
|
||||
|
||||
const pool = new Pool({ connectionString })
|
||||
const adapter = new PrismaPg(pool)
|
||||
|
||||
declare global {
|
||||
var prisma: PrismaClient | undefined
|
||||
}
|
||||
|
||||
export const db = globalThis.prisma ?? new PrismaClient({ adapter })
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
globalThis.prisma = db
|
||||
}
|
||||
2
packages/db/src/index.ts
Normal file
2
packages/db/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { db } from "./client"
|
||||
export { createPost, listPosts } from "./posts"
|
||||
19
packages/db/src/posts.ts
Normal file
19
packages/db/src/posts.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { upsertPostSchema } from "@cms/content"
|
||||
|
||||
import { db } from "./client"
|
||||
|
||||
export async function listPosts() {
|
||||
return db.post.findMany({
|
||||
orderBy: {
|
||||
updatedAt: "desc",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function createPost(input: unknown) {
|
||||
const payload = upsertPostSchema.parse(input)
|
||||
|
||||
return db.post.create({
|
||||
data: payload,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user