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

improvement(Model): make merge, fill & save chainable #554

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ declare module '@ioc:Adonis/Lucid/Model' {
$consumeAdapterResult (adapterResult: ModelObject, sideloadAttributes?: ModelObject): void
$hydrateOriginals(): void

fill (value: Partial<ModelAttributes<this>>, allowNonExtraProperties?: boolean): void
merge (value: Partial<ModelAttributes<this>>, allowNonExtraProperties?: boolean): void
save (): Promise<void>
fill (value: Partial<ModelAttributes<this>>, allowNonExtraProperties?: boolean): this
merge (value: Partial<ModelAttributes<this>>, allowNonExtraProperties?: boolean): this
save (): Promise<this>
delete (): Promise<void>
refresh (): Promise<void>
preload: ModelBuilderPreloadFn<this>
Expand Down
14 changes: 9 additions & 5 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1413,10 +1413,11 @@ export class BaseModel implements LucidRow {
* fill isn't allowed, since we disallow setting relationships
* locally
*/
public fill (values: any, allowNonExtraProperties: boolean = false) {
public fill (values: any, allowNonExtraProperties: boolean = false): this {
this.$attributes = {}
this.merge(values, allowNonExtraProperties)
this.fillInvoked = true
return this
}

/**
Expand All @@ -1425,7 +1426,7 @@ export class BaseModel implements LucidRow {
* 1. If key is unknown, it will be added to the `extras` object.
* 2. If key is defined as a relationship, it will be ignored and one must call `$setRelated`.
*/
public merge (values: any, allowNonExtraProperties: boolean = false) {
public merge (values: any, allowNonExtraProperties: boolean = false): this {
const Model = this.constructor as typeof BaseModel

/**
Expand Down Expand Up @@ -1474,6 +1475,8 @@ export class BaseModel implements LucidRow {
this.$extras[key] = value
})
}

return this
}

/**
Expand All @@ -1498,7 +1501,7 @@ export class BaseModel implements LucidRow {
/**
* Perform save on the model instance to commit mutations.
*/
public async save () {
public async save (): Promise<this> {
this.ensureIsntDeleted()
const Model = this.constructor as typeof BaseModel

Expand All @@ -1517,7 +1520,7 @@ export class BaseModel implements LucidRow {

await Model.$hooks.exec('after', 'create', this)
await Model.$hooks.exec('after', 'save', this)
return
return this
}

/**
Expand All @@ -1532,7 +1535,7 @@ export class BaseModel implements LucidRow {
* Do not issue updates when model doesn't have any mutations
*/
if (!this.$isDirty) {
return
return this
}

/**
Expand All @@ -1545,6 +1548,7 @@ export class BaseModel implements LucidRow {

await Model.$hooks.exec('after', 'update', this)
await Model.$hooks.exec('after', 'save', this)
return this
}

/**
Expand Down
55 changes: 55 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,61 @@ test.group('Base model | boot', (group) => {
await db.manager.closeAll()
})

test('ensure save 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()
user.username = 'virk'
user.age = 22
const chained = await user.save()

assert.instanceOf(chained, User)
})

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

@column()
public age: number
}

const user = new User()
const chained = user.fill({
username: 'virk',
age: 22,
})

assert.instanceOf(chained, User)
})

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

@column()
public age: number
}

const user = new User()
const chained = user.merge({
username: 'virk',
age: 22,
})

assert.instanceOf(chained, User)
})

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