Class defaultAbstract

Serves as an abstract base class for entities in the ORM system. It defines standard fields such as id, type, createdAt, and updatedAt, and provides static methods for CRUD operations and queries. This class encapsulates common behaviors and properties that all entities share, leveraging decorators for attribute metadata and supporting operations like finding, creating, updating, and deleting entities.

Table classes should extend this class, and each entity should extend the table class

Entities extending DynaRecord can utilize these operations to interact with their corresponding records in the database, including handling relationships between different entities.

Example

@Table({ name: "my-table", delimiter: "#" })
abstract class MyTable extends DynaRecord {
@PartitionKeyAttribute()
public readonly pk: PartitionKey;

@SortKeyAttribute()
public readonly sk: SortKey;
}

@Entity
class User extends MyTable {
// User implementation
}

Hierarchy (view full)

Implements

Constructors

Properties

createdAt: Date

The timestamp marking when the entity was created

id: string

A unique identifier for the entity itself, automatically generated upon creation.

type: string

The type of the Entity

updatedAt: Date

The timestamp marking the last update to the entity. Initially set to the same value as createdAt.

Methods

  • Create an entity. If foreign keys are included in the attributes then BelongsToLinks will be demoralized accordingly

    Type Parameters

    Parameters

    Returns Promise<T>

    The new Entity

    const newUser = await User.create({ name: "Alice", email: "alice@example.com", profileId: "123" });
    
  • Delete an entity by ID

    • Delete all BelongsToLinks
    • Disassociate all foreign keys of linked models

    Type Parameters

    Parameters

    • this: EntityClass<T>
    • id: string

      The id of the entity to update

    Returns Promise<void>

    Example: Delete an entity

    await User.delete("userId");
    
  • Find an entity by Id and optionally include associations

    Type Parameters

    Parameters

    Returns Promise<Optional<T | FindByIdIncludesRes<T, Opts>>>

    An entity with included associations serialized

    Example: Without included relationships

    const user = await User.findById("userId");
    

    Example: With included relationships

    const user = await User.findById("userId", { include: [{ association: "profile" }] });
    
  • Constructs the partition key value

    Parameters

    • id: string

      Entity Id

    Returns string

    Constructed partition key value

    Example

    const pkValue = User.partitionKeyValue("userId");
    
  • Query by PartitionKey and optional SortKey/Filter/Index conditions

    Type Parameters

    Parameters

    Returns Promise<QueryResults<T>>

    Example: By partition key only

    const user = await User.query({ pk: "User#123" });
    

    Example: By partition key and sort key exact match

    const user = await User.query({ pk: "User#123", sk: "Profile#123" });
    

    Example: By partition key and sort key begins with

    const user = await User.query({ pk: "User#123", sk: { $beginsWith: "Profile" } });
    

    Example: With filter (arbitrary example)

    const result = await User.query(
    {
    myPk: "User|123"
    },
    {
    filter: {
    type: ["BelongsToLink", "Profile"],
    createdAt: { $beginsWith: "202" },
    $or: [
    {
    foreignKey: "111",
    updatedAt: { $beginsWith: "2023-02-15" }
    },
    {
    foreignKey: ["222", "333"],
    createdAt: { $beginsWith: "2021-09-15T" },
    foreignEntityType: "Order"
    },
    {
    id: "123"
    }
    ]
    }
    }
    );

    Example: On index

     const result = await User.query(
    {
    pk: "User#123",
    sk: { $beginsWith: "Profile" }
    },
    { indexName: "myIndex" }
    );
  • Query an EntityPartition by EntityId and optional SortKey/Filter conditions. QueryByIndex not supported. Use Query with keys if indexName is needed

    Type Parameters

    Parameters

    • this: EntityClass<T>
    • id: string

      Entity Id

    • Optional options: Omit<QueryOptions, "indexName">

      QueryOptions. Supports filter and skCondition. indexName is not supported

    Returns Promise<QueryResults<T>>

    Example: By partition key only

    const user = await User.query("123");
    

    Example: By partition key and sort key exact match

    const user = await User.query("123", { skCondition: "Profile#111" });
    

    Example: By partition key and sort key begins with

    const user = await User.query("123", { skCondition: { $beginsWith: "Profile" } })
    

    Example: With filter (arbitrary example)

    const user = await User.query("123", {
    filter: {
    type: "BelongsToLink",
    createdAt: "2023-11-21T12:31:21.148Z"
    }
    });
  • Update an entity. If foreign keys are included in the attribute then:

    • BelongsToLinks will be created accordingly
    • If the entity already had a foreign key relationship, then those BelongsToLinks will be deleted
    • Validation errors will be thrown if the attribute being removed is not nullable

    Type Parameters

    Parameters

    Returns Promise<void>

    Example: Updating an entity.

    await User.update("userId", { email: "newemail@example.com", profileId: 789 });
    

    Example: Removing a nullable entities attributes

    await User.update("userId", { email: "newemail@example.com", someKey: null });
    

Generated using TypeDoc