Skip to content

Commit

Permalink
feat(Model): make refresh chainable
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Jun 10, 2020
1 parent 7a518d8 commit d09e3b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
merge (value: Partial<ModelAttributes<this>>, allowNonExtraProperties?: boolean): this
save (): Promise<this>
delete (): Promise<void>
refresh (): Promise<void>
refresh (): Promise<this>
preload: ModelBuilderPreloadFn<this>

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ export class BaseModel implements LucidRow {
* Noop when model instance is not persisted
*/
if (!this.$isPersisted) {
return
return this
}

/**
Expand All @@ -1781,5 +1781,6 @@ export class BaseModel implements LucidRow {

this.fill(freshModelInstance.$attributes)
this.$hydrateOriginals()
return this
}
}
17 changes: 17 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ test.group('Base model | boot', (group) => {
assert.instanceOf(chained, User)
})

test('ensure refresh method is chainable', async (assert) => {
const adapter = new FakeAdapter()
class User extends BaseModel {
@column()
public username: string

@column()
public age: number
}
User.$adapter = adapter

const user = new User()
const chained = user.refresh()

assert.instanceOf(chained, User)
})

test('compute table name from model name', async (assert) => {
class User extends BaseModel {
@column({ isPrimary: true })
Expand Down

0 comments on commit d09e3b5

Please sign in to comment.