Module

db-decorators

Database decorators for TypeScript applications

View Source index.ts, line 8

Interfaces

BulkCrudOperator

Extends the basic CRUD operations to support bulk operations on multiple models at once

Contextual

Provides context management for database operations

CrudOperator

Exposes a standard Create, Read, Update, Delete API for database operations

IRepository

Interface holding basic CRUD APIs for database models, providing standard operations and metadata

Members

# static constant DBKeys

Collection of keys used for reflection metadata in database operations

Database reflection keys

View Source model/constants.ts, line 3

# static constant DBOperations

Maps out groups of CRUD operations for easier mapping of decorators

Grouped CRUD operations for decorator mapping

View Source operations/constants.ts, line 33

# static constant DEFAULT_TIMESTAMP_FORMAT

Standard date format string used for timestamp fields in database models

Default format for timestamp fields

View Source model/constants.ts, line 32

# static constant DefaultContextFactory

A factory function that creates new Context instances with the provided repository flags. It automatically adds a timestamp to the context and returns a properly typed context instance.

Default factory for creating context instances.

View Source repository/Context.ts, line 4

# static constant DefaultRepositoryFlags

Provides default values for repository operation flags, excluding the timestamp property. These flags control behavior such as context handling, validation, error handling, and more.

Default configuration flags for repository operations.

View Source repository/constants.ts, line 2

# static constant DefaultSeparator

The default separator character used when concatenating multiple fields into a single index

Default separator character for composite indexes

View Source model/constants.ts, line 25

# static constant VERSION

Stores the semantic version number of the package

Current version of the reflection package

View Source index.ts, line 13

string

# static readonly exports.BulkCrudOperationKeys

Enum defining bulk CRUD operations for handling multiple records at once

Bulk database operation key constants

View Source operations/constants.ts, line 26

string

# static readonly exports.OperationKeys

Enum defining CRUD operations and their lifecycle phases

Database operation key constants

View Source operations/constants.ts, line 9

Methods

# static composedFromCreateUpdate(context, data, key, model) → {void}

Callback function used by composed decorators to generate a property value from other properties

Composes a property value from other properties during create or update operations

Parameters:
Name Type Description
context C

The operation context

data V

Metadata for the composition

key

The property key to set the composed value on

model M

The model being processed

View Source model/decorators.ts, line 44

void

# static findModelId(model, returnEmptyopt) → {string|number|bigint}

Searches for the ID-decorated property in the model and returns its value

Retrieves the primary key value from a model

sequenceDiagram participant Caller participant findModelId participant findPrimaryKey Caller->>findModelId: model, returnEmpty findModelId->>findPrimaryKey: model findPrimaryKey-->>findModelId: {id, props} findModelId->>findModelId: extract model[id] findModelId->>findModelId: validate ID exists if required findModelId-->>Caller: ID value
Parameters:
Name Type Attributes Default Description
model Model

The model object to extract the ID from

returnEmpty boolean <optional>
false

Whether to return undefined if no ID value is found

View Source identity/utils.ts, line 48

The primary key value

string | number | bigint

# static findPrimaryKey(model) → {Object}

Searches in all the properties in the object for an id decorated property and returns the property key and metadata

Finds the primary key attribute for a model

sequenceDiagram participant Caller participant findPrimaryKey participant getAllPropertyDecoratorsRecursive Caller->>findPrimaryKey: model findPrimaryKey->>getAllPropertyDecoratorsRecursive: get decorators getAllPropertyDecoratorsRecursive-->>findPrimaryKey: decorators findPrimaryKey->>findPrimaryKey: filter ID decorators findPrimaryKey->>findPrimaryKey: validate single ID property findPrimaryKey-->>Caller: {id, props}
Parameters:
Name Type Description
model Model

The model object to search for primary key

View Source identity/utils.ts, line 6

An object containing the id property name and its metadata

Object

# static hasErrors(previousVersionopt, …exclusions) → {ModelErrorDefinition|undefined}

Validates the current model state and optionally compares with a previous version

Validates the model and checks for errors

Parameters:
Name Type Attributes Description
previousVersion M | any <optional>

Optional previous version of the model for comparison

exclusions Array.<any> <repeatable>

Properties to exclude from validation

View Source model/model.ts, line 4

Error definition if validation fails, undefined otherwise

ModelErrorDefinition | undefined

# static hashOnCreateUpdate(context, data, key, model, oldModelopt) → {void}

