(system: DbSystem, dialect: SQLDialect, jsonColumnType: string)
| 65 | try { |
| 66 | result[key] = JSON.parse(value) |
| 67 | } catch { |
| 68 | result[key] = value |
| 69 | } |
| 70 | } else { |
| 71 | result[key] = value |
| 72 | } |
| 73 | } |
| 74 | return result |
| 75 | } |
| 76 | |
| 77 | function makeSQLStoreInt(system: DbSystem, dialect: SQLDialect, jsonColumnType: string) { |
| 78 | return Effect.fnUntraced(function*({ prefix }: StorageConfig) { |
| 79 | const sql = yield* SqlClient.SqlClient |
| 80 | return { |
| 81 | make: Effect.fnUntraced(function*<IdKey extends keyof Encoded, Encoded extends FieldValues, R = never, E = never>( |
| 82 | name: string, |
| 83 | idKey: IdKey, |
| 84 | seed?: Effect.Effect<Iterable<Encoded>, E, R>, |
| 85 | config?: StoreConfig<Encoded> |
| 86 | ) { |
| 87 | type PM = PersistenceModelType<Encoded> |
| 88 | const tableName = `${prefix}${name}` |
| 89 | const defaultValues = config?.defaultValues ?? {} |
| 90 | |
| 91 | const resolveNamespace = !config?.allowNamespace |
| 92 | ? Effect.succeed("primary") |
| 93 | : storeId.pipe(Effect.map((namespace) => { |
| 94 | if (namespace !== "primary" && !config.allowNamespace!(namespace)) { |
| 95 | throw new Error(`Namespace ${namespace} not allowed!`) |
| 96 | } |
| 97 | return namespace |
| 98 | })) |
| 99 | |
| 100 | const ensureTable = sql |
| 101 | .unsafe( |
| 102 | `CREATE TABLE IF NOT EXISTS "${tableName}" (id TEXT NOT NULL, _namespace TEXT NOT NULL DEFAULT 'primary', _etag TEXT, data ${jsonColumnType} NOT NULL, PRIMARY KEY (id, _namespace))` |
| 103 | ) |
| 104 | .pipe( |
| 105 | Effect.andThen( |
| 106 | sql.unsafe( |
| 107 | `CREATE TABLE IF NOT EXISTS "_migrations" (id TEXT NOT NULL, version TEXT NOT NULL, PRIMARY KEY (id, version))` |
| 108 | ) |
| 109 | ), |
| 110 | Effect.orDie, |
| 111 | Effect.asVoid |
| 112 | ) |
| 113 | |
| 114 | const toRow = (e: PM) => { |
| 115 | const newE = makeETag(e) |
| 116 | const id = newE[idKey] as string |
| 117 | const { _etag, [idKey]: _id, ...rest } = newE as any |
| 118 | const data = JSON.stringify(rest) |
| 119 | return { id, _etag: newE._etag!, data, item: newE } |
| 120 | } |
| 121 | |
| 122 | const exec = (query: string, params?: readonly unknown[]) => |
| 123 | sql.unsafe(query, params as any).pipe(Effect.mapError(toDatabaseError)) |
| 124 |
no test coverage detected
searching dependent graphs…