From f100e2087172d7e4ab8c0d1ee9a5780b9712382a Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 25 Sep 2024 19:48:32 +1200 Subject: [PATCH] use decoded ids in sql resolvers (#3683) --- .changeset/cold-stingrays-complain.md | 5 ++ packages/sql/src/SqlResolver.ts | 80 +++++++++++++-------------- 2 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 .changeset/cold-stingrays-complain.md diff --git a/.changeset/cold-stingrays-complain.md b/.changeset/cold-stingrays-complain.md new file mode 100644 index 00000000000..1d5d8120db5 --- /dev/null +++ b/.changeset/cold-stingrays-complain.md @@ -0,0 +1,5 @@ +--- +"@effect/sql": minor +--- + +use decoded ids in sql resolvers diff --git a/packages/sql/src/SqlResolver.ts b/packages/sql/src/SqlResolver.ts index 1a3dd9ea272..9c5d1aa2ae9 100644 --- a/packages/sql/src/SqlResolver.ts +++ b/packages/sql/src/SqlResolver.ts @@ -10,6 +10,7 @@ import * as Equal from "effect/Equal" import * as Exit from "effect/Exit" import * as FiberRef from "effect/FiberRef" import * as Hash from "effect/Hash" +import * as MutableHashMap from "effect/MutableHashMap" import * as Option from "effect/Option" import * as Request from "effect/Request" import * as RequestResolver from "effect/RequestResolver" @@ -26,6 +27,7 @@ export interface SqlRequest extends Request.Request( +const makeRequest = ( tag: T, input: I, + encoded: II, span: Tracer.Span ): SqlRequest => { const self = Object.create(SqlRequestProto) as Types.Mutable> @@ -54,6 +57,7 @@ const makeRequest = ( attributes: {} } self.input = input + self.encoded = encoded return self } @@ -64,24 +68,24 @@ const partitionRequests = (requests: ReadonlyArray() => (requests: ReadonlyArray>) => { +const partitionRequestsById = () => (requests: ReadonlyArray>) => { const len = requests.length - const inputs: Array = new Array(len) + const inputs: Array = new Array(len) const spanLinks: Array = new Array(len) - const byIdMap = new Map>() + const byIdMap = MutableHashMap.empty>() for (let i = 0; i < len; i++) { const request = requests[i] - inputs[i] = request.input + inputs[i] = request.encoded as II spanLinks[i] = request.spanLink - byIdMap.set(request.input as I, request) + MutableHashMap.set(byIdMap, request.input as I, request) } return [inputs, spanLinks, byIdMap] as const @@ -101,8 +105,8 @@ export interface SqlResolver readonly cachePopulate: ( id: I, result: A - ) => Effect.Effect - readonly cacheInvalidate: (id: I) => Effect.Effect + ) => Effect.Effect + readonly cacheInvalidate: (id: I) => Effect.Effect readonly request: (input: I) => Effect.Effect, ParseError, R> } @@ -122,7 +126,7 @@ const makeResolver = ( (span) => Effect.withFiberRuntime((fiber) => { span.attribute("request.input", input) - const currentContext = fiber.getFiberRef(FiberRef.currentContext) + const currentContext = fiber.currentContext const connection = currentContext.unsafeMap.get( internalClient.TransactionConnection.key ) @@ -146,7 +150,7 @@ const makeResolver = ( : RequestResolver.provideContext(self, toProvide) return Effect.flatMap( encode(input), - (input) => Effect.request(makeRequest(tag, input, span), resolver) + (encoded) => Effect.request(makeRequest(tag, input, encoded, span), resolver) ) }) ) @@ -156,23 +160,18 @@ const makeResolver = ( request(input: I) { return Effect.withFiberRuntime, ParseError, RI>( (fiber) => { - const span = fiber - .getFiberRef(FiberRef.currentContext) - .unsafeMap.get(Tracer.ParentSpan.key) - return Effect.map(encode(input), (input) => makeRequest(tag, input, span)) + const span = fiber.currentContext.unsafeMap.get(Tracer.ParentSpan.key) + return Effect.map(encode(input), (encoded) => makeRequest(tag, input, encoded, span)) } ) }, cachePopulate(input: I, value: A) { - return Effect.flatMap( - encode(input), - (input) => Effect.cacheRequestResult(makeRequest(tag, input, null as any), Exit.succeed(value)) - ) + return Effect.cacheRequestResult(makeRequest(tag, input, null as any, null as any), Exit.succeed(value)) }, cacheInvalidate(input: I) { - return Effect.withFiberRuntime((fiber) => { + return Effect.withFiberRuntime((fiber) => { const cache = fiber.getFiberRef(FiberRef.currentRequestCache) - return Effect.flatMap(encode(input), (input) => cache.invalidate(makeRequest(tag, input, null as any))) + return cache.invalidate(makeRequest(tag, input, null as any, null as any)) }) }, makeExecute, @@ -269,7 +268,7 @@ export const grouped = - readonly RequestGroupKey: (request: Types.NoInfer) => K + readonly RequestGroupKey: (request: Types.NoInfer) => K readonly Result: Schema.Schema readonly ResultGroupKey: (result: Types.NoInfer, row: Types.NoInfer) => K readonly execute: ( @@ -279,7 +278,7 @@ export const grouped = - readonly RequestGroupKey: (request: Types.NoInfer) => K + readonly RequestGroupKey: (request: Types.NoInfer) => K readonly Result: Schema.Schema readonly ResultGroupKey: (result: Types.NoInfer, row: Types.NoInfer) => K readonly execute: ( @@ -292,7 +291,7 @@ export const grouped = , E>>) => { const [inputs, spanLinks] = partitionRequests(requests) - const resultMap = new Map>() + const resultMap = MutableHashMap.empty>() return options.execute(inputs as any).pipe( Effect.bindTo("rawResults"), Effect.bind("results", ({ rawResults }) => decodeResults(rawResults)), @@ -300,19 +299,20 @@ export const grouped = { - const key = options.RequestGroupKey(request.input as II) - return Request.succeed(request, resultMap.get(key) ?? []) + const key = options.RequestGroupKey(request.input as I) + const result = MutableHashMap.get(resultMap, key) + return Request.succeed(request, result._tag === "None" ? [] : result.value) }, { discard: true } ) @@ -348,7 +348,7 @@ export const findById = readonly Result: Schema.Schema - readonly ResultId: (result: Types.NoInfer, row: Types.NoInfer) => II + readonly ResultId: (result: Types.NoInfer, row: Types.NoInfer) => I readonly execute: ( requests: Array> ) => Effect.Effect, E> @@ -357,7 +357,7 @@ export const findById = readonly Result: Schema.Schema - readonly ResultId: (result: Types.NoInfer, row: Types.NoInfer) => II + readonly ResultId: (result: Types.NoInfer, row: Types.NoInfer) => I readonly execute: ( requests: Array> ) => Effect.Effect, E, R> @@ -367,7 +367,7 @@ export const findById = , E>>) => { - const [inputs, spanLinks, idMap] = partitionRequestsById()(requests) + const [inputs, spanLinks, idMap] = partitionRequestsById()(requests) return options.execute(inputs as any).pipe( Effect.bindTo("rawResults"), Effect.bind("results", ({ rawResults }) => decodeResults(rawResults)), @@ -376,23 +376,23 @@ export const findById = { const id = options.ResultId(result, rawResults[i]) - const request = idMap.get(id) - if (request === undefined) { + const request = MutableHashMap.get(idMap, id) + if (request._tag === "None") { return Effect.void } - idMap.delete(id) - return Request.succeed(request, Option.some(result)) + MutableHashMap.remove(idMap, id) + return Request.succeed(request.value, Option.some(result)) }, { discard: true } ) ), Effect.tap((_) => { - if (idMap.size === 0) { + if (MutableHashMap.size(idMap) === 0) { return Effect.void } return Effect.forEach( - idMap.values(), - (request) => Request.succeed(request, Option.none()), + idMap, + ([, request]) => Request.succeed(request, Option.none()), { discard: true } ) }),