( database: Knex, version: IVersion, relation: IRelation, value: any, )
| 628 | }; |
| 629 | |
| 630 | const validateForeignValues = async ( |
| 631 | database: Knex, |
| 632 | version: IVersion, |
| 633 | relation: IRelation, |
| 634 | value: any, |
| 635 | ): Promise<boolean> => { |
| 636 | if (!value) { |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | const foreignModel = version.modelList |
| 641 | .get() |
| 642 | .find((model) => model.name === relation.model); |
| 643 | |
| 644 | if (!foreignModel) { |
| 645 | throw new Error(`The model not found: ${relation.model}`); |
| 646 | } |
| 647 | |
| 648 | const found = await database(foreignModel.instance.table) |
| 649 | .where(foreignModel.instance.primaryKey, value) |
| 650 | .first(); |
| 651 | |
| 652 | return !!found; |
| 653 | }; |
| 654 | |
| 655 | export const getForeignKeyValueErrors = async (context: IContext) => { |
| 656 | const { model, database, version } = context; |
no test coverage detected