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:
24
backend/src/database/schemas/categories.ts
Normal file
24
backend/src/database/schemas/categories.ts
Normal 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;
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
uuid,
|
||||
varchar,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { categories } from "./categories";
|
||||
import { tags } from "./tags";
|
||||
import { users } from "./users";
|
||||
|
||||
@@ -21,10 +22,16 @@ export const contents = pgTable(
|
||||
.notNull()
|
||||
.references(() => users.uuid, { onDelete: "cascade" }),
|
||||
type: contentType("type").notNull(),
|
||||
categoryId: uuid("category_id").references(() => categories.id, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
title: varchar("title", { length: 255 }).notNull(),
|
||||
slug: varchar("slug", { length: 255 }).notNull().unique(),
|
||||
storageKey: varchar("storage_key", { length: 512 }).notNull().unique(), // Clé interne S3
|
||||
mimeType: varchar("mime_type", { length: 128 }).notNull(), // Pour le Content-Type HTTP
|
||||
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 })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
|
||||
24
backend/src/database/schemas/favorites.ts
Normal file
24
backend/src/database/schemas/favorites.ts
Normal 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;
|
||||
@@ -1,6 +1,8 @@
|
||||
export * from "./api_keys";
|
||||
export * from "./audit_logs";
|
||||
export * from "./categories";
|
||||
export * from "./content";
|
||||
export * from "./favorites";
|
||||
export * from "./rbac";
|
||||
export * from "./reports";
|
||||
export * from "./sessions";
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { index, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
|
||||
import { users } from "./users";
|
||||
|
||||
export const tags = pgTable(
|
||||
"tags",
|
||||
@@ -6,6 +7,9 @@ export const tags = pgTable(
|
||||
id: uuid("id").primaryKey().defaultRandom(),
|
||||
name: varchar("name", { 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 })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
|
||||
Reference in New Issue
Block a user