import { CollectionFilter, DocumentBaseline, FileData, Migration, ObjectIdentifier, Operation, } from '@verdant-web/common'; import { Context, ContextWithoutPersistence } from '../context/context.js'; export interface AckInfo { globalAckTimestamp: string | null; } export interface LocalReplicaInfo { id: string; userId: string | null; ackedLogicalTime: string | null; lastSyncedLogicalTime: string | null; } export type ClientOperation = Operation & { isLocal: boolean; }; export interface MetadataExport { operations: Operation[]; baselines: DocumentBaseline[]; localReplica?: LocalReplicaInfo; schemaVersion: number; } export interface ExportedData { data: MetadataExport; fileData: Array>; files: File[]; } export type AbstractTransaction = any; export type QueryMode = 'readwrite' | 'readonly'; export interface CommonQueryOptions< Tx extends AbstractTransaction = AbstractTransaction, > { transaction?: Tx; mode?: QueryMode; } export type Iterator = (item: T) => void | boolean; export interface PersistenceMetadataDb< Tx extends AbstractTransaction = AbstractTransaction, > { transaction( opts: { mode?: QueryMode; storeNames: string[]; abort?: AbortSignal; }, procedure: (tx: Tx) => Promise, ): Promise; // infos getAckInfo(): Promise; setGlobalAck(ack: string): Promise; getLocalReplica( opts?: CommonQueryOptions, ): Promise; updateLocalReplica( data: LocalReplicaInfo, opts?: CommonQueryOptions, ): Promise; // baselines iterateDocumentBaselines( rootOid: string, iterator: Iterator, opts?: CommonQueryOptions, ): Promise; iterateCollectionBaselines( collection: string, iterator: Iterator, opts?: CommonQueryOptions, ): Promise; iterateAllBaselines( iterator: Iterator, opts?: CommonQueryOptions, ): Promise; getBaseline( oid: string, opts?: CommonQueryOptions, ): Promise; setBaselines( baselines: DocumentBaseline[], opts?: CommonQueryOptions, ): Promise; deleteBaseline(oid: string, opts?: CommonQueryOptions): Promise; // operations iterateDocumentOperations( rootOid: string, iterator: Iterator, opts?: CommonQueryOptions & { to?: string | null; }, ): Promise; iterateEntityOperations( oid: string, iterator: Iterator, opts?: CommonQueryOptions & { to?: string | null; reverse?: boolean }, ): Promise; iterateCollectionOperations( collection: string, iterator: Iterator, opts?: CommonQueryOptions, ): Promise; iterateLocalOperations( iterator: Iterator, opts?: CommonQueryOptions & { before?: string | null; after?: string | null; }, ): Promise; /** Iterates over operations for an entity for processing and deletes them as it goes. */ deleteEntityOperations( oid: string, opts: CommonQueryOptions & { to: string | null }, ): Promise; iterateAllOperations( iterator: Iterator, opts?: CommonQueryOptions & { before?: string | null; from?: string | null; }, ): Promise; /** * @returns a list of all document (root) OIDs affected by the adds. */ addOperations( ops: ClientOperation[], opts?: CommonQueryOptions, ): Promise; purgeDocumentData( oid: ObjectIdentifier, opts?: CommonQueryOptions, ): Promise; /* WARNING: deletes all data */ reset(opts?: { clearReplica?: boolean; transaction?: Tx }): Promise; stats(): Promise<{ operationsSize: { count: number; size: number }; baselinesSize: { count: number; size: number }; }>; } export interface PersistenceDocumentDb { findOneOid(opts: { collection: string; index?: CollectionFilter; }): Promise; findAllOids(opts: { collection: string; index?: CollectionFilter; limit?: number; offset?: number; }): Promise<{ result: ObjectIdentifier[]; hasNextPage: boolean }>; saveEntities( entities: { oid: ObjectIdentifier; getSnapshot: () => any }[], optsAndInfo: { abort?: AbortSignal; collections: string[] }, ): Promise; reset(): Promise; stats(): Promise>; close(): Promise; } export interface PersistedFileData extends FileData { deletedAt: number | null; } export interface PersistenceFileDb { /** * Should add a file to local persistence. This is idempotent; * the same file being added multiple times should not create duplicates * and should update the existing file entry. */ add(file: FileData, options?: { downloadRemote?: boolean }): Promise; markUploaded(fileId: string): Promise; get(fileId: string): Promise; delete(fileId: string): Promise; markPendingDelete(fileId: string): Promise; listUnsynced(): Promise; resetSyncedStatusSince(since: string | null): Promise; iterateOverPendingDelete( iterator: (file: PersistedFileData) => void, ): Promise; loadFileContents( file: FileData, ctx: Omit, ): Promise; getAll(): Promise; stats(): Promise<{ size: { count: number; size: number } }>; } export interface PersistenceNamespace { openMetadata(ctx: ContextWithoutPersistence): Promise; /** * Open the Documents database according to the schema in the given * context. By the time this is called with a version, relevant migrations * will have been applied. */ openDocuments(ctx: ContextWithoutPersistence): Promise; /** * Apply a migration to the namespace provided in ctx. * This should make any transformations necessary to the * document database to accommodate the new schema indexes. * The migration itself contains a lot of information about * what changed between versions. * * This method should also store the new version to persisted * metadata, however your implementation chooses to do that. */ applyMigration( ctx: ContextWithoutPersistence, migration: Migration, ): Promise; openFiles(ctx: ContextWithoutPersistence): Promise; } export interface PersistenceImplementation { name: string; openNamespace( namespace: string, ctx: Pick, ): Promise; /** Returns a list of all persisted namespaces visible to this app. */ getNamespaces(): Promise; /** Deletes all data from a particular namespace. */ deleteNamespace( namespace: string, ctx: ContextWithoutPersistence, ): Promise; /** Gets the schema version of the given namespace */ getNamespaceVersion(namespace: string): Promise; /** * Copies all data from one namespace to another. It should * overwrite the target namespace such that data and database * schema are identical. */ copyNamespace( from: string, to: string, ctx: ContextWithoutPersistence, ): Promise; }