简体   繁体   中英

Using modifier with Objection.js relationmapping

I have four tables vehicle_parts , part_pricing , labour_pricing and paint_pricing . Vehicle_parts table is having one to many relationship with remaining tables. Each table is having a field is_active indicating whether record is active or not. So ideally for every part in vehicle_parts table there will be only one active price in part_pricing.

I am using Objection.js to build my model as shown below - 在此处输入图像描述

I have a function where i am querying the vehicle part model to fetch vehicle parts along with associated prices, as shown below - 在此处输入图像描述

I am using withGraphFetched() method to get relational data and i am getting that.

The problem i am facing is when i am getting vehicle parts, i am getting active parts only, however i am getting non-active prices as well along with active price in relational data. I know this can be solved using modifiers but i am not sure how to use that. In simple words i need to check is_Active flag in every relation when fetching data using withGraphFetched() .

Thanks for sharing your wisdom.

You should apply filters in the model when setting up the relationship.

Like, for example,

 part_pricing: { relation: BaseModel.HasManyRelation, modelClass: path.join(__dirname, '/PartPricing'), filter: (builder) => builder.where('is_active', true), join: { from: 'vehicle_parts.id', to: 'part_pricing.vehicle_part_id', }, }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM