(options?: {
readonly entryTablePrefix?: string
readonly remoteIdTable?: string
readonly insertBatchSize?: number
})
| 22 | * @category constructors |
| 23 | */ |
| 24 | export const makeStorage = (options?: { |
| 25 | readonly entryTablePrefix?: string |
| 26 | readonly remoteIdTable?: string |
| 27 | readonly insertBatchSize?: number |
| 28 | }): Effect.Effect< |
| 29 | typeof EventLogServer.Storage.Service, |
| 30 | SqlError, |
| 31 | SqlClient.SqlClient | EventLogEncryption | Scope |
| 32 | > => |
| 33 | Effect.gen(function*() { |
| 34 | const encryptions = yield* EventLogEncryption |
| 35 | const sql = yield* SqlClient.SqlClient |
| 36 | |
| 37 | const tablePrefix = options?.entryTablePrefix ?? "effect_events" |
| 38 | const remoteIdTable = options?.remoteIdTable ?? "effect_remote_id" |
| 39 | const insertBatchSize = options?.insertBatchSize ?? 200 |
| 40 | |
| 41 | yield* sql.onDialectOrElse({ |
| 42 | pg: () => |
| 43 | sql`CREATE TABLE IF NOT EXISTS ${sql(remoteIdTable)} ( |
| 44 | remote_id BYTEA PRIMARY KEY |
| 45 | )`.withoutTransform, |
| 46 | mysql: () => |
| 47 | sql` |
| 48 | CREATE TABLE IF NOT EXISTS ${sql(remoteIdTable)} ( |
| 49 | remote_id BINARY(16) PRIMARY KEY |
| 50 | )`.withoutTransform, |
| 51 | mssql: () => |
| 52 | sql` |
| 53 | CREATE TABLE IF NOT EXISTS ${sql(remoteIdTable)} ( |
| 54 | remote_id VARBINARY(16) PRIMARY KEY |
| 55 | )`.withoutTransform, |
| 56 | orElse: () => |
| 57 | sql` |
| 58 | CREATE TABLE IF NOT EXISTS ${sql(remoteIdTable)} ( |
| 59 | remote_id BLOB PRIMARY KEY |
| 60 | )`.withoutTransform |
| 61 | }) |
| 62 | const remoteId = yield* sql<{ remote_id: Uint8Array }>`SELECT remote_id FROM ${sql(remoteIdTable)}`.pipe( |
| 63 | Effect.flatMap((results) => { |
| 64 | if (results.length > 0) { |
| 65 | return Effect.succeed(RemoteId.make(results[0].remote_id)) |
| 66 | } |
| 67 | const newRemoteId = makeRemoteId() |
| 68 | return Effect.as( |
| 69 | sql`INSERT INTO ${sql(remoteIdTable)} (remote_id) VALUES (${newRemoteId})`, |
| 70 | RemoteId.make(newRemoteId) |
| 71 | ) |
| 72 | }) |
| 73 | ) |
| 74 | |
| 75 | const resources = yield* RcMap.make({ |
| 76 | lookup: (publicKey: string) => |
| 77 | Effect.gen(function*() { |
| 78 | const publicKeyHash = (yield* encryptions.sha256String(new TextEncoder().encode(publicKey))).slice(0, 16) |
| 79 | const table = `${tablePrefix}_${publicKeyHash}` |
| 80 | |
| 81 | yield* sql.onDialectOrElse({ |
no test coverage detected
searching dependent graphs…