# core/factories/field.factory.ts

> 25 lines of code and 8 definitions.

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

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

## Definitions

- `withInverse` (lexical_declaration, line 3)
- `ref` (lexical_declaration, line 7, exported)
- `optionalRef` (lexical_declaration, line 11, exported)
- `refs` (lexical_declaration, line 15, exported)
- `optionalRefs` (lexical_declaration, line 19, exported)
- `freeRefs` (lexical_declaration, line 23, exported)
- `listedBy` (lexical_declaration, line 27, exported)
- `kept` (lexical_declaration, line 31, exported)

## Contained in

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

## Linked from

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

## Source

```typescript
import type { FieldSpec } from "#types/field.types";

const withInverse = function withInverse(spec: FieldSpec, inverse: string | undefined): FieldSpec {
    return inverse === undefined ? spec : { ...spec, inverse };
};

export const ref = function ref(target: string, inverse?: string): FieldSpec {
    return withInverse({ required: true, target, type: "ref" }, inverse);
};

export const optionalRef = function optionalRef(target: string, inverse?: string): FieldSpec {
    return withInverse({ required: false, target, type: "ref" }, inverse);
};

export const refs = function refs(target: string, inverse?: string): FieldSpec {
    return withInverse({ required: true, target, type: "refs" }, inverse);
};

export const optionalRefs = function optionalRefs(target: string, inverse?: string): FieldSpec {
    return withInverse({ required: false, target, type: "refs" }, inverse);
};

export const freeRefs = function freeRefs(inverse: string): FieldSpec {
    return { inverse, required: false, type: "refs" };
};

export const listedBy = function listedBy(spec: FieldSpec, relation: string): FieldSpec {
    return { ...spec, flip: true, relation };
};

export const kept = function kept(spec: FieldSpec, relation: string): FieldSpec {
    return { ...spec, flip: false, relation };
};
```
