Skip to content

Commit

Permalink
Merge pull request #27 from pantharshit00/fix-dmmf-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pantharshit00 authored Mar 11, 2021
2 parents fff351a + 17e189d commit b31fb4b
Show file tree
Hide file tree
Showing 12 changed files with 1,735 additions and 1,340 deletions.
38 changes: 20 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,36 @@
"url": "https://github.com/pantharshit00/prisma-docs-generator/issues"
},
"dependencies": {
"@prisma/generator-helper": "^2.11.0",
"@prisma/sdk": "^2.11.0",
"@prisma/generator-helper": "^2.18.0",
"@prisma/sdk": "^2.18.0",
"express": "^4.17.1",
"indent-string": "^4.0.0",
"kleur": "^4.1.3",
"meow": "^8.0.0",
"kleur": "^4.1.4",
"meow": "^9.0.0",
"pluralize": "^8.0.0",
"prismjs": "^1.22.0"
"prismjs": "^1.23.0"
},
"devDependencies": {
"@prisma/cli": "^2.11.0",
"@prisma/client": "^2.11.0",
"@types/express": "4.17.7",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.8",
"@types/prismjs": "^1.16.2",
"@prisma/client": "^2.18.0",
"@types/express": "4.17.11",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.33",
"@types/prismjs": "^1.16.3",
"cpy-cli": "^3.1.1",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"ts-jest": "^26.4.4",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
"prettier": "^2.2.1",
"prisma": "^2.18.0",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.3",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
},
"scripts": {
"start": "ts-node main.ts",
"generate": "prisma generate",
"clean": "rm -rf dist && rm -rf styles_generator/dist && rm src/styles/main.css",
"build:gen": "tsc -d && cp -r src/styles dist",
"build:styles": "cd styles_generator && yarn build && cp ./dist/main.css ../src/styles/",
"clean": "rimraf dist && rimraf styles_generator/dist && rimraf src/styles/main.css",
"build:gen": "tsc -d && cpy src/styles dist/styles",
"build:styles": "cd styles_generator && yarn build && cpy ./dist/main.css ../src/styles/",
"build": "yarn build:styles && yarn build:gen",
"test": "jest",
"format": "prettier --ignore-path .gitignore src/**/*.ts --write"
Expand Down
10 changes: 5 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["atomicNumberOperations"]
provider = "prisma-client-js"
}

