Overview
EntitySchemaRegistry provides a centralized mapping between your domain entities and database tables. It handles field name translations, foreign key relationships, and collection configurations - making your mappers cleaner and more maintainable.
Why Use Schema Registry?
Centralized Mapping
Define all entity-to-table mappings in one place
Field Translation
Map camelCase domain fields to snake_case database columns
FK Management
Configure parent-child relationships and foreign keys
Collection Types
Distinguish between owned (1:N) and reference (N:N) relations
Basic Usage
Registering Entities
Bulk Registration
Field Mapping
Map domain property names to database column names:Mapping Entity Data
Transform a complete domain entity to database format:Mapping Partial Fields
Transform only specific fields (useful for updates):Parent-Child Relationships
Configure foreign key relationships for 1:N (owned) relationships:Getting FK Information
Collection Configuration
Configure how collections (1:N and N:N) should be handled:Collection Types
| Type | Relationship | Behavior |
|---|---|---|
owned | 1:N | Children are created/deleted with parent |
reference | N:N | Only links are created/removed, entities persist |
Checking Collection Types
Query Methods
Using with Mappers
The registry is commonly used in persistence mappers:Using with BatchExecutor
The registry integrates withPrismaBatchExecutor for automatic field mapping:
Complete Example
API Reference
EntitySchema Interface
Registry Methods
| Method | Returns | Description |
|---|---|---|
register(schema) | this | Register an entity schema |
registerAll(schemas) | this | Register multiple schemas |
has(entity) | boolean | Check if entity is registered |
getSchema(entity) | EntitySchema | Get schema (throws if not found) |
tryGetSchema(entity) | EntitySchema | null | Get schema or null |
getTable(entity) | string | Get table name |
getFieldsMap(entity) | Record<string, string> | Get field mappings |
mapFieldName(entity, field) | string | Map single field name |
mapFields(entity, data) | object | Map partial fields |
mapEntity(entity, domainEntity) | object | Map complete entity |
getParentEntity(entity) | string | null | Get parent entity name |
getParentFkField(entity) | string | null | Get FK field name |
getParentFk(entity, parentId) | object | null | Get FK object |
getCollectionConfig(entity, field) | CollectionConfig | null | Get collection config |
isReferenceCollection(entity, field) | boolean | Check if N:N relation |
isOwnedCollection(entity, field) | boolean | Check if 1:N relation |
getRegisteredEntities() | string[] | Get all entity names |
getAllSchemas() | EntitySchema[] | Get all schemas |
clear() | void | Remove all registrations |