diesel_database_design/entity/post.rs
1//! `post` table's entity.
2
3use chrono::{DateTime, Utc};
4use diesel::prelude::*;
5use uuid::Uuid;
6
7/// ## テーブル情報
8///
9/// | 項目 | 内容 |
10/// |--------------|------------------------------------|
11/// | 論理名 | 投稿テーブル |
12/// | 物理名 | `posts` |
13/// | 説明 | ユーザーによる投稿情報を格納する。 |
14#[derive(Queryable, Selectable, Debug, Clone, PartialEq, Eq)]
15#[diesel(table_name = crate::schema::posts)]
16#[diesel(check_for_backend(diesel::pg::Pg))]
17pub struct Post {
18 /// ### カラム情報
19 ///
20 /// | 項目 | 内容 |
21 /// |--------------|-------------------------------------|
22 /// | 論理名 | 投稿ID |
23 /// | 物理名 | `id` |
24 /// | データ型 | `UUID` |
25 /// | 説明 | 投稿を一位に識別するID。 |
26 /// | 制約 | `PRIMARY KEY`, `DEFAULT uuid_generate_v7()` |
27 pub id: Uuid,
28
29 /// ### カラム情報
30 ///
31 /// | 項目 | 内容 |
32 /// |--------------|-------------------------------------|
33 /// | 論理名 | タイトル |
34 /// | 物理名 | `title` |
35 /// | データ型 | `VARCHAR` |
36 /// | 説明 | 投稿のタイトル。 |
37 /// | 制約 | `NOT NULL` |
38 pub title: String,
39
40 /// ### カラム情報
41 ///
42 /// | 項目 | 内容 |
43 /// |--------------|-------------------------------------|
44 /// | 論理名 | 本文 |
45 /// | 物理名 | `text` |
46 /// | データ型 | `TEXT` |
47 /// | 説明 | 投稿の本文。 |
48 /// | 制約 | `NOT NULL` |
49 pub text: String,
50
51 /// ### カラム情報
52 ///
53 /// | 項目 | 内容 |
54 /// |--------------|-------------------------------------|
55 /// | 論理名 | 投稿者ID |
56 /// | 物理名 | `user_id` |
57 /// | データ型 | `UUID` |
58 /// | 説明 | この投稿を作成したユーザーのID。 |
59 /// | 制約 | `NOT NULL`, `FK(users.id)` |
60 pub user_id: Uuid,
61
62 /// ### カラム情報
63 ///
64 /// | 項目 | 内容 |
65 /// |--------------|-------------------------------------|
66 /// | 論理名 | 作成日時 |
67 /// | 物理名 | `created_at` |
68 /// | データ型 | `TIMESTAMP WITH TIME ZONE` |
69 /// | 説明 | レコードが作成された日時。 |
70 /// | 制約 | `NOT NULL`, `DEFAULT NOW()` |
71 pub created_at: DateTime<Utc>,
72
73 /// ### カラム情報
74 ///
75 /// | 項目 | 内容 |
76 /// |--------------|-------------------------------------|
77 /// | 論理名 | 更新日時 |
78 /// | 物理名 | `updated_at` |
79 /// | データ型 | `TIMESTAMP WITH TIME ZONE` |
80 /// | 説明 | レコードが最後に更新された日時。 |
81 /// | 制約 | `NOT NULL`, `DEFAULT NOW()` |
82 pub updated_at: DateTime<Utc>,
83}
84
85/// 投稿作成用の構造体
86#[derive(Insertable, Debug)]
87#[diesel(table_name = crate::schema::posts)]
88pub struct NewPost<'a> {
89 pub title: &'a str,
90 pub text: &'a str,
91 pub user_id: Uuid,
92}