From c02f2d5036a65cde3bfee5bfdcdd6cf89eee3acb Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Sat, 15 Jun 2024 20:17:45 +0800 Subject: [PATCH] create post table and entity --- packages/api/src/entity/post.ts | 45 +++++++++++++++++++++++ packages/db/migrations/0183.do.post.sql | 24 ++++++++++++ packages/db/migrations/0183.undo.post.sql | 11 ++++++ 3 files changed, 80 insertions(+) create mode 100644 packages/api/src/entity/post.ts create mode 100755 packages/db/migrations/0183.do.post.sql create mode 100755 packages/db/migrations/0183.undo.post.sql diff --git a/packages/api/src/entity/post.ts b/packages/api/src/entity/post.ts new file mode 100644 index 000000000..2451c7817 --- /dev/null +++ b/packages/api/src/entity/post.ts @@ -0,0 +1,45 @@ +import { + Column, + Entity, + JoinColumn, + ManyToOne, + PrimaryGeneratedColumn, +} from 'typeorm' +import { User } from './user' + +@Entity({ name: 'post' }) +export class Post { + @PrimaryGeneratedColumn('uuid') + id!: string + + @Column('uuid') + userId!: string + + @ManyToOne(() => User) + @JoinColumn({ name: 'user_id' }) + user!: User + + @Column('uuid', { array: true }) + libraryItemIds!: string[] + + @Column('uuid', { array: true }) + highlightIds!: string[] + + @Column('text') + title!: string + + @Column('text') + content!: string + + @Column('text', { nullable: true }) + thumbnail?: string + + @Column('text', { nullable: true }) + thought?: string + + @Column('timestamptz') + createdAt!: Date + + @Column('timestamptz') + updatedAt!: Date +} diff --git a/packages/db/migrations/0183.do.post.sql b/packages/db/migrations/0183.do.post.sql new file mode 100755 index 000000000..26f1d5e31 --- /dev/null +++ b/packages/db/migrations/0183.do.post.sql @@ -0,0 +1,24 @@ +-- Type: DO +-- Name: post +-- Description: Create a post table + +BEGIN; + +CREATE TABLE omnivore.post ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v1mc(), + user_id UUID NOT NULL REFERENCES omnivore.user(id) ON DELETE CASCADE, + library_item_ids UUID[], + highlight_ids UUID[], + title TEXT NOT NULL, + content TEXT NOT NULL, -- generated from template + thumbnail TEXT, + thought TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX post_user_id_idx ON omnivore.post(user_id); + +ALTER TABLE omnivore.user_profile ALTER COLUMN private SET DEFAULT true; + +COMMIT; diff --git a/packages/db/migrations/0183.undo.post.sql b/packages/db/migrations/0183.undo.post.sql new file mode 100755 index 000000000..aa74c9420 --- /dev/null +++ b/packages/db/migrations/0183.undo.post.sql @@ -0,0 +1,11 @@ +-- Type: UNDO +-- Name: post +-- Description: Create a post table + +BEGIN; + +ALTER TABLE omnivore.user_profile ALTER COLUMN private SET DEFAULT false; + +DROP TABLE omnivore.post; + +COMMIT;