Interface MongodbDocument<IdType>

interface MongodbDocument {
    $attributes: ModelAttributes<MongodbDocument<IdType>>;
    $dirty: Partial<ModelAttributes<MongodbDocument<IdType>>>;
    $isDeleted: boolean;
    $isDirty: boolean;
    $isLocal: boolean;
    $isNew: boolean;
    $isPersisted: boolean;
    $isTransaction: boolean;
    $original: ModelAttributes<MongodbDocument<IdType>>;
    $trx: undefined | ClientSession;
    _id: IdType;
    createdAt: Date;
    id: IdType;
    updatedAt: Date;
    delete(options?): Promise<boolean>;
    fill<T>(values): MongodbDocument<IdType>;
    merge<T>(values): MongodbDocument<IdType>;
    save(options?): Promise<boolean>;
    toJSON(): unknown;
    useTransaction(client): MongodbDocument<IdType>;
}

Type Parameters

  • IdType

Properties

$attributes: ModelAttributes<MongodbDocument<IdType>>
$dirty: Partial<ModelAttributes<MongodbDocument<IdType>>>

Returns an object with the field values that have been changed.

$isDeleted: boolean

true if the entry has been removed from the database.

$isDirty: boolean

true if the entry has unsaved modifications.

$isLocal: boolean

true if the entry has been created locally. Similar to $isNew, but stays true after the entry is persisted to the database.

$isNew: boolean

Opposite of $isPersisted.

$isPersisted: boolean

true if the entry has been persisted to the database.

$isTransaction: boolean
$original: ModelAttributes<MongodbDocument<IdType>>
$trx: undefined | ClientSession

Return the client session of the transaction

_id: IdType
createdAt: Date
id: IdType
updatedAt: Date

Methods

  • Delete the entry from the database.

    Parameters

    Returns Promise<boolean>

    • whether the entry was deleted.
  • Assign client to model options for transactions use. Will throw an error if model instance already linked to a session

    It allows to use model init outside a transaction, but save it within a transaction.

    Parameters

    • client: ClientSession

    Returns MongodbDocument<IdType>

    Example

    const label = await Label.findOrFail(1);
    // edit some label props

    Database.transaction((client) => {
    const documents = await Document.query({ labels: label._id }, { client }).all()
    // remove label from documents when new label definition is incompatible
    // call .save() for each changed documents (aware of transaction because is from query with client option)

    label.useTransaction(client);
    label.save();
    })