I am using bookshelf with knex query builder. I have 3 sql tables: forms, users, exams and their related models: formsModel,usersModel, examsModel. I am successfully fetching data from forms table with related to exams table as below: at models:
var examsModel = bookshelf.Model.extend({
tableName: 'exams',
hasTimestamps: true,
spf: function () {
return this.belongsTo("formsModel", "pf_no", "pf_no");
}
});
at api call:
let query = examsModel.forge();
query = query.orderBy("updated_at", "desc");
const response = await query.fetchPage({
pageSize: 5,
page: page,
withRelated: ['spf']
});
let responseData = await response.toJSON();
Here in above api call, active users checking is not done.
Now, if I want to add condition such that fetch data of only active users. Then this can also achieved by following code:
data = await examsModel.forge().query((qb) => {
qb.from('users').innerJoin('exams', 'exams.pf_no', 'users.pf_number')
.where('users.active', '=', '1')
})
.fetchPage({
pageSize: 5,
page: page,
withRelated: ['spf']
});
But, My working code is very big and I have to replace at lot many places in my code which is very time consuming process and if i want to add some more conditions then again lengthy process.
I want that I dont want to replace existing code, But I should get data by comparing as a active users only. For that we need to make changes in this Bookshelf Model definition. How can I achieve this? I want the solution that makes changes in Model definition such that by making a single file change, It will give me data filtered on basis on third users table?