Skip to content

Commit aadb4d6

Browse files
Init Documents scheme
1 parent 0a5fc11 commit aadb4d6

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/db/schemas/common-fileds.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { text, timestamp } from 'drizzle-orm/pg-core'
2+
3+
export const timestamps = {
4+
created_at: timestamp('created_at').defaultNow().notNull(),
5+
updated_at: timestamp('updated_at')
6+
.defaultNow()
7+
.notNull()
8+
.$onUpdateFn(() => new Date()),
9+
}
10+
11+
export const randomId = {
12+
id: text('id')
13+
.primaryKey()
14+
.$defaultFn(() => crypto.randomUUID()),
15+
}

src/db/schemas/documents.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { pgTable, text, integer, boolean } from 'drizzle-orm/pg-core'
2+
import { users } from './auth'
3+
import { timestamps, randomId } from './common-fileds'
4+
5+
// Main documents table
6+
export const documents = pgTable('documents', {
7+
...randomId,
8+
title: text('title').notNull(),
9+
description: text('description'),
10+
author_id: text('author_id').references(() => users.id, {
11+
onDelete: 'set null',
12+
}),
13+
is_published: boolean('is_published').notNull().default(false),
14+
...timestamps,
15+
})
16+
17+
// Text blocks
18+
export const documentTextBlocks = pgTable('document_text_blocks', {
19+
...randomId,
20+
document_id: text('document_id')
21+
.notNull()
22+
.references(() => documents.id, { onDelete: 'cascade' }),
23+
position: integer('position').notNull(),
24+
// 'paragraph', 'heading1', 'heading2', 'heading3', 'quote', 'list-item'
25+
text_type: text('text_type').notNull(),
26+
content: text('content').notNull(),
27+
...timestamps,
28+
})
29+
30+
// Media blocks
31+
export const documentMediaBlocks = pgTable('document_media_blocks', {
32+
...randomId,
33+
document_id: text('document_id')
34+
.notNull()
35+
.references(() => documents.id, { onDelete: 'cascade' }),
36+
position: integer('position').notNull(),
37+
provider: text('provider').notNull(), // 'youtube', 'vimeo', 'dailymotion', 'image', 'audio', 'video'
38+
url: text('url').notNull(),
39+
id: text('id').notNull(),
40+
title: text('title'),
41+
...timestamps,
42+
})

0 commit comments

Comments
 (0)