import type { LoggerAdapterEvent, LoggerAdapterGenericLogMethod, LoggerAdapterLogger, LoggerAdapterLogMethod, LoggerAdapterWriter, NormalizedLoggerAdapter } from "@package/logger-adapter"; import type { ResultLike } from "@package/result"; export type MaybePromise = T | Promise; export type StoreLogMethod = LoggerAdapterLogMethod; export type StoreLogEvent = LoggerAdapterEvent; export type StoreGenericLogMethod = LoggerAdapterGenericLogMethod; export type StoreLogger = LoggerAdapterLogger; export type StoreLoggerAdapter = LoggerAdapterWriter; export type NormalizedStoreLogger = NormalizedLoggerAdapter; export type StoreRecord = { id: string; } & Record; export type StoreContext = Record; export type StoreContextInput = StoreContext | null | undefined; export type StoreWhere = Record; export type StoreMode = "raw" | "full" | (string & {}); export type StorePrivateUnlocks = boolean | string[] | Record; export type StoreSortDirection = "asc" | "desc"; export type StoreSortSpec = `${string}:${StoreSortDirection}`; export type StoreErrorCode = "store-cache-error" | "store-enriched-marker-persisted" | "store-enriched-record" | "store-entity-not-found" | "store-invalid-context" | "store-invalid-id" | "store-invalid-mode" | "store-invalid-record" | "store-invalid-where" | "store-missing-id" | "store-sql-identifier" | "store-sql-placeholder" | "store-storage-error" | "store-sub-entity-not-found"; export type StoreResult = Omit, "data" | "message"> & { data: T; message: string; }; export type StoreErrorDetails = { code: StoreErrorCode; message?: string; entity?: string; field?: string; id?: string; mode?: string; storage?: string; cause?: unknown; }; export interface EntityMetadata { [key: string]: unknown; } export interface EntityModeDefinition { name?: string; enrich?: string; hooks?: EntityModeHookMap; privateFields?: string[]; metadata?: EntityMetadata; select?(record: TRecord): StoreRecord; } export interface EntityDefinition { table: string; storage: string; context?: readonly string[]; aliases?: readonly string[]; modes?: Record>; privateFields?: Record; metadata?: EntityMetadata; } export type EntityRegistry = Record; export interface ResolvedEntity { name: string; definition: EntityDefinition; } export interface StoreReadOptions { mode?: StoreMode; where?: StoreWhere; limit?: number; sort?: readonly StoreSortSpec[]; includePrivate?: StorePrivateUnlocks; scope?: "context" | "all"; cache?: boolean; cacheBypass?: boolean; cacheMeta?: boolean; } export interface StoreWriteOptions { scope?: "context" | "all"; } export interface StoreCacheInspection { enabled: boolean; hit: "l1" | "l2" | "miss" | "deduped"; key: string | null; version: number; } export interface StoreReadMeta { cache?: StoreCacheInspection; } export interface StorageReadOptions { scope?: "context" | "all"; bypassCache?: boolean; where?: StoreWhere; limit?: number; sort?: readonly StoreSortSpec[]; } export interface StorageAdapter { all(entity: ResolvedEntity, context: StoreContext, options?: StorageReadOptions): Promise; by(entity: ResolvedEntity, where: StoreWhere, context: StoreContext, options?: StorageReadOptions): Promise; byIds(entity: ResolvedEntity, ids: string[], context: StoreContext, options?: StorageReadOptions): Promise; count(entity: ResolvedEntity, context: StoreContext, options?: StorageReadOptions): Promise; hasAny(entity: ResolvedEntity, context: StoreContext, options?: StorageReadOptions): Promise; put(entity: ResolvedEntity, context: StoreContext, record: TRecord, options?: StoreWriteOptions): Promise; remove(entity: ResolvedEntity, context: StoreContext, id: string, options?: StoreWriteOptions): Promise; removeMany?(entity: ResolvedEntity, context: StoreContext, ids: string[], options?: StoreWriteOptions): Promise; ensureReadyFor?(entity: ResolvedEntity): Promise; } export interface ModeEnricherContext { entity: string; mode: string; context: StoreContext; } export type ModeEnricher = (record: StoreRecord, context: ModeEnricherContext) => MaybePromise; export type ModeEnricherRegistry = Record; export type EntityModeHookMap = Record | readonly string[]; export interface ModeEnricherHookContext extends ModeEnricherContext { hook: string; } export interface ModeEnricherHookApi { recorded_at: string; readAll(entity: string, context: StoreContext, options?: StoreReadOptions): Promise>; readById(entity: string, id: string, context: StoreContext, options?: StoreReadOptions): Promise>; } export type ModeEnricherHook = (record: StoreRecord, api: ModeEnricherHookApi, context: ModeEnricherHookContext) => MaybePromise; export type ModeEnricherHookLoader = (input: { entity: string; hook: string; mode: string; }) => MaybePromise; export interface ModeEnricherRegistryBuilderOptions { entities: TRegistry; loadHook: ModeEnricherHookLoader; getStore?: () => Store; readAll?(entity: string, context: StoreContext, options?: StoreReadOptions): Promise>; readById?(entity: string, id: string, context: StoreContext, options?: StoreReadOptions): Promise>; now?(): string; } export interface L2CacheAdapter { get(key: string): Promise; set(key: string, value: T): Promise; delete?(key: string): Promise; } export interface StoreCacheOptions { enabled?: boolean; l2?: L2CacheAdapter; ignoredContextKeys?: readonly string[]; } export interface CreateStoreOptions { entities: TRegistry; storage?: StorageAdapter | Record; storages?: Record; enrichers?: ModeEnricherRegistry; cache?: boolean | StoreCacheOptions; subEntities?: SubEntityRegistry; logger?: StoreLogger; loggerAdapter?: StoreLoggerAdapter; } export interface StoreEntityRead { all(entity: string, context: StoreContextInput, options?: StoreReadOptions): Promise>; by(entity: string, where: StoreWhere, context: StoreContextInput, options?: StoreReadOptions): Promise>; count(entity: string, context: StoreContextInput, options?: StoreReadOptions): Promise>; hasAny(entity: string, context: StoreContextInput, options?: StoreReadOptions): Promise>; } export interface StoreEntityWrite { put(entity: string, context: StoreContextInput, record: TRecord, options?: StoreWriteOptions): Promise>; by(entity: string, where: StoreWhere, context: StoreContextInput, patch: TPatch, options?: StoreWriteOptions): Promise>; remove(entity: string, context: StoreContextInput, id: string, options?: StoreWriteOptions): Promise>; removeMany(entity: string, ids: string[], context?: StoreContextInput, options?: StoreWriteOptions): Promise>; } export interface Store { entity: { read: StoreEntityRead; write: StoreEntityWrite; }; cache: StoreCacheController; records(entity: string, views: TViews): RecordViewRegistry; repair: StoreRepairApi; subEntity: StoreSubEntityRead; inspectCache(): StoreCacheState; } export interface StoreCacheController { inspect(): StoreCacheState; invalidateEntity(entity: string): void; } export interface StoreCacheState { enabled: boolean; entityVersions: Record; l1Size: number; trackedKeys: Record; inflight: number; } export interface StoreBulkRemoveResult { requested: number; removed: number; missing: number; ids: string[]; } export type RecordViewDefaults = Partial | ((patch?: Partial) => Partial); export interface RecordViewConfig { kind: string; discriminatorField?: string; defaults?: RecordViewDefaults; normalize?(row: StoreRecord): StoreRecord; sort?: readonly StoreSortSpec[]; uniqueBy?: readonly string[]; } export type RecordViewConfigMap = Record; export interface RecordViewOptions extends StoreReadOptions { context?: StoreContext; } export interface RecordViewWriteOptions extends StoreWriteOptions { context?: StoreContext; } export interface RecordViewListOptions extends RecordViewOptions { limit?: number; sort?: readonly StoreSortSpec[]; } export interface RecordViewUniqueUpsertOptions extends RecordViewWriteOptions { } export interface RecordView { entity: string; config: RecordViewConfig; is(row: StoreRecord): boolean; create(patch?: Partial): TRecord; normalize(row: Partial | TRecord): TRecord; byId(id: string, options?: RecordViewOptions): Promise>; by(where: StoreWhere, options?: RecordViewOptions): Promise>; list(options?: RecordViewListOptions): Promise>; put(row: TRecord, options?: RecordViewWriteOptions): Promise>; patch(where: StoreWhere, patch: Partial, options?: RecordViewWriteOptions): Promise>; remove(id: string, options?: RecordViewWriteOptions): Promise>; upsertUnique(row: TRecord, options?: RecordViewUniqueUpsertOptions): Promise>; } export type RecordViewRegistry = { [K in keyof TViews]: RecordView; }; export interface StoreRepairApi { orphansAndDuplicates(input: StoreRepairOrphansAndDuplicatesInput): Promise; } export interface StoreRepairOrphansAndDuplicatesInput { child: RecordView; parent: RecordView; childParentKey: string; uniqueBy: readonly string[]; keep: "freshest"; freshnessFields: readonly string[]; context?: StoreContext; } export interface StoreRepairSummary { scannedParentCount: number; scannedChildCount: number; deletedOrphanCount: number; deletedDuplicateCount: number; deletedTotal: number; remainingChildCount: number; skipped: boolean; } export interface SubEntityDefinition { parent: string; childKey: string; identityField: string; sourceMode?: StoreMode; validateContext?(context: StoreContext): StoreResult | null | undefined; list?(children: StoreRecord[], context: SubEntityContext): MaybePromise; by?(children: StoreRecord[], where: StoreWhere, context: SubEntityContext): MaybePromise; count?(children: StoreRecord[], context: SubEntityContext): MaybePromise; enrich?(record: StoreRecord, context: SubEntityContext): MaybePromise; } export type SubEntityRegistry = Record; export interface SubEntityContext { name: string; definition: SubEntityDefinition; parent: StoreRecord; context: StoreContext; } export interface StoreSubEntityRead { list(name: string, parentWhere: StoreWhere, context: StoreContext, options?: StoreReadOptions): Promise>; by(name: string, parentWhere: StoreWhere, where: StoreWhere, context: StoreContext, options?: StoreReadOptions): Promise>; count(name: string, parentWhere: StoreWhere, context: StoreContext, options?: StoreReadOptions): Promise>; } export interface PostgresStoreClient { query>(sql: string, params?: unknown[]): Promise<{ rows: T[]; }>; } export interface PostgresJsonbAdapterOptions { client: PostgresStoreClient; schema?: string; } export type StoreRequestContextMeta = Record; export interface StoreRequestContext { entityLoaders: Map; meta: StoreRequestContextMeta; values: Map; } //# sourceMappingURL=types.d.ts.map