Skip to content

IndexedDbQueryBuilder

Builds effectful, schema-aware queries for typed browser IndexedDB versions.

An IndexedDbQueryBuilder is created from an open database and a version's table descriptors, then exposes from(tableName) as the entry point for table operations. Query objects can select, count, delete, insert, upsert, clear tables, stream paged reads, react to invalidations, and run multiple effects in a shared IDBTransaction with withTransaction. Reads decode stored rows with the table schema, and writes encode input values before sending them to IndexedDB.

8 exports Added in v4.0.0 Source

Constructors

make

Added in v4.0.0 Source

Creates an IndexedDbQueryBuilder from an open database reference, key-range constructor, table map, and reactivity service.

Signature

declare function make<Source extends AnyWithProps>(__namedParameters: {
  readonly database: MutableRef<IDBDatabase>;
  readonly IDBKeyRange: {
    (): IDBKeyRange;
    prototype: IDBKeyRange;
    bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    lowerBound(lower: any, open?: boolean): IDBKeyRange;
    only(value: any): IDBKeyRange;
    upperBound(upper: any, open?: boolean): IDBKeyRange;
  };
  readonly reactivity: {
    readonly invalidate: (keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>) => Effect<void>;
    readonly invalidateUnsafe: (keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>) => void;
    readonly mutation: <A, E, R>(keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, effect: Effect<A, E, R>) => Effect<A, E, R>;
    readonly query: <A, E, R>(keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, effect: Effect<A, E, R>) => Effect<Dequeue<A, E>, never, R | Scope>;
    readonly registerUnsafe: (keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, handler: () => void) => () => void;
    readonly stream: <A, E, R>(keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, effect: Effect<A, E, R>) => Stream<A, E, Exclude<R, Scope>>;
    readonly withBatch: <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>;
  };
  readonly tables: ReadonlyMap<string, Tables<Source>>;
}): IndexedDbQueryBuilder<Source>

Errors

ErrorReason type

Added in v4.0.0 Source

String union describing IndexedDB query failure categories such as decoding, encoding, and transaction errors.

Signature

type ErrorReason = "UnknownError" | "DecodeError" | "EncodeError" | "TransactionError";

Tagged error for IndexedDB query operations, carrying a query error reason and the original cause.

Details

reason is the query failure category, cause preserves the underlying schema, IndexedDB request, transaction, or user callback failure, and message is set to the reason.

See

Signature

declare class IndexedDbQueryError extends YieldableError<this> & {
  readonly _tag: "IndexedDbQueryError";
} & Readonly<{
  cause: unknown;
  reason: ErrorReason;
}> {
  constructor(args: {
    readonly cause: unknown;
    readonly reason: ErrorReason;
  });
  readonly "~@effect/platform-browser/IndexedDbQueryBuilder/IndexedDbQueryError": "~@effect/platform-browser/IndexedDbQueryBuilder/IndexedDbQueryError";
  readonly message: ErrorReason;
}

Models

IndexedDbQueryBuilder interface

Added in v4.0.0 Source

Typed query builder for an IndexedDB version, with helpers for table queries, database access, clearing data, and running effects in a shared transaction.

Signature

interface IndexedDbQueryBuilder<Source extends IndexedDbVersion.AnyWithProps> extends Pipeable, Inspectable {
  readonly clearAll: Effect<void, IndexedDbQueryError>;
  readonly database: MutableRef<IDBDatabase>;
  readonly from: <Name extends string>(table: Name) => From<Extract<Tables<Source>, {
    readonly tableName: Name;
  }>>;
  readonly IDBKeyRange: {
    (): IDBKeyRange;
    prototype: IDBKeyRange;
    bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    lowerBound(lower: any, open?: boolean): IDBKeyRange;
    only(value: any): IDBKeyRange;
    upperBound(upper: any, open?: boolean): IDBKeyRange;
  };
  readonly IDBTransaction: IDBTransaction | undefined;
  readonly reactivity: {
    readonly invalidate: (keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>) => Effect<void>;
    readonly invalidateUnsafe: (keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>) => void;
    readonly mutation: <A, E, R>(keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, effect: Effect<A, E, R>) => Effect<A, E, R>;
    readonly query: <A, E, R>(keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, effect: Effect<A, E, R>) => Effect<Dequeue<A, E>, never, R | Scope>;
    readonly registerUnsafe: (keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, handler: () => void) => () => void;
    readonly stream: <A, E, R>(keys: readonly Array<unknown> | ReadonlyRecord<string, readonly Array<unknown>>, effect: Effect<A, E, R>) => Stream<A, E, Exclude<R, Scope>>;
    readonly withBatch: <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>;
  };
  readonly tables: ReadonlyMap<string, Tables<Source>>;
  readonly use: <A = unknown>(f: (database: IDBDatabase) => A) => Effect<A, IndexedDbQueryError>;
  readonly withTransaction: <Tables extends readonly [TableName<Tables<Source>>, TableName<Tables<Source>>], Mode extends "readonly" | "readwrite">(options: {
    readonly durability?: IDBTransactionDurability;
    readonly mode: Mode;
    readonly tables: Tables;
  }) => <A, E, R>(effect: Effect<A, E, R>) => Effect<A, IndexedDbQueryError | E, Exclude<R, IndexedDbTransaction>>;
}

Other

Namespace containing the typed IndexedDB query model interfaces and helper types.

Services

Service tag for the active IDBTransaction used to share a transaction across IndexedDB query effects.

Signature

declare class IndexedDbTransaction extends Shape<
  "@effect/platform-browser/IndexedDbQueryBuilder/IndexedDbTransaction",
  IDBTransaction,
  this
> {
  constructor(_: never);
}

Utility Types

KeyPath type

Added in v4.0.0 Source

Valid key-path type for a table schema, using encoded fields whose values are IndexedDB-valid keys.

Signature

type KeyPath<TableSchema extends IndexedDbTable.AnySchemaStruct> =
  | IndexedDbValidKeys<TableSchema>
  | NonEmptyReadonlyArray<IndexedDbValidKeys<TableSchema>>;

KeyPathNumber type

Added in v4.0.0 Source

Valid numeric key-path type for a table schema, used for auto-increment key paths.

Signature

type KeyPathNumber<TableSchema extends IndexedDbTable.AnySchemaStruct> =
  | IndexedDbValidNumberKeys<TableSchema>
  | NonEmptyReadonlyArray<IndexedDbValidNumberKeys<TableSchema>>;