Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter Drizzle - make sqlite calls async #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/adapter-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -45,7 +45,8 @@
"dev": "drizzle-kit generate:mysql --schema=src/schema.ts --out=.drizzle && tsc -w"
},
"dependencies": {
"@auth/core": "workspace:*"
"@auth/core": "workspace:*",
"@libsql/client": "0.4.0-pre.2"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.4",
79 changes: 48 additions & 31 deletions packages/adapter-drizzle/src/lib/sqlite.ts
Original file line number Diff line number Diff line change
@@ -74,59 +74,66 @@ export function SQLiteDrizzleAdapter(
createTables(tableFn)

return {
createUser(data) {
return client
async createUser(data) {
return await client
.insert(users)
.values({ ...data, id: crypto.randomUUID() })
.returning()
.get()
},
getUser(data) {
return client.select().from(users).where(eq(users.id, data)).get() ?? null
async getUser(data) {
return (
(await client.select().from(users).where(eq(users.id, data)).get()) ??
null
)
},
getUserByEmail(data) {
async getUserByEmail(data) {
return (
client.select().from(users).where(eq(users.email, data)).get() ?? null
(await client
.select()
.from(users)
.where(eq(users.email, data))
.get()) ?? null
)
},
createSession(data) {
return client.insert(sessions).values(data).returning().get()
async createSession(data) {
return await client.insert(sessions).values(data).returning().get()
},
getSessionAndUser(data) {
async getSessionAndUser(data) {
return (
client
(await client
.select({
session: sessions,
user: users,
})
.from(sessions)
.where(eq(sessions.sessionToken, data))
.innerJoin(users, eq(users.id, sessions.userId))
.get() ?? null
.get()) ?? null
)
},
updateUser(data) {
async updateUser(data) {
if (!data.id) {
throw new Error("No user id.")
}

return client
return await client
.update(users)
.set(data)
.where(eq(users.id, data.id))
.returning()
.get()
},
updateSession(data) {
return client
async updateSession(data) {
return await client
.update(sessions)
.set(data)
.where(eq(sessions.sessionToken, data.sessionToken))
.returning()
.get()
},
linkAccount(rawAccount) {
const updatedAccount = client
async linkAccount(rawAccount) {
const updatedAccount = await client
.insert(accounts)
.values(rawAccount)
.returning()
@@ -146,8 +153,8 @@ export function SQLiteDrizzleAdapter(

return account
},
getUserByAccount(account) {
const results = client
async getUserByAccount(account) {
const results = await client
.select()
.from(accounts)
.leftJoin(users, eq(users.id, accounts.userId))
@@ -161,22 +168,26 @@ export function SQLiteDrizzleAdapter(

return results?.user ?? null
},
deleteSession(sessionToken) {
async deleteSession(sessionToken) {
return (
client
(await client
.delete(sessions)
.where(eq(sessions.sessionToken, sessionToken))
.returning()
.get() ?? null
.get()) ?? null
)
},
createVerificationToken(token) {
return client.insert(verificationTokens).values(token).returning().get()
async createVerificationToken(token) {
return await client
.insert(verificationTokens)
.values(token)
.returning()
.get()
},
useVerificationToken(token) {
async useVerificationToken(token) {
try {
return (
client
(await client
.delete(verificationTokens)
.where(
and(
@@ -185,17 +196,23 @@ export function SQLiteDrizzleAdapter(
)
)
.returning()
.get() ?? null
.get()) ?? null
)
} catch (err) {
throw new Error("No verification token found.")
}
},
deleteUser(id) {
return client.delete(users).where(eq(users.id, id)).returning().get()
async deleteUser(id) {
return (
(await client
.delete(users)
.where(eq(users.id, id))
.returning()
.get()) ?? null
)
},
unlinkAccount(account) {
client
async unlinkAccount(account) {
await client
.delete(accounts)
.where(
and(
15 changes: 8 additions & 7 deletions packages/adapter-drizzle/tests/sqlite/index.test.ts
Original file line number Diff line number Diff line change
@@ -24,16 +24,17 @@ runBasicTests({
db.delete(users),
])
},
user: (id) => db.select().from(users).where(eq(users.id, id)).get() ?? null,
session: (sessionToken) =>
db
user: async (id) =>
(await db.select().from(users).where(eq(users.id, id)).get()) ?? null,
session: async (sessionToken) =>
(await db
.select()
.from(sessions)
.where(eq(sessions.sessionToken, sessionToken))
.get() ?? null,
account: (provider_providerAccountId) => {
.get()) ?? null,
account: async (provider_providerAccountId) => {
return (
db
(await db
.select()
.from(accounts)
.where(
@@ -42,7 +43,7 @@ runBasicTests({
provider_providerAccountId.providerAccountId
)
)
.get() ?? null
.get()) ?? null
)
},
verificationToken: (identifier_token) =>
1 change: 0 additions & 1 deletion packages/adapter-drizzle/tests/sqlite/schema.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import { createTables } from "../../src/lib/sqlite"
import { sqliteTable } from "drizzle-orm/sqlite-core"

const sqlite = new Database("db.sqlite")

export const { users, accounts, sessions, verificationTokens } =
createTables(sqliteTable)
export const schema = { users, accounts, sessions, verificationTokens }
172 changes: 161 additions & 11 deletions pnpm-lock.yaml