generator docs {
Expand All @@ -22,23 +21,24 @@ model Post {
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int @default(1)
@@map("posts")
@@id([id, authorId])
@@unique([authorId])
@@unique(name: "test", fields: [content, title])
@@index([id, authorId])
@@index([authorId, title])
@@map("posts")
}

model Profile {
id Int @default(autoincrement()) @id
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}

model User {
id Int @default(autoincrement()) @id
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
Expand Down
31 changes: 8 additions & 23 deletions src/generator/apitypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type TGType = {
type TGTypeField = {
name: string;
type: DMMF.SchemaArgInputType[];
kind: string;
required: boolean;
nullable: boolean;
};

Expand Down Expand Up @@ -49,14 +47,6 @@ class TypesGenerator implements Generatable<TypesGeneratorStructure> {
.join(' | ')}
</td>
<td class="px-4 py-2 border">
${field.kind}
</td>
<td class="px-4 py-2 border">
${field.required ? '<strong>Yes</strong>' : 'No'}
</td>
<td class="px-4 py-2 border">
${field.nullable ? '<strong>Yes</strong>' : 'No'}
</td>
Expand All @@ -75,8 +65,6 @@ class TypesGenerator implements Generatable<TypesGeneratorStructure> {
<tr>
<th class="px-4 py-2 border">Name</th>
<th class="px-4 py-2 border">Type</th>
<th class="px-4 py-2 border">Kind</th>
<th class="px-4 py-2 border">Required</th>
<th class="px-4 py-2 border">Nullable</th>
</tr>
</thead>
Expand Down Expand Up @@ -119,10 +107,8 @@ class TypesGenerator implements Generatable<TypesGeneratorStructure> {
return dmmfInputType.map((inputType) => ({
name: inputType.name,
fields: inputType.fields.map((ip) => ({
kind: ip.inputTypes.length > 1 ? 'union' : ip.inputTypes[0].kind,
name: ip.name,
nullable: ip.isNullable,
required: ip.isRequired,
type: ip.inputTypes,
})),
}));
Expand All @@ -132,16 +118,14 @@ class TypesGenerator implements Generatable<TypesGeneratorStructure> {
return dmmfOutputTypes.map((outputType) => ({
name: outputType.name,
fields: outputType.fields.map((op) => ({
kind: op.outputType.kind,
name: op.name,
nullable: Boolean(op.isNullable),
required: op.isRequired,
nullable: !op.isNullable,
list: (op.outputType as any).isList,
type: [
{
isList: op.outputType.isList,
kind: op.outputType.kind,
type: op.outputType.type as string,
location: op.outputType.location,
},
],
})),
Expand All @@ -150,12 +134,13 @@ class TypesGenerator implements Generatable<TypesGeneratorStructure> {

getData(d: DMMFDocument) {
return {
inputTypes: this.getInputTypes(d.schema.inputTypes),
outputTypes: this.getOutputTypes(
d.schema.outputTypes.filter(
inputTypes: this.getInputTypes(d.schema.inputObjectTypes.prisma),
outputTypes: this.getOutputTypes([
...d.schema.outputObjectTypes.model,
...d.schema.outputObjectTypes.prisma.filter(
(op) => op.name !== 'Query' && op.name !== 'Mutation'
)
),
),
]),
};
}
}
Expand Down
77 changes: 49 additions & 28 deletions src/generator/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type MGModelOperationKeys = {

type MGModelOperationOutput = {
type: string;
kind: string;
required: boolean;
list: boolean;
};
Expand Down Expand Up @@ -184,8 +183,6 @@ export default class ModelGenerator
<div><strong>Type: </strong> <a href="#type-outputType-${
operation.output.type
}">${operation.output.type}</a></div>
<div><strong>Kind: </strong>
${operation.output.kind}</div>
<div><strong>Required: </strong>
${operation.output.required ? `Yes` : `No`}</div>
<div><strong>List: </strong>
Expand Down Expand Up @@ -373,7 +370,7 @@ export default class ModelGenerator
const method = `prisma.${lowerCase(model.name)}.${op}`;
switch (op) {
case DMMF.ModelAction.create: {
const field = schema.outputTypes
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Mutation')
?.fields.find((f) => f.name === val);
ops.push({
Expand All @@ -397,15 +394,14 @@ const ${singular} = await ${method}({
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}
case DMMF.ModelAction.deleteMany: {
const field = schema.outputTypes
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Mutation')
?.fields.find((f) => f.name === val);
ops.push({
Expand All @@ -429,15 +425,14 @@ const { count } = await ${method}({
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}
case DMMF.ModelAction.delete: {
const field = schema.outputTypes
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Mutation')
?.fields.find((f) => f.name === val);
ops.push({
Expand All @@ -460,15 +455,14 @@ const ${singular} = await ${method}({
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}
case DMMF.ModelAction.findMany: {
const field = schema.outputTypes
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Query')
?.fields.find((f) => f.name === val);
ops.push({
Expand All @@ -490,15 +484,14 @@ const ${plural} = await ${method}({ take: 10 })
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}
case DMMF.ModelAction.findOne: {
const field = schema.outputTypes
case DMMF.ModelAction.findUnique: {
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Query')
?.fields.find((f) => f.name === val);
ops.push({
Expand All @@ -522,16 +515,47 @@ const ${lowerCase(singular)} = await ${method}({
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}

case DMMF.ModelAction.findFirst: {
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Query')
?.fields.find((f) => f.name === val);
ops.push({
name: op,
description: `Find first ${plural}`,
usage: Prism.highlight(
`// Get one ${singular}
const ${lowerCase(singular)} = await ${method}({
where: {
// ... provide filter here
}
})
`,
Prism.languages.javascript,
'javascript'
),
opKeys: field?.args.map((a) => ({
name: a.name,
types: a.inputTypes,
required: a.isRequired,
})),
output: {
type: field?.outputType.type as string,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}

case DMMF.ModelAction.update: {
const field = schema.outputTypes
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Mutation')
?.fields.find((f) => f.name === val);
ops.push({
Expand All @@ -558,16 +582,15 @@ const ${lowerCase(singular)} = await ${method}({
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}

case DMMF.ModelAction.updateMany: {
const field = schema.outputTypes
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Mutation')
?.fields.find((f) => f.name === val);
ops.push({
Expand All @@ -592,15 +615,14 @@ const ${lowerCase(singular)} = await ${method}({
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
break;
}
case DMMF.ModelAction.upsert: {
const field = schema.outputTypes
const field = schema.outputObjectTypes.prisma
.find((t) => t.name === 'Mutation')
?.fields.find((f) => f.name === val);
ops.push({
Expand Down Expand Up @@ -629,8 +651,7 @@ const ${lowerCase(singular)} = await ${method}({
})),
output: {
type: field?.outputType.type as string,
kind: field?.outputType.kind,
required: field?.isRequired,
required: !field?.isNullable,
list: field?.outputType.isList,
},
});
Expand Down
15 changes: 9 additions & 6 deletions src/generator/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type TOCModel = {
type TOCTypes = {
inputTypes: string[];
outputTypes: string[];
enums: string[];
};

export default class TOCGenerator implements Generatable<TOCStructure> {
Expand Down Expand Up @@ -122,11 +121,15 @@ export default class TOCGenerator implements Generatable<TOCStructure> {

getTypes(dmmfSchema: DMMF.Schema): TOCTypes {
return {
inputTypes: dmmfSchema.inputTypes.map((inputType) => inputType.name),
outputTypes: dmmfSchema.outputTypes
.map((outputType) => outputType.name)
.filter((ot) => ot !== 'Query' && ot !== 'Mutation'),
enums: dmmfSchema.enums.map((x) => x.name), // can't use enum as variable as it is reserved word in TS
inputTypes: dmmfSchema.inputObjectTypes.prisma.map(
(inputType) => inputType.name
),
outputTypes: [
...dmmfSchema.outputObjectTypes.model.map((ot) => ot.name),
...dmmfSchema.outputObjectTypes.prisma
.map((outputType) => outputType.name)
.filter((ot) => ot !== 'Query' && ot !== 'Mutation'),
],
};
}

Expand Down
Loading

0 comments on commit b31fb4b

Please sign in to comment.