Initial commit

This commit is contained in:
2026-02-10 01:25:57 +01:00
commit 7020a6a339
53 changed files with 1503 additions and 0 deletions

37
packages/db/package.json Normal file
View 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"
}
}

View 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"),
},
})

View 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
}

View 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
View 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
View File

@@ -0,0 +1,2 @@
export { db } from "./client"
export { createPost, listPosts } from "./posts"

19
packages/db/src/posts.ts Normal file
View 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,
})
}

View File

@@ -0,0 +1,9 @@
{
"extends": "@cms/config/tsconfig/base",
"compilerOptions": {
"noEmit": false,
"types": ["node"],
"outDir": "dist"
},
"include": ["src/**/*.ts", "prisma/**/*.ts"]
}