Callback function used by the hash decorator to apply hashing to a property value

Hashes a property value during create or update operations

Parameters:
Name Type Attributes Description
context C

The operation context

data V

Metadata for the operation

key

The property key to hash

model M

The model being processed

oldModel M <optional>

The previous model state (for updates)

View Source model/decorators.ts, line 9

void

# static isTransient(model) → {boolean}

Determines whether a model class has been decorated with the transient decorator

Checks if a model is marked as transient

Parameters:
Name Type Description
model M

The model instance to check

View Source model/utils.ts, line 5

True if the model is transient, false otherwise

boolean

# static modelToTransient(model) → {Object}

Extracts properties marked as transient into a separate object

Separates transient properties from a model

sequenceDiagram participant Caller participant modelToTransient participant isTransient participant getAllPropertyDecoratorsRecursive Caller->>modelToTransient: model modelToTransient->>isTransient: check if model is transient isTransient-->>modelToTransient: transient status alt model is not transient modelToTransient-->>Caller: {model} else model is transient modelToTransient->>getAllPropertyDecoratorsRecursive: get transient properties getAllPropertyDecoratorsRecursive-->>modelToTransient: property decorators modelToTransient->>modelToTransient: separate properties modelToTransient->>Model.build: rebuild model without transient props modelToTransient-->>Caller: {model, transient} end
Parameters:
Name Type Description
model M

The model instance to process

Properties:
Name Type Attributes Description
model M

The model with transient properties removed

transient Record.<string, any> <optional>

Object containing the transient properties

View Source model/utils.ts, line 18

Object containing the model without transient properties and a separate transient object

Object

# static prefixMethod(obj, after, prefix, afterNameopt)

Util method to change a method of an object prefixing it with another

Parameters:
Name Type Attributes Description
obj any

The Base Object

after function

The original method

prefix function

The Prefix method. The output will be used as arguments in the original method

afterName string <optional>

When the after function anme cannot be extracted, pass it here

View Source repository/wrappers.ts, line 4

# static serializeAfterAll(context, data, key, model) → {Promise.<void>}

Converts a JSON string property back to its original complex object form after retrieving it from the database.

Handler function that deserializes a property from JSON string after database operations.

Parameters:
Name Type Description
context C

The repository context

data V

The data being processed

key

The property key to deserialize

model M

The model instance being processed

View Source validation/decorators.ts, line 135

A promise that resolves when the property has been deserialized

Promise.<void>

# static serializeOnCreateUpdate(context, data, key, model) → {Promise.<void>}

Converts a complex object property to a JSON string before storing it in the database.

Handler function that serializes a property to JSON string during create and update operations.

Parameters:
Name Type Description
context C

The repository context

data V

The data being processed

key

The property key to serialize

model M

The model instance being processed

View Source validation/decorators.ts, line 108

A promise that resolves when the property has been serialized

Promise.<void>

# static timestampHandler(context, data, key, model) → {Promise.<void>}

Updates a model property with the current timestamp from the repository context.

Handler function that sets a timestamp property to the current timestamp.

Parameters:
Name Type Description
context C

The repository context containing the current timestamp

data V

The data being processed

key

The property key to update

model M

The model instance being updated

View Source validation/decorators.ts, line 27

A promise that resolves when the timestamp has been set

Promise.<void>

# static updateKey(key) → {string}

Builds the key to store as metadata under Reflections for update validation by prefixing the provided key with the update validation prefix.

Generates a key for update validation metadata.

Parameters:
Name Type Description
key string

The base key to be prefixed

View Source validation/validation.ts, line 4

The complete metadata key for update validation

string

# static validateCompare(oldModel, newModel, …exceptions) → {ModelErrorDefinition|undefined}

Compares an old and new model version to validate update operations

Validates changes between two model versions

sequenceDiagram participant Caller participant validateCompare participant Reflection participant Validation Caller->>validateCompare: oldModel, newModel, exceptions validateCompare->>Reflection: get decorated properties Reflection-->>validateCompare: property decorators loop For each decorated property validateCompare->>Validation: get validator Validation-->>validateCompare: validator validateCompare->>validateCompare: validate property update end loop For nested models validateCompare->>validateCompare: validate nested models end validateCompare-->>Caller: validation errors or undefined
Parameters:
Name Type Attributes Description
oldModel M

The original model version

newModel M

The updated model version

exceptions Array.<string> <repeatable>

