/
DME
/
Notes
Обзор
Документация
Войти
/
DME
/
Notes
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/server/db/schema.ts
165 строк
5 KB
Karasique333
Commit
19 июн 2025, 15:09
19 июн 2025, 15:09
d96c829
Код
Авторство
О чём код?
import { relations, sql } from 'drizzle-orm' import { boolean, index, integer, pgEnum, pgTable, pgTableCreator, text, timestamp, varchar, } from 'drizzle-orm/pg-core' export const createTable = pgTableCreator( (name) => `note-taking-web-app_${name}` ) export const posts = createTable( 'post', { id: integer('id').primaryKey().generatedByDefaultAsIdentity(), name: varchar('name', { length: 256 }), createdAt: timestamp('created_at', { withTimezone: true }) .default(sql`CURRENT_TIMESTAMP`) .notNull(), updatedAt: timestamp('updated_at', { withTimezone: true }).$onUpdate( () => new Date() ), }, (example) => ({ nameIndex: index('name_idx').on(example.name), }) ) export const user = pgTable('user', { id: text('id').primaryKey(), name: text('name').notNull(), email: text('email').notNull().unique(), emailVerified: boolean('emailVerified').notNull(), image: text('image'), createdAt: timestamp('createdAt').notNull(), updatedAt: timestamp('updatedAt').notNull(), }) export const userRelations = relations(user, ({ many }) => ({ notes: many(note), })) export const session = pgTable('session', { id: text('id').primaryKey(), expiresAt: timestamp('expiresAt').notNull(), token: text('token').notNull().unique(), createdAt: timestamp('createdAt').notNull(), updatedAt: timestamp('updatedAt').notNull(), ipAddress: text('ipAddress'), userAgent: text('userAgent'), userId: text('userId') .notNull() .references(() => user.id), }) export const account = pgTable('account', { id: text('id').primaryKey(), accountId: text('accountId').notNull(), providerId: text('providerId').notNull(), userId: text('userId') .notNull() .references(() => user.id), accessToken: text('accessToken'), refreshToken: text('refreshToken'), idToken: text('idToken'), accessTokenExpiresAt: timestamp('accessTokenExpiresAt'), refreshTokenExpiresAt: timestamp('refreshTokenExpiresAt'), scope: text('scope'), password: text('password'), createdAt: timestamp('createdAt').notNull(), updatedAt: timestamp('updatedAt').notNull(), }) export const verification = pgTable('verification', { id: text('id').primaryKey(), identifier: text('identifier').notNull(), value: text('value').notNull(), expiresAt: timestamp('expiresAt').notNull(), createdAt: timestamp('createdAt'), updatedAt: timestamp('updatedAt'), }) export const noteStatusEnum = pgEnum('note_status', ['archived', 'active']) export const note = pgTable( 'note', { id: text('id').primaryKey(), userId: text('userId') .notNull() .references(() => user.id), title: varchar('title', { length: 256 }).notNull(), tags: text('tags') .array() .notNull() .default(sql`'{}'::text[]`), status: noteStatusEnum('status').notNull().default('active'), content: text('content'), createdAt: timestamp('createdAt') .default(sql`CURRENT_TIMESTAMP`) .notNull(), updatedAt: timestamp('updatedAt') .default(sql`CURRENT_TIMESTAMP`) .$onUpdate(() => new Date()), }, (note) => ({ userIdIndex: index('userId_idx').on(note.userId), titleIndex: index('title_idx').on(note.title), tagsGinIndex: index('tags_gin_idx').using('gin', sql`${note.tags}`), statusIndex: index('status_idx').on(note.status), contentGinIndex: index('content_gin_idx').using( 'gin', sql`to_tsvector('english', ${note.content})` ), }) ) export type Note = typeof note.$inferSelect export const checklistItem = pgTable('checklist_item', { id: text('id').primaryKey().default(sql`gen_random_uuid()`), noteId: text('note_id').notNull().references(() => note.id, { onDelete: 'cascade' }), content: text('content').notNull(), completed: boolean('completed').default(false).notNull(), createdAt: timestamp('created_at').defaultNow().notNull(), }); export const noteImage = pgTable('note_image', { id: text('id').primaryKey().default(sql`gen_random_uuid()`), noteId: text('note_id').notNull().references(() => note.id, { onDelete: 'cascade' }), url: text('url').notNull(), createdAt: timestamp('created_at').defaultNow().notNull(), }); export const task = pgTable('task', { id: text('id').primaryKey().default(sql`gen_random_uuid()`), userId: text('user_id').notNull().references(() => user.id, { onDelete: 'cascade' }), title: text('title').notNull(), description: text('description'), dueDate: timestamp('due_date', { withTimezone: true }), completed: boolean('completed').notNull().default(false), createdAt: timestamp('created_at', { withTimezone: true }).notNull().default(sql`CURRENT_TIMESTAMP`), updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().default(sql`CURRENT_TIMESTAMP`), notifiedAt: timestamp('notified_at'), overdueNotifiedAt: timestamp('overdue_notified_at'), }); export type Task = typeof task.$inferSelect; export const noteRelations = relations(note, ({ one }) => ({ user: one(user, { fields: [note.userId], references: [user.id], }), }))