| 1 | import type { DbEngineAdapter } from "./index"; |
| 2 | |
| 3 | function authUri(conn: |
| 4 | return `mongodb://${encodeURIComponent |
| 5 | } |
| 6 | |
| 7 | export const mongodbAdapter: |
| 8 | defaultPort: 27017, |
| 9 | |
| 10 | async testConnection(conn |
| 11 | try { |
| 12 | const { MongoClient |
| 13 | const client = |
| 14 | await client.connect |
| 15 | await client.db |
| 16 | await client.close |
| 17 | return { ok: |
| 18 | } catch (err) |
| 19 | return { ok: |
| 20 | } |
| 21 | }, |
| 22 | |
| 23 | async createDatabase(conn |
| 24 | const { MongoClient |
| 25 | const client = |
| 26 | await client.connect |
| 27 | try { |
| 28 | await client.db |
| 29 | createUser: opts.username |
| 30 | pwd: opts.password |
| 31 | roles: [{ role: |
| 32 | }); |
| 33 | } finally { |
| 34 | await client.close |
| 35 | } |
| 36 | }, |
| 37 | |
| 38 | async rotatePassword(conn |
| 39 | const { MongoClient |
| 40 | const client = |
| 41 | await client.connect |
| 42 | try { |
| 43 | await client.db |
| 44 | updateUser: opts.username |
| 45 | pwd: opts.newPassword |
| 46 | }); |
| 47 | } finally { |
| 48 | await client.close |
| 49 | } |
| 50 | }, |
| 51 | |
| 52 | async dropDatabase(conn |
| 53 | const { MongoClient |
| 54 | const client = |
| 55 | await client.connect |
| 56 | try { |
| 57 | await client |
| 58 | .db(opts.database |
| 59 | .command({ dropUser: |
| 60 | .catch(() => |
| 61 | await client.db |
| 62 | } finally { |
| 63 | await client.close |
| 64 | } |
| 65 | }, |
| 66 | |
| 67 | formatConnectionString(opts) { |
| 68 | return `mongodb://${encodeURIComponent |
| 69 | }, |
| 70 | }; |