-
For a given model, I have one boolean: table.boolean('free').defaultTo(false) Depending on its value, it is saved as 1 or 0 in the MySQL database. When I am returning the model (or multiple instances in a pagination result), I get 0 or 1 for this value. Is there a way to cast this value to a real boolean (false or true)? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Along with the serialized property names, you can also customize the value of a given column by defining a custom serialize method. export default class Post extends BaseModel {
@column({
serialize: (value?: Number) => {
return Boolean(value)
},
})
public published: boolean
} |
Beta Was this translation helpful? Give feedback.
-
I solve this issue with this:
|
Beta Was this translation helpful? Give feedback.
-
You can also use the shorter decorator serialization: However, I find this to be quite an unexpected behavior for booleans in typescript, is there any way to set this serialization globally for all boolean attributes in models? |
Beta Was this translation helpful? Give feedback.
-
Actually it looks like both methods are necessary to make boolean work as expected: @column({
consume: Boolean,
serialize: Boolean
})
public async destroy(user: User) {
return user.isAdmin
} // this results in an always false authorization when not using consume: Boolean |
Beta Was this translation helpful? Give feedback.
Along with the serialized property names, you can also customize the value of a given column by defining a custom serialize method.