Skip to content

Commit

Permalink
refactor: remove nullable from columns and add serialize property
Browse files Browse the repository at this point in the history
Serialize property allows stopping the column from getting serialized
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent 1029bb2 commit 6259c52
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
export type ColumnNode = {
castAs: string,
serializeAs: string,
nullable: boolean,
serialize: boolean,
primary: boolean,
hasGetter: boolean,
hasSetter: boolean,
Expand Down
20 changes: 13 additions & 7 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ export class BaseModel implements ModelContract {
const descriptor = Object.getOwnPropertyDescriptor(this.prototype, name)

const column: ColumnNode = {
castAs: options.castAs || snakeCase(name),
serializeAs: options.serializeAs || snakeCase(name),
nullable: options.nullable || false,
primary: options.primary || false,
castAs: options.castAs || snakeCase(name),
hasGetter: !!(descriptor && descriptor.get),
hasSetter: !!(descriptor && descriptor.set),
serializeAs: options.serializeAs || snakeCase(name),
serialize: options.serialize === false ? false : true,
}

/**
Expand Down Expand Up @@ -782,12 +782,18 @@ export class BaseModel implements ModelContract {
const Model = this.constructor as typeof BaseModel
const results = {}

Model.$computed.forEach((value, key) => {
results[value.serializeAs] = this[key]
Object.keys(this.$attributes).forEach((key) => {
const column = Model.$getColumn(key)!
if (column.serialize) {
results[column.serializeAs] = this.$attributes[key]
}
})

Object.keys(this.$attributes).forEach((key) => {
results[Model.$getColumn(key)!.serializeAs] = this.$attributes[key]
Model.$computed.forEach((value, key) => {
const computedValue = this[key]
if (computedValue !== undefined) {
results[value.serializeAs] = computedValue
}
})

Object.keys(this.$preloaded).forEach((key) => {
Expand Down
33 changes: 33 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,20 @@ test.group('Base Model | toJSON', () => {
assert.deepEqual(user.toJSON(), { theUsername: 'virk' })
})

test('do not serialize when serialize is set to false', async (assert) => {
const BaseModel = getBaseModel(ormAdapter())

class User extends BaseModel {
@column({ serialize: false })
public username: string
}

const user = new User()
user.username = 'virk'

assert.deepEqual(user.toJSON(), {})
})

test('add computed properties to toJSON result', async (assert) => {
const BaseModel = getBaseModel(ormAdapter())

Expand All @@ -740,6 +754,25 @@ test.group('Base Model | toJSON', () => {

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

test('do not add computed property when it returns undefined', async (assert) => {
const BaseModel = getBaseModel(ormAdapter())

class User extends BaseModel {
@column()
public username: string

@computed()
public get fullName () {
return undefined
}
}

const user = new User()
user.username = 'virk'

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

test.group('BaseModel | cache', () => {
Expand Down

0 comments on commit 6259c52

Please sign in to comment.