forked from telegraf/session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkysely.ts
89 lines (75 loc) · 2.19 KB
/
kysely.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
import { Kysely } from "kysely";
import type { KyselyConfig } from "kysely";
import type { SessionStore } from "./types";
import { defaults } from "./defaults";
interface SessionTable {
key: string;
session: string;
}
interface Database {
"telegraf-sessions": SessionTable;
}
interface NewClientOpts {
/**
* Knex database config.
*
* Remember to install the corresponding db driver.
*
* @see {@link https://knexjs.org/guide/#configuration-options Knex | Configuration Options}
* */
config: KyselyConfig;
/** Table name to use for sessions. Defaults to "telegraf-sessions". */
table?: string;
/** Called on fatal connection or setup errors */
onInitError?: (err: unknown) => void;
}
interface ExistingClientOpts {
/** If passed, we'll reuse this client instead of creating our own. */
client: Kysely<Database>;
/** Table name to use for sessions. Defaults to "telegraf-sessions". */
table?: string;
/** Called on fatal connection or setup errors */
onInitError?: (err: unknown) => void;
}
export type Opts = NewClientOpts | ExistingClientOpts;
/** @unstable */
export const KyselyStore = <Session>(opts: Opts): SessionStore<Session> => {
// this assertion is a hack to make the Database type work
const table = (opts.table ?? defaults.table) as "telegraf-sessions";
let client: Kysely<Database>;
if ("client" in opts) client = opts.client;
else client = new Kysely(opts.config);
const create = client.schema
.createTable(table)
.ifNotExists()
.addColumn("key", "varchar(32)", col => col.notNull())
.addColumn("session", "text")
.execute();
if ("onInitError" in opts) create.catch(opts.onInitError);
return {
async get(key) {
await create;
const value = (
await client
//
.selectFrom(table)
.select("session")
.where("key", "=", key)
.executeTakeFirst()
)?.session;
return value ? JSON.parse(value) : undefined;
},
async set(key: string, session: Session) {
await create;
await client
.updateTable(table)
.set({ session: JSON.stringify(session) })
.where("key", "=", key)
.executeTakeFirst();
},
async delete(key: string) {
await create;
await client.deleteFrom(table).where("key", "=", key);
},
};
};