# core/factories/record.factory.ts

> 48 lines of code and 11 definitions.

Tree: GovLab Patterns
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/patterns#file-patterns-core-factories-record-factory-ts
Source text: https://banes-lab.com/source/patterns/core/factories/record.factory.ts.txt

Listed in [core/factories](https://banes-lab.com/api/source/patterns/core/factories.md), after [core/factories/pattern.factory.ts](https://banes-lab.com/source/patterns/core/factories/pattern.factory.ts.md) and before [core/factories/representation.factory.ts](https://banes-lab.com/source/patterns/core/factories/representation.factory.ts.md).

## Definitions

- `drawRecord` (lexical_declaration, line 27)
- `synthesize` (lexical_declaration, line 41, exported)
- `DEFAULT_COUNT` (lexical_declaration, line 9)
- `buildSamplers` (lexical_declaration, line 11)
- `factory` (lexical_declaration, line 17)
- `runtime` (lexical_declaration, line 19)
- `record` (lexical_declaration, line 31)
- `value` (lexical_declaration, line 33)
- `mapping` (lexical_declaration, line 45, exported)
- `samplers` (lexical_declaration, line 46, exported)
- `rng` (lexical_declaration, line 50, exported)

## Contained in

- [core/factories](https://banes-lab.com/anatomy/patterns/folder-patterns-core-factories.md)

## Linked from

- [core/factories](https://banes-lab.com/anatomy/patterns/folder-patterns-core-factories.md)

## Source

```typescript
import type { RepresentationRuntime } from "#types/representation.types";
import type { Rng } from "#types/seed.types";
import type { SynthesizeOptions } from "#types/record.types";
import { createRng } from "#core/factories/seed.factory";
import { detectSchema } from "#core/analyzers/schema.analyzer";
import { inferMapping } from "#core/resolvers/representation.resolver";
import { runtimeFor } from "#core/selectors/representation.selector";

const DEFAULT_COUNT = 10;

const buildSamplers = function buildSamplers(
    mapping: ReadonlyMap<string, readonly string[]>,
    records: readonly unknown[],
): Map<string, RepresentationRuntime> {
    const samplers = new Map<string, RepresentationRuntime>();
    for (const [field, [first]] of mapping) {
        const factory = first === undefined ? undefined : runtimeFor(first);
        if (factory) {
            const runtime = factory(field);
            runtime.update(records);
            samplers.set(field, runtime);
        }
    }
    return samplers;
};

const drawRecord = function drawRecord(
    samplers: ReadonlyMap<string, RepresentationRuntime>,
    rng: Rng,
): Record<string, unknown> {
    const record: Record<string, unknown> = {};
    for (const [field, runtime] of samplers) {
        const value = runtime.sample(rng);
        if (value !== null) {
            record[field] = value;
        }
    }
    return record;
};

export const synthesize = function synthesize(
    records: readonly unknown[],
    options: SynthesizeOptions = {},
): Record<string, unknown>[] {
    const mapping = options.mapping ?? inferMapping(detectSchema(records, options.floatFields));
    const samplers = buildSamplers(mapping, records);
    if (samplers.size === 0) {
        return [];
    }
    const rng = createRng(options.seed ?? 0);
    return Array.from({ length: options.count ?? DEFAULT_COUNT }, () => drawRecord(samplers, rng));
};
```
