Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/orm/base_model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ class BaseModelImpl implements LucidRow {
*/
const value =
typeof attribute.consume === 'function'
? attribute.consume(adapterResult[key], attributeName, this)
? attribute.consume(adapterResult[key], attributeName, this, adapterResult)
: adapterResult[key]

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export type ColumnOptions = {
/**
* Invoked when row is fetched from the database
*/
consume?: (value: any, attribute: string, model: LucidRow) => any
consume?: (value: any, attribute: string, model: LucidRow, adapterResult: ModelObject) => any
}

/**
Expand Down
15 changes: 11 additions & 4 deletions test/orm/base_model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1817,18 +1817,25 @@ test.group('Base Model | create from adapter results', (group) => {
declare username: string

@column({
consume: (value) => value.toUpperCase(),
consume: (value, attribute, instance, adapterResult) =>
`${value.toUpperCase()} ${attribute} ${instance.constructor.name.toString()} ${adapterResult.username}`,
})
declare fullName: string
}

const user = User.$createFromAdapterResult({ full_name: 'virk' })
const user = User.$createFromAdapterResult({ full_name: 'Harminder Virk', username: 'virk' })

assert.isTrue(user!.$isPersisted)
assert.isFalse(user!.$isDirty)
assert.isFalse(user!.$isLocal)
assert.deepEqual(user!.$attributes, { fullName: 'VIRK' })
assert.deepEqual(user!.$original, { fullName: 'VIRK' })
assert.deepEqual(user!.$attributes, {
fullName: 'HARMINDER VIRK fullName User virk',
username: 'virk',
})
assert.deepEqual(user!.$original, {
fullName: 'HARMINDER VIRK fullName User virk',
username: 'virk',
})
})

test('original and attributes should not be shared', async ({ fs, assert }) => {
Expand Down