import { DBChanges, Triple, KVStore, EntitySyncStore, Timestamp, PreparedQuery } from './types.js'; import { HybridLogicalClock } from './hybrid-clock.js'; import { ViewEntity } from './query-engine.js'; import { IVM } from './ivm/index.js'; import { DBSession } from './session.js'; import { TypeConverters } from './schema/converters.js'; import { Logger } from '@triplit/logger'; import { CollectionNameFromModels, FailedSchemaChange, Models, Roles, SchemaChange } from './schema/index.js'; import { CollectionQuery, FetchResult, QueryResultCardinality, SchemaQuery } from './query/types/index.js'; import { QueryBuilder } from './query/query-builder.js'; import { ClearOptions, DBOptions, EntityWriteOptions, FetchOptions, OnCommitCallback, ReadModel, SchemaChangeListener, SubscriptionResultsCallback, TransactCallback, TransactOptions, UpdatePayload, WriteModel } from './types/db.js'; export type DBSchema = Models> = { collections: M; roles?: Roles; }; export declare class DB = Models, E extends EntitySyncStore = EntitySyncStore> { entityStore: E; clock: HybridLogicalClock; session: DBSession | undefined; globalVars: Record; logger: Logger; subscribedQueries: Map; lastResult?: any[]; }>; readonly clientId: string; ivm: IVM; schema: DBSchema | undefined; onCommitListeners: Set; readonly kv: KVStore; private typeConverters?; constructor(options?: DBOptions); static getSchemaFromStorage(kv: KVStore): Promise; getSchema(): DBSchema | undefined; subscribe>(query: Q, onResults: SubscriptionResultsCallback, onError?: (error: Error) => void, options?: FetchOptions & { queryState?: { timestamp: Timestamp; entityIds: Record; }; queryKey?: string; }): () => void; subscribeRaw(query: PreparedQuery, onResults: SubscriptionResultsCallback, onError?: (error: Error) => void, options?: { queryState?: { timestamp: Timestamp; entityIds: Record; }; queryKey?: string; }): () => void; /** * @deprecated TODO remove */ subscribeWithChanges>(query: Q, onResults: (args: { results: FetchResult; changes: DBChanges; }) => void, onError?: (error: Error) => void, options?: FetchOptions): () => void; /** * @deprecated TODO remove */ subscribeChanges>(query: Q, onResults: (results: DBChanges, queryId?: string) => void | Promise, options?: FetchOptions & { queryState?: { timestamp: Timestamp; entityIds: Record; }; } & { queryKey?: string; errorCallback?: (error: Error) => void; }): () => void; fetch>(query: Q, options?: FetchOptions): Promise>; rawFetch(query: PreparedQuery, vars?: any): Promise; /** * @deprecated * @param query * @param options * @returns */ fetchChanges(query: CollectionQuery, options?: FetchOptions): Promise; fetchOne>(query: Q, options?: FetchOptions): Promise>; fetchById>(collectionName: CN, id: string, options?: FetchOptions): Promise>; insert>(collectionName: CN, data: WriteModel, options?: EntityWriteOptions): Promise>; update>(collectionName: CN, id: string, data: UpdatePayload, options?: EntityWriteOptions): Promise; delete>(collectionName: CN, id: string, options?: EntityWriteOptions): Promise; onCommit(callback: OnCommitCallback): () => void; updateQueryViews(): Promise; broadcastToQuerySubscribers(): void; transact(callback: TransactCallback, options?: TransactOptions): Promise; applyChanges(changes: DBChanges, options: EntityWriteOptions): Promise; applyChangesWithTimestamp(changes: DBChanges, timestamp: Timestamp, options: EntityWriteOptions): Promise; getCollectionStats(): Promise>; overrideSchema(newSchema: DBSchema, options?: { failOnBackwardsIncompatibleChange?: boolean; }): Promise; private _overrideSchema; setMetadata(key: string[], value: any): Promise; getMetadata(key: string[]): Promise; /** * Runs a transaction on the kv store that updates the schema * This WILL NOT perform any validation or compatibility checks, so ensure the update is valid beforehand */ private updateSchema; /** * WARNING: (I think) using `this` in the constructor will not pick up the proxy information created here * Example: new IVM(this) // db.session will be undefined */ withSessionVars(variables: Record): DB; schemaChangeListeners: SchemaChangeListener[]; onSchemaChange(callback: SchemaChangeListener): () => void; query>(collectionName: CN): QueryBuilder, {}>>; Query>(collectionName: CN): QueryBuilder, {}>>; clear(options?: ClearOptions): Promise; updateGlobalVariables(vars: Record): void; get systemVars(): { $global: Record; $session: Record; }; private validateEntityChange; private checkWritePermission; } export declare function changesToTriples(changes: DBChanges, timestamp: Timestamp): Triple[]; export type DBInitializationEvent = { type: 'ERROR'; error: Error; } | { type: 'SCHEMA_UPDATE_FAILED'; change: FailedSchemaChange; } | { type: 'SUCCESS'; change: SchemaChange | undefined; }; /** * Creates a new database instance and performs and necessary async operations as part of the initialization. * It is guaranteed to return a DB instance, however it may throw an error if a DB instance could not be created. * The event parameter will indicate the result of the initialization process, some notable cases include: * - if the provided schema cannot be applied, an instance with the old schema will be returned */ export declare function createDB = Models, E extends EntitySyncStore = EntitySyncStore>(options: DBOptions & { failOnBackwardsIncompatibleChange?: boolean; }): Promise<{ db: DB; event: DBInitializationEvent; }>; export declare function applyProjectionsAndConversions(results: ViewEntity[] | ViewEntity | null, query: PreparedQuery, cardinality: QueryResultCardinality, typeConverters?: TypeConverters): any[] | any;