Skip to content

Commit

Permalink
Merge pull request #64 from meetsmore/fmy/includeRelationFields-option
Browse files Browse the repository at this point in the history
  • Loading branch information
pantharshit00 authored Feb 15, 2023
2 parents 667030b + a3ceed3 commit e8481a3
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ generator docs {
}
```

### includeRelationFields

You can specify whether relation fields are shown or not.
Default value is `true`.

```prisma
generator docs {
provider = "node node_modules/prisma-docs-generator"
includeRelationFields = false
}
```

## CLI

This package also ships with a CLI which is used to serve the docs right now. It has the following subcommands:
Expand Down
16 changes: 15 additions & 1 deletion src/generator/transformDMMF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@ export type DMMFDocument = Omit<ExternalDMMF.Document, 'mappings'> & {
mappings: DMMFMapping[];
};

type OptionsForTransformDMMF = {
includeRelationFields: boolean
}

export default function transformDMMF(
dmmf: ExternalDMMF.Document
dmmf: ExternalDMMF.Document,
{ includeRelationFields }: OptionsForTransformDMMF
): DMMFDocument {
if (!includeRelationFields) {
dmmf.datamodel.models = dmmf.datamodel.models.map(model => {
model.fields = model.fields.filter(
field => !field.relationName
);
return model;
});
}

return {
...dmmf,
mappings: getMappings(dmmf.mappings, dmmf.datamodel),
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ generatorHandler({
};
},
async onGenerate(options) {
const dmmf = transformDMMF(options.dmmf);
const { config } = options.generator;
const includeRelationFields = config.includeRelationFields === 'false' ? false : true;

const dmmf = transformDMMF(options.dmmf, {
includeRelationFields,
});
const html = new HTMLPrinter(dmmf);

const output = options.generator.output?.value;
Expand Down
12 changes: 9 additions & 3 deletions src/tests/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('model generator', () => {
`;

const dmmf = await getDMMF({ datamodel: datamodelString });
const transformedDmmf = transformDMMF(dmmf);
const transformedDmmf = transformDMMF(dmmf, {
includeRelationFields: true,
});

const modelGen = new ModelGenerator(transformedDmmf);
const spy = jest.spyOn(modelGen, 'getModelDiretiveHTML');
Expand Down Expand Up @@ -61,7 +63,9 @@ describe('model generator', () => {
`;

const dmmf = await getDMMF({ datamodel: datamodelString });
const transformedDmmf = transformDMMF(dmmf);
const transformedDmmf = transformDMMF(dmmf, {
includeRelationFields: true,
});

const modelGen = new ModelGenerator(transformedDmmf);
const spy = jest.spyOn(modelGen, 'getModelFieldTableRow');
Expand Down Expand Up @@ -150,7 +154,9 @@ describe('model generator', () => {
`;

const dmmf = await getDMMF({ datamodel: datamodelString });
const transformedDmmf = transformDMMF(dmmf);
const transformedDmmf = transformDMMF(dmmf, {
includeRelationFields: true,
});

const modelGen = new ModelGenerator(transformedDmmf);
const spy = jest.spyOn(modelGen, 'getModelOperationMarkup');
Expand Down
12 changes: 9 additions & 3 deletions src/tests/toc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const datamodel = /* Prisma */ `
describe('TOC', () => {
it('renders TOC Subheader correctly', async () => {
const dmmf = await getDMMF({ datamodel });
const toc = new TOCGenerator(transformDMMF(dmmf));
const toc = new TOCGenerator(transformDMMF(dmmf, {
includeRelationFields: true,
}));
const spy = jest.spyOn(toc, 'getTOCSubHeaderHTML');
// trigger the function
toc.toHTML();
Expand All @@ -30,7 +32,9 @@ describe('TOC', () => {

it('renders TOC subfield correctly', async () => {
const dmmf = await getDMMF({ datamodel });
const toc = new TOCGenerator(transformDMMF(dmmf));
const toc = new TOCGenerator(transformDMMF(dmmf, {
includeRelationFields: true,
}));

const spy = jest.spyOn(toc, 'getSubFieldHTML');
toc.toHTML();
Expand All @@ -50,7 +54,9 @@ describe('TOC', () => {

it('renders on toHTML', async () => {
const dmmf = await getDMMF({ datamodel });
const toc = new TOCGenerator(transformDMMF(dmmf));
const toc = new TOCGenerator(transformDMMF(dmmf, {
includeRelationFields: true,
}));

const subheaderSpy = jest.spyOn(toc, 'getTOCSubHeaderHTML');
const subfieldSpy = jest.spyOn(toc, 'getSubFieldHTML');
Expand Down
99 changes: 99 additions & 0 deletions src/tests/transformDMMF.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import transformDMMF from '../generator/transformDMMF';
//@ts-ignore
import { getDMMF } from '@prisma/sdk';

describe('transformDMMF', () => {
it('show relation fields when includeRelationFields = true', async () => {
const datamodelString = /* Prisma */ `
model User {
id String @default(cuid()) @id
name String
otherField Int
posts Post[]
}
model Post {
id String @id @default(cuid())
title String?
userId String
user User @relation(fields:[userId], references:[id])
}
`;

const dmmf = await getDMMF({ datamodel: datamodelString });
const transformedDmmf = transformDMMF(dmmf, {
includeRelationFields: true,
});

expect(transformedDmmf).toMatchObject({
datamodel: {
models: [
{
name: 'User',
fields: [
{ name: 'id' },
{ name: 'name' },
{ name: 'otherField' },
{ name: 'posts' },
],
},
{
name: 'Post',
fields: [
{ name: 'id' },
{ name: 'title' },
{ name: 'userId' },
{ name: 'user' },
],
},
],
},
});
});

it('hide relation fields when includeRelationFields = false', async () => {
const datamodelString = /* Prisma */ `
model User {
id String @default(cuid()) @id
name String
otherField Int
posts Post[]
}
model Post {
id String @id @default(cuid())
title String?
userId String
user User @relation(fields:[userId], references:[id])
}
`;

const dmmf = await getDMMF({ datamodel: datamodelString });
const transformedDmmf = transformDMMF(dmmf, {
includeRelationFields: false,
});

expect(transformedDmmf).toMatchObject({
datamodel: {
models: [
{
name: 'User',
fields: [
{ name: 'id' },
{ name: 'name' },
{ name: 'otherField' },
],
},
{
name: 'Post',
fields: [
{ name: 'id' },
{ name: 'title' },
{ name: 'userId' },
],
},
],
},
});
});
});

0 comments on commit e8481a3

Please sign in to comment.