Skip to content

Commit

Permalink
remove id opt in for composite primary key instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JipSterk committed May 10, 2024
1 parent abfa66e commit c488cde
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 74 deletions.
63 changes: 33 additions & 30 deletions packages/adapter-drizzle/src/lib/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,43 @@ export function defineTables(
token: varchar("token", { length: 255 }).notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(vt) => ({
compositePk: primaryKey({ columns: [vt.identifier, vt.token] }),
(verficationToken) => ({
compositePk: primaryKey({
columns: [verficationToken.identifier, verficationToken.token],
}),
})
) satisfies DefaultMySqlVerificationTokenTable)

const authenticatorsTable =
schema.authenticatorsTable ??
(mysqlTable("authenticator", {
id: varchar("id", { length: 255 }).notNull().primaryKey(),
credentialID: varchar("credentialID", { length: 255 }).notNull().unique(),
userId: varchar("userId", { length: 255 })
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
providerAccountId: varchar("providerAccountId", {
length: 255,
}).notNull(),
credentialPublicKey: varchar("credentialPublicKey", {
length: 255,
}).notNull(),
counter: int("counter").notNull(),
credentialDeviceType: varchar("credentialDeviceType", {
length: 255,
}).notNull(),
credentialBackedUp: boolean("credentialBackedUp").notNull(),
transports: varchar("transports", { length: 255 }),
}) satisfies DefaultMySqlAuthenticatorTable)
(mysqlTable(
"authenticator",
{
credentialID: varchar("credentialID", { length: 255 })
.notNull()
.unique(),
userId: varchar("userId", { length: 255 })
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
providerAccountId: varchar("providerAccountId", {
length: 255,
}).notNull(),
credentialPublicKey: varchar("credentialPublicKey", {
length: 255,
}).notNull(),
counter: int("counter").notNull(),
credentialDeviceType: varchar("credentialDeviceType", {
length: 255,
}).notNull(),
credentialBackedUp: boolean("credentialBackedUp").notNull(),
transports: varchar("transports", { length: 255 }),
},
(authenticator) => ({
compositePk: primaryKey({
columns: [authenticator.userId, authenticator.credentialID],
}),
})
) satisfies DefaultMySqlAuthenticatorTable)

return {
usersTable,
Expand Down Expand Up @@ -303,9 +314,7 @@ export function MySqlDrizzleAdapter(
)
},
async createAuthenticator(data: AdapterAuthenticator) {
await client
.insert(authenticatorsTable)
.values({ ...data, id: crypto.randomUUID() })
await client.insert(authenticatorsTable).values(data)

return await client
.select()
Expand Down Expand Up @@ -535,12 +544,6 @@ export type DefaultMySqlVerificationTokenTable = MySqlTableWithColumns<{
export type DefaultMySqlAuthenticatorTable = MySqlTableWithColumns<{
name: string
columns: {
id: DefaultMyqlColumn<{
columnType: "MySqlVarChar" | "MySqlText"
data: string
notNull: true
dataType: "string"
}>
credentialID: DefaultMyqlColumn<{
columnType: "MySqlVarChar" | "MySqlText"
data: string
Expand Down
63 changes: 31 additions & 32 deletions packages/adapter-drizzle/src/lib/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ export function defineTables(
id_token: text("id_token"),
session_state: text("session_state"),
},
(table) => {
return {
compositePk: primaryKey({
columns: [table.provider, table.providerAccountId],
}),
}
}
(account) => ({
compositePk: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
})
) satisfies DefaultPostgresAccountsTable)

const sessionsTable =
Expand All @@ -84,28 +82,35 @@ export function defineTables(
token: text("token").notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(table) => {
return {
compositePk: primaryKey({ columns: [table.identifier, table.token] }),
}
}
(verficationToken) => ({
compositePk: primaryKey({
columns: [verficationToken.identifier, verficationToken.token],
}),
})
) satisfies DefaultPostgresVerificationTokenTable)

const authenticatorsTable =
schema.authenticatorsTable ??
(pgTable("authenticator", {
id: text("id").notNull().primaryKey(),
credentialID: text("credentialID").notNull().unique(),
userId: text("userId")
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
providerAccountId: text("providerAccountId").notNull(),
credentialPublicKey: text("credentialPublicKey").notNull(),
counter: integer("counter").notNull(),
credentialDeviceType: text("credentialDeviceType").notNull(),
credentialBackedUp: boolean("credentialBackedUp").notNull(),
transports: text("transports"),
}) satisfies DefaultPostgresAuthenticatorTable)
(pgTable(
"authenticator",
{
credentialID: text("credentialID").notNull().unique(),
userId: text("userId")
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
providerAccountId: text("providerAccountId").notNull(),
credentialPublicKey: text("credentialPublicKey").notNull(),
counter: integer("counter").notNull(),
credentialDeviceType: text("credentialDeviceType").notNull(),
credentialBackedUp: boolean("credentialBackedUp").notNull(),
transports: text("transports"),
},
(authenticator) => ({
compositePK: primaryKey({
columns: [authenticator.userId, authenticator.credentialID],
}),
})
) satisfies DefaultPostgresAuthenticatorTable)

return {
usersTable,
Expand Down Expand Up @@ -267,7 +272,7 @@ export function PostgresDrizzleAdapter(
async createAuthenticator(data: AdapterAuthenticator) {
const user = await client
.insert(authenticatorsTable)
.values({ ...data, id: crypto.randomUUID() })
.values(data)
.returning()
.then((res) => res[0] ?? null)

Expand Down Expand Up @@ -491,12 +496,6 @@ export type DefaultPostgresVerificationTokenTable = PgTableWithColumns<{
export type DefaultPostgresAuthenticatorTable = PgTableWithColumns<{
name: string
columns: {
id: DefaultPostgresColumn<{
columnType: "PgVarchar" | "PgText"
data: string
notNull: true
dataType: "string"
}>
credentialID: DefaultPostgresColumn<{
columnType: "PgVarchar" | "PgText"
data: string
Expand Down
19 changes: 7 additions & 12 deletions packages/adapter-drizzle/src/lib/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,14 @@ export function defineTables(
token: text("token").notNull(),
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
},
(vt) => ({
compositePk: primaryKey({ columns: [vt.identifier, vt.token] }),
(verficationToken) => ({
compositePk: primaryKey({ columns: [verficationToken.identifier, verficationToken.token] }),
})
) satisfies DefaultSQLiteVerificationTokenTable)

const authenticatorsTable =
schema.authenticatorsTable ??
(sqliteTable("authenticator", {
id: text("id").notNull().primaryKey(),
credentialID: text("credentialID").notNull().unique(),
userId: text("userId")
.notNull()
Expand All @@ -100,7 +99,9 @@ export function defineTables(
mode: "boolean",
}).notNull(),
transports: text("transports"),
}) satisfies DefaultSQLiteAuthenticatorTable)
}, (authenticator) => ({
compositePK: primaryKey({ columns: [authenticator.userId, authenticator.credentialID]})
})) satisfies DefaultSQLiteAuthenticatorTable)

return {
usersTable,
Expand Down Expand Up @@ -271,7 +272,7 @@ export function SQLiteDrizzleAdapter(
async createAuthenticator(data: AdapterAuthenticator) {
const user = await client
.insert(authenticatorsTable)
.values({ ...data, id: crypto.randomUUID() })
.values(data)
.returning()
.then((res) => (res[0]) ?? null)

Expand All @@ -290,7 +291,7 @@ export function SQLiteDrizzleAdapter(
.select()
.from(authenticatorsTable)
.where(eq(authenticatorsTable.userId, userId))
.then((res) => res.map())
.then((res) => res)
},
async updateAuthenticatorCounter(credentialID: string, newCounter: number) {
return await client
Expand Down Expand Up @@ -521,12 +522,6 @@ export type DefaultSQLiteVerificationTokenTable = SQLiteTableWithColumns<{
export type DefaultSQLiteAuthenticatorTable = SQLiteTableWithColumns<{
name: string
columns: {
id: DefaultSQLiteColumn<{
columnType: "SQLiteText"
data: string
notNull: true
dataType: "string"
}>
credentialID: DefaultSQLiteColumn<{
columnType: "SQLiteText"
data: string
Expand Down

0 comments on commit c488cde

Please sign in to comment.