Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding options on refresh() method (Fixing issue #649) #650

Closed
wants to merge 8 commits into from
Closed
2 changes: 1 addition & 1 deletion adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
merge(value: Partial<ModelAttributes<this>>, allowNonExtraProperties?: boolean): this
save(): Promise<this>
delete(): Promise<void>
refresh(options?: ModelAdapterOptions): Promise<this>
refresh(): Promise<this>
load: LucidRowPreload<this>
preload: LucidRowPreload<this>

Expand Down
7 changes: 5 additions & 2 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ export class BaseModel implements LucidRow {
/**
* Reload/Refresh the model instance
*/
public async refresh(options?: ModelAdapterOptions) {
public async refresh() {
this.ensureIsntDeleted()
const modelConstructor = this.constructor as typeof BaseModel
const { table } = modelConstructor
Expand All @@ -1908,7 +1908,10 @@ export class BaseModel implements LucidRow {
* This will occur, when some other part of the application removes
* the row
*/
const freshModelInstance = await modelConstructor.find(this.$primaryKeyValue, options)

const freshModelInstance = await modelConstructor.find(this.$primaryKeyValue, {
client: this.$trx,
})
if (!freshModelInstance) {
urishabh11 marked this conversation as resolved.
Show resolved Hide resolved
throw new Exception(
[
Expand Down
28 changes: 28 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@ test.group('Base model | boot', (group) => {

assert.instanceOf(chained, User)
})
test('ensure refresh works on trx', async (assert) => {
urishabh11 marked this conversation as resolved.
Show resolved Hide resolved
class User extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public username: string

@column()
public age: number
}

const user = await User.firstOrCreate({ username: 'virik' })

const trx = await db.transaction()

user.useTransaction(trx)

user.username = 'virik2'

await user.save()

await user.refresh()

await trx.rollback()

assert.equal(user.username, 'virik2')
})

test('compute table name from model name', async (assert) => {
class User extends BaseModel {
Expand Down