Finish migration prisma to drizzle.

This commit is contained in:
charlesgauthereau
2025-07-16 18:15:28 +02:00
parent 385777535f
commit de9fe33194
21 changed files with 278 additions and 293 deletions
@@ -1,26 +1,26 @@
"use client";
import { ColumnDef } from "@tanstack/react-table";
import { Badge } from "@/components/ui/badge";
import { User } from "@prisma/client";
import { updateUserAction } from "@/components/wrappers/dashboard/profile/UserForm/user-form.action";
import { useMutation } from "@tanstack/react-query";
import { toast } from "sonner";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { Trash2 } from "lucide-react";
import { deleteUserAction } from "@/components/wrappers/dashboard/profile/ButtonDeleteAccount/delete-account.action";
import { ButtonWithLoading } from "@/components/wrappers/common/button/button-with-loading";
"use client"
import {ColumnDef} from "@tanstack/react-table";
import {Badge} from "@/components/ui/badge";
import {updateUserAction} from "@/components/wrappers/dashboard/profile/UserForm/user-form.action";
import {useMutation} from "@tanstack/react-query";
import {toast} from "sonner";
import {useRouter} from "next/navigation";
import {useState} from "react";
import {Trash2} from "lucide-react";
import {deleteUserAction} from "@/components/wrappers/dashboard/profile/ButtonDeleteAccount/delete-account.action";
import {ButtonWithLoading} from "@/components/wrappers/common/button/button-with-loading";
import {User} from "@/db/schema/01_user";
import {useSession} from "@/lib/auth/auth-client";
export const usersColumnsAdmin: ColumnDef<User>[] = [
{
accessorKey: "role",
header: "Role",
cell: ({ row }) => {
cell: ({row}) => {
const [role, setRole] = useState<string>(row.getValue("role"));
const updateMutation = useMutation({
mutationFn: () => updateUserAction({ id: row.original.id, data: { role: role } }),
mutationFn: () => updateUserAction({id: row.original.id, data: {role: role}}),
onSuccess: () => {
toast.success(`User updated successfully.`);
},
@@ -53,22 +53,19 @@ export const usersColumnsAdmin: ColumnDef<User>[] = [
{
accessorKey: "updatedAt",
header: "Updated At",
cell: ({ row }) => {
return new Date(row.getValue("updatedAt")).toLocaleString("fr-FR");
},
},
{
accessorKey: "authMethod",
header: "Method",
cell: ({ row }) => {
return <Badge variant="outline">{row.getValue("authMethod")}</Badge>;
cell: ({row}) => {
return new Date(row.getValue("updatedAt")).toLocaleString("fr-FR", {
timeZone: "Europe/Paris",
});
},
},
{
header: "Action",
id: "actions",
cell: ({ row }) => {
cell: ({row}) => {
const router = useRouter();
const {data: session, isPending} = useSession();
const mutation = useMutation({
mutationFn: () => deleteUserAction(row.original.id),
onSuccess: async () => {
@@ -77,17 +74,23 @@ export const usersColumnsAdmin: ColumnDef<User>[] = [
},
});
return (
<div className="flex items-center gap-2">
<ButtonWithLoading
variant="outline"
text=""
icon={<Trash2 color="red" size={15} />}
onClick={async () => {
await mutation.mutateAsync();
}}
size="icon"
/>
{!session || session?.user.email === row.original.email ? null :
<ButtonWithLoading
variant="outline"
text=""
icon={<Trash2 color="red" size={15}/>}
onClick={async () => {
await mutation.mutateAsync();
}}
size="icon"
/>
}
</div>
);
},
@@ -1,8 +1,8 @@
"use client";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { ProjectWith } from "@/db/schema";
import Link from "next/link";
import {ProjectWith} from "@/db/schema/05_project";
export type projectCardProps = {
data: ProjectWith;
@@ -13,7 +13,7 @@ export const ProjectCard = (props: projectCardProps) => {
const { data: project, organizationSlug } = props;
return (
<Link href={`/dashboard/${organizationSlug}/projects/${project.id}`}>
<Link href={`/dashboard/projects/${project.id}`}>
<Card className="flex flex-row justify-between">
<div className="">
<CardHeader>{project.name}</CardHeader>
@@ -17,7 +17,7 @@ export const ProjectDatabaseCard = (props: projectDatabaseCardProps) => {
const { organizationSlug, data: database, extendedProps: extendedProps } = props;
return (
<Link href={`/dashboard/${organizationSlug}/projects/${extendedProps.id}/database/${database.id}`}>
<Link href={`/dashboard/projects/${extendedProps.id}/database/${database.id}`}>
<DatabaseCard data={database} />
</Link>
);
@@ -66,7 +66,7 @@ export const ProjectForm = (props: projectFormProps) => {
if (project && project.data) {
if (project.data.success) {
project.data.actionSuccess && toast.success(project.data.actionSuccess.message);
router.push(`/dashboard/${props.organization.slug}/projects/${project.data.value!.id}`);
router.push(`/dashboard/projects/${project.data.value!.id}`);
router.refresh();
} else {
project.data.actionError && toast.error(project.data.actionError.message || "Unknown error occurred.");
@@ -2,20 +2,14 @@ import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarMenu as SM,
SidebarMenuItem,
} from "@/components/ui/sidebar";
import {SidebarItem, SidebarMenu} from "@/components/wrappers/dashboard/sideBar/SideBarMenu/SideBarMenu";
import {OrganizationCombobox} from "@/components/wrappers/dashboard/organization/organization-combobox";
import {SideBarLogo} from "@/components/wrappers/dashboard/sideBar/SideBarLogo/SideBarLogo";
import {SideBarFooterCredit} from "@/components/wrappers/dashboard/sideBar/SideBarFooterCredit/SideBarFooterCredit";
import {LoggedInButton} from "@/components/wrappers/dashboard/loggedInButton/LoggedInButton";
import {Layers, ChartArea, Settings, ShieldHalf} from "lucide-react";
import {authClient} from "@/lib/auth/auth-client";
import {SidebarContentA} from "./sidebar-content";
import {getActiveMember, getOrganization} from "@/lib/auth/auth";
import {notFound} from "next/navigation";
@@ -31,7 +25,6 @@ export async function AppSidebar() {
const organization = await getOrganization({organizationId: member.organizationId});
//todo: à revoir
console.log("organization", organization);
@@ -41,6 +41,8 @@ export const SidebarContentA = () => {
});
}
const adminItems: SidebarItem[] = [
{
type: "list",
@@ -62,13 +64,24 @@ export const SidebarContentA = () => {
return (
activeOrganization && (
<>
<SidebarGroup>
<SidebarGroupLabel>Application</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu items={appItems} baseUrl={`/dashboard`} />
</SidebarGroupContent>
</SidebarGroup>
{(session && (session.user.role === "admin" || session.user.role === "superadmin")) && (
<SidebarGroup>
<SidebarGroupLabel>Administration</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu items={adminItems} baseUrl={`/dashboard`} />
</SidebarGroupContent>
</SidebarGroup>
)}
</>
<SidebarGroup>
<SidebarGroupLabel>Application</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu items={appItems} baseUrl={`/dashboard/${activeOrganization.slug}`} />
</SidebarGroupContent>
</SidebarGroup>
)
);
};
+2 -3
View File
@@ -11,7 +11,7 @@ export const database = pgTable("databases", {
agentDatabaseId: uuid("agent_database_id").notNull().defaultRandom(),
name: text("name").notNull(),
dbms: dbmsEnum("dbms").notNull(),
description: text("description").notNull(),
description: text("description"),
backupPolicy: text("backup_policy"),
isWaitingForBackup: boolean("is_waiting_for_backup").default(false).notNull(),
backupToRestore: text("backup_to_restore"),
@@ -23,8 +23,7 @@ export const database = pgTable("databases", {
lastContact: timestamp("last_contact"),
projectId: uuid("project_id")
.references(() => project.id)
.notNull(),
.references(() => project.id),
});
export const backup = pgTable(
+7
View File
@@ -1,6 +1,8 @@
import { pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
import { createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import {database} from "@/db/schema/06_database";
import {relations} from "drizzle-orm";
export const agent = pgTable("agents", {
id: uuid("id").primaryKey().defaultRandom(),
@@ -14,3 +16,8 @@ export const agent = pgTable("agents", {
export const agentSchema = createSelectSchema(agent);
export type Agent = z.infer<typeof agentSchema>;
export const agentRelations = relations(agent, ({ many }) => ({
databases: many(database),
}));
+1 -1
View File
@@ -97,7 +97,7 @@ export const auth = betterAuth({
await db.insert(drizzleDb.schemas.member).values({
userId: user.id,
organizationId: defaultOrg.id,
role: "orgOwner",
role: "owner",
});
} else {
console.warn("Default organization not found. Cannot assign member.");