UNPKG

datum-focus

Version:

Data shape, model, metadata, JSON, JSON Schema, GraphQL, MongoDB query and aggregations, iterator generators

49 lines (42 loc) 1.18 kB
import { Model } from '../model'; import { KnownRecord } from '../record'; export class GenerationContext<T extends {} = KnownRecord> { private readonly generators: Record<string, () => number | string> = {}; private readonly indexes: Record< string, Record<number | string, number> > = {}; constructor( public readonly collection: T[], public readonly target: Model<T>, public readonly sources: Record<string, Model> ) { this.claim = this.claim.bind(this); this.find = this.find.bind(this); } public claim(key: string, val: number | string, index: number): boolean { let map = this.indexes[key]; if (!map) { this.indexes[key] = map = {}; } else if (map[val]) { return false; } map[val] = index; return true; } public find(key: string, val: number | string): number | undefined { const map = this.indexes[key]; return map && map[val]; } public init() { Object.entries(this.target.fields).reduce((acc, [key, val]) => { return acc; }, {}); } public make(): T { throw ""; } public push(item: T): number { return this.collection.push(item) - 1; } }