/** * @since 1.0.0 */ import type * as Chunk from "@effect/data/Chunk" import type * as Context from "@effect/data/Context" import type * as Either from "@effect/data/Either" import type * as Equal from "@effect/data/Equal" import type * as Option from "@effect/data/Option" import type * as Effect from "@effect/io/Effect" import type * as CompletedRequestMap from "@effect/query/CompletedRequestMap" import type * as Described from "@effect/query/Described" import * as internal from "@effect/query/internal_effect_untraced/dataSource" import type * as Request from "@effect/query/Request" /** * @since 1.0.0 * @category symbols */ export const DataSourceTypeId: unique symbol = internal.DataSourceTypeId /** * @since 1.0.0 * @category symbols */ export type DataSourceTypeId = typeof DataSourceTypeId /** * A `DataSource` requires an environment `R` and is capable of executing * requests of type `A`. * * Data sources must implement the method `runAll` which takes a collection of * requests and returns an effect with a `CompletedRequestMap` containing a * mapping from requests to results. The type of the collection of requests is * a `Chunk>`. The outer `Chunk` represents batches of requests that * must be performed sequentially. The inner `Chunk` represents a batch of * requests that can be performed in parallel. This allows data sources to * introspect on all the requests being executed and optimize the query. * * Data sources will typically be parameterized on a subtype of `Request`, * though that is not strictly necessarily as long as the data source can map * the request type to a `Request`. Data sources can then pattern match on * the collection of requests to determine the information requested, execute * the query, and place the results into the `CompletedRequestMap` using * `CompletedRequestMap.empty` and `CompletedRequestMap.insert`. Data * sources must provide results for all requests received. Failure to do so * will cause a query to die with a `QueryFailure` when run. * * @since 1.0.0 * @category models */ export interface DataSource extends Equal.Equal { /** * The data source's identifier. */ readonly identifier: string /** * Execute a collection of requests. The outer `Chunk` represents batches * of requests that must be performed sequentially. The inner `Chunk` * represents a batch of requests that can be performed in parallel. */ runAll(requests: Chunk.Chunk>): Effect.Effect } /** * @since 1.0.0 */ export declare namespace DataSource { /** * @since 1.0.0 * @category models */ export interface Variance { readonly [DataSourceTypeId]: { readonly _R: (_: never) => R readonly _A: (_: never) => A } } } /** * Returns `true` if the specified value is a `DataSource`, `false` otherwise. * * @since 1.0.0 * @category refinements */ export const isDataSource: (u: unknown) => u is DataSource = internal.isDataSource /** * Constructs a data source with the specified identifier and method to run * requests. * * @since 1.0.0 * @category constructors */ export const make: ( identifier: string, runAll: (requests: Chunk.Chunk>) => Effect.Effect ) => DataSource, A> = internal.make /** * Constructs a data source from a function taking a collection of requests * and returning a `CompletedRequestMap`. * * @since 1.0.0 * @category constructors */ export const makeBatched: >( identifier: string, run: (requests: Chunk.Chunk) => Effect.Effect ) => DataSource, A> = internal.makeBatched /** * A data source aspect that executes requests between two effects, `before` * and `after`, where the result of `before` can be used by `after`. * * @since 1.0.0 * @category combinators */ export const around: { ( before: Described.Described>, after: Described.Described<(a: A2) => Effect.Effect> ): (self: DataSource) => DataSource ( self: DataSource, before: Described.Described>, after: Described.Described<(a: A2) => Effect.Effect> ): DataSource } = internal.around /** * Returns a data source that executes at most `n` requests in parallel. * * @since 1.0.0 * @category combinators */ export const batchN: { (n: number): (self: DataSource) => DataSource (self: DataSource, n: number): DataSource } = internal.batchN /** * Returns a new data source that executes requests of type `B` using the * specified function to transform `B` requests into requests that this data * source can execute. * * @since 1.0.0 * @category combinators */ export const contramap: { , B extends Request.Request>( f: Described.Described<(_: B) => A> ): (self: DataSource) => DataSource , B extends Request.Request>( self: DataSource, f: Described.Described<(_: B) => A> ): DataSource } = internal.contramap /** * Provides this data source with part of its required context. * * @since 1.0.0 * @category context */ export const contramapContext: { ( f: Described.Described<(context: Context.Context) => Context.Context> ): >(self: DataSource) => DataSource , R0>( self: DataSource, f: Described.Described<(context: Context.Context) => Context.Context> ): DataSource } = internal.contramapContext /** * Returns a new data source that executes requests of type `B` using the * specified effectual function to transform `B` requests into requests that * this data source can execute. * * @since 1.0.0 * @category combinators */ export const contramapEffect: { , R2, B extends Request.Request>( f: Described.Described<(_: B) => Effect.Effect> ): (self: DataSource) => DataSource , R2, B extends Request.Request>( self: DataSource, f: Described.Described<(_: B) => Effect.Effect> ): DataSource } = internal.contramapEffect /** * Returns a new data source that executes requests of type `C` using the * specified function to transform `C` requests into requests that either this * data source or that data source can execute. * * @since 1.0.0 * @category combinators */ export const eitherWith: { , R2, B extends Request.Request, C extends Request.Request>( that: DataSource, f: Described.Described<(_: C) => Either.Either> ): (self: DataSource) => DataSource < R, A extends Request.Request, R2, B extends Request.Request, C extends Request.Request >( self: DataSource, that: DataSource, f: Described.Described<(_: C) => Either.Either> ): DataSource } = internal.eitherWith /** * Constructs a data source from a pure function. * * @since 1.0.0 * @category constructors */ export const fromFunction: >( name: string, f: (request: A) => Request.Request.Success ) => DataSource = internal.fromFunction /** * Constructs a data source from a pure function that takes a list of requests * and returns a list of results of the same size. Each item in the result * list must correspond to the item at the same index in the request list. * * @since 1.0.0 * @category constructors */ export const fromFunctionBatched: >( name: string, f: (chunk: Chunk.Chunk) => Chunk.Chunk> ) => DataSource = internal.fromFunctionBatched /** * Constructs a data source from an effectual function that takes a list of * requests and returns a list of results of the same size. Each item in the * result list must correspond to the item at the same index in the request * list. * * @since 1.0.0 * @category constructors */ export const fromFunctionBatchedEffect: >( name: string, f: (chunk: Chunk.Chunk) => Effect.Effect, Chunk.Chunk>> ) => DataSource = internal.fromFunctionBatchedEffect /** * Constructs a data source from a pure function that takes a list of requests * and returns a list of optional results of the same size. Each item in the * result list must correspond to the item at the same index in the request * list. * * @since 1.0.0 * @category constructors */ export const fromFunctionBatchedOption: >( name: string, f: (chunk: Chunk.Chunk) => Chunk.Chunk>> ) => DataSource = internal.fromFunctionBatchedOption /** * Constructs a data source from an effectual function that takes a list of * requests and returns a list of optional results of the same size. Each item * in the result list must correspond to the item at the same index in the * request list. * * @since 1.0.0 * @category constructors */ export const fromFunctionBatchedOptionEffect: >( name: string, f: ( chunk: Chunk.Chunk ) => Effect.Effect, Chunk.Chunk>>> ) => DataSource = internal.fromFunctionBatchedOptionEffect /** * Constructs a data source from a function that takes a list of requests and * returns a list of results of the same size. Uses the specified function to * associate each result with the corresponding effect, allowing the function * to return the list of results in a different order than the list of * requests. * * @since 1.0.0 * @category constructors */ export const fromFunctionBatchedWith: >( name: string, f: (chunk: Chunk.Chunk) => Chunk.Chunk>, g: (value: Request.Request.Success) => Request.Request> ) => DataSource = internal.fromFunctionBatchedWith /** * Constructs a data source from an effectual function that takes a list of * requests and returns a list of results of the same size. Uses the specified * function to associate each result with the corresponding effect, allowing * the function to return the list of results in a different order than the * list of requests. * * @since 1.0.0 * @category constructors */ export const fromFunctionBatchedWithEffect: >( name: string, f: (chunk: Chunk.Chunk) => Effect.Effect, Chunk.Chunk>>, g: (b: Request.Request.Success) => Request.Request, Request.Request.Success> ) => DataSource = internal.fromFunctionBatchedWithEffect /** * Constructs a data source from an effectual function. * * @since 1.0.0 * @category constructors */ export const fromFunctionEffect: >( name: string, f: (a: A) => Effect.Effect, Request.Request.Success> ) => DataSource = internal.fromFunctionEffect /** * Constructs a data source from a pure function that may not provide results * for all requests received. * * @since 1.0.0 * @category constructors */ export const fromFunctionOption: >( name: string, f: (a: A) => Option.Option> ) => DataSource = internal.fromFunctionOption /** * Constructs a data source from an effectual function that may not provide * results for all requests received. * * @since 1.0.0 * @category constructors */ export const fromFunctionOptionEffect: >( name: string, f: (a: A) => Effect.Effect, Option.Option>> ) => DataSource = internal.fromFunctionOptionEffect /** * A data source that never executes requests. * * @since 1.0.0 * @category constructors */ export const never: (_: void) => DataSource = internal.never /** * Provides this data source with its required context. * * @since 1.0.0 * @category context */ export const provideContext: { ( context: Described.Described> ): >(self: DataSource) => DataSource >( self: DataSource, context: Described.Described> ): DataSource } = internal.provideContext /** * Returns a new data source that executes requests by sending them to this * data source and that data source, returning the results from the first data * source to complete and safely interrupting the loser. * * @since 1.0.0 * @category combinators */ export const race: { >( that: DataSource ): >(self: DataSource) => DataSource , R2, A2 extends Request.Request>( self: DataSource, that: DataSource ): DataSource } = internal.race