diff --git a/src/Orm/BaseModel/index.ts b/src/Orm/BaseModel/index.ts index 892a116a..2e4f8867 100644 --- a/src/Orm/BaseModel/index.ts +++ b/src/Orm/BaseModel/index.ts @@ -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 diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index 63d9be04..aee003ba 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -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) => {