|
| 1 | +import { primaryKey, timestamp, text, varchar, index } from 'drizzle-orm/pg-core'; |
| 2 | +import { createId } from '@paralleldrive/cuid2'; |
| 3 | +import { roleEnum, statusEnum } from './enums'; |
| 4 | +import { baseSchema, users } from 'src/shared/entities'; |
| 5 | + |
| 6 | +export const teams = baseSchema.table( |
| 7 | + 'teams', |
| 8 | + { |
| 9 | + id: text('id') |
| 10 | + .primaryKey() |
| 11 | + .$defaultFn(() => createId()), |
| 12 | + slug: varchar('slug', { length: 120 }).unique().notNull(), |
| 13 | + name: varchar('name', { length: 100 }).notNull(), |
| 14 | + description: text('description'), |
| 15 | + avatarUrl: text('avatar_url'), |
| 16 | + coverUrl: text('cover_url'), |
| 17 | + ownerId: text('owner_id').references(() => users.id), |
| 18 | + createdAt: timestamp('created_at').defaultNow().notNull(), |
| 19 | + updatedAt: timestamp('updated_at').defaultNow().notNull(), |
| 20 | + }, |
| 21 | + (t) => ({ |
| 22 | + slugIdx: index('team_slug_idx').on(t.slug), |
| 23 | + }), |
| 24 | +); |
| 25 | + |
| 26 | +export const teamMembers = baseSchema.table( |
| 27 | + 'team_members', |
| 28 | + { |
| 29 | + teamId: text('team_id') |
| 30 | + .references(() => teams.id, { onDelete: 'cascade' }) |
| 31 | + .notNull(), |
| 32 | + userId: text('user_id') |
| 33 | + .references(() => users.id, { onDelete: 'cascade' }) |
| 34 | + .notNull(), |
| 35 | + role: roleEnum('role').default('member').notNull(), |
| 36 | + status: statusEnum('status').default('pending').notNull(), |
| 37 | + joinedAt: timestamp('joined_at'), |
| 38 | + createdAt: timestamp('created_at').defaultNow().notNull(), |
| 39 | + }, |
| 40 | + (t) => ({ |
| 41 | + pk: primaryKey({ columns: [t.teamId, t.userId] }), |
| 42 | + statusIdx: index('member_status_idx').on(t.status), |
| 43 | + }), |
| 44 | +); |
| 45 | + |
| 46 | +export const tags = baseSchema.table('tags', { |
| 47 | + id: text('id') |
| 48 | + .primaryKey() |
| 49 | + .$defaultFn(() => createId()), |
| 50 | + name: varchar('name', { length: 50 }).unique().notNull(), |
| 51 | +}); |
| 52 | + |
| 53 | +export const teamsToTags = baseSchema.table( |
| 54 | + 'teams_to_tags', |
| 55 | + { |
| 56 | + teamId: text('team_id') |
| 57 | + .references(() => teams.id, { onDelete: 'cascade' }) |
| 58 | + .notNull(), |
| 59 | + tagId: text('tag_id') |
| 60 | + .references(() => tags.id, { onDelete: 'cascade' }) |
| 61 | + .notNull(), |
| 62 | + }, |
| 63 | + (t) => ({ |
| 64 | + pk: primaryKey({ columns: [t.teamId, t.tagId] }), |
| 65 | + }), |
| 66 | +); |
0 commit comments