Skip to content

Commit

Permalink
fix: bug on multiple relation
Browse files Browse the repository at this point in the history
  • Loading branch information
adeyahya committed Jun 23, 2021
1 parent 977fa48 commit 51ba403
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/generator/transformDMMF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ const transformFields = (fields: DMMF.Field[]) => {
};
};

const transformModel = (model: DMMF.Model, dependant: string | null = null) => {
const transformModel = (model: DMMF.Model, models?: DMMF.Model[]) => {
const fields = transformFields(model.fields);
let raw = [
`${dependant ? '' : `export const ${model.name} = `}Type.Object({\n\t`,
`${models ? '' : `export const ${model.name} = `}Type.Object({\n\t`,
fields.rawString,
'})',
].join('\n');

if (typeof dependant === 'string') {
const re = new RegExp(`.+::${dependant}.+\n`);
raw = raw.replace(re, '');
if (Array.isArray(models)) {
models.forEach((md) => {
const re = new RegExp(`.+::${md.name}.+\n`, 'gm');
raw = raw.replace(re, '');
});
}

return {
Expand Down Expand Up @@ -91,7 +93,7 @@ export function transformDMMF(dmmf: DMMF.Document) {
[...deps].forEach((d) => {
const depsModel = models.find((m) => m.name === d) as DMMF.Model;
if (depsModel) {
const replacer = transformModel(depsModel, model.name);
const replacer = transformModel(depsModel, models);
const re = new RegExp(`::${d}::`, 'gm');
raw = raw.replace(re, replacer.raw);
}
Expand Down

0 comments on commit 51ba403

Please sign in to comment.