UNPKG

@obsidize/rx-map

Version:

ES6 Map with rxjs extensions for change detection

51 lines (50 loc) 1.98 kB
import { Observable } from 'rxjs'; import { EntityPropertyChangeEvent } from '../events/entity-property-change-event'; import { PropertySelector } from '../common/utility'; import { RxEntityMap } from '../maps/rx-entity-map'; import { OneToManyContext } from './one-to-many-context'; /** * Cache map for tracking model foreign key relationships so that we have rapid lookup speed for associated models. * * @example * * ```typescript * // Has many orders * interface Product { * id: number; // is the primary key * } * * // Has exactly one product * interface ProductOrder { * id: number; * productId: number; // is the foreign key * } * ``` */ export declare class OneToManyRelationship<K, V, T> { protected readonly entityMap: RxEntityMap<K, V>; protected readonly selectForeignKey: PropertySelector<T, V>; protected readonly store: Map<T, OneToManyContext<T, K>>; /** * Changes for foriegn entity relations usually happen on the foreign entity itself, rather than the primary entity. * * For example: * ProductOrder has 'id' and 'productId' - a Product can have many orders associated with it, but the value * being changed is 'productId' on ProductOrder, not 'id' on Product. */ readonly changes: Observable<EntityPropertyChangeEvent<K, T>>; constructor(entityMap: RxEntityMap<K, V>, selectForeignKey: PropertySelector<T, V>); getPrimaryKeys(): T[]; clear(): void; associate(id: T, fk: K): void; disassociate(id: T, fk: K): void; hasAssociation(id: T, fk: K): boolean; getRelatedKeys(id: T): K[]; getRelatedKeyCount(id: T): number; hasAnyAssociation(id: T): boolean; getRelatedValues(id: T): V[]; consume(ev: EntityPropertyChangeEvent<K, T>): void; watchPrimaryKey(id: T): Observable<V[]>; deletePrimaryKeyContext(id: T): boolean; getPrimaryKeyContext(id: T): OneToManyContext<T, K> | undefined; }