( version: IVersion, data: any[], withArray: IWith[], model: IModelService, modelList: ModelListService, database: Knex | Knex.Transaction, handler: HandlerTypes, request: AxeRequest, )
| 267 | }; |
| 268 | |
| 269 | export const getRelatedData = async ( |
| 270 | version: IVersion, |
| 271 | data: any[], |
| 272 | withArray: IWith[], |
| 273 | model: IModelService, |
| 274 | modelList: ModelListService, |
| 275 | database: Knex | Knex.Transaction, |
| 276 | handler: HandlerTypes, |
| 277 | request: AxeRequest, |
| 278 | ) => { |
| 279 | if (withArray.length === 0) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | const models = modelList.get(); |
| 284 | |
| 285 | for (const clientQuery of withArray) { |
| 286 | // Find the relation of the model. If the model doesn't have any relationship like the |
| 287 | // user wants, we can't show anything. |
| 288 | const definedRelation = model.relations.find( |
| 289 | (relation) => relation.name === clientQuery.relationship, |
| 290 | ); |
| 291 | if (!definedRelation) { |
| 292 | throw new ApiError(`Undefined relation: ${clientQuery.relationship}`); |
| 293 | } |
| 294 | |
| 295 | // Find the foreign model by the relationship |
| 296 | const foreignModel = models.find( |
| 297 | (model) => model.name === definedRelation.model, |
| 298 | ); |
| 299 | if (!foreignModel) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | // Validating the query limit |
| 304 | valideteQueryFeature( |
| 305 | model, |
| 306 | RelationQueryFeatureMap[definedRelation.type], |
| 307 | `${model.instance.table}.${definedRelation.name}`, |
| 308 | `${model.instance.table}.${definedRelation.name}`, |
| 309 | ); |
| 310 | |
| 311 | let dataField = "primaryKey"; |
| 312 | let searchField = "foreignKey"; |
| 313 | if (definedRelation.type !== Relationships.HAS_MANY) { |
| 314 | dataField = "foreignKey"; |
| 315 | searchField = "primaryKey"; |
| 316 | } |
| 317 | |
| 318 | const dataFieldKey = getPrimaryOrForeignKeyByRelation( |
| 319 | definedRelation, |
| 320 | dataField, |
| 321 | ); |
| 322 | const searchFieldKey = getPrimaryOrForeignKeyByRelation( |
| 323 | definedRelation, |
| 324 | searchField, |
| 325 | ); |
| 326 |
no test coverage detected