Skip to content

Commit

Permalink
improvement: raise error when trying to merge or fill extra propertie…
Browse files Browse the repository at this point in the history
…s to model
  • Loading branch information
thetutlage committed Apr 18, 2020
1 parent 1167b54 commit 003f1c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,17 @@ export class BaseModel implements LucidRow {
return
}

/**
* Resolve the attribute name from the column names. Since people
* usaully define the column names directly as well by
* accepting them directly from the API.
*/
const attributeName = Model.$keys.columnsToAttributes.get(key)
if (attributeName) {
this[attributeName] = value
return
}

/**
* Resolve the attribute name from the column names. Since people
* usaully define the column names directly as well by
Expand Down
31 changes: 31 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,37 @@ test.group('BaseModel | fill/merge', (group) => {
user.fill({ username: 'virk', age: 22 })
assert.deepEqual(user.$attributes, { username: 'virk', age: 23 })
})

test('fill using the column name', (assert) => {
class User extends BaseModel {
@column()
public firstName: string
}

const user = new User()
user.fill({ first_name: 'virk' } as any)
assert.deepEqual(user.$attributes, { firstName: 'virk' })
})

test('invoke setter during fill when using column name', (assert) => {
class User extends BaseModel {
@column()
public username: string

@column({ columnName: 'user_age' })
public get age (): number {
return this.$getAttribute('age')
}

public set age (age: number) {
this.$setAttribute('age', age + 1)
}
}

const user = new User()
user.fill({ user_age: 22 } as any)
assert.deepEqual(user.$attributes, { age: 23 })
})
})

test.group('Base | apdater', (group) => {
Expand Down

0 comments on commit 003f1c4

Please sign in to comment.