Properties to exclude from validation

View Source model/validation.ts, line 6

Error definition if validation fails, undefined otherwise

ModelErrorDefinition | undefined

# static versionCreateUpdate(operation) → {function}

Factory function that generates a callback for incrementing version numbers

Creates a function that updates a version property during operations

sequenceDiagram participant Caller participant versionCreateUpdate Caller->>versionCreateUpdate: operation versionCreateUpdate-->>Caller: callback function Note over Caller,versionCreateUpdate: When callback is executed: Caller->>versionCreateUpdate: context, data, key, model alt operation is CREATE versionCreateUpdate->>versionCreateUpdate: set version to 1 else operation is UPDATE versionCreateUpdate->>versionCreateUpdate: increment version else invalid operation versionCreateUpdate->>versionCreateUpdate: throw error end versionCreateUpdate-->>Caller: void
Parameters:
Name Type Description
operation CrudOperations

The type of operation (CREATE or UPDATE)

View Source model/decorators.ts, line 142

A callback function that updates the version property

function

# static wrapMethodWithContext(obj, before, method, after, methodNameopt)

Util method to wrap a method of an object with additional logic

Parameters:
Name Type Attributes Description
obj any

The Base Object

before function

the method to be prefixed

method function

the method to be wrapped

after function

The method to be suffixed

methodName string <optional>

When the after function anme cannot be extracted, pass it here

View Source repository/wrappers.ts, line 56

Type Definitions

string

# BulkCrudOperations

Union type of the four bulk database operations for handling multiple records at once

Type for bulk CRUD operations

View Source operations/constants.ts, line 63

Object

# ComposedFromMetadata

Configuration options for property composition from other properties

Metadata for composed property decorators

Properties:
Name Type Attributes Description
args Array.<string>

Property names to compose from

separator string

Character used to join the composed values

hashResult boolean

Whether to hash the composed result

type "keys" | "values"

Whether to use property keys or values

prefix string <optional>

Optional prefix to add to the composed value

suffix string <optional>

Optional suffix to add to the composed value

args Array
separator string
hashResult boolean
type "keys" | "values"
prefix string <optional>
suffix string <optional>

View Source model/decorators.ts, line 251

Object

# ContextArgs

Represents the context and arguments for repository operations. This type is used to pass context and arguments between repository methods.

Context arguments for repository operations.

Properties:
Name Type Description
context C

The operation context

args Array.<any>

The operation arguments

context C
args Array

View Source repository/utils.ts, line 186

# ContextFactory(arg)

Defines a function type that creates context instances with specific repository flags.

Factory type for creating context instances.

Parameters:
Name Type Description
arg Omit.<F, "timestamp">

View Source repository/Context.ts, line 234

string

# CrudOperations

Union type of the four basic database operations: create, read, update, delete

Type for basic CRUD operations

View Source operations/constants.ts, line 55

Object

# OperationMetadata

Contains information about an operation, its handler, and associated metadata

Metadata for database operations

Properties:
Name Type Attributes Description
operation OperationKeys

The type of operation

handler string

The name of the handler function

metadata V <optional>

Optional metadata associated with the operation

operation OperationKeys
handler string
metadata V <optional>

View Source operations/types.ts, line 9

Object

# RepositoryFlags

Defines the configuration options that control repository behavior during operations. These flags manage context relationships, validation behavior, operation metadata, and error handling.

Configuration flags for repository operations.

Properties:
Name Type Attributes Description
parentContext Context <optional>

The parent context for hierarchical operations

childContexts Array.<Context> <optional>

Child contexts spawned from this context

callArgs Array.<any> <optional>

Arguments passed to the operation

ignoredValidationProperties Array.<string>

Properties to exclude from validation

affectedTables

Tables or models affected by the operation

writeOperation boolean

Whether the operation modifies data

timestamp Date

When the operation was initiated

operation OperationKeys <optional>

The type of operation being performed

breakOnHandlerError boolean

Whether to stop processing on handler errors

rebuildWithTransient boolean

Whether to include transient properties when rebuilding models

parentContext Context.<any> <optional>
childContexts Array <optional>
callArgs Array <optional>
ignoredValidationProperties Array
affectedTables Array | string | Constructor.<ModelExtension>
writeOperation boolean
timestamp Date
operation OperationKeys <optional>
breakOnHandlerError boolean
rebuildWithTransient boolean

View Source repository/types.ts, line 8