| 1 | import type { DbEngineAdapter, DbHostConn } from "./index"; |
| 2 | |
| 3 | async function createClient( |
| 4 | const { Redis |
| 5 | return new Redis |
| 6 | host: conn.host |
| 7 | port: conn.port |
| 8 | username: conn.username |
| 9 | password: conn.password |
| 10 | tls: conn.ssl |
| 11 | lazyConnect: true, |
| 12 | maxRetriesPerRequest: 1, |
| 13 | }); |
| 14 | } |
| 15 | |
| 16 | export const redisAdapter: |
| 17 | defaultPort: 6379, |
| 18 | |
| 19 | async testConnection(conn |
| 20 | const client = |
| 21 | try { |
| 22 | await client.connect |
| 23 | await client.ping |
| 24 | return { ok: |
| 25 | } catch (err) |
| 26 | return { ok: |
| 27 | } finally { |
| 28 | client.disconnect(); |
| 29 | } |
| 30 | }, |
| 31 | |
| 32 | async createDatabase(conn |
| 33 | const client = |
| 34 | try { |
| 35 | await client.connect |
| 36 | // ponytail: no per-key isolation (~*), matches MySQL's GRANT ALL trust |
| 37 | // model for this host type - a deliberate, confirmed simplification. |
| 38 | await client.call |
| 39 | "ACL", |
| 40 | "SETUSER", |
| 41 | opts.username, |
| 42 | "on", |
| 43 | `>${opts.password |
| 44 | "~*", |
| 45 | "+@all", |
| 46 | ); |
| 47 | } finally { |
| 48 | client.disconnect(); |
| 49 | } |
| 50 | }, |
| 51 | |
| 52 | async rotatePassword(conn |
| 53 | const client = |
| 54 | try { |
| 55 | await client.connect |
| 56 | await client.call |
| 57 | "ACL", |
| 58 | "SETUSER", |
| 59 | opts.username, |
| 60 | "on", |
| 61 | "resetpass", |
| 62 | `>${opts.newPassword |
| 63 | "~*", |
| 64 | "+@all", |
| 65 | ); |
| 66 | } finally { |
| 67 | client.disconnect(); |
| 68 | } |
| 69 | }, |
| 70 | |
| 71 | async dropDatabase(conn |
| 72 | const client = |
| 73 | try { |
| 74 | await client.connect |
| 75 | await client.call |
| 76 | } finally { |
| 77 | client.disconnect(); |
| 78 | } |
| 79 | }, |
| 80 | |
| 81 | formatConnectionString(opts) { |
| 82 | return `redis://${encodeURIComponent |
| 83 | }, |
| 84 | }; |