Skip to main content

ModelSerializer

@webda/models


Class: ModelSerializer

Defined in: packages/models/src/model.model.ts:70

Serializer used by repositories to store a model.

ObjectSerializer walks a model with for...in, which only reaches its enumerable fields. Accessors generated by @webda/content-mapper (Date coercion, relation links) are non-enumerable prototype properties whose values live in WEBDA_STORAGE, so a repository silently dropped them. This serializer adds them, and stores a relation link (ModelLink, ModelLinksSimpleArray, ...) as its toJSON() — the primary key(s) — which is what the generated setter accepts back on load and what queries compare against.

Extends​

  • ObjectSerializer

Constructors​

Constructor​

new ModelSerializer(constructorType?, staticProperties?): ModelSerializer

Defined in: packages/serialize/lib/builtin/object.d.ts:57

Create an object serializer.

Parameters​

constructorType?​

any

The constructor to use when deserializing, or null for plain objects

staticProperties?​

Record<string, any>

Predefined serialization logic for specific properties. When a property is defined here, its metadata is not stored in the serialized output, reducing size. Useful for properties that always have the same type (e.g., a createdAt property that's always a Date).

Can be either:
- An object with a `type` field (e.g., `{ type: "Date" }`)
- A function that transforms the raw value (e.g., `(val) => new Date(val)`)

Returns​

ModelSerializer

Example​

// Define a User class with a Date property
class User {
name: string;
createdAt: Date;
}

// Create serializer with static property definition
const userSerializer = new ObjectSerializer(User, {
createdAt: { type: "Date" } // Always deserialize as Date
});

Inherited from​

ObjectSerializer.constructor

Properties​

constructorType​

constructorType: any

Defined in: packages/serialize/lib/builtin/object.d.ts:26

Inherited from​

ObjectSerializer.constructorType


staticProperties​

protected staticProperties: Record<string, object & any | ((value) => any)>

Defined in: packages/serialize/lib/builtin/object.d.ts:27

Inherited from​

ObjectSerializer.staticProperties

Methods​

deserializer()​

deserializer(obj, metadata, context): any

Defined in: packages/serialize/lib/builtin/object.d.ts:92

Deserialize an object, restoring proper types for nested values.

After populating the new instance, this method invokes __hydrateBehaviors(rawData) on the instance when present. Webda model classes that carry Behavior-typed properties get this method emitted at compile time by @webda/content-mapper's behaviours generator; calling it here ensures store-load paths (which route through this serializer) apply the same Behavior coercion as the user-driven model.load(...) path. Instances without the method (plain objects, non-Webda classes) simply skip the call via the optional chain.

Parameters​

obj​

any

The raw object from JSON.parse

metadata​

any

The metadata from the serializer

context​

SerializerContext

The serializer context for handling nested values and references

Returns​

any

The deserialized object instance

Inherited from​

ObjectSerializer.deserializer


serializer()​

serializer(obj, context): object

Defined in: packages/models/src/model.model.ts:77

Serialize the model's fields, including those held in WEBDA_STORAGE

Parameters​

obj​

any

the model to serialize

context​

SerializerContext

the serializer context

Returns​

object

the serialized value and metadata

metadata​

metadata: {[key: string]: any; } | undefined

value​

value: object

Index Signature​

[key: string]: any

Overrides​

ObjectSerializer.serializer