Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add generator option for custom import for emitSingle #230

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
- <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 - <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

Expand Down
3 changes: 3 additions & 0 deletions src/handlers/generate-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
26 changes: 26 additions & 0 deletions src/helpers/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>) {
const config = merge({}, unflatten(data, { delimiter: '_' })) as Record<
Expand Down Expand Up @@ -96,6 +103,24 @@ export function createConfig(data: Record<string, unknown>) {
});
}

const customImport: CustomImport[] = []
const configCustomImport: (Record<string, string> | 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),
Expand Down Expand Up @@ -123,6 +148,7 @@ export function createConfig(data: Record<string, unknown>) {
ImportNameSpec | undefined
>,
decorate,
customImport,
};
}

Expand Down
Loading