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, CommandEntity } from '@mbc-cqrs-serverless/core';

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

const internal = deserializeToInternal(external, CommandEntity);

API Reference

serializeToExternal

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

Parameters:

  • item:Internal entity (CommandEntity or DataEntity).
  • options: Optional serialization options
    • keepAttributes: Keep the attributes field in output (default: false)
    • flattenDepth: Maximum depth for flattening nested objects (default: unlimited)

Returns:

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

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).
descriptionDescription
statusStatus (for CQRS processing).
dueDateUsed for DynamoDB TTL

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
descriptiondescriptionEntity description
statusstatusCQRS processing status
dueDatedueDateTTL for DynamoDB expiration
attributes.**Flattened attributes from internal structure