From 831c580d694e06e0838931a45a4cf3aa758bb757 Mon Sep 17 00:00:00 2001 From: Yozan Adriel Date: Thu, 23 Jan 2025 17:42:19 +0900 Subject: [PATCH] add generator option for custom import for emitSingle --- README.md | 58 +++++++++++++++++++++++----------- src/handlers/generate-files.ts | 3 ++ src/helpers/create-config.ts | 26 +++++++++++++++ 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8a519878..491dcdb5 100644 --- a/README.md +++ b/README.md @@ -361,6 +361,28 @@ export class BigIntFilter { It will affect all inputs and outputs types (including models). +#### `customImport` + +Allow to declare custom import statements. (Only works with emitSingle = true) + +```sh +generator nestgraphql { + decorate_{key}_from = "module specifier" + decorate_{key}_name = "import name" + decorate_{key}_defaultImport = "default import name" | true + decorate_{key}_namespaceImport = "namespace import name" + decorate_{key}_namedImport = "import name" | true +} +``` + +Where `{key}` any identifier to group values (written in [flatten](https://github.com/hughsk/flat) style) + +- `decorate_{key}_from` - module specifier to import from (e.g `class-validator`) +- `decorate_{key}_name` - import name or name with namespace +- `decorate_{key}_defaultImport` - import as default +- `decorate_{key}_namespaceImport` - use this name as import namespace +- `decorate_{key}_namedImport` - named import (without namespace) + ## Documentation and field options Comments with triple slash will projected to typescript code comments @@ -736,27 +758,27 @@ import { generate } from 'prisma-nestjs-graphql/generate'; ## Similar Projects -- https://github.com/jasonraimondi/prisma-generator-nestjs-graphql -- https://github.com/omar-dulaimi/prisma-class-validator-generator -- https://github.com/kimjbstar/prisma-class-generator -- https://github.com/odroe/nest-gql-mix -- https://github.com/rfermann/nestjs-prisma-graphql-generator -- https://github.com/madscience/graphql-codegen-nestjs -- https://github.com/wSedlacek/prisma-generators/tree/master/libs/nestjs -- https://github.com/EndyKaufman/typegraphql-prisma-nestjs -- https://github.com/MichalLytek/typegraphql-prisma -- https://github.com/mk668a/nestjs-prisma-graphql-crud-gen +- +- +- +- +- +- +- +- +- +- ## Resources -- Todo - https://github.com/unlight/prisma-nestjs-graphql/issues/2 -- https://github.com/prisma/prisma/blob/main/packages/client/src/generation/TSClient/TSClient.ts -- https://ts-ast-viewer.com/ -- https://github.com/unlight/nestjs-graphql-prisma-realworld-example-app -- https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model -- JSON type for the code first approach - https://github.com/nestjs/graphql/issues/111#issuecomment-631452899 -- https://github.com/paljs/prisma-tools/tree/master/packages/plugins -- https://github.com/wasp-lang/wasp +- Todo - +- +- +- +- +- JSON type for the code first approach - +- +- ## TODO diff --git a/src/handlers/generate-files.ts b/src/handlers/generate-files.ts index c22f2563..4debdd9c 100644 --- a/src/handlers/generate-files.ts +++ b/src/handlers/generate-files.ts @@ -100,6 +100,9 @@ export async function generateFiles(args: EventArguments) { } } } + for (const customImport of config.customImport) { + imports.create(customImport); + } sourceFile.set({ kind: StructureKind.SourceFile, statements: [...imports.toStatements(), ...enums, ...classes], diff --git a/src/helpers/create-config.ts b/src/helpers/create-config.ts index 7b8f419e..a93fb77a 100644 --- a/src/helpers/create-config.ts +++ b/src/helpers/create-config.ts @@ -21,6 +21,13 @@ type DecorateElement = { defaultImport?: string | true; namespaceImport?: string; }; +type CustomImport = { + from: string; + name: string; + namedImport: boolean; + defaultImport?: string | true; + namespaceImport?: string; +}; export function createConfig(data: Record) { const config = merge({}, unflatten(data, { delimiter: '_' })) as Record< @@ -96,6 +103,24 @@ export function createConfig(data: Record) { }); } + const customImport: CustomImport[] = [] + const configCustomImport: (Record | undefined)[] = Object.values( + (config.customImport as any) || {}, + ); + for (const element of configCustomImport) { + if (!element) continue; + ok( + element.from && element.name, + `Missed 'from' or 'name' part in configuration for customImport`, + ); + customImport.push({ + from: element.from, + name: element.name, + namedImport: toBoolean(element.namedImport), + defaultImport: toBoolean(element.defaultImport) ? true : element.defaultImport, + namespaceImport: element.namespaceImport, + }); + } return { outputFilePattern, tsConfigFilePath: createTsConfigFilePathValue(config.tsConfigFilePath), @@ -123,6 +148,7 @@ export function createConfig(data: Record) { ImportNameSpec | undefined >, decorate, + customImport, }; }