💨

【Drizzle ORM】NextJs14 と Drizzle ORM【#15 Drizzle One to Many2 】

2024/08/13に公開

【#15 Drizzle One to Many2 】

YouTube: https://youtu.be/UNFVePRREYg
https://youtu.be/UNFVePRREYg

今回も引き続き、Drizzle ORM のスキーマの設定について見ていきます。
今回は「One-to-many」のリレーションの設定についてさらに実装をしていきます。

https://orm.drizzle.team/docs/rqb#one-to-many

「comments」のテーブルを作成して、
「posts」と「users」のテーブルそれぞれに対して紐づけを行います。

db/schema.ts
import { relations } from "drizzle-orm";
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";

export const users = pgTable("users_table", {
  id: text("id").primaryKey(),
  clerkId: text("clerk_id").notNull().unique(),
  name: text("name").notNull(),
  email: text("email").notNull().unique(),
  imageUrl: text("image_url"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at")
    .notNull()
    .$onUpdate(() => new Date()),
});

export const usersRelations = relations(users, ({ one, many }) => ({
  profile: one(profile),
  posts: many(posts),
  comments: many(comments),
}));

export const insertUsersSchema = createInsertSchema(users);

export const profile = pgTable("profile_table", {
  id: text("id").primaryKey(),
  userId: text("user_id").references(() => users.id, { onDelete: "cascade" }),
  message: text("message"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at")
    .notNull()
    .$onUpdate(() => new Date()),
});

export const insertProfileSchema = createInsertSchema(profile);

export const posts = pgTable("posts_table", {
  id: text("id").primaryKey(),
  userId: text("user_id").references(() => users.id),
  content: text("content"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at")
    .notNull()
    .$onUpdate(() => new Date()),
});

export const postsRelations = relations(posts, ({ one, many }) => ({
  author: one(users, {
    fields: [posts.userId],
    references: [users.id],
  }),
  comments: many(comments),
}));

export const insertPostsSchema = createInsertSchema(posts);

export const comments = pgTable("comments_table", {
  id: text("id").primaryKey(),
  userId: text("user_id").references(() => users.id),
  postId: text("post_id").references(() => posts.id),
  comment: text("comment"),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at")
    .notNull()
    .$onUpdate(() => new Date()),
});

export const commentsRelations = relations(comments, ({ one, many }) => ({
  user: one(users, {
    fields: [comments.userId],
    references: [users.id],
  }),
  post: one(posts, {
    fields: [comments.postId],
    references: [posts.id],
  }),
}));

export const insertCommentsSchema = createInsertSchema(comments);

Discussion