Skip to content

Commit

Permalink
fix(orm): use naming strategy for computed field (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz authored Dec 15, 2023
1 parent 5e99253 commit 073fee4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/orm/base_model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ class BaseModelImpl implements LucidRow {
*/
static $addComputed(name: string, options: Partial<ComputedOptions>) {
const computed: ComputedOptions = {
serializeAs: options.serializeAs || name,
serializeAs:
options.serializeAs !== undefined
? options.serializeAs
: this.namingStrategy.serializedName(this, name),
meta: options.meta,
}
this.$computedDefinitions.set(name, computed)
Expand Down
12 changes: 6 additions & 6 deletions test/orm/base_model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,7 @@ test.group('Base Model | toJSON', (group) => {
const user = new User()
user.username = 'virk'

assert.deepEqual(user.toJSON(), { username: 'virk', fullName: 'VIRK' })
assert.deepEqual(user.toJSON(), { username: 'virk', full_name: 'VIRK' })
})

test('do not add computed property when it returns undefined', async ({ fs, assert }) => {
Expand Down Expand Up @@ -2585,7 +2585,7 @@ test.group('Base Model | toJSON', (group) => {

assert.deepEqual(user.toJSON(), {
username: 'virk',
fullName: 'VIRK',
full_name: 'VIRK',
meta: {
postsCount: 10,
},
Expand Down Expand Up @@ -2623,7 +2623,7 @@ test.group('Base Model | toJSON', (group) => {

assert.deepEqual(user.toJSON(), {
username: 'virk',
fullName: 'VIRK',
full_name: 'VIRK',
posts: {
count: 10,
},
Expand Down Expand Up @@ -7612,7 +7612,7 @@ test.group('Base model | inheritance', (group) => {
'fullName',
{
meta: undefined,
serializeAs: 'fullName',
serializeAs: 'full_name',
},
],
[
Expand All @@ -7638,7 +7638,7 @@ test.group('Base model | inheritance', (group) => {
'fullName',
{
meta: undefined,
serializeAs: 'fullName',
serializeAs: 'full_name',
},
],
])
Expand Down Expand Up @@ -7733,7 +7733,7 @@ test.group('Base model | inheritance', (group) => {
'fullName',
{
meta: undefined,
serializeAs: 'fullName',
serializeAs: 'full_name',
},
],
])
Expand Down

0 comments on commit 073fee4

Please sign in to comment.