Skip to content

Commit 5ce2916

Browse files
authored
Merge pull request #19 from dahlia/postgres
Add PostgreSQL repository package
2 parents 05ac370 + 3c35886 commit 5ce2916

14 files changed

Lines changed: 2369 additions & 6 deletions

File tree

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ To be released.
5656
[#14]: https://github.com/fedify-dev/botkit/pull/14
5757
[#18]: https://github.com/fedify-dev/botkit/issues/18
5858

59+
### @fedify/botkit-postgres
60+
61+
- Added a new PostgreSQL repository package, *`@fedify/botkit-postgres`*,
62+
which provides `PostgresRepository`, `PostgresRepositoryOptions`, and
63+
`initializePostgresRepositorySchema()`. [[#11], [#19]]
64+
65+
[#11]: https://github.com/fedify-dev/botkit/issues/11
66+
[#19]: https://github.com/fedify-dev/botkit/pull/19
67+
5968

6069
Version 0.3.1
6170
-------------

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"check-versions": "deno run --allow-read --allow-write scripts/check_versions.ts",
3636
"fmt": "deno fmt && deno task hongdown --write",
3737
"install": "deno cache packages/*/src/*.ts && pnpm install",
38-
"test": "deno test --allow-read --allow-write --allow-env --allow-net=hollo.social --parallel",
38+
"test": "deno test --allow-read --allow-write --allow-env --allow-net=hollo.social,localhost,127.0.0.1 --parallel",
3939
"test:node": "pnpm install && pnpm run -r test",
4040
"test-all": {
4141
"dependencies": ["check", "test", "test:node"]

deno.lock

Lines changed: 147 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/concepts/bot.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ the key–value store specified in the [`kv`](#createbotoptions-kv) option.
9797

9898
For more information, see the [*Repository* section](./repository.md).
9999

100+
If you want to use a SQL-backed repository directly instead of
101+
[`KvRepository`](./repository.md#kvrepository), BotKit also provides concrete
102+
repository packages such as
103+
[`SqliteRepository`](./repository.md#sqliterepository) and
104+
[`PostgresRepository`](./repository.md#postgresrepository).
105+
100106
### `~CreateBotOptions.identifier`
101107

102108
The internal identifier of the bot actor. It is used for the URI of the bot

docs/concepts/repository.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,88 @@ properties:
135135
[`node:sqlite`]: https://nodejs.org/api/sqlite.html
136136

137137

138+
`PostgresRepository`
139+
--------------------
140+
141+
*This API is available since BotKit 0.4.0.*
142+
143+
The `PostgresRepository` is a repository that stores data in PostgreSQL using
144+
the [Postgres.js] driver. It is suited for deployments where multiple bot
145+
processes need to share the same persistent repository state, or where you
146+
already operate PostgreSQL for other infrastructure.
147+
148+
Unlike [`KvRepository`](#kvrepository), `PostgresRepository` stores BotKit data
149+
in ordinary PostgreSQL tables rather than a key-value abstraction. It creates
150+
tables inside a dedicated PostgreSQL schema, uses transactions for multi-step
151+
updates, and supports either an internally owned connection pool or an injected
152+
Postgres.js client.
153+
154+
In order to use `PostgresRepository`, you need to install the
155+
*@fedify/botkit-postgres* package:
156+
157+
::: code-group
158+
159+
~~~~ sh [Deno]
160+
deno add jsr:@fedify/botkit-postgres
161+
~~~~
162+
163+
~~~~ sh [npm]
164+
npm add @fedify/botkit-postgres
165+
~~~~
166+
167+
~~~~ sh [pnpm]
168+
pnpm add @fedify/botkit-postgres
169+
~~~~
170+
171+
~~~~ sh [Yarn]
172+
yarn add @fedify/botkit-postgres
173+
~~~~
174+
175+
:::
176+
177+
The `PostgresRepository` constructor accepts an options object with the
178+
following properties:
179+
180+
`sql`
181+
: An existing [Postgres.js] client. When this is provided, the repository
182+
does not own the client and calling `close()` will not shut it down.
183+
184+
`url`
185+
: A PostgreSQL connection string used to create an internal connection pool.
186+
Exactly one of `sql` and `url` must be provided.
187+
188+
`schema` (optional)
189+
: The PostgreSQL schema used for BotKit tables. Defaults to `"botkit"`.
190+
191+
`maxConnections` (optional)
192+
: The maximum number of connections for the internally created pool. This
193+
option is only valid when `url` is used.
194+
195+
`prepare` (optional)
196+
: Whether to use prepared statements for repository queries. Defaults to
197+
`true`.
198+
199+
These options are mutually exclusive: use either `sql` or `url`. The
200+
`maxConnections` option is only meaningful together with `url`.
201+
202+
The repository initializes its tables and indexes automatically. If you want
203+
to provision them before creating the repository, use the exported
204+
`initializePostgresRepositorySchema()` helper:
205+
206+
~~~~ typescript
207+
import postgres from "postgres";
208+
import { initializePostgresRepositorySchema } from "@fedify/botkit-postgres";
209+
210+
const sql = postgres("postgresql://localhost/botkit");
211+
await initializePostgresRepositorySchema(sql, "botkit");
212+
~~~~
213+
214+
If you disable prepared statements for PgBouncer-style deployments, pass
215+
`false` as the third argument so schema initialization uses the same setting.
216+
217+
[Postgres.js]: https://github.com/porsager/postgres
218+
219+
138220
`MemoryRepository`
139221
------------------
140222

0 commit comments

Comments
 (0)