forked from telegraf/session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg.ts
58 lines (55 loc) · 1.77 KB
/
pg.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 { PostgresDialect } from "kysely";
import { Pool, PoolConfig } from "pg";
import { KyselyStore } from "./kysely";
import { SessionStore } from "./types";
interface NewPoolOpts {
host?: string | undefined;
port?: number | undefined;
database?: string | undefined;
user?: string | undefined;
password?: string | (() => string | Promise<string>) | undefined;
/**
* Postgres Pool config.
*
* Remember to install the db driver `'pg'`.
*
* @see {@link https://node-postgres.com/apis/pool node-postgres | Pool Options}
* */
config: Omit<PoolConfig, "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 pg 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 Postgres<Session>(opts: NewPoolOpts): SessionStore<Session>;
export function Postgres<Session>(opts: ExistingPoolOpts): SessionStore<Session>;
export function Postgres<Session>(opts: NewPoolOpts | ExistingPoolOpts) {
return KyselyStore<Session>({
config:
"pool" in opts
? { dialect: new PostgresDialect({ pool: opts.pool }) }
: {
dialect: new PostgresDialect({
pool: new Pool({
host: opts.host,
port: opts.port,
database: opts.database,
user: opts.user,
password: opts.password,
...opts.config,
}),
}),
},
table: opts.table,
onInitError: opts.onInitError,
});
}