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