Skip to content

Commit de6972c

Browse files
authored
Merge pull request #597 from dahlia/mysql
Add `@fedify/mysql` package with `MysqlKvStore`
2 parents 1eea01b + ea46b1d commit de6972c

24 files changed

Lines changed: 1587 additions & 34 deletions

File tree

.github/workflows/main.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,21 @@ jobs:
5656
--health-retries 5
5757
ports:
5858
- 6379:6379
59+
mysql:
60+
image: mysql:8
61+
env:
62+
MYSQL_ROOT_PASSWORD: mysql
63+
MYSQL_DATABASE: fedify
64+
options: >-
65+
--health-cmd "mysqladmin ping -h 127.0.0.1 -uroot -pmysql"
66+
--health-interval 10s
67+
--health-timeout 5s
68+
--health-retries 5
69+
ports:
70+
- 3306:3306
5971
env:
6072
AMQP_URL: amqp://guest:guest@localhost:5672
73+
MYSQL_URL: mysql://root:mysql@localhost:3306/fedify
6174
POSTGRES_URL: postgres://postgres:postgres@localhost:5432/postgres
6275
REDIS_URL: redis://localhost:6379
6376
steps:
@@ -127,8 +140,21 @@ jobs:
127140
--health-retries 5
128141
ports:
129142
- 6379:6379
143+
mysql:
144+
image: mysql:8
145+
env:
146+
MYSQL_ROOT_PASSWORD: mysql
147+
MYSQL_DATABASE: fedify
148+
options: >-
149+
--health-cmd "mysqladmin ping -h 127.0.0.1 -uroot -pmysql"
150+
--health-interval 10s
151+
--health-timeout 5s
152+
--health-retries 5
153+
ports:
154+
- 3306:3306
130155
env:
131156
AMQP_URL: amqp://guest:guest@localhost:5672
157+
MYSQL_URL: mysql://root:mysql@localhost:3306/fedify
132158
POSTGRES_URL: postgres://postgres:postgres@localhost:5432/postgres
133159
REDIS_URL: redis://localhost:6379
134160
steps:
@@ -177,8 +203,21 @@ jobs:
177203
--health-retries 5
178204
ports:
179205
- 6379:6379
206+
mysql:
207+
image: mysql:8
208+
env:
209+
MYSQL_ROOT_PASSWORD: mysql
210+
MYSQL_DATABASE: fedify
211+
options: >-
212+
--health-cmd "mysqladmin ping -h 127.0.0.1 -uroot -pmysql"
213+
--health-interval 10s
214+
--health-timeout 5s
215+
--health-retries 5
216+
ports:
217+
- 3306:3306
180218
env:
181219
AMQP_URL: amqp://guest:guest@localhost:5672
220+
MYSQL_URL: mysql://root:mysql@localhost:3306/fedify
182221
POSTGRES_URL: postgres://postgres:postgres@localhost:5432/postgres
183222
REDIS_URL: redis://localhost:6379
184223
steps:

