UNPKG

@mikro-orm/core

Version:

TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.

57 lines (56 loc) 4.48 kB
import type { ArrayElement, AutoPath, CleanTypeConfig, ExtractFieldsHint, FromEntityType, ResolveSerializeFields, SerializeDTO, SerializeFieldsKeepPK, TypeConfig, UnboxArray } from '../typings.js'; import { type PopulatePath } from '../enums.js'; /** Converts entity instances to plain DTOs via `serialize()`, with fine-grained control over populate, exclude, fields, and serialization groups. */ export declare class EntitySerializer { /** Serializes an entity to a plain DTO, with fine-grained control over population, exclusion, fields, groups, and custom types. */ static serialize<T extends object, P extends string = never, E extends string = never, F extends string = never>(entity: T, options?: SerializeOptions<T, P, E, F>): SerializeDTO<T, P, E, never, ResolveSerializeFields<F>, SerializeFieldsKeepPK<F>>; private static propertyName; private static processProperty; private static processCustomType; private static extractChildOptions; /** * Extracts the nested `fields` whitelist for a child property. A bare parent name (`fields: ['books']`) or a * wildcard removes the whitelist on the sub-tree (everything is included), so consumers don't have to repeat * every field of the child. Otherwise dot-paths are stripped of the parent prefix and passed down. */ private static extractChildFields; private static processEntity; private static processCollection; } export interface SerializeOptions<T, P extends string = never, E extends string = never, F extends string = never> { /** Specify which relation should be serialized as populated and which as a FK. */ populate?: readonly AutoPath<T, P, `${PopulatePath.ALL}`>[]; /** Specify which properties should be omitted. */ exclude?: readonly AutoPath<T, E>[]; /** * Whitelist of properties to serialize, supports dot-paths (e.g. `['name', 'books.title']`). When set, only the * listed properties end up in the output, including primary keys. A bare property name keeps its entire sub-tree; * a dot-path additionally narrows the nested object to the listed sub-keys. `exclude` takes precedence on conflict. */ fields?: readonly AutoPath<T, F, `${PopulatePath.ALL}`>[]; /** Enforce unpopulated references to be returned as objects, e.g. `{ author: { id: 1 } }` instead of `{ author: 1 }`. */ forceObject?: boolean; /** Ignore custom property serializers. */ ignoreSerializers?: boolean; /** Include properties marked as `hidden`. */ includeHidden?: boolean; /** Skip properties with `null` value. */ skipNull?: boolean; /** Only include properties for a specific group. If a property does not specify any group, it will be included, otherwise only properties with a matching group are included. */ groups?: string[]; /** Convert custom types to their database representation. By default, the `Type.toJSON` method is invoked instead. */ convertCustomTypes?: boolean; } /** * Converts entity instance to POJO, converting the `Collection`s to arrays and unwrapping the `Reference` wrapper, while respecting the serialization options. * This method accepts either a single entity or an array of entities, and returns the corresponding POJO or an array of POJO. * To serialize a single entity, you can also use `wrap(entity).serialize()` which handles a single entity only. * * ```ts * const dtos = serialize([user1, user, ...], { exclude: ['id', 'email'], forceObject: true }); * const [dto2, dto3] = serialize([user2, user3], { exclude: ['id', 'email'], forceObject: true }); * const dto1 = serialize(user, { exclude: ['id', 'email'], forceObject: true }); * const dto2 = wrap(user).serialize({ exclude: ['id', 'email'], forceObject: true }); * ``` */ export declare function serialize<Entity extends object, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Populate extends string = never, Exclude extends string = never, Fields extends string = never, Config extends TypeConfig = never>(entity: Entity, options?: Config & SerializeOptions<UnboxArray<Entity>, Populate, Exclude, Fields>): Naked extends object[] ? SerializeDTO<ArrayElement<Naked>, Populate, Exclude, CleanTypeConfig<Config>, ResolveSerializeFields<Fields, ExtractFieldsHint<Entity>>, SerializeFieldsKeepPK<Fields>>[] : SerializeDTO<Naked, Populate, Exclude, CleanTypeConfig<Config>, ResolveSerializeFields<Fields, ExtractFieldsHint<Entity>>, SerializeFieldsKeepPK<Fields>>;