Class DatabaseSession<ADAPTER>

Type Parameters

Hierarchy

  • DatabaseSession

Constructors

Properties

adapter: ADAPTER
assignedTransaction?: ReturnType<ADAPTER["createTransaction"]>

When this session belongs to a transaction, then this is set. All connection handlers should make sure that when a query/persistence object requests a connection, it should always be the same for a given transaction. (that's how transaction work). The connection between a transaction and connection should be unlinked when the transaction commits/rollbacks.

currentPersistence?: DatabasePersistence = undefined
entityRegistry: DatabaseEntityRegistry = ...
eventDispatcher: EventDispatcherInterface = ...
id: number = ...
identityMap: IdentityMap = ...
inCommit: boolean = false
logger: DatabaseLogger = ...
pluginRegistry: DatabasePluginRegistry = ...
query: ReturnType<ADAPTER["queryFactory"]>["createQuery"]

Creates a new DatabaseQuery instance which can be used to query and manipulate data.

raw: ReturnType<ADAPTER["rawFactory"]>["create"]
rounds: DatabaseSessionRound<ADAPTER>[] = []
stopwatch?: Stopwatch
withIdentityMap: boolean = true
onCommitPre: EventToken<UnitOfWorkCommitEvent<any>> = ...
onDeletePost: EventToken<UnitOfWorkEvent<any>> = ...
onDeletePre: EventToken<UnitOfWorkEvent<any>> = ...
onInsertPost: EventToken<UnitOfWorkEvent<any>> = ...
onInsertPre: EventToken<UnitOfWorkEvent<any>> = ...
onUpdatePost: EventToken<UnitOfWorkUpdateEvent<any>> = ...
onUpdatePre: EventToken<UnitOfWorkUpdateEvent<any>> = ...

Methods

  • Adds a single or multiple items to the to add/update queue. Use session.commit() to persist all queued items to the database.

    This works like Git: you add files, and later commit all in one batch.

    Parameters

    Returns void

  • Commits all open changes (pending inserts, updates, deletions) in optimized batches.

    If a transaction is assigned, this will automatically call a transaction commit and the transaction released. Use flush() if you don't want to end the transaction and keep making changes to the current transaction.

    Returns Promise<void>

  • If a transaction is assigned, a transaction commit is executed.

    This does not commit changes made to your objects in memory. Use commit() for that instead (which executes commitTransaction() as well).

    Returns Promise<void>

  • Commits all open changes (pending inserts, updates, deletions) in optimized batches.

    The transaction (if there is any) is still alive. You can call flush() multiple times in an active transaction. commit() does the same as flush() but also automatically commits and closes the transaction.

    Returns Promise<void>

  • Creates or returns an existing reference.

    If no instance is known in the identity map, it creates a proxy reference (where only primary keys are populated). You can work with this entity instance to assign new references, but reading for not-hydrated values is not possible. Writing not-hydrated is possible and lead to a change in the change-detection. Completely hydrate the object using the hydrateEntity function.

    const user = session.getReference(User, 1);
    

    Type Parameters

    • T

    Parameters

    • classType: ClassType<T> | ReflectionClass<T>
    • primaryKey: any

    Returns T

  • If a transaction is assigned, a transaction rollback is executed and the transaction released.

    This does not rollback changes made to objects in memory.

    Returns Promise<void>

  • Executes an async callback inside of a new transaction. If the callback succeeds (not throwing), the session is automatically committed (and thus its transaction committed and all changes flushed). If the callback throws, the session executes rollback() automatically, and the error is rethrown.

    await session.transaction(async (session) => {
    await session.query(...);
    session.add(...);

    //...
    });

    Type Parameters

    • T

    Parameters

    Returns Promise<T>

  • Marks this session as transactional. On the next query or flush/commit() a transaction on the database adapter is started. Use flush(), commit(), and rollback() to control the transaction behavior. All created query objects from this session are running in this transaction as well.

    The transaction is released when commit()/rollback is executed. When the transaction is released then this session is not marked as transactional anymore. You have to use useTransaction() again if you want to have a new transaction on this session.

    Returns ReturnType<ADAPTER["createTransaction"]>

Generated using TypeDoc