-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
162 lines (146 loc) · 3.57 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/// <reference types="@astrojs/db" />
import { eq, lte } from "astro:db";
import type { SqliteDB, Table } from "@astrojs/db/runtime";
import type { Adapter, DatabaseSession, DatabaseUser, UserId } from "lucia";
export class AstroDBAdapter implements Adapter {
private db: SqliteDB;
private sessionTable: SessionTable;
private userTable: UserTable;
constructor(db: SqliteDB, sessionTable: SessionTable, userTable: UserTable) {
this.db = db;
this.sessionTable = sessionTable;
this.userTable = userTable;
}
public async deleteSession(sessionId: string): Promise<void> {
await this.db.delete(this.sessionTable).where(eq(this.sessionTable.id, sessionId));
}
public async deleteUserSessions(userId: UserId): Promise<void> {
await this.db.delete(this.sessionTable).where(eq(this.sessionTable.userId, userId));
}
public async getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
const result = await this.db
.select({
user: this.userTable,
session: this.sessionTable
})
.from(this.sessionTable)
.innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id))
.where(eq(this.sessionTable.id, sessionId))
.get();
if (!result) return [null, null];
return [transformIntoDatabaseSession(result.session), transformIntoDatabaseUser(result.user)];
}
public async getUserSessions(userId: UserId): Promise<DatabaseSession[]> {
const result = await this.db
.select()
.from(this.sessionTable)
.where(eq(this.sessionTable.userId, userId))
.all();
return result.map((val) => {
return transformIntoDatabaseSession(val);
});
}
public async setSession(session: DatabaseSession): Promise<void> {
await this.db
.insert(this.sessionTable)
.values({
id: session.id,
userId: session.userId,
expiresAt: session.expiresAt,
...session.attributes
})
.run();
}
public async updateSessionExpiration(sessionId: string, expiresAt: Date): Promise<void> {
await this.db
.update(this.sessionTable)
.set({
expiresAt: expiresAt
})
.where(eq(this.sessionTable.id, sessionId))
.run();
}
public async deleteExpiredSessions(): Promise<void> {
await this.db.delete(this.sessionTable).where(lte(this.sessionTable.expiresAt, new Date()));
}
}
function transformIntoDatabaseSession(raw: any): DatabaseSession {
const { id, userId, expiresAt, ...attributes } = raw;
return {
userId,
id,
expiresAt,
attributes
};
}
function transformIntoDatabaseUser(raw: any): DatabaseUser {
const { id, ...attributes } = raw;
return {
id,
attributes
};
}
export type UserTable = Table<
any,
{
id: {
type: UserIdColumnType;
schema: {
unique: false;
deprecated: any;
name: any;
collection: any;
primaryKey: true;
};
};
}
>;
export type SessionTable = Table<
any,
{
id: {
type: "text";
schema: {
unique: false;
deprecated: any;
name: any;
collection: any;
primaryKey: true;
};
};
expiresAt: {
type: "date";
schema: {
optional: false;
unique: false;
deprecated: any;
name: any;
collection: any;
};
};
userId: {
type: UserIdColumnType;
schema: {
unique: false;
deprecated: false;
name: any;
collection: any;
primaryKey: false;
optional: false;
references: {
type: UserIdColumnType;
schema: {
unique: false;
deprecated: false;
name: any;
collection: any;
primaryKey: true;
};
};
};
};
}
>;
type UserIdColumnType = UserId extends string ? "text" : UserId extends number ? "number" : never;