import { TupleValue, TripleRow, PermissionWriteOperations } from '@triplit/db'; import { type Timestamp as HybridClockTimestamp } from './hybrid-clock.js'; import { Tuple } from './codec.js'; import { EntityMetadataStore } from './entity-metadata-store.js'; export type EntityId = string; export type CollectionName = string; export type Timestamp = HybridClockTimestamp; export type Update = { [attribute: string]: TupleValue; }; export type Insert = { [attribute: string]: TupleValue; id: EntityId; }; export type Entity = 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; }; interface EntityDataStore { } export interface EntitySyncStore extends EntityStore { readonly metadataStore: EntityMetadataStore; readonly dataStore: EntityStore; applyChangesWithTimestamp(tx: KVStoreTransaction, buffer: DBChanges, timestamp: Timestamp, options: ApplyChangesOptions): Promise; insertTriples(tx: KVStoreTransaction, triples: Triple[]): Promise; } export interface EntityStore { onChange(callback: (changes: DBChanges) => void): () => void; applyChanges(tx: KVStoreTransaction, buffer: DBChanges, options: ApplyChangesOptions): Promise; getEntity(storage: KVStoreOrTransaction, collection: string, id: string): Promise; getEntitiesInCollection(storage: KVStoreOrTransaction, collection: CollectionName): AsyncIterable<[Tuple, Entity]>; getCollectionStats(storage: KVStoreOrTransaction, knownCollections?: CollectionName[]): Promise>; } export interface WriteBuffer extends EntityDataStore { clear(tx: KVStoreTransaction): 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; } 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; prefix: Tuple; } export type KVStoreOrTransaction = KVStore | KVStoreTransaction; export interface KVStoreAPI { get(key: Tuple, prefix?: Tuple): Promise; set(key: Tuple, value: any, prefix?: Tuple): Promise; delete(key: Tuple, prefix?: Tuple): Promise; scan(options: ScanOptions): AsyncIterable<[Tuple, any]>; clear(): Promise; count(prefix: Tuple): Promise; } export interface ScanOptions { 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 EntityWriteOptions = { skipRules: boolean; }; export type ApplyChangesOptions = { checkWritePermission: WritePermissionCheck | undefined; entityChangeValidator: EntityChangeValidator | undefined; }; export type WritePermissionCheck = (storage: KVStoreOrTransaction, collection: string, entity: any, operation: PermissionWriteOperations) => Promise; export type EntityChangeValidator = (collection: string, change: Change, options: { ignoreRequiredProperties: boolean; }) => void; export {};