Skip to content

Commit

Permalink
refactor(orm): use naming strategy for computed field
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Nov 6, 2023
1 parent 3717436 commit e988bcd
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
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
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 e988bcd

Please sign in to comment.