Skip to content

Commit c197086

Browse files
committed
feat(model): add way to set transaction for models
1 parent 7777b44 commit c197086

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/database",
3-
"version": "5.30.0",
3+
"version": "5.31.0",
44
"description": "The Athenna database handler for SQL/NoSQL.",
55
"license": "MIT",
66
"author": "João Lenon <[email protected]>",

src/models/builders/ModelQueryBuilder.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ import type { Driver } from '#src/database/drivers/Driver'
2626
import { QueryBuilder } from '#src/database/builders/QueryBuilder'
2727
import type { ModelSchema } from '#src/models/schemas/ModelSchema'
2828
import { ModelGenerator } from '#src/models/factories/ModelGenerator'
29+
import type { Transaction } from '#src/database/transactions/Transaction'
2930
import { UniqueValueException } from '#src/exceptions/UniqueValueException'
3031
import { HasOneRelation } from '#src/models/relations/HasOne/HasOneRelation'
3132
import { NotFoundDataException } from '#src/exceptions/NotFoundDataException'
3233
import { HasManyRelation } from '#src/models/relations/HasMany/HasManyRelation'
3334
import { NullableValueException } from '#src/exceptions/NullableValueException'
3435
import { BelongsToRelation } from '#src/models/relations/BelongsTo/BelongsToRelation'
35-
import { BelongsToManyRelation } from '../relations/BelongsToMany/BelongsToManyRelation.js'
36+
import { BelongsToManyRelation } from '#src/models/relations/BelongsToMany/BelongsToManyRelation'
3637

3738
export class ModelQueryBuilder<
3839
M extends BaseModel = any,
@@ -73,6 +74,22 @@ export class ModelQueryBuilder<
7374
this.setPrimaryKey(this.primaryKeyName)
7475
}
7576

77+
/**
78+
* Define a transaction to be used by the model query builder.
79+
*/
80+
public setTransaction(trx: Transaction) {
81+
return this.setDriver(trx.driver, this.Model.table())
82+
}
83+
84+
/**
85+
* Set a different driver to the model query builder.
86+
*/
87+
public setDriver(driver: Driver, tableName?: string) {
88+
super.setDriver(driver, tableName)
89+
90+
return this
91+
}
92+
7693
/**
7794
* Calculate the average of a given column.
7895
*/

0 commit comments

Comments
 (0)