From d69d265b07357bf178db13eefb7710ac5dcb86dd Mon Sep 17 00:00:00 2001 From: Rishabh Goyal Date: Thu, 11 Feb 2021 12:54:34 +0530 Subject: [PATCH 1/7] adding options on refresh() method --- adonis-typings/model.ts | 2 +- src/Orm/BaseModel/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adonis-typings/model.ts b/adonis-typings/model.ts index 821e1eed..1235c9b8 100644 --- a/adonis-typings/model.ts +++ b/adonis-typings/model.ts @@ -542,7 +542,7 @@ declare module '@ioc:Adonis/Lucid/Model' { merge(value: Partial>, allowNonExtraProperties?: boolean): this save(): Promise delete(): Promise - refresh(): Promise + refresh(options?: ModelAdapterOptions): Promise load: LucidRowPreload preload: LucidRowPreload diff --git a/src/Orm/BaseModel/index.ts b/src/Orm/BaseModel/index.ts index ebcf17ba..09fac610 100644 --- a/src/Orm/BaseModel/index.ts +++ b/src/Orm/BaseModel/index.ts @@ -1888,7 +1888,7 @@ export class BaseModel implements LucidRow { /** * Reload/Refresh the model instance */ - public async refresh() { + public async refresh(options?: ModelAdapterOptions) { this.ensureIsntDeleted() const modelConstructor = this.constructor as typeof BaseModel const { table } = modelConstructor @@ -1908,7 +1908,7 @@ 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) + const freshModelInstance = await modelConstructor.find(this.$primaryKeyValue, options) if (!freshModelInstance) { throw new Exception( [ From 525c89ef714decbd937d292513adab4d600373e2 Mon Sep 17 00:00:00 2001 From: Rishabh Goyal Date: Thu, 11 Feb 2021 22:22:19 +0530 Subject: [PATCH 2/7] es lint no shadow --- .eslintrc.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index b6199527..24914154 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -10,6 +10,8 @@ "rules": { "prettier/prettier": [ "error" - ] + ], + "no-shadow": "off", + "@typescript-eslint/no-shadow": ["error"] } } From ca2858c0e4675fb208a686d01778bc7a0101b6ae Mon Sep 17 00:00:00 2001 From: Rishabh Goyal Date: Sat, 13 Feb 2021 18:03:16 +0530 Subject: [PATCH 3/7] Refresh method using $trx and test on trx --- adonis-typings/model.ts | 2 +- src/Orm/BaseModel/index.ts | 7 +++++-- test/orm/base-model.spec.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/adonis-typings/model.ts b/adonis-typings/model.ts index 1235c9b8..821e1eed 100644 --- a/adonis-typings/model.ts +++ b/adonis-typings/model.ts @@ -542,7 +542,7 @@ declare module '@ioc:Adonis/Lucid/Model' { merge(value: Partial>, allowNonExtraProperties?: boolean): this save(): Promise delete(): Promise - refresh(options?: ModelAdapterOptions): Promise + refresh(): Promise load: LucidRowPreload preload: LucidRowPreload diff --git a/src/Orm/BaseModel/index.ts b/src/Orm/BaseModel/index.ts index 09fac610..2d7839a0 100644 --- a/src/Orm/BaseModel/index.ts +++ b/src/Orm/BaseModel/index.ts @@ -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 @@ -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) { throw new Exception( [ diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index 99ed1e94..f64e3121 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -142,6 +142,34 @@ test.group('Base model | boot', (group) => { assert.instanceOf(chained, User) }) + test('ensure refresh works on trx', async (assert) => { + 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 { From 19221befa33d460c57b9441452d1e76c5200c8f7 Mon Sep 17 00:00:00 2001 From: Rishabh Goyal Date: Sat, 13 Feb 2021 19:12:07 +0530 Subject: [PATCH 4/7] Revert "es lint no shadow" This reverts commit 525c89ef714decbd937d292513adab4d600373e2. --- .eslintrc.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 24914154..b6199527 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -10,8 +10,6 @@ "rules": { "prettier/prettier": [ "error" - ], - "no-shadow": "off", - "@typescript-eslint/no-shadow": ["error"] + ] } } From 4583a8f885e36bbd5a757646067ad2e563812e9e Mon Sep 17 00:00:00 2001 From: urishabh11 <65100548+urishabh11@users.noreply.github.com> Date: Sat, 13 Feb 2021 22:37:12 +0530 Subject: [PATCH 5/7] refactor: refresh method Co-authored-by: Romain Lanz <2793951+RomainLanz@users.noreply.github.com> --- src/Orm/BaseModel/index.ts | 1 + test/orm/base-model.spec.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Orm/BaseModel/index.ts b/src/Orm/BaseModel/index.ts index 2d7839a0..d30302de 100644 --- a/src/Orm/BaseModel/index.ts +++ b/src/Orm/BaseModel/index.ts @@ -1912,6 +1912,7 @@ export class BaseModel implements LucidRow { const freshModelInstance = await modelConstructor.find(this.$primaryKeyValue, { client: this.$trx, }) + if (!freshModelInstance) { throw new Exception( [ diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index f64e3121..8abc78fb 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -142,6 +142,7 @@ test.group('Base model | boot', (group) => { assert.instanceOf(chained, User) }) + test('ensure refresh works on trx', async (assert) => { class User extends BaseModel { @column({ isPrimary: true }) From fbb10beebe08e709a244d50de16435845f976696 Mon Sep 17 00:00:00 2001 From: Rishabh Goyal Date: Sat, 13 Feb 2021 23:01:53 +0530 Subject: [PATCH 6/7] Fixing no shallow(es lint errors) on example and FactoryModel.ts --- example/index.ts | 7 ++++--- src/Factory/FactoryModel.ts | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/example/index.ts b/example/index.ts index b4b4356c..636d3bda 100644 --- a/example/index.ts +++ b/example/index.ts @@ -2,8 +2,9 @@ import { DateTime } from 'luxon' import { BaseModel, HasOne, hasOne, scope, column } from '@ioc:Adonis/Lucid/Orm' import Factory from '@ioc:Adonis/Lucid/Factory' -enum ProfileTypes { - TWITTER = 'TWITTER', +const ProfileTypes = { + TWITTER: 'TWITTER' as 'TWITTER', + FACEBOOK: 'FACEBOOK' as 'FACEBOOK', } class Profile extends BaseModel { @@ -11,7 +12,7 @@ class Profile extends BaseModel { public userId: string public user: HasOne - public type: ProfileTypes + public type: keyof typeof ProfileTypes @column.dateTime() public createdAt?: DateTime diff --git a/src/Factory/FactoryModel.ts b/src/Factory/FactoryModel.ts index dddb6e0b..f93e9190 100644 --- a/src/Factory/FactoryModel.ts +++ b/src/Factory/FactoryModel.ts @@ -42,16 +42,16 @@ export class FactoryModel implements FactoryModelContr public newUpModelInstance: NewUpCallback> = function ( attributes: any ) { - const Model = this.model + const model = this.model /** * Handling case, where someone returns model instance directly */ - if (attributes instanceof Model) { + if (attributes instanceof model) { return attributes } - const modelInstance = new Model() + const modelInstance = new model() modelInstance.merge(attributes) return modelInstance }.bind(this) From 1ee6784db940a75cc423bceabdfef1ba8d2fb77d Mon Sep 17 00:00:00 2001 From: Rishabh Goyal Date: Sat, 13 Feb 2021 23:01:53 +0530 Subject: [PATCH 7/7] Fixing es lint errors on example and FactoryModel.ts --- example/index.ts | 7 ++++--- src/Factory/FactoryModel.ts | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/example/index.ts b/example/index.ts index b4b4356c..636d3bda 100644 --- a/example/index.ts +++ b/example/index.ts @@ -2,8 +2,9 @@ import { DateTime } from 'luxon' import { BaseModel, HasOne, hasOne, scope, column } from '@ioc:Adonis/Lucid/Orm' import Factory from '@ioc:Adonis/Lucid/Factory' -enum ProfileTypes { - TWITTER = 'TWITTER', +const ProfileTypes = { + TWITTER: 'TWITTER' as 'TWITTER', + FACEBOOK: 'FACEBOOK' as 'FACEBOOK', } class Profile extends BaseModel { @@ -11,7 +12,7 @@ class Profile extends BaseModel { public userId: string public user: HasOne - public type: ProfileTypes + public type: keyof typeof ProfileTypes @column.dateTime() public createdAt?: DateTime diff --git a/src/Factory/FactoryModel.ts b/src/Factory/FactoryModel.ts index dddb6e0b..f93e9190 100644 --- a/src/Factory/FactoryModel.ts +++ b/src/Factory/FactoryModel.ts @@ -42,16 +42,16 @@ export class FactoryModel implements FactoryModelContr public newUpModelInstance: NewUpCallback> = function ( attributes: any ) { - const Model = this.model + const model = this.model /** * Handling case, where someone returns model instance directly */ - if (attributes instanceof Model) { + if (attributes instanceof model) { return attributes } - const modelInstance = new Model() + const modelInstance = new model() modelInstance.merge(attributes) return modelInstance }.bind(this)