Skip to content

Commit

Permalink
feat: add support for input type decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann committed Aug 25, 2021
1 parent 423a6bf commit c79eb58
Show file tree
Hide file tree
Showing 18 changed files with 511 additions and 32 deletions.
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"eslint": "^7.23.0",
"husky": "^5.2.0",
"prettier": "^2.2.1",
"prisma": "^2.20.1",
"prisma": "2.28",
"size-limit": "^4.10.1",
"tsdx": "^0.14.1",
"tslib": "^2.1.0",
Expand Down
15 changes: 13 additions & 2 deletions src/Generator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("Generator", () => {
expect([...folderList]).toStrictEqual(["enums", "index.ts", "Session", "shared", "User"]);
});
it("should log the correct actions", async () => {
expect.assertions(36);
expect.assertions(42);

const generator = new Generator({
datamodel: "",
Expand All @@ -93,6 +93,10 @@ describe("Generator", () => {
dmmf: null,
generator: {
...generatorConfig,
config: {
...generatorConfig.config,
inputTypeDecoratorsPath: `${process.cwd()}/fixtures/Generator/output1/inputTypeDecorators`,
},
output: {
fromEnvVar: null,
value: `${process.cwd()}/fixtures/Generator/output1`,
Expand All @@ -110,7 +114,7 @@ describe("Generator", () => {

consoleLog.mockRestore();

expect(values).toHaveLength(36);
expect(values).toHaveLength(42);

// expect starting and closing messages to appear in the correct order
expect(values[0]).toMatch(getStartedMessage(Generator.messages.init));
Expand Down Expand Up @@ -155,5 +159,12 @@ describe("Generator", () => {
expect(findCompletedMessage(values, Generator.messages.resolvers.parse)).toHaveLength(1);
expect(findStartedMessage(values, Generator.messages.resolvers.generate)).toHaveLength(1);
expect(findCompletedMessage(values, Generator.messages.resolvers.generate)).toHaveLength(1);

expect(findStartedMessage(values, Generator.messages.inputTypeDecorators.title)).toHaveLength(1);
expect(findCompletedMessage(values, Generator.messages.inputTypeDecorators.title)).toHaveLength(1);
expect(findStartedMessage(values, Generator.messages.inputTypeDecorators.parse)).toHaveLength(1);
expect(findCompletedMessage(values, Generator.messages.inputTypeDecorators.parse)).toHaveLength(1);
expect(findStartedMessage(values, Generator.messages.inputTypeDecorators.generate)).toHaveLength(1);
expect(findCompletedMessage(values, Generator.messages.inputTypeDecorators.generate)).toHaveLength(1);
});
});
26 changes: 26 additions & 0 deletions src/Generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GeneratorConfig } from "../GeneratorConfig";
import {
BarrelFileHandler,
EnumHandler,
InputTypeDecoratorHandler,
InputTypeHandler,
ModelHandler,
OutputTypeHandler,
Expand All @@ -23,6 +24,11 @@ export class Generator {
title: "Parsing and generating Enums",
},
init: "Initialize generator",
inputTypeDecorators: {
generate: "Generating Input Type structure",
parse: "Parsing Input Types structure",
title: "Parsing and generating Input Type structure",
},
inputTypes: {
generate: "Generating Input Types",
parse: "Parsing Input Types",
Expand Down Expand Up @@ -55,6 +61,8 @@ export class Generator {

private _enumHandler!: EnumHandler;

private _inputTypeDecoratorHandler!: InputTypeDecoratorHandler;

private _inputTypeHandler!: InputTypeHandler;

private _modelHandler!: ModelHandler;
Expand Down Expand Up @@ -174,6 +182,23 @@ export class Generator {
),
title: Generator.messages.objects,
},
{
enabled: () => typeof this._config.inputTypeDecoratorsPath === "string",
task: async () =>
new Listr([
{
task: () => this._inputTypeDecoratorHandler.parse(this._inputTypeHandler.getInputTypes()),
title: Generator.messages.inputTypeDecorators.parse,
},
{
task: async () => {
await this._inputTypeDecoratorHandler.createFile();
},
title: Generator.messages.inputTypeDecorators.generate,
},
]),
title: Generator.messages.inputTypeDecorators.title,
},
],
{ concurrent: false }
),
Expand All @@ -197,6 +222,7 @@ export class Generator {
this._enumHandler = new EnumHandler({ config: this._config, dmmf: this._dmmf });
this._modelHandler = new ModelHandler({ config: this._config, dmmf: this._dmmf });
this._inputTypeHandler = new InputTypeHandler({ config: this._config, dmmf: this._dmmf });
this._inputTypeDecoratorHandler = new InputTypeDecoratorHandler({ config: this._config, dmmf: this._dmmf });
this._outputTypeHandler = new OutputTypeHandler({ config: this._config, dmmf: this._dmmf });
this._resolverHandler = new ResolverHandler({ config: this._config, dmmf: this._dmmf });

Expand Down
5 changes: 5 additions & 0 deletions src/GeneratorConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { GeneratorConfig as PrismaGeneratorConfig } from "@prisma/generator

interface Paths {
enums: string;
inputTypeDecorators: string;
inputTypes: string;
model: string;
outputTypes: string;
Expand All @@ -21,6 +22,8 @@ export class GeneratorConfig {

readonly inputArgumentsName: string;

readonly inputTypeDecoratorsPath: string | undefined;

readonly paths: Paths;

readonly prismaClientImportPath: string;
Expand All @@ -47,6 +50,7 @@ export class GeneratorConfig {
this.basePath = output.value;
this.paths = {
enums: "enums",
inputTypeDecorators: "inputTypeDecorators",
inputTypes: "inputTypes",
model: "model",
outputTypes: "outputTypes",
Expand All @@ -55,6 +59,7 @@ export class GeneratorConfig {
};
this.includePrismaSelect = config.includePrismaSelect === "true" || false;
this.inputArgumentsName = config.inputArgumentsName || "input";
this.inputTypeDecoratorsPath = config.inputTypeDecoratorsPath || undefined;
this.prismaClientImportPath = prismaClientPath.output.value;
this.prismaServiceImport = config.prismaServiceImport || "PrismaService";
this.prismaServiceImportPath = config.prismaServiceImportPath;
Expand Down

0 comments on commit c79eb58

Please sign in to comment.