Skip to content

Commit

Permalink
feat(baseModel): adding serialize to offload work from toJSON method
Browse files Browse the repository at this point in the history
The toJSON method is meant to be overwritten for having custom serialize
behavior. However, re-writing the entire logic of serialize  is not
desired, that's why we move all the logic to serialize method and
to toJSON works as an alias by default
  • Loading branch information
thetutlage committed Sep 30, 2019
1 parent 8009256 commit be37cfb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ declare module '@ioc:Adonis/Lucid/Model' {

save (): Promise<void>
delete (): Promise<void>
serialize (): ModelObject
toJSON (): ModelObject
}

Expand Down
10 changes: 9 additions & 1 deletion src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ export class BaseModel implements ModelContract {
/**
* Converting model to it's JSON representation
*/
public toJSON () {
public serialize () {
const Model = this.constructor as typeof BaseModel
const results = {}

Expand Down Expand Up @@ -806,6 +806,14 @@ export class BaseModel implements ModelContract {
return results
}

/**
* Returns the serialize method output. However, any model can overwrite
* it to define it's custom serialize output
*/
public toJSON () {
return this.serialize()
}

/**
* Returns the query for `insert`, `update` or `delete` actions.
* Since the query builder for these actions are not exposed to
Expand Down

0 comments on commit be37cfb

Please sign in to comment.