Skip to main content

Serialization Helpers

Overview

The MBC CQRS Serverless Framework provides helper functions for converting between internal DynamoDB structures and external flat structures. These helpers ensure consistent data transformation while maintaining type safety.

Data Structure Conversion

Internal DynamoDB Structure

{
pk: "PROJECT",
sk: "123",
name: "Test Project",
attributes: {
details: {
status: "active",
category: "development"
}
}
}

External Flat Structure

{
id: "PROJECT#123", // Combination of pk and sk
code: "123", // Mainly sk
name: "Test Project", // First level in DynamoDB
details: { // Flattened from attributes
status: "active",
category: "development"
}
}

Usage

Converting Internal to External Format

import { serializeToExternal } from '@mbc-cqrs-serverless/core';

const internal = {
pk: "PROJECT",
sk: "123",
name: "Test Project",
attributes: {
details: {
status: "active",
category: "development"
}
}
};

const external = serializeToExternal(internal);

Converting External to Internal Format

import { deserializeToInternal, DataEntity } from '@mbc-cqrs-serverless/core';

const external = {
id: "PROJECT#123",
code: "123",
name: "Test Project",
details: {
status: "active",
category: "development"
}
};

// Use DataEntity for data table entities, CommandEntity for command table entities
const internal = deserializeToInternal(external, DataEntity);
Fallback Behavior

When deserializing, if the id field doesn't contain the # separator to split into pk and sk, the code field is used as the sk value. Any fields not in the metadata field list are automatically placed in the attributes object.

API Reference

serializeToExternal

function serializeToExternal<T extends CommandEntity | DataEntity>(
item: T | null | undefined,
options?: SerializerOptions
): Record<string, any> | null

interface SerializerOptions {
keepAttributes?: boolean; // Reserved for future use
flattenDepth?: number; // Reserved for future use
}

Parameters:

  • item: Internal entity (CommandEntity or DataEntity).
  • options: Optional serialization options (reserved for future use).

Returns:

  • Flattened external structure or null if input is null/undefined
note

The SerializerOptions interface is defined for future extensibility but is not currently used by the function. The function always flattens attributes to the top level.

deserializeToInternal

function deserializeToInternal<T extends CommandEntity | DataEntity>(
data: Record<string, any> | null | undefined,
EntityClass: new () => T
): T | null

Parameters:

  • data: External flat structure
  • EntityClass: Entity class to instantiate (CommandEntity or DataEntity)

Returns:

  • Internal entity instance or null if input is null/undefined

Field Mapping

Metadata Fields

FieldDescription
idPrimary key
cpkCommand table primary key
cskCommand table sort key
pkData table primary key
skData table sort key
tenantCodeTenant code
typeEntity type (embedded in pk, e.g., "PROJECT")
seqSort order
codeCode (may be used as part of sk)
nameName
versionVersion number
isDeletedDeletion flag
createdByCreator's user ID or username
createdIpCreator's IP address
createdAtCreation timestamp
updatedByUpdater's user ID or username (set at creation)
updatedIpUpdater's IP address (set at creation)
updatedAtUpdate timestamp (set at creation).
statusStatus (for CQRS processing).
ttlDynamoDB TTL timestamp (Unix epoch seconds)

Serialization Mapping

Internal FieldExternal FieldDescription
pk + skidCombined primary key for unique identification
cpkcpkCommand table primary key
cskcskCommand table sort key
pkpkData table primary key
skskData table sort key
skcodeSort key used as code identifier
tenantCodetenantCodeTenant identifier
typetypeEntity type (e.g., PROJECT)
seqseqSequence number for ordering
namenameEntity name (first level property)
versionversionEntity version for optimistic locking
isDeletedisDeletedSoft delete flag
createdBycreatedByUser ID or name of creator
createdIpcreatedIpIP address of creator
createdAtcreatedAtCreation timestamp
updatedByupdatedByUser ID or name of last updater
updatedIpupdatedIpIP address of last updater
updatedAtupdatedAtLast update timestamp
statusstatusCQRS processing status
ttlttlDynamoDB TTL timestamp (Unix epoch seconds)
attributes.**Flattened attributes from internal structure