Skip to content

Commit 4685095

Browse files
committed
feat: optimize schema indexes
1 parent f9f2781 commit 4685095

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

packages/db/src/drizzle/schema.ts

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export const account = pgTable(
8585
updatedAt: timestamp('updated_at', { mode: 'string' }).notNull(),
8686
},
8787
(table) => [
88+
index('accounts_userId_idx').using(
89+
'btree',
90+
table.userId.asc().nullsLast().op('text_ops')
91+
),
8892
foreignKey({
8993
columns: [table.userId],
9094
foreignColumns: [user.id],
@@ -109,11 +113,11 @@ export const session = pgTable(
109113
activeOrganizationId: text('active_organization_id'),
110114
},
111115
(table) => [
112-
uniqueIndex('session_token_key').using(
116+
uniqueIndex('sessions_token_key').using(
113117
'btree',
114118
table.token.asc().nullsLast().op('text_ops')
115119
),
116-
index('session_userId_idx').using(
120+
index('sessions_userId_idx').using(
117121
'btree',
118122
table.userId.asc().nullsLast().op('text_ops')
119123
),
@@ -140,6 +144,14 @@ export const invitation = pgTable(
140144
inviterId: text('inviter_id').notNull(),
141145
},
142146
(table) => [
147+
index('invitations_email_idx').using(
148+
'btree',
149+
table.email.asc().nullsLast().op('text_ops')
150+
),
151+
index('invitations_organizationId_idx').using(
152+
'btree',
153+
table.organizationId.asc().nullsLast().op('text_ops')
154+
),
143155
foreignKey({
144156
columns: [table.organizationId],
145157
foreignColumns: [organization.id],
@@ -164,6 +176,14 @@ export const member = pgTable(
164176
createdAt: timestamp('created_at', { mode: 'string' }).notNull(),
165177
},
166178
(table) => [
179+
index('members_userId_idx').using(
180+
'btree',
181+
table.userId.asc().nullsLast().op('text_ops')
182+
),
183+
index('members_organizationId_idx').using(
184+
'btree',
185+
table.organizationId.asc().nullsLast().op('text_ops')
186+
),
167187
foreignKey({
168188
columns: [table.organizationId],
169189
foreignColumns: [organization.id],
@@ -177,14 +197,23 @@ export const member = pgTable(
177197
]
178198
);
179199

180-
export const verification = pgTable('verification', {
181-
id: text().primaryKey().notNull(),
182-
identifier: text().notNull(),
183-
value: text().notNull(),
184-
expiresAt: timestamp('expires_at', { mode: 'string' }).notNull(),
185-
createdAt: timestamp('created_at', { mode: 'string' }),
186-
updatedAt: timestamp('updated_at', { mode: 'string' }),
187-
});
200+
export const verification = pgTable(
201+
'verification',
202+
{
203+
id: text().primaryKey().notNull(),
204+
identifier: text().notNull(),
205+
value: text().notNull(),
206+
expiresAt: timestamp('expires_at', { mode: 'string' }).notNull(),
207+
createdAt: timestamp('created_at', { mode: 'string' }),
208+
updatedAt: timestamp('updated_at', { mode: 'string' }),
209+
},
210+
(table) => [
211+
index('verifications_identifier_idx').using(
212+
'btree',
213+
table.identifier.asc().nullsLast().op('text_ops')
214+
),
215+
]
216+
);
188217

189218
export const twoFactor = pgTable(
190219
'two_factor',
@@ -195,6 +224,10 @@ export const twoFactor = pgTable(
195224
userId: text('user_id').notNull(),
196225
},
197226
(table) => [
227+
index('twoFactor_secret_idx').using(
228+
'btree',
229+
table.secret.asc().nullsLast().op('text_ops')
230+
),
198231
foreignKey({
199232
columns: [table.userId],
200233
foreignColumns: [user.id],
@@ -299,7 +332,13 @@ export const user = pgTable(
299332
role: role().default('USER').notNull(),
300333
twoFactorEnabled: boolean('two_factor_enabled'),
301334
},
302-
(table) => [unique('user_email_unique').on(table.email)]
335+
(table) => [
336+
unique('users_email_unique').on(table.email),
337+
index('users_email_idx').using(
338+
'btree',
339+
table.email.asc().nullsLast().op('text_ops')
340+
),
341+
]
303342
);
304343

305344
export const userStripeConfig = pgTable(
@@ -442,6 +481,16 @@ export const goals = pgTable(
442481
'btree',
443482
table.createdBy.asc().nullsLast().op('text_ops')
444483
),
484+
index('goals_websiteId_deletedAt_createdAt_idx').using(
485+
'btree',
486+
table.websiteId.asc().nullsLast().op('text_ops'),
487+
table.deletedAt.asc().nullsLast(),
488+
table.createdAt.desc().nullsLast()
489+
),
490+
index('goals_deletedAt_idx').using(
491+
'btree',
492+
table.deletedAt.asc().nullsLast()
493+
),
445494
foreignKey({
446495
columns: [table.websiteId],
447496
foreignColumns: [websites.id],
@@ -553,7 +602,13 @@ export const organization = pgTable(
553602
createdAt: timestamp('created_at', { mode: 'string' }).notNull(),
554603
metadata: text(),
555604
},
556-
(table) => [unique('organization_slug_unique').on(table.slug)]
605+
(table) => [
606+
unique('organizations_slug_unique').on(table.slug),
607+
index('organizations_slug_idx').using(
608+
'btree',
609+
table.slug.asc().nullsLast().op('text_ops')
610+
),
611+
]
557612
);
558613

559614
export const abTestStatus = pgEnum('ab_test_status', [

0 commit comments

Comments
 (0)