Skip to content

Commit

Permalink
remove unnecessary ternary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
JipSterk committed May 11, 2024
1 parent c488cde commit ff9356f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 56 deletions.
5 changes: 2 additions & 3 deletions packages/adapter-drizzle/src/lib/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,11 @@ export function MySqlDrizzleAdapter(
.then((res) => res[0] ?? null)
},
async getAuthenticator(credentialID: string) {
const authenticator = await client
return await client
.select()
.from(authenticatorsTable)
.where(eq(authenticatorsTable.credentialID, credentialID))
.then((res) => (res.length ? res[0] : null))
return authenticator ? authenticator : null
.then((res) => res[0] ?? null)
},
async listAuthenticatorsByUserId(userId: string) {
return await client
Expand Down
5 changes: 2 additions & 3 deletions packages/adapter-drizzle/src/lib/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,11 @@ export function PostgresDrizzleAdapter(
return user
},
async getAuthenticator(credentialID: string) {
const authenticator = await client
return await client
.select()
.from(authenticatorsTable)
.where(eq(authenticatorsTable.credentialID, credentialID))
.then((res) => (res.length ? res[0] : null))
return authenticator ? authenticator : null
.then((res) => res[0] ?? null)
},
async listAuthenticatorsByUserId(userId: string) {
return await client
Expand Down
79 changes: 29 additions & 50 deletions packages/adapter-drizzle/src/lib/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,36 @@ export function defineTables(
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
},
(verficationToken) => ({
compositePk: primaryKey({ columns: [verficationToken.identifier, verficationToken.token] }),
compositePk: primaryKey({
columns: [verficationToken.identifier, verficationToken.token],
}),
})
) satisfies DefaultSQLiteVerificationTokenTable)

const authenticatorsTable =
schema.authenticatorsTable ??
(sqliteTable("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: integer("credentialBackedUp", {
mode: "boolean",
}).notNull(),
transports: text("transports"),
}, (authenticator) => ({
compositePK: primaryKey({ columns: [authenticator.userId, authenticator.credentialID]})
})) satisfies DefaultSQLiteAuthenticatorTable)
(sqliteTable(
"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: integer("credentialBackedUp", {
mode: "boolean",
}).notNull(),
transports: text("transports"),
},
(authenticator) => ({
compositePK: primaryKey({
columns: [authenticator.userId, authenticator.credentialID],
}),
})
) satisfies DefaultSQLiteAuthenticatorTable)

return {
usersTable,
Expand Down Expand Up @@ -274,17 +282,16 @@ export function SQLiteDrizzleAdapter(
.insert(authenticatorsTable)
.values(data)
.returning()
.then((res) => (res[0]) ?? null)
.then((res) => res[0] ?? null)

return user
},
async getAuthenticator(credentialID: string) {
const authenticator = await client
return await client
.select()
.from(authenticatorsTable)
.where(eq(authenticatorsTable.credentialID, credentialID))
.then((res) => (res.length ? (res[0]) : null))
return authenticator ? authenticator : null
.then((res) => res[0] ?? null)
},
async listAuthenticatorsByUserId(userId: string) {
return await client
Expand All @@ -299,39 +306,11 @@ export function SQLiteDrizzleAdapter(
.set({ counter: newCounter })
.where(eq(authenticatorsTable.credentialID, credentialID))
.returning()
.then((res) => (res[0]) ?? null)
.then((res) => res[0] ?? null)
},
}
}

type BaseAuthenticator = InferInsertModel<
ReturnType<typeof defineTables>["authenticatorsTable"]
>
export type DrizzleAuthenticator = BaseAuthenticator &
Required<
Pick<
BaseAuthenticator,
| "userId"
| "providerAccountId"
| "counter"
| "credentialBackedUp"
| "credentialID"
| "credentialPublicKey"
| "credentialDeviceType"
>
>

function (
authenticator: DrizzleAuthenticator
): AdapterAuthenticator {
const { transports, id, ...other } = authenticator

return {
...other,
transports: transports || undefined,
}
}

type DefaultSQLiteColumn<
T extends {
data: string | boolean | number | Date
Expand Down

0 comments on commit ff9356f

Please sign in to comment.