import type { LogContext } from '@rocicorp/logger'; import type { JSONValue, ReadonlyJSONValue } from '../../shared/src/json.ts'; import { type IndexKey } from './db/index.ts'; import type { Read } from './db/read.ts'; import type { Write } from './db/write.ts'; import type { IndexDefinition } from './index-defs.ts'; import type { ScanResult } from './scan-iterator.ts'; import { type ScanIndexOptions, type ScanNoIndexOptions, type ScanOptions } from './scan-options.ts'; import type { ScanSubscriptionInfo } from './subscriptions.ts'; import type { ClientID } from './sync/ids.ts'; import type { ZeroTxData } from './replicache-options.ts'; export type TransactionEnvironment = 'client' | 'server'; export type TransactionLocation = TransactionEnvironment; export type TransactionReason = 'initial' | 'rebase' | 'authoritative'; /** * Basic deep readonly type. It works for {@link JSONValue}. */ export type DeepReadonly = T extends null | boolean | string | number | undefined ? T : DeepReadonlyObject; export type DeepReadonlyObject = { readonly [K in keyof T]: DeepReadonly; }; /** * ReadTransactions are used with {@link Replicache.query} and * {@link Replicache.subscribe} and allows read operations on the * database. */ export interface ReadTransaction { readonly clientID: ClientID; /** @deprecated Use {@link ReadTransaction.location} instead. */ readonly environment: TransactionLocation; readonly location: TransactionLocation; /** * Get a single value from the database. If the `key` is not present this * returns `undefined`. * * Important: The returned JSON is readonly and should not be modified. This * is only enforced statically by TypeScript and there are no runtime checks * for performance reasons. If you mutate the return value you will get * undefined behavior. */ get(key: string): Promise; get(key: string): Promise | undefined>; /** Determines if a single `key` is present in the database. */ has(key: string): Promise; /** Whether the database is empty. */ isEmpty(): Promise; /** * Gets many values from the database. This returns a {@link ScanResult} which * implements `AsyncIterable`. It also has methods to iterate over the * {@link ScanResult.keys | keys} and {@link ScanResult.entries | entries}. * * If `options` has an `indexName`, then this does a scan over an index with * that name. A scan over an index uses a tuple for the key consisting of * `[secondary: string, primary: string]`. * * If the {@link ScanResult} is used after the `ReadTransaction` has been closed * it will throw a {@link TransactionClosedError}. * * Important: The returned JSON is readonly and should not be modified. This * is only enforced statically by TypeScript and there are no runtime checks * for performance reasons. If you mutate the return value you will get * undefined behavior. */ scan(options: ScanIndexOptions): ScanResult; scan(options?: ScanNoIndexOptions): ScanResult; scan(options?: ScanOptions): ScanResult; scan(options: ScanIndexOptions): ScanResult>; scan(options?: ScanNoIndexOptions): ScanResult>; scan(options?: ScanOptions): ScanResult>; } export declare class ReadTransactionImpl implements ReadTransaction { readonly clientID: ClientID; readonly dbtx: Read; protected readonly _lc: LogContext; /** * The location in which this transaction is being used. This is either `client` or `server`. */ readonly location: TransactionLocation; /** @deprecated Use {@link ReadTransaction.location} instead. */ readonly environment: TransactionLocation; constructor(clientID: ClientID, dbRead: Read, lc: LogContext, rpcName?: string); get(key: string): Promise; has(key: string): Promise; isEmpty(): Promise; scan(options: ScanIndexOptions): ScanResult; scan(options?: ScanNoIndexOptions): ScanResult; scan(options?: ScanOptions): ScanResult; scan(options: ScanIndexOptions): ScanResult>; scan(options?: ScanNoIndexOptions): ScanResult>; scan(options?: ScanOptions): ScanResult>; } export declare class SubscriptionTransactionWrapper implements ReadTransaction { #private; constructor(tx: ReadTransactionImpl); get environment(): TransactionLocation; get location(): TransactionLocation; get clientID(): string; isEmpty(): Promise; get(key: string): Promise; has(key: string): Promise; scan(options: ScanIndexOptions): ScanResult; scan(options?: ScanNoIndexOptions): ScanResult; scan(options?: ScanOptions): ScanResult; scan(options: ScanIndexOptions): ScanResult>; scan(options?: ScanNoIndexOptions): ScanResult>; scan(options?: ScanOptions): ScanResult>; get keys(): ReadonlySet; get scans(): ScanSubscriptionInfo[]; } /** * WriteTransactions are used with *mutators* which are registered using * {@link ReplicacheOptions.mutators} and allows read and write operations on the * database. */ export interface WriteTransaction extends ReadTransaction { /** * The ID of the mutation that is being applied. */ readonly mutationID: number; /** * The reason for the transaction. This can be `initial`, `rebase` or `authoriative`. */ readonly reason: TransactionReason; /** * Sets a single `value` in the database. The value will be frozen (using * `Object.freeze`) in debug mode. */ set(key: string, value: ReadonlyJSONValue): Promise; /** * @deprecated Use {@link WriteTransaction.set} instead. */ put(key: string, value: ReadonlyJSONValue): Promise; /** * Removes a `key` and its value from the database. Returns `true` if there was a * `key` to remove. */ del(key: string): Promise; } export declare const zeroData: unique symbol; export declare class WriteTransactionImpl extends ReadTransactionImpl implements WriteTransaction { readonly dbtx: Write; readonly reason: TransactionReason; readonly mutationID: number; readonly [zeroData]: ZeroTxData | undefined; constructor(clientID: ClientID, mutationID: number, reason: TransactionReason, zData: ZeroTxData | undefined, dbWrite: Write, lc: LogContext, rpcName?: string); put(key: string, value: ReadonlyJSONValue): Promise; set(key: string, value: ReadonlyJSONValue): Promise; del(key: string): Promise; } export type CreateIndexDefinition = IndexDefinition & { name: string; }; type Entry = readonly [key: Key, value: Value]; type IndexKeyEntry = Entry; type StringKeyEntry = Entry; export type EntryForOptions = Options extends ScanIndexOptions ? IndexKeyEntry : StringKeyEntry; export declare function fromKeyForNonIndexScan(options: ScanNoIndexOptions | undefined): string; export {}; //# sourceMappingURL=transactions.d.ts.map