Skip to content

Commit 9b44640

Browse files
committed
add sample
1 parent c571138 commit 9b44640

11 files changed

Lines changed: 861 additions & 398 deletions

File tree

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Sample app notes:
3030
- `sample/session.ts` uses `MemorySessionStore`
3131
- `sample/session_cookie.ts` shows the cookie store pattern
3232
- `sample/session_kv.ts` shows the Deno KV store pattern
33+
- `sample/session_redis.ts` shows the Redis store pattern
34+
35+
Redis sample notes:
36+
- Uses `REDIS_HOST` and `REDIS_PORT` (defaults: `127.0.0.1:6379`)
3337

3438
### 1. Create a session middleware
3539

@@ -138,23 +142,19 @@ For distributed environments with Redis.
138142

139143
```ts ignore
140144
import { type RedisClient, RedisSessionStore } from "./mod.ts";
141-
import { connect } from "npm:ioredis";
145+
import { connect } from "jsr:@db/redis";
142146

143-
const redis = new Redis({
144-
host: "localhost",
147+
const redis = await connect({
148+
hostname: "127.0.0.1",
145149
port: 6379,
146150
});
147151

148152
// Adapt to RedisClient interface
149153
const client: RedisClient = {
150154
get: (key) => redis.get(key),
151-
set: (key, value, options) => {
152-
if (options?.ex) {
153-
return redis.set(key, value, "EX", options.ex).then(() => {});
154-
}
155-
return redis.set(key, value).then(() => {});
156-
},
157-
del: (key) => redis.del(key).then(() => {}),
155+
set: (key, value, options) =>
156+
redis.set(key, value, options?.ex ? { ex: options.ex } : undefined),
157+
del: (key) => redis.del(key),
158158
};
159159

160160
const store = new RedisSessionStore({ client, keyPrefix: "session:" });
@@ -169,9 +169,7 @@ For applications using relational databases.
169169
CREATE TABLE sessions (
170170
session_id VARCHAR(36) PRIMARY KEY,
171171
data TEXT NOT NULL,
172-
expires_at DATETIME NULL,
173-
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
174-
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
172+
expires_at DATETIME NULL
175173
);
176174
CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
177175
```

deno.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
"sample": "deno run -EN --watch sample/main.ts",
88
"sample:memory": "deno run -EN --watch sample/main.ts memory",
99
"sample:cookie": "deno run -EN --watch sample/main.ts cookie",
10+
"sample:redis": "deno run -A scripts/with-resource.ts -- deno run -EN --watch sample/main.ts redis",
1011
"sample:kv": "deno run -EN --watch sample/main.ts kv",
11-
"test": "deno test -E",
12-
"test:doc": "deno test --doc -E"
12+
"sample:mysql": "deno run -A scripts/with-resource.ts -- deno run -EN --watch sample/main.ts mysql",
13+
"test": "deno run -A scripts/with-resource.ts -- deno test -EN",
14+
"test:doc": "deno run -A scripts/with-resource.ts -- deno test --doc -EN"
1315
},
1416
"test": {
1517
"include": ["mod_test.ts", "src/**/*_test.ts"]
1618
},
1719
"license": "MIT",
1820
"imports": {
21+
"@db/redis": "jsr:@db/redis@^0.40.0",
1922
"@fresh/core": "jsr:@fresh/core@^2.2.0",
2023
"@std/assert": "jsr:@std/assert@1",
2124
"@std/http/cookie": "jsr:@std/http@1/cookie",
22-
"ioredis-mock": "npm:ioredis-mock@^8.13.1",
25+
"mysql2": "npm:mysql2@^3.16.2",
2326
"preact": "npm:preact@^10.28.3"
2427
},
2528
"unstable": [

0 commit comments

Comments
 (0)