@ydbjs/query
Version:
High-level, type-safe YQL query and transaction client for YDB. Supports tagged template syntax, parameter binding, transactions, and statistics.
93 lines • 4.2 kB
TypeScript
import { EventEmitter } from 'node:events';
import { type QueryStats, StatsMode, Syntax } from '@ydbjs/api/query';
import type { Driver } from '@ydbjs/core';
import { type RetryContext } from '@ydbjs/retry';
import { type Value } from '@ydbjs/value';
import type { Metadata } from 'nice-grpc';
type ArrayifyTuple<T extends any[]> = {
[K in keyof T]: T[K][];
};
export type QueryEventMap = {
'done': [ArrayifyTuple<any>];
'retry': [RetryContext];
'error': [unknown];
'stats': [QueryStats];
'cancel': [];
'metadata': [Metadata];
};
export declare class Query<T extends any[] = unknown[]> extends EventEmitter<QueryEventMap> implements PromiseLike<ArrayifyTuple<T>>, AsyncDisposable {
#private;
constructor(driver: Driver, text: string, params: Record<string, Value>);
static get [Symbol.species](): PromiseConstructor;
/** Returns the result of the query */
then<TResult1 = ArrayifyTuple<T>, TResult2 = never>(onfulfilled?: (value: ArrayifyTuple<T>) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: unknown) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
/** Indicates if the query is currently executing */
get active(): boolean;
/** Indicates if the query has been cancelled */
get cancelled(): boolean;
get text(): string;
get parameters(): Record<string, Value>;
syntax(syntax: Exclude<Syntax, Syntax.UNSPECIFIED>): Query<T>;
pool(poolId: string): Query<T>;
/** Adds a parameter to the query */
parameter(name: string, parameter: Value | undefined): Query<T>;
/** Adds a parameter to the query */
param(name: string, parameter: Value | undefined): Query<T>;
/**
* Sets the idempotent flag for the query.
*
* ONLY FOR SINGLE EXECUTE CALLS.
* DO NOTHING IN TRANSACTION CONTEXT (sql.begin or sql.transaction).
*
* Idempotent queries may be retried without side effects.
*/
idempotent(idempotent?: boolean): Query<T>;
/**
* Sets the transaction isolation level for a single execute call.
*
* ONLY FOR SINGLE EXECUTE CALLS.
* DO NOTHING IN TRANSACTION CONTEXT (sql.begin or sql.transaction).
*
* A transaction is always used. If `mode` is 'implicit', the database decides the isolation level.
* If a specific isolation `mode` is provided, the query will be executed within a single transaction (with inline begin and commit)
* using the specified isolation level.
*
* @param mode Transaction isolation level:
* - 'serializableReadWrite' — serializable read/write
* - 'snapshotReadOnly' — snapshot read-only
* - 'onlineReadOnly' — online read-only
* - 'staleReadOnly' — stale read-only
* - 'implicit' — isolation is not set, server decides
* - 'implicit' is the default value
* @param settings Additional options, e.g., allowInconsistentReads — allow inconsistent reads only with 'onlineReadOnly'
* @returns The current instance for chaining
*/
isolation(mode: 'implicit' | 'serializableReadWrite' | 'snapshotReadOnly' | 'onlineReadOnly' | 'staleReadOnly', settings?: {
allowInconsistentReads: boolean;
} | {} | undefined): this;
/** Returns the query execution statistics */
stats(): QueryStats | undefined;
/** Returns a query with statistics enabled */
withStats(mode: Exclude<StatsMode, StatsMode.UNSPECIFIED>): Query<T>;
/** Sets the query timeout */
timeout(timeout: number): Query<T>;
/** Cancels the executing query */
cancel(): Query<T>;
signal(signal: AbortSignal): Query<T>;
/** Executes the query */
execute(): Query<T>;
/** Returns only the values from the query result */
values(): Query<[unknown][]>;
/** Returns raw values */
raw(): Query<T>;
/**
* Disposes the query and releases all resources.
* This method is called automatically when the query is done.
* It is recommended to call this method explicitly when the query is no longer needed.
*/
dispose(): Promise<void>;
[Symbol.dispose](): void;
[Symbol.asyncDispose](): Promise<void>;
}
export {};
//# sourceMappingURL=query.d.ts.map