.hongdown.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ proper_nouns = [
2626
"@fedify/h3",
2727
"@fedify/hono",
2828
"@fedify/koa",
29+
"@fedify/mysql",
2930
"@fedify/nestjs",
3031
"@fedify/postgres",
3132
"@fedify/redis",

AGENTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ Common tasks
169169
1. For core KV/MQ interfaces: implement in *packages/fedify/src/federation/kv.ts*
170170
and *packages/fedify/src/federation/mq.ts*
171171
2. For specific database adapters: create dedicated packages
172-
(*packages/sqlite/*, *packages/postgres/*, *packages/redis/*,
173-
*packages/amqp/*)
172+
(*packages/sqlite/*, *packages/postgres/*, *packages/mysql/*,
173+
*packages/redis/*, *packages/amqp/*)
174174
3. Follow the pattern from existing database adapter packages
175175
4. Implement both KV store and message queue interfaces as needed
176176

@@ -326,8 +326,8 @@ The monorepo uses different build processes for different packages:
326326
3. **Database adapters and integrations**: Use tsdown for TypeScript compilation:
327327
- *packages/amqp/*, *packages/astro/*, *packages/elysia*,
328328
*packages/express/*, *packages/h3/*,
329-
*packages/sqlite/*, *packages/postgres/*, *packages/redis/*,
330-
*packages/nestjs/*
329+
*packages/mysql/*, *packages/sqlite/*, *packages/postgres/*,
330+
*packages/redis/*, *packages/nestjs/*
331331
- Built to support Node.js and Bun environments
332332

333333
Ensure changes work across all distribution formats and target environments.

CHANGES.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,6 @@ To be released.
1919
[#473]: https://github.com/fedify-dev/fedify/issues/473
2020
[#589]: https://github.com/fedify-dev/fedify/pull/589
2121

22-
### @fedify/astro
23-
24-
- Added `@fedify/astro` package for integrating Fedify with [Astro].
25-
It provides `fedifyIntegration()` for Vite SSR configuration and
26-
`fedifyMiddleware()` for request handling. [[#50] by Chanhaeng Lee]
27-
28-
[Astro]: https://astro.build/
29-
[#50]: https://github.com/fedify-dev/fedify/issues/50
30-
31-
### @fedify/init
32-
33-
- Changed `fedify init` to add `"temporal"` to `deno.json`'s `"unstable"`
34-
field only when the installed Deno version is earlier than 2.7.0.
35-
On Deno 2.7.0 or later, it is no longer added.
36-
37-
- `fedify init` now omits the `"unstable"` field entirely when no unstable
38-
feature is required for the generated Deno project.
39-
40-
- Supported [Astro] as a web framework option in `fedify init`, with
41-
runtime-specific templates for Deno, Bun, and Node.js environments.
42-
[[#50] by ChanHaeng Lee]
43-
4422
### @fedify/vocab
4523

4624
- Fixed `Endpoints.toJsonLd()` to no longer emit invalid
@@ -63,6 +41,40 @@ To be released.
6341
compact form) in the serialized JSON-LD. This is useful for types
6442
that are not real vocabulary types but rather anonymous object structures.
6543

44+
### @fedify/init
45+
46+
- Changed `fedify init` to add `"temporal"` to `deno.json`'s `"unstable"`
47+
field only when the installed Deno version is earlier than 2.7.0.
48+
On Deno 2.7.0 or later, it is no longer added.
49+
50+
- `fedify init` now omits the `"unstable"` field entirely when no unstable
51+
feature is required for the generated Deno project.
52+
53+
- Supported [Astro] as a web framework option in `fedify init`, with
54+
runtime-specific templates for Deno, Bun, and Node.js environments.
55+
[[#50] by ChanHaeng Lee]
56+
57+
[Astro]: https://astro.build/
58+
[#50]: https://github.com/fedify-dev/fedify/issues/50
59+
60+
### @fedify/astro
61+
62+
- Added `@fedify/astro` package for integrating Fedify with [Astro].
63+
It provides `fedifyIntegration()` for Vite SSR configuration and
64+
`fedifyMiddleware()` for request handling. [[#50] by Chanhaeng Lee]
65+
66+
### @fedify/mysql
67+
68+
- Added `@fedify/mysql` package, a MySQL/MariaDB-backed `KvStore`
69+
implementation. It provides `MysqlKvStore`, which stores key–value
70+
pairs in a MySQL table using the [`mysql2`] driver. Supports TTL,
71+
prefix listing, and compare-and-swap (`cas()`) operations.
72+
[[#585], [#597]]
73+
74+
[`mysql2`]: https://www.npmjs.com/package/mysql2
75+
[#585]: https://github.com/fedify-dev/fedify/issues/585
76+
[#597]: https://github.com/fedify-dev/fedify/pull/597
77+
6678

6779
Version 2.0.3
6880
-------------

deno.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"./packages/hono",
1717
"./packages/koa",
1818
"./packages/lint",
19+
"./packages/mysql",
1920
"./packages/postgres",
2021
"./packages/redis",
2122
"./packages/relay",
@@ -63,6 +64,8 @@
6364
"hono": "jsr:@hono/hono@^4.8.3",
6465
"ioredis": "npm:ioredis@^5.8.2",
6566
"json-preserve-indent": "npm:json-preserve-indent@^1.1.3",
67+
"mysql2": "npm:mysql2@^3.18.0",
68+
"mysql2/promise": "npm:mysql2@^3.18.0/promise",
6669
"postgres": "npm:postgres@^3.4.7",
6770
"preact": "npm:preact@10.19.6",
6871
"tsdown": "npm:tsdown@^0.18.4"

deno.lock

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

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const REFERENCES = {
102102
{ text: "@fedify/h3", link: "https://jsr.io/@fedify/h3/doc" },
103103
{ text: "@fedify/hono", link: "https://jsr.io/@fedify/hono/doc" },
104104
{ text: "@fedify/koa", link: "https://jsr.io/@fedify/koa/doc" },
105+
{ text: "@fedify/mysql", link: "https://jsr.io/@fedify/mysql/doc" },
105106
{ text: "@fedify/postgres", link: "https://jsr.io/@fedify/postgres/doc" },
106107
{ text: "@fedify/redis", link: "https://jsr.io/@fedify/redis/doc" },
107108
{ text: "@fedify/relay", link: "https://jsr.io/@fedify/relay/doc" },

docs/manual/federation.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,19 @@ implementation for production use (as you can guess from the name, it is only
5252
available in Deno runtime).
5353

5454
As separate packages, [`@fedify/redis`] provides [`RedisKvStore`] class, which
55-
is a Redis-backed implementation for production use, and [`@fedify/postgres`]
55+
is a Redis-backed implementation for production use, [`@fedify/postgres`]
5656
provides [`PostgresKvStore`] class, which is a PostgreSQL-backed implementation
57-
for production use.
57+
for production use, and [`@fedify/mysql`] provides [`MysqlKvStore`] class, which
58+
is a MySQL/MariaDB-backed implementation for production use.
5859

5960
Further details are explained in the [*Key–value store* section](./kv.md).
6061

6162
[`@fedify/redis`]: https://github.com/fedify-dev/fedify/tree/main/packages/redis
6263
[`RedisKvStore`]: https://jsr.io/@fedify/redis/doc/kv/~/RedisKvStore
6364
[`@fedify/postgres`]: https://github.com/fedify-dev/fedify/tree/main/packages/postgres
6465
[`PostgresKvStore`]: https://jsr.io/@fedify/postgres/doc/kv/~/PostgresKvStore
66+
[`@fedify/mysql`]: https://github.com/fedify-dev/fedify/tree/main/packages/mysql
67+
[`MysqlKvStore`]: https://jsr.io/@fedify/mysql/doc/kv/~/MysqlKvStore
6568

6669
### `kvPrefixes`
6770

docs/manual/kv.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,65 @@ const federation = createFederation<void>({
306306

307307
[`PostgresKvStore`]: https://jsr.io/@fedify/postgres/doc/kv/~/PostgresKvStore
308308

309+
### [`MysqlKvStore`]
310+
311+
*This API is available since Fedify 2.1.0.*
312+
313+
To use the [`MysqlKvStore`], you need to install the *@fedify/mysql* package
314+
first:
315+
316+
::: code-group
317+
318+
~~~~ bash [Deno]
319+
deno add jsr:@fedify/mysql
320+
~~~~
321+
322+
~~~~ bash [npm]
323+
npm add @fedify/mysql mysql2
324+
~~~~
325+
326+
~~~~ bash [pnpm]
327+
pnpm add @fedify/mysql mysql2
328+
~~~~
329+
330+
~~~~ bash [Yarn]
331+
yarn add @fedify/mysql mysql2
332+
~~~~
333+
334+
~~~~ bash [Bun]
335+
bun add @fedify/mysql mysql2
336+
~~~~
337+
338+
:::
339+
340+
[`MysqlKvStore`] is a key-value store implementation that uses MySQL (or
341+
MariaDB) as the backend storage. It provides scalability and high performance,
342+
making it suitable for production use in distributed systems. It requires
343+
a MySQL or MariaDB server setup and maintenance.
344+
345+
Best for
346+
: Production use, a system that already uses MySQL or MariaDB.
347+
348+
Pros
349+
: Scalable, no additional setup required if already using MySQL/MariaDB.
350+
351+
Cons
352+
: Requires MySQL/MariaDB setup and maintenance.
353+
354+
~~~~ typescript twoslash
355+
import { createFederation } from "@fedify/fedify";
356+
import { MysqlKvStore } from "@fedify/mysql";
357+
import mysql from "mysql2/promise";
358+
359+
const pool = mysql.createPool("mysql://user:pass@localhost/db");
360+
const federation = createFederation<void>({
361+
kv: new MysqlKvStore(pool),
362+
// ... other options
363+
});
364+
~~~~
365+
366+
[`MysqlKvStore`]: https://jsr.io/@fedify/mysql/doc/kv/~/MysqlKvStore
367+
309368
### `WorkersKvStore` (Cloudflare Workers only)
310369

311370
*This API is available since Fedify 1.6.0.*

0 commit comments

Comments
 (0)