import { type SyncTimestamp as HybridClockTimestamp } from '@triplit/types/sync.js'; import { Tuple } from './codec.js'; import { EntityMetadataStore } from './entity-metadata-store.js'; import { PermissionWriteOperations } from './schema/index.js'; import { TripleRow, TupleValue } from './legacy.js'; // TODO: refactor paths and organization export type * from './query/types/index.js'; export type * from './types/index.js'; export type * from './utils/types.js'; export type EntityId = string; export type CollectionName = string; export type Timestamp = HybridClockTimestamp; export type Update = { [attribute: string]: TupleValue }; type Delete = null; export type Insert = { [attribute: string]: TupleValue; id: EntityId }; export type DBEntity = Insert; export type EntityMetadata = { [attribute: string]: Timestamp }; export type Change = Update | Insert; export type Deletes = Set; export type CollectionChanges = { sets: Map; deletes: Deletes; }; export type DBChanges = Record; export type LegacyTriple = TripleRow; export type Triple = { id: EntityId; collection: CollectionName; attribute: string[]; value: TupleValue; timestamp: Timestamp; }; export interface EntitySyncStore extends EntityStore { readonly metadataStore: EntityMetadataStore; readonly dataStore: EntityStore; applyChangesWithTimestamp( tx: KVStoreTransaction, buffer: DBChanges, timestamp: Timestamp, options: ApplyChangesOptions ): Promise; } export interface EntityStore { applyChanges( tx: KVStoreTransaction, buffer: DBChanges, options: ApplyChangesOptions ): Promise; getEntity( storage: KVStoreOrTransaction, collection: string, id: string // TODO: make this null ): Promise; getEntitiesInCollection( storage: KVStoreOrTransaction, collection: CollectionName ): AsyncIterable; getCollectionStats( storage: KVStoreOrTransaction, knownCollections?: CollectionName[] ): Promise>; } export interface WriteBuffer { clear(tx: KVStoreTransaction): Promise; clearChangesForEntity( tx: KVStoreTransaction, collectionName: CollectionName, id: EntityId ): Promise; write(tx: KVStoreTransaction, buffer: DBChanges): Promise; getChanges(storage: KVStoreOrTransaction): Promise; getChangesForCollection( storage: KVStoreOrTransaction, collectionName: CollectionName ): Promise; getChangesForEntity( storage: KVStoreOrTransaction, collectionName: CollectionName, id: EntityId ): Promise<{ update: Change; delete: boolean } | undefined>; isEmpty(storage: KVStoreOrTransaction): Promise; } // Delete this doesn't belong here export interface DoubleBuffer extends WriteBuffer { lockAndSwitchBuffers(): void; getLockedBuffer(): WriteBuffer; getUnlockedBuffer(): WriteBuffer; } export interface KVStore extends KVStoreAPI { scope(prefix: Tuple): KVStore; transact(): KVStoreTransaction; /** * For internal use only */ applyEdits( sets: AsyncIterable<[Tuple, any]> | Iterable<[Tuple, any]>, deletes: AsyncIterable | Iterable ): Promise; } export type KVStoreOrTransaction = KVStore | KVStoreTransaction; export interface KVStoreAPI { // scope(prefix: string): KV; get(key: Tuple, scope?: Tuple): Promise; set(key: Tuple, value: any, scope?: Tuple): Promise; delete(key: Tuple, scope?: Tuple): Promise; scan(options: ScanOptions, scope?: Tuple): AsyncIterable<[Tuple, any]>; scanValues(options: ScanOptions, scope?: Tuple): AsyncIterable; clear(scope?: Tuple): Promise; count(countOptions: CountOptions, scope?: Tuple): Promise; } export interface ScanOptions { prefix: Tuple; } export interface CountOptions { prefix: Tuple; } export type TxStatus = 'open' | 'committed' | 'cancelled'; export interface KVStoreTransaction extends KVStoreAPI { scope(prefix: Tuple): KVStoreTransaction; commit(): Promise; cancel(): void; status: TxStatus; } export type ApplyChangesOptions = { checkWritePermission: WritePermissionCheck | undefined; entityChangeValidator: EntityChangeValidator | undefined; }; export type Delta = { id: string; collection: string; prev: any; next: any; change: any; operation: 'insert' | 'upsert' | 'update' | 'delete'; }; export type WritePermissionCheck = ( storage: KVStoreOrTransaction, delta: Delta, operation: PermissionWriteOperations ) => Promise; export type EntityChangeValidator = ( collection: string, change: Change, options: { ignoreRequiredProperties: boolean } ) => void;