Initial commit
This commit is contained in:
37
packages/db/package.json
Normal file
37
packages/db/package.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@cms/db",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"lint": "biome check src prisma/seed.ts",
|
||||
"typecheck": "tsc -p tsconfig.json --noEmit",
|
||||
"db:generate": "bun --env-file=../../.env prisma generate",
|
||||
"db:migrate": "bun --env-file=../../.env prisma migrate dev",
|
||||
"db:push": "bun --env-file=../../.env prisma db push",
|
||||
"db:studio": "bun --env-file=../../.env prisma studio",
|
||||
"db:seed": "bun --env-file=../../.env prisma/seed.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cms/content": "workspace:*",
|
||||
"@prisma/adapter-pg": "latest",
|
||||
"@prisma/client": "latest",
|
||||
"pg": "latest",
|
||||
"zod": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cms/config": "workspace:*",
|
||||
"@biomejs/biome": "latest",
|
||||
"@types/node": "latest",
|
||||
"@types/pg": "latest",
|
||||
"prisma": "latest",
|
||||
"typescript": "latest"
|
||||
},
|
||||
"prisma": {
|
||||
"seed": "bun --env-file=../../.env prisma/seed.ts"
|
||||
}
|
||||
}
|
||||
12
packages/db/prisma.config.ts
Normal file
12
packages/db/prisma.config.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { defineConfig, env } from "prisma/config"
|
||||
|
||||
export default defineConfig({
|
||||
schema: "prisma/schema.prisma",
|
||||
migrations: {
|
||||
path: "prisma/migrations",
|
||||
seed: "bun prisma/seed.ts",
|
||||
},
|
||||
datasource: {
|
||||
url: env("DATABASE_URL"),
|
||||
},
|
||||
})
|
||||
18
packages/db/prisma/schema.prisma
Normal file
18
packages/db/prisma/schema.prisma
Normal file
@@ -0,0 +1,18 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
}
|
||||
|
||||
model Post {
|
||||
id String @id @default(uuid())
|
||||
title String
|
||||
slug String @unique
|
||||
excerpt String?
|
||||
body String
|
||||
status String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
24
packages/db/prisma/seed.ts
Normal file
24
packages/db/prisma/seed.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { db } from "../src/client"
|
||||
|
||||
async function main() {
|
||||
await db.post.upsert({
|
||||
where: { slug: "welcome" },
|
||||
update: {},
|
||||
create: {
|
||||
title: "Welcome to your CMS",
|
||||
slug: "welcome",
|
||||
excerpt: "Your first seeded post",
|
||||
body: "Edit or delete this post from your admin area.",
|
||||
status: "published",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((error) => {
|
||||
console.error(error)
|
||||
process.exit(1)
|
||||
})
|
||||
.finally(async () => {
|
||||
await db.$disconnect()
|
||||
})
|
||||
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,
|
||||
})
|
||||
}
|
||||
9
packages/db/tsconfig.json
Normal file
9
packages/db/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "@cms/config/tsconfig/base",
|
||||
"compilerOptions": {
|
||||
"noEmit": false,
|
||||
"types": ["node"],
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src/**/*.ts", "prisma/**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user