![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@gnoesiboe/entity-to-object-transformer
Advanced tools
Transforms entities to native objects and back. Enables you to use domain entities in your application, but store them as plain objects, and re-hydrate them when they come back from the database.
Transforms entities to native objects and back using a mapping. Enables you to use domain entities in your application, but store them as plain objects, and re-hydrate them when they come back from the database.
import EntityToObjectTransformer, { ObjectMapping } from '@gnoesiboe/entity-to-object-transformer'
type AuthorAsObjectType = {
_id: string;
name: string;
createdAt: string;
};
type BlogItemAsObject = {
_id: string;
title: string;
description: string;
createdAt: string;
author: AuthorAsObjectType;
}
const author = new Author(new Uuid(), 'Peter Pan')
const blogItem = new BlogItem(new Uuid(), 'Some title', 'Some description', author);
const mapping: ObjectMapping = {
type: 'object',
constructor: BlogItem,
properties: {
uuid: {
type: 'property',
as: '_id',
transformer: new UuidToStringTransformer(),
},
_title: {
type: 'property',
as: 'title',
},
_description: {
type: 'property',
as: 'description',
},
createdAt: {
type: 'property',
transformer: new DateToStringTransformer(),
},
author: {
type: 'object',
constructor: Author,
properties: {
uuid: {
type: 'property',
as: '_id',
transformer: new UuidToStringTransformer(),
},
_name: {
as: 'name',
type: 'property',
},
createdAt: {
type: 'property',
transformer: new DateToStringTransformer(),
},
},
},
}
}
const transformer = new EntityToObjectTransformer<BlogItem, BlogItemAsObject>(mapping);
const blogItemAsObject = transformer.transform(blogItem);
/*
OUTPUT:
{
_id: 'ae1a0d1a-2f6b-40e5-90b6-5b3d2f00e83a',
title: 'Some title',
description: 'Some description',
createdAt: '2021-10-14T06:28:37.021Z',
author: {
_id: '352cc994-12c0-4e07-b837-0b4e6c31711b',
name: 'Peter Pan',
createdAt: '2021-10-14T06:29:00.841Z',
},
}
*/
const blogItemAsModelAgain = transformer.reverseTransform(blogItemAsObject);
// OUTPUT: Your entity, re-hydrated again
The EntityToObjectTransformer
exposes two methods:
transform
→ transforms an entity into an object by plucking and transforming the instances properties according to the supplied mappingreverseTransform
→ transforms an object into an entity by constructing it, and setting the properties on it, according to the supplied mappingThe mapping implements two types:
export type PropertyMapping = {
type: 'property';
as?: string;
transformer?: PropValueTransformer;
};
export type ObjectMapping = {
type: 'object';
constructor: ClassConstructor;
as?: string;
properties: {
[key: string]: PropertyMapping | ObjectMapping;
};
ignoredProperties?: string[];
};
Starting with an ObjectType
, you can nest them together to form a mapping tree:
Example:
const mapping: ObjectMapping = {
type: 'object',
constructor: BlogItem,
properties: {
uuid: {
type: 'property',
as: '_id',
transformer: new UuidToStringTransformer(),
},
_title: {
type: 'property',
as: 'title',
},
_description: {
type: 'property',
as: 'description',
},
createdAt: {
type: 'property',
transformer: new DateToStringTransformer(),
},
author: {
type: 'object',
constructor: Author,
properties: {
uuid: {
type: 'property',
as: '_id',
transformer: new UuidToStringTransformer(),
},
_name: {
as: 'name',
type: 'property',
},
createdAt: {
type: 'property',
transformer: new DateToStringTransformer(),
},
},
},
}
}
ObjectType
Represents an instance transformed into an object or an array of instances transformed into an array of objects.
key | type | description |
---|---|---|
type | "object" | This should always be "object" and should be provided at all times to make distinction between object -types and property -types |
constructor | ClassConstructor | A class constructor to use for reverse transforming objects into entity instances |
as | string | undefined | When defining a child entity in the mapping, this can be used to specify the name of the key on the parent object that it is transformed into |
properties | Record<string, PropertyMapping | ObjectMapping> | An object containing as key the name of the property on the instance, and as value ObjectType or PropertyType mappings |
ignoredProperties | string[] | By default the transformer will throw an Error when you forget to map properties, to prevent any mistakes. When you want a instance property not to be mapped, add the property key in this array. |
PropertyType
Represents a property transformed into something else (up to ypu), or an array of properties transformed.
key | type | description |
---|---|---|
type | "property" | This should always be "property" and should be provided at all times to make distinction between object -types and property -types |
as | string | undefined | If you don't want the original property name to be outputted in the object, define an alternate name |
transformer | PropValueTransformer | A class instance implementing PropValueTransformer interface that can be called during the transformations to change the property value into anything you want |
When transforming a child collection of mixed instance types, for instance a child collection with instances of Features and Colors, don't define it as an object
-mapping, but as a property
-mapping and write a custom transformer. Like:
export default class ProductAttributeToObjectTransformer
implements PropValueTransformer<Color | Feature, ColorAsObject | FeatureAsObject>
{
reverseTransform(to: ColorAsObject | FeatureAsObject): Color | Feature {
// ...
}
transform(from: Color | Feature): ColorAsObject | FeatureAsObject {
// ...
}
}
Feel free however to re-use the EntityToObjectTransformer
(or any other custom transformer you wrote) inside this transformer, to transform any child objects of some sorts.
As the EntityToObjectTranformer
will, when reverse transforming, instantiate objects without supplying constructor variables, and there might be some runtime validation there, this might result in errors. To prevent this, for now, you need to write a custom transformer for this and supply this in your mapping.
FAQs
Transforms entities to native objects and back. Enables you to use domain entities in your application, but store them as plain objects, and re-hydrate them when they come back from the database.
We found that @gnoesiboe/entity-to-object-transformer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.