REFACTORED - migrate the data layer from Prisma to Drizzle (unify the stack)
- Add lib/db (Drizzle schema: 7 tables + 6 enums + relations, postgres.js client), drizzle.config.ts, lib/db/enums.ts (Prisma-compatible enum objects) - Rewrite all 14 app consumers + 4 admin components to Drizzle - Move the test DB layer to Drizzle + in-process PGlite; convert all 8 integration test files + factories (371 tests green) - Remove Prisma: deps, prisma/ schema+migrations, lib/prisma.ts, Dockerfile prisma generate; wire db:generate/migrate/push/studio to drizzle-kit; update Makefile - Preserve the old seed as scripts/legacy-prisma-seed.cjs (needs a Drizzle rewrite)
This commit is contained in:
+58
-37
@@ -1,6 +1,13 @@
|
||||
import { MediaKind, MediaSource, MediaUsageType, PortfolioSectionType } from "@prisma/client";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db } from "@/lib/db";
|
||||
import {
|
||||
category,
|
||||
mediaAsset,
|
||||
mediaUsage,
|
||||
portfolioAsset,
|
||||
portfolioProject,
|
||||
portfolioSection,
|
||||
} from "@/lib/db/schema";
|
||||
import { MediaKind, MediaSource, MediaUsageType, PortfolioSectionType } from "@/lib/db/enums";
|
||||
|
||||
let counter = 0;
|
||||
function uniq(prefix: string) {
|
||||
@@ -8,11 +15,11 @@ function uniq(prefix: string) {
|
||||
return `${prefix}-${counter}`;
|
||||
}
|
||||
|
||||
export function createCategory(overrides: Record<string, unknown> = {}) {
|
||||
const slug = (overrides.slug as string) ?? uniq("cat");
|
||||
return prisma.category.create({
|
||||
data: {
|
||||
slug,
|
||||
export async function createCategory(overrides: Record<string, unknown> = {}) {
|
||||
const [row] = await db
|
||||
.insert(category)
|
||||
.values({
|
||||
slug: (overrides.slug as string) ?? uniq("cat"),
|
||||
nameAr: "الاسم",
|
||||
nameEn: "Name",
|
||||
nameDe: "Name",
|
||||
@@ -22,16 +29,19 @@ export function createCategory(overrides: Record<string, unknown> = {}) {
|
||||
sortOrder: 0,
|
||||
isActive: true,
|
||||
...overrides,
|
||||
},
|
||||
});
|
||||
} as typeof category.$inferInsert)
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function createProject(overrides: Record<string, unknown> = {}) {
|
||||
const categoryId = (overrides.categoryId as string) ?? (await createCategory()).id;
|
||||
const isPublished = (overrides.isPublished as boolean) ?? true;
|
||||
const slug = (overrides.slug as string) ?? uniq("proj");
|
||||
return prisma.portfolioProject.create({
|
||||
data: {
|
||||
const [row] = await db
|
||||
.insert(portfolioProject)
|
||||
.values({
|
||||
categoryId,
|
||||
slug,
|
||||
viewMode: "GRID",
|
||||
@@ -51,13 +61,16 @@ export async function createProject(overrides: Record<string, unknown> = {}) {
|
||||
...overrides,
|
||||
isPublished,
|
||||
publishedAt: isPublished ? new Date() : null,
|
||||
},
|
||||
});
|
||||
} as typeof portfolioProject.$inferInsert)
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function createSection(projectId: string, overrides: Record<string, unknown> = {}) {
|
||||
return prisma.portfolioSection.create({
|
||||
data: {
|
||||
export async function createSection(projectId: string, overrides: Record<string, unknown> = {}) {
|
||||
const [row] = await db
|
||||
.insert(portfolioSection)
|
||||
.values({
|
||||
projectId,
|
||||
type: PortfolioSectionType.RICH_TEXT,
|
||||
titleAr: "ع",
|
||||
@@ -68,13 +81,16 @@ export function createSection(projectId: string, overrides: Record<string, unkno
|
||||
bodyDe: "b",
|
||||
sortOrder: 0,
|
||||
...overrides,
|
||||
},
|
||||
});
|
||||
} as typeof portfolioSection.$inferInsert)
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function createAsset(projectId: string, overrides: Record<string, unknown> = {}) {
|
||||
return prisma.portfolioAsset.create({
|
||||
data: {
|
||||
export async function createAsset(projectId: string, overrides: Record<string, unknown> = {}) {
|
||||
const [row] = await db
|
||||
.insert(portfolioAsset)
|
||||
.values({
|
||||
projectId,
|
||||
kind: "IMAGE",
|
||||
filePath: "/uploads/media/assets/x.svg",
|
||||
@@ -83,35 +99,40 @@ export function createAsset(projectId: string, overrides: Record<string, unknown
|
||||
altDe: "a",
|
||||
sortOrder: 0,
|
||||
...overrides,
|
||||
},
|
||||
});
|
||||
} as typeof portfolioAsset.$inferInsert)
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function createMediaAsset(overrides: Record<string, unknown> = {}) {
|
||||
return prisma.mediaAsset.create({
|
||||
data: {
|
||||
export async function createMediaAsset(overrides: Record<string, unknown> = {}) {
|
||||
const [row] = await db
|
||||
.insert(mediaAsset)
|
||||
.values({
|
||||
source: MediaSource.EXTERNAL,
|
||||
kind: MediaKind.IMAGE,
|
||||
url: (overrides.url as string) ?? `https://cdn.example.com/${uniq("img")}.png`,
|
||||
fileName: "img.png",
|
||||
label: "Image",
|
||||
...overrides,
|
||||
},
|
||||
});
|
||||
} as typeof mediaAsset.$inferInsert)
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function createMediaUsage(
|
||||
assetId: string,
|
||||
overrides: Record<string, unknown> = {},
|
||||
) {
|
||||
return prisma.mediaUsage.create({
|
||||
data: {
|
||||
export async function createMediaUsage(assetId: string, overrides: Record<string, unknown> = {}) {
|
||||
const [row] = await db
|
||||
.insert(mediaUsage)
|
||||
.values({
|
||||
assetId,
|
||||
usageType: MediaUsageType.GENERIC,
|
||||
entityType: "test-entity",
|
||||
entityId: "e1",
|
||||
fieldKey: uniq("field"),
|
||||
...overrides,
|
||||
},
|
||||
});
|
||||
} as typeof mediaUsage.$inferInsert)
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user