feat: add categories and favorites schemas with integrations

Added `categories` and `favorites` database schemas with full type inference support. Integrated categories into `content` schema with new properties (`categoryId`, `slug`, `views`, and `usageCount`). Updated `tags` schema to include `userId` with reference to `users`. Exported new schemas in index for broader usage.
This commit is contained in:
Mathis HERRIOT
2026-01-08 15:25:51 +01:00
parent fe309bc1e3
commit 912394477b
5 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import { index, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
export const categories = pgTable(
"categories",
{
id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 64 }).notNull().unique(),
slug: varchar("slug", { length: 64 }).notNull().unique(),
description: varchar("description", { length: 255 }),
iconUrl: varchar("icon_url", { length: 512 }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => ({
slugIdx: index("categories_slug_idx").on(table.slug),
}),
);
export type CategoryInDb = typeof categories.$inferSelect;
export type NewCategoryInDb = typeof categories.$inferInsert;

View File

@@ -8,6 +8,7 @@ import {
uuid, uuid,
varchar, varchar,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { categories } from "./categories";
import { tags } from "./tags"; import { tags } from "./tags";
import { users } from "./users"; import { users } from "./users";
@@ -21,10 +22,16 @@ export const contents = pgTable(
.notNull() .notNull()
.references(() => users.uuid, { onDelete: "cascade" }), .references(() => users.uuid, { onDelete: "cascade" }),
type: contentType("type").notNull(), type: contentType("type").notNull(),
categoryId: uuid("category_id").references(() => categories.id, {
onDelete: "set null",
}),
title: varchar("title", { length: 255 }).notNull(), title: varchar("title", { length: 255 }).notNull(),
slug: varchar("slug", { length: 255 }).notNull().unique(),
storageKey: varchar("storage_key", { length: 512 }).notNull().unique(), // Clé interne S3 storageKey: varchar("storage_key", { length: 512 }).notNull().unique(), // Clé interne S3
mimeType: varchar("mime_type", { length: 128 }).notNull(), // Pour le Content-Type HTTP mimeType: varchar("mime_type", { length: 128 }).notNull(), // Pour le Content-Type HTTP
fileSize: integer("file_size").notNull(), // Taille en octets fileSize: integer("file_size").notNull(), // Taille en octets
views: integer("views").notNull().default(0),
usageCount: integer("usage_count").notNull().default(0),
createdAt: timestamp("created_at", { withTimezone: true }) createdAt: timestamp("created_at", { withTimezone: true })
.notNull() .notNull()
.defaultNow(), .defaultNow(),

View File

@@ -0,0 +1,24 @@
import { pgTable, primaryKey, timestamp, uuid } from "drizzle-orm/pg-core";
import { contents } from "./content";
import { users } from "./users";
export const favorites = pgTable(
"favorites",
{
userId: uuid("user_id")
.notNull()
.references(() => users.uuid, { onDelete: "cascade" }),
contentId: uuid("content_id")
.notNull()
.references(() => contents.id, { onDelete: "cascade" }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(t) => ({
pk: primaryKey({ columns: [t.userId, t.contentId] }),
}),
);
export type FavoriteInDb = typeof favorites.$inferSelect;
export type NewFavoriteInDb = typeof favorites.$inferInsert;

View File

@@ -1,6 +1,8 @@
export * from "./api_keys"; export * from "./api_keys";
export * from "./audit_logs"; export * from "./audit_logs";
export * from "./categories";
export * from "./content"; export * from "./content";
export * from "./favorites";
export * from "./rbac"; export * from "./rbac";
export * from "./reports"; export * from "./reports";
export * from "./sessions"; export * from "./sessions";

View File

@@ -1,4 +1,5 @@
import { index, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; import { index, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
import { users } from "./users";
export const tags = pgTable( export const tags = pgTable(
"tags", "tags",
@@ -6,6 +7,9 @@ export const tags = pgTable(
id: uuid("id").primaryKey().defaultRandom(), id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 64 }).notNull().unique(), name: varchar("name", { length: 64 }).notNull().unique(),
slug: varchar("slug", { length: 64 }).notNull().unique(), slug: varchar("slug", { length: 64 }).notNull().unique(),
userId: uuid("user_id").references(() => users.uuid, {
onDelete: "set null",
}), // Créateur du tag
createdAt: timestamp("created_at", { withTimezone: true }) createdAt: timestamp("created_at", { withTimezone: true })
.notNull() .notNull()
.defaultNow(), .defaultNow(),