Skip to content

Commit 46fed69

Browse files
authored
feat: Add generator option for custom import for emitSingle
1 parent 085f8fd commit 46fed69

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

README.md

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,28 @@ export class BigIntFilter {
361361

362362
It will affect all inputs and outputs types (including models).
363363

364+
#### `customImport`
365+
366+
Allow to declare custom import statements. (Only works with emitSingle = true)
367+
368+
```sh
369+
generator nestgraphql {
370+
decorate_{key}_from = "module specifier"
371+
decorate_{key}_name = "import name"
372+
decorate_{key}_defaultImport = "default import name" | true
373+
decorate_{key}_namespaceImport = "namespace import name"
374+
decorate_{key}_namedImport = "import name" | true
375+
}
376+
```
377+
378+
Where `{key}` any identifier to group values (written in [flatten](https://github.com/hughsk/flat) style)
379+
380+
- `decorate_{key}_from` - module specifier to import from (e.g `class-validator`)
381+
- `decorate_{key}_name` - import name or name with namespace
382+
- `decorate_{key}_defaultImport` - import as default
383+
- `decorate_{key}_namespaceImport` - use this name as import namespace
384+
- `decorate_{key}_namedImport` - named import (without namespace)
385+
364386
## Documentation and field options
365387

366388
Comments with triple slash will projected to typescript code comments
@@ -736,27 +758,27 @@ import { generate } from 'prisma-nestjs-graphql/generate';
736758
737759
## Similar Projects
738760
739-
- https://github.com/jasonraimondi/prisma-generator-nestjs-graphql
740-
- https://github.com/omar-dulaimi/prisma-class-validator-generator
741-
- https://github.com/kimjbstar/prisma-class-generator
742-
- https://github.com/odroe/nest-gql-mix
743-
- https://github.com/rfermann/nestjs-prisma-graphql-generator
744-
- https://github.com/madscience/graphql-codegen-nestjs
745-
- https://github.com/wSedlacek/prisma-generators/tree/master/libs/nestjs
746-
- https://github.com/EndyKaufman/typegraphql-prisma-nestjs
747-
- https://github.com/MichalLytek/typegraphql-prisma
748-
- https://github.com/mk668a/nestjs-prisma-graphql-crud-gen
761+
- <https://github.com/jasonraimondi/prisma-generator-nestjs-graphql>
762+
- <https://github.com/omar-dulaimi/prisma-class-validator-generator>
763+
- <https://github.com/kimjbstar/prisma-class-generator>
764+
- <https://github.com/odroe/nest-gql-mix>
765+
- <https://github.com/rfermann/nestjs-prisma-graphql-generator>
766+
- <https://github.com/madscience/graphql-codegen-nestjs>
767+
- <https://github.com/wSedlacek/prisma-generators/tree/master/libs/nestjs>
768+
- <https://github.com/EndyKaufman/typegraphql-prisma-nestjs>
769+
- <https://github.com/MichalLytek/typegraphql-prisma>
770+
- <https://github.com/mk668a/nestjs-prisma-graphql-crud-gen>
749771
750772
## Resources
751773
752-
- Todo - https://github.com/unlight/prisma-nestjs-graphql/issues/2
753-
- https://github.com/prisma/prisma/blob/main/packages/client/src/generation/TSClient/TSClient.ts
754-
- https://ts-ast-viewer.com/
755-
- https://github.com/unlight/nestjs-graphql-prisma-realworld-example-app
756-
- https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model
757-
- JSON type for the code first approach - https://github.com/nestjs/graphql/issues/111#issuecomment-631452899
758-
- https://github.com/paljs/prisma-tools/tree/master/packages/plugins
759-
- https://github.com/wasp-lang/wasp
774+
- Todo - <https://github.com/unlight/prisma-nestjs-graphql/issues/2>
775+
- <https://github.com/prisma/prisma/blob/main/packages/client/src/generation/TSClient/TSClient.ts>
776+
- <https://ts-ast-viewer.com/>
777+
- <https://github.com/unlight/nestjs-graphql-prisma-realworld-example-app>
778+
- <https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model>
779+
- JSON type for the code first approach - <https://github.com/nestjs/graphql/issues/111#issuecomment-631452899>
780+
- <https://github.com/paljs/prisma-tools/tree/master/packages/plugins>
781+
- <https://github.com/wasp-lang/wasp>
760782
761783
## TODO
762784

src/handlers/generate-files.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ export async function generateFiles(args: EventArguments) {
100100
}
101101
}
102102
}
103+
for (const customImport of config.customImport) {
104+
imports.create(customImport);
105+
}
103106
sourceFile.set({
104107
kind: StructureKind.SourceFile,
105108
statements: [...imports.toStatements(), ...enums, ...classes],

src/helpers/create-config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ type DecorateElement = {
2121
defaultImport?: string | true;
2222
namespaceImport?: string;
2323
};
24+
type CustomImport = {
25+
from: string;
26+
name: string;
27+
namedImport: boolean;
28+
defaultImport?: string | true;
29+
namespaceImport?: string;
30+
};
2431

2532
export function createConfig(data: Record<string, unknown>) {
2633
const config = merge({}, unflatten(data, { delimiter: '_' })) as Record<
@@ -96,6 +103,24 @@ export function createConfig(data: Record<string, unknown>) {
96103
});
97104
}
98105

106+
const customImport: CustomImport[] = []
107+
const configCustomImport: (Record<string, string> | undefined)[] = Object.values(
108+
(config.customImport as any) || {},
109+
);
110+
for (const element of configCustomImport) {
111+
if (!element) continue;
112+
ok(
113+
element.from && element.name,
114+
`Missed 'from' or 'name' part in configuration for customImport`,
115+
);
116+
customImport.push({
117+
from: element.from,
118+
name: element.name,
119+
namedImport: toBoolean(element.namedImport),
120+
defaultImport: toBoolean(element.defaultImport) ? true : element.defaultImport,
121+
namespaceImport: element.namespaceImport,
122+
});
123+
}
99124
return {
100125
outputFilePattern,
101126
tsConfigFilePath: createTsConfigFilePathValue(config.tsConfigFilePath),
@@ -123,6 +148,7 @@ export function createConfig(data: Record<string, unknown>) {
123148
ImportNameSpec | undefined
124149
>,
125150
decorate,
151+
customImport,
126152
};
127153
}
128154

0 commit comments

Comments
 (0)