-
Notifications
You must be signed in to change notification settings - Fork 8
/
mysql.ts
58 lines (55 loc) · 1.77 KB
/
mysql.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
import { MysqlDialect } from "kysely";
import { createPool, Pool, PoolOptions } from "mysql2";
import { KyselyStore } from "./kysely";
import { SessionStore } from "./types";
interface NewPoolOpts {
host?: string | undefined;
port?: number | undefined;
database?: string | undefined;
user?: string | undefined;
password?: string;
/**
* MySQL2 Pool options.
*
* Remember to install the db driver `'mysql2'`.
*
* @see {@link https://github.com/sidorares/node-mysql2#using-connection-pools Node MySQL 2 | Using connection pools}
* */
config?: Omit<PoolOptions, "host" | "port" | "database" | "user" | "password">;
/** Table name to use for sessions. Defaults to "telegraf-sessions". */
table?: string;
/** Called on fatal connection or setup errors */
onInitError?: (err: unknown) => void;
}
interface ExistingPoolOpts {
/** If passed, we'll reuse this instance of MySQL2 Pool instead of creating our own. */
pool: Pool;
/** Table name to use for sessions. Defaults to "telegraf-sessions". */
table?: string;
/** Called on fatal connection or setup errors */
onInitError?: (err: unknown) => void;
}
/** @unstable */
export function MySQL<Session>(opts: NewPoolOpts): SessionStore<Session>;
export function MySQL<Session>(opts: ExistingPoolOpts): SessionStore<Session>;
export function MySQL<Session>(opts: NewPoolOpts | ExistingPoolOpts) {
return KyselyStore<Session>({
config:
"pool" in opts
? { dialect: new MysqlDialect({ pool: opts.pool }) }
: {
dialect: new MysqlDialect({
pool: createPool({
host: opts.host,
port: opts.port,
database: opts.database,
user: opts.user,
password: opts.password,
...opts.config,
}),
}),
},
table: opts.table,
onInitError: opts.onInitError,
});
}