Database decorators for TypeScript applications
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
# static constant DBOperations
Maps out groups of CRUD operations for easier mapping of decorators
Grouped CRUD operations for decorator mapping
# static constant DEFAULT_TIMESTAMP_FORMAT
Standard date format string used for timestamp fields in database models
Default format for timestamp fields
# 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.
# 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.
# static constant DefaultSeparator
The default separator character used when concatenating multiple fields into a single index
Default separator character for composite indexes
# static constant VERSION
Stores the semantic version number of the package
Current version of the reflection package
string
# static readonly exports.BulkCrudOperationKeys
Enum defining bulk CRUD operations for handling multiple records at once
Bulk database operation key constants
string
# static readonly exports.OperationKeys
Enum defining CRUD operations and their lifecycle phases
Database operation key constants
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 |
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
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 |
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
Parameters:
Name | Type | Description |
---|---|---|
model |
Model
|
The model object to search for primary key |
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 |
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) |
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 |
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
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 |
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 |
# 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 |
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 |
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 |
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 |
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
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 |
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
Parameters:
Name | Type | Description |
---|---|---|
operation |
CrudOperations
|
The type of operation (CREATE or UPDATE) |
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 |
Type Definitions
string
# BulkCrudOperations
Union type of the four bulk database operations for handling multiple records at once
Type for bulk CRUD operations
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> |
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
|
# 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">
|
string
# CrudOperations
Union type of the four basic database operations: create, read, update, delete
Type for basic CRUD operations
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> |
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
|