Skip to content

Commit 2ba0c30

Browse files
committed
feat: move essential params out of { config }
1 parent e6e1f05 commit 2ba0c30

9 files changed

Lines changed: 172 additions & 121 deletions

File tree

README.md

Lines changed: 128 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,135 @@
11
# `@telegraf/session`
22

3-
This package provides official storage adapters for Telegraf v4.12+ sessions.
3+
This package provides official storage adapters for Telegraf v4.12+ sessions [[see motivation]](#background).
44

5-
An in-memory session module is bundled with Telegraf. These modules are available here:
5+
> ⚠️ Very Important!
6+
>
7+
> Read one of the following sections before using this module. You're not meant to import the default route!
68
7-
- [Redis](./docs/redis.md)
8-
- [MongoDB](./docs/mongodb.md)
9-
- [SQLite](./docs/sqlite.md)
10-
- [PostgreSQL](./docs/pg.md)
11-
- [MySQL / MariaDB](./docs/mysql.md)
9+
An in-memory session module is bundled with Telegraf. The following modules are available here:
10+
11+
- [Redis](#redis)
12+
- [MongoDB](#mongodb)
13+
- [SQLite](#sqlite)
14+
- [PostgreSQL](#postgresql)
15+
- [MySQL / MariaDB](#mysql--mariadb)
16+
17+
## Redis
18+
19+
Install the official Redis driver alongside this module.
20+
21+
```shell
22+
npm i @telegraf/session redis
23+
```
24+
25+
Usage is pretty straightforward:
26+
27+
```TS
28+
import { Redis } from "@telegraf/session/redis";
29+
30+
const store = Redis({ url: "redis://127.0.0.1:6379" });
31+
32+
const bot = new Telegraf(token, opts);
33+
bot.use(session({ store }));
34+
35+
// the rest of your bot
36+
```
37+
38+
## MongoDB
39+
40+
Install the official MongoDB driver alongside this module.
41+
42+
```shell
43+
npm i @telegraf/session mongodb
44+
```
45+
46+
Usage is pretty straightforward:
47+
48+
```TS
49+
import { Mongo } from "@telegraf/session/mongodb";
50+
51+
const store = Mongo({ url: "mongodb://127.0.0.1:27017", database: "telegraf-bot" });
52+
53+
const bot = new Telegraf(token, opts);
54+
bot.use(session({ store }));
55+
56+
// the rest of your bot
57+
```
58+
59+
## SQLite
60+
61+
Install the Better-SQLite3 driver and types alongside this module.
62+
63+
```shell
64+
npm i @telegraf/session kysely better-sqlite3
65+
npm i --save-dev @types/better-sqlite3
66+
```
67+
68+
Usage is pretty straightforward:
69+
70+
```TS
71+
import { SQLite } from "@telegraf/session/sqlite";
72+
73+
const store = SQLite({ filename: "./telegraf-sessions.sqlite" });
74+
75+
const bot = new Telegraf(token, opts);
76+
bot.use(session({ store }));
77+
78+
// the rest of your bot
79+
```
80+
81+
## PostgreSQL
82+
83+
Install the 'pg' PostgreSQL driver and types alongside this module.
84+
85+
```shell
86+
npm i @telegraf/session kysely pg
87+
npm i --save-dev @types/pg
88+
```
89+
90+
Usage is pretty straightforward:
91+
92+
```TS
93+
import { Postgres } from "@telegraf/session/pg";
94+
95+
const store = Postgres({
96+
host: "127.0.0.1",
97+
database: "telegraf-test",
98+
user: "database-user",
99+
password: "hunter2",
100+
});
101+
102+
const bot = new Telegraf(token, opts);
103+
bot.use(session({ store }));
104+
105+
// the rest of your bot
106+
```
107+
108+
## MySQL / MariaDB
109+
110+
Install the 'mysql2' MySQL driver alongside this module.
111+
112+
```shell
113+
npm i @telegraf/session kysely mysql2
114+
```
115+
116+
Usage is pretty straightforward:
117+
118+
```TS
119+
import { MySQL } from "@telegraf/session/mysql";
120+
121+
const store = MySQL({
122+
host: "127.0.0.1",
123+
database: "telegraf-test",
124+
user: "database-user",
125+
password: "hunter2",
126+
});
127+
128+
const bot = new Telegraf(token, opts);
129+
bot.use(session({ store }));
130+
131+
// the rest of your bot
132+
```
12133

13134
## Background
14135

docs/mongodb.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/mysql.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/pg.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

docs/redis.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/sqlite.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

mysql.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import { createPool, Pool, PoolOptions } from "mysql2";
33
import { KyselyStore } from "./kysely";
44

55
interface NewPoolOpts {
6+
host?: string | undefined;
7+
port?: number | undefined;
8+
database?: string | undefined;
9+
user?: string | undefined;
10+
password?: string;
611
/**
712
* MySQL2 Pool options.
813
*
914
* Remember to install the db driver `'mysql2'`.
1015
*
1116
* @see {@link https://github.com/sidorares/node-mysql2#using-connection-pools Node MySQL 2 | Using connection pools}
1217
* */
13-
config: PoolOptions;
18+
config: Omit<PoolOptions, "host" | "port" | "database" | "user" | "password">;
1419
/** Table name to use for sessions. Defaults to "telegraf-sessions". */
1520
table?: string;
1621
/** Called on fatal connection or setup errors */
@@ -34,7 +39,18 @@ export const MySQL = <Session>(opts: Opts) =>
3439
config:
3540
"pool" in opts
3641
? { dialect: new MysqlDialect({ pool: opts.pool }) }
37-
: { dialect: new MysqlDialect({ pool: createPool(opts.config) }) },
42+
: {
43+
dialect: new MysqlDialect({
44+
pool: createPool({
45+
host: opts.host,
46+
port: opts.port,
47+
database: opts.database,
48+
user: opts.user,
49+
password: opts.password,
50+
...opts.config,
51+
}),
52+
}),
53+
},
3854
table: opts.table,
3955
onInitError: opts.onInitError,
4056
});

pg.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import { Pool, PoolConfig } from "pg";
33
import { KyselyStore } from "./kysely";
44

55
interface NewPoolOpts {
6+
host?: string | undefined;
7+
port?: number | undefined;
8+
database?: string | undefined;
9+
user?: string | undefined;
10+
password?: string | (() => string | Promise<string>) | undefined;
611
/**
712
* Postgres Pool config.
813
*
914
* Remember to install the db driver `'pg'`.
1015
*
1116
* @see {@link https://node-postgres.com/apis/pool node-postgres | Pool Options}
1217
* */
13-
config: PoolConfig;
18+
config: Omit<PoolConfig, "host" | "port" | "database" | "user" | "password">;
1419
/** Table name to use for sessions. Defaults to "telegraf-sessions". */
1520
table?: string;
1621
/** Called on fatal connection or setup errors */
@@ -34,7 +39,18 @@ export const Postgres = <Session>(opts: Opts) =>
3439
config:
3540
"pool" in opts
3641
? { dialect: new PostgresDialect({ pool: opts.pool }) }
37-
: { dialect: new PostgresDialect({ pool: new Pool(opts.config) }) },
42+
: {
43+
dialect: new PostgresDialect({
44+
pool: new Pool({
45+
host: opts.host,
46+
port: opts.port,
47+
database: opts.database,
48+
user: opts.user,
49+
password: opts.password,
50+
...opts.config,
51+
}),
52+
}),
53+
},
3854
table: opts.table,
3955
onInitError: opts.onInitError,
4056
});

redis.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { SessionStore } from "./types";
55
type Client = ReturnType<typeof createClient>;
66

77
interface NewClientOpts {
8-
config: RedisClientOptions;
8+
/**
9+
* `redis[s]://[[username][:password]@][host][:port][/db-number]`
10+
*
11+
* See [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details
12+
*/
13+
url?: string;
14+
config: Omit<RedisClientOptions, "url">;
915
/** Prefix to use for session keys. Defaults to "telegraf:". */
1016
prefix?: string;
1117
/** Called on fatal connection or setup errors */
@@ -25,7 +31,7 @@ export type Opts = NewClientOpts | ExistingClientOpts;
2531
export const Redis = <Session>(opts: Opts): SessionStore<Session> => {
2632
let client: Client;
2733
if ("client" in opts) client = opts.client;
28-
else client = createClient(opts.config);
34+
else client = createClient({ ...opts.config, url: opts.url });
2935

3036
const connection = client.connect();
3137

0 commit comments

Comments
 (0)