| 1 | import { relations } from 'drizzle-orm' |
| 2 | import { |
| 3 | boolean, |
| 4 | index, |
| 5 | integer, |
| 6 | pgTable, |
| 7 | text, |
| 8 | timestamp, |
| 9 | uniqueIndex, |
| 10 | } from 'drizzle-orm/pg-core' |
| 11 | |
| 12 | export const users = |
| 13 | id: text('id |
| 14 | name: text('name |
| 15 | email: text('email |
| 16 | emailVerified: boolean('email_verified |
| 17 | image: text('image |
| 18 | createdAt: timestamp('created_at |
| 19 | updatedAt: timestamp('updated_at |
| 20 | .defaultNow() |
| 21 | .$onUpdate(() => |
| 22 | .notNull(), |
| 23 | }) |
| 24 | |
| 25 | export const sessions = |
| 26 | 'sessions', |
| 27 | { |
| 28 | id: text('id |
| 29 | expiresAt: timestamp('expires_at |
| 30 | token: text('token |
| 31 | createdAt: timestamp('created_at |
| 32 | updatedAt: timestamp('updated_at |
| 33 | .$onUpdate(() => |
| 34 | .notNull(), |
| 35 | ipAddress: text('ip_address |
| 36 | userAgent: text('user_agent |
| 37 | userId: text('user_id |
| 38 | .notNull() |
| 39 | .references(() => |
| 40 | activeOrganizationId: text('active_organization_id |
| 41 | activeTeamId: text('active_team_id |
| 42 | }, |
| 43 | (table) => |
| 44 | ) |
| 45 | |
| 46 | export const accounts = |
| 47 | 'accounts', |
| 48 | { |
| 49 | id: text('id |
| 50 | accountId: text('account_id |
| 51 | providerId: text('provider_id |
| 52 | userId: text('user_id |
| 53 | .notNull() |
| 54 | .references(() => |
| 55 | accessToken: text('access_token |
| 56 | refreshToken: text('refresh_token |
| 57 | idToken: text('id_token |
| 58 | accessTokenExpiresAt: timestamp('access_token_expires_at |
| 59 | refreshTokenExpiresAt: timestamp('refresh_token_expires_at |
| 60 | scope: text('scope |
| 61 | password: text('password |
| 62 | createdAt: timestamp('created_at |
| 63 | updatedAt: timestamp('updated_at |
| 64 | .$onUpdate(() => |
| 65 | .notNull(), |
| 66 | }, |
| 67 | (table) => |
| 68 | ) |
| 69 | |
| 70 | export const verifications = |
| 71 | 'verifications', |
| 72 | { |
| 73 | id: text('id |
| 74 | identifier: text('identifier |
| 75 | value: text('value |
| 76 | expiresAt: timestamp('expires_at |
| 77 | createdAt: timestamp('created_at |
| 78 | updatedAt: timestamp('updated_at |
| 79 | .defaultNow() |
| 80 | .$onUpdate(() => |
| 81 | .notNull(), |
| 82 | }, |
| 83 | (table) => |
| 84 | ) |
| 85 | |
| 86 | export const apikeys = |
| 87 | 'apikeys', |
| 88 | { |
| 89 | id: text('id |
| 90 | configId: text('config_id |
| 91 | name: text('name |
| 92 | start: text('start |
| 93 | referenceId: text('reference_id |
| 94 | prefix: text('prefix |
| 95 | key: text('key |
| 96 | refillInterval: integer('refill_interval |
| 97 | refillAmount: integer('refill_amount |
| 98 | lastRefillAt: timestamp('last_refill_at |
| 99 | enabled: boolean('enabled |
| 100 | rateLimitEnabled: boolean('rate_limit_enabled |
| 101 | rateLimitTimeWindow: integer('rate_limit_time_window |
| 102 | rateLimitMax: integer('rate_limit_max |
| 103 | requestCount: integer('request_count |
| 104 | remaining: integer('remaining |
| 105 | lastRequest: timestamp('last_request |
| 106 | expiresAt: timestamp('expires_at |
| 107 | createdAt: timestamp('created_at |
| 108 | updatedAt: timestamp('updated_at |
| 109 | permissions: text('permissions |
| 110 | metadata: text('metadata |
| 111 | }, |
| 112 | (table) => |
| 113 | index('apikeys_configId_idx'). |
| 114 | index('apikeys_referenceId_idx'). |
| 115 | index('apikeys_key_idx'). |
| 116 | ] |
| 117 | ) |
| 118 | |
| 119 | export const organizations = |
| 120 | 'organizations', |
| 121 | { |
| 122 | id: text('id |
| 123 | name: text('name |
| 124 | slug: text('slug |
| 125 | logo: text('logo |
| 126 | createdAt: timestamp('created_at |
| 127 | metadata: text('metadata |
| 128 | }, |
| 129 | (table) => |
| 130 | ) |
| 131 | |
| 132 | export const teams = |
| 133 | 'teams', |
| 134 | { |
| 135 | id: text('id |
| 136 | name: text('name |
| 137 | organizationId: text('organization_id |
| 138 | .notNull() |
| 139 | .references(() => |
| 140 | createdAt: timestamp('created_at |
| 141 | updatedAt: timestamp('updated_at |
| 142 | () => /* @__PURE__ |
| 143 | ), |
| 144 | }, |
| 145 | (table) => |
| 146 | ) |
| 147 | |
| 148 | export const teamMembers = |
| 149 | 'team_members', |
| 150 | { |
| 151 | id: text('id |
| 152 | teamId: text('team_id |
| 153 | .notNull() |
| 154 | .references(() => |
| 155 | userId: text('user_id |
| 156 | .notNull() |
| 157 | .references(() => |
| 158 | createdAt: timestamp('created_at |
| 159 | }, |
| 160 | (table) => |
| 161 | index('teamMembers_teamId_idx'). |
| 162 | index('teamMembers_userId_idx'). |
| 163 | ] |
| 164 | ) |
| 165 | |
| 166 | export const members = |
| 167 | 'members', |
| 168 | { |
| 169 | id: text('id |
| 170 | organizationId: text('organization_id |
| 171 | .notNull() |
| 172 | .references(() => |
| 173 | userId: text('user_id |
| 174 | .notNull() |
| 175 | .references(() => |
| 176 | role: text('role |
| 177 | createdAt: timestamp('created_at |
| 178 | }, |
| 179 | (table) => |
| 180 | index('members_organizationId_idx'). |
| 181 | index('members_userId_idx'). |
| 182 | ] |
| 183 | ) |
| 184 | |
| 185 | export const invitations = |
| 186 | 'invitations', |
| 187 | { |
| 188 | id: text('id |
| 189 | organizationId: text('organization_id |
| 190 | .notNull() |
| 191 | .references(() => |
| 192 | email: text('email |
| 193 | role: text('role |
| 194 | teamId: text('team_id |
| 195 | status: text('status |
| 196 | expiresAt: timestamp('expires_at |
| 197 | createdAt: timestamp('created_at |
| 198 | inviterId: text('inviter_id |
| 199 | .notNull() |
| 200 | .references(() => |
| 201 | }, |
| 202 | (table) => |
| 203 | index('invitations_organizationId_idx'). |
| 204 | index('invitations_email_idx'). |
| 205 | ] |
| 206 | ) |
| 207 | |
| 208 | export const usersRelations = |
| 209 | sessions: many(sessions |
| 210 | accounts: many(accounts |
| 211 | teamMembers: many(teamMembers |
| 212 | members: many(members |
| 213 | invitations: many(invitations |
| 214 | })) |
| 215 | |
| 216 | export const sessionsRelations = |
| 217 | users: one(users |
| 218 | fields: [sessions.userId |
| 219 | references: [users.id |
| 220 | }), |
| 221 | })) |
| 222 | |
| 223 | export const accountsRelations = |
| 224 | users: one(users |
| 225 | fields: [accounts.userId |
| 226 | references: [users.id |
| 227 | }), |
| 228 | })) |
| 229 | |
| 230 | export const organizationsRelations = |
| 231 | teams: many(teams |
| 232 | members: many(members |
| 233 | invitations: many(invitations |
| 234 | })) |
| 235 | |
| 236 | export const teamsRelations = |
| 237 | organizations: one(organizations |
| 238 | fields: [teams.organizationId |
| 239 | references: [organizations.id |
| 240 | }), |
| 241 | teamMembers: many(teamMembers |
| 242 | })) |
| 243 | |
| 244 | export const teamMembersRelations = |
| 245 | teams: one(teams |
| 246 | fields: [teamMembers.teamId |
| 247 | references: [teams.id |
| 248 | }), |
| 249 | users: one(users |
| 250 | fields: [teamMembers.userId |
| 251 | references: [users.id |
| 252 | }), |
| 253 | })) |
| 254 | |
| 255 | export const membersRelations = |
| 256 | organizations: one(organizations |
| 257 | fields: [members.organizationId |
| 258 | references: [organizations.id |
| 259 | }), |
| 260 | users: one(users |
| 261 | fields: [members.userId |
| 262 | references: [users.id |
| 263 | }), |
| 264 | })) |
| 265 | |
| 266 | export const invitationsRelations = |
| 267 | organizations: one(organizations |
| 268 | fields: [invitations.organizationId |
| 269 | references: [organizations.id |
| 270 | }), |
| 271 | users: one(users |
| 272 | fields: [invitations.inviterId |
| 273 | references: [users.id |
| 274 | }), |
| 275 | })) |