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.

@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
}

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

  • Same as the static update method but on an instance. Returns the full updated instance

    Type Parameters

    Parameters

    Returns Promise<T>

    const updatedInstance = await instance.update({ email: "newemail@example.com", profileId: 789 });
    
    const updatedInstance = await instance.update({ email: "newemail@example.com", someKey: null });
    
  • Delete an entity by ID

    • Delete all denormalized records
    • 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>

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

    Type Parameters

    Parameters

    • this: EntityClass<T>
    • id: string

      Entity Id.

    • Optionaloptions: undefined

      No options provided, returns the entity without included associations.

    Returns Promise<Optional<EntityAttributesInstance<T>>>

    An entity without included associations serialized.

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

    const user = await User.findById("userId", { include: [{ association: "profile" }] });
    
  • Find an entity by Id and optionally include associations.

    Type Parameters

    Parameters

    • this: EntityClass<T>
    • id: string

      Entity Id.

    • Optionaloptions: Opts

      No options provided, returns the entity without included associations.

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

    An entity without included associations serialized.

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

    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

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

    Type Parameters

    Parameters

    Returns Promise<QueryResults<T>>

    const user = await User.query({ pk: "User#123" });
    
    const user = await User.query({ pk: "User#123", sk: "Profile#123" });
    
    const user = await User.query({ pk: "User#123", sk: { $beginsWith: "Profile" } });
    
    const result = await User.query(
    {
    myPk: "User|123"
    },
    {
    filter: {
    type: ["Profile", "Preferences"],
    createdAt: { $beginsWith: "2023" },
    $or: [
    {
    name: "John",
    email: { $beginsWith: "testing }
    },
    {
    name: "Jane",
    updatedAt: { $beginsWith: "2024" },
    },
    {
    id: "123"
    }
    ]
    }
    }
    );
     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

    • Optionaloptions: Omit<QueryOptions, "indexName">

      QueryOptions. Supports filter and skCondition. indexName is not supported

    Returns Promise<QueryResults<T>>

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

    • Manages associated relationship links as needed
    • If the entity already had a foreign key relationship, then denormalized records will be deleted from each partition
    • Validation errors will be thrown if the attribute being removed is not nullable

    Type Parameters

    Parameters

    Returns Promise<void>

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