I have 2 models
Book: id, name, data (json)Author: id, name
I want to extend Book model with method author which return the author when query withRelated. This could be easy to achieve with below snippet, provided that I have author_id in Book
const Book = bookshelf.model('Book', {
tableName: 'books',
author() {
return this.belongsTo('Author', 'author_id', 'id')
}
})
But the problem is, author_id is a property of data (which is json type). I tried the snippet below but it not worked
const Book = bookshelf.model('Book', {
tableName: 'books',
author() {
return this.belongsTo('Author', `data->>'author_id'`, 'id')
}
})
If I am not allowed to have any changes in the origin models, how could I get author in this case? Thanks in advance.