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

Add a type-casting annotation #12

Merged
merged 5 commits into from
Jan 6, 2023
Merged

Conversation

zackdotcomputer
Copy link

Adds an annotation @DtoCastType that can be used to override the type of a field in the generated entities.

@Brakebein
Copy link
Owner

Thanks for the PR! It seems to be a useful feature.

I checked out your branch, but the generated output is not as expected.

1. Everything between the parentheses are set as type

/// @DtoCastType(MyType, my-package)
attributes  Json
/// @DtoCastType(OtherType, other-package, default)
otherAttributes  Json?
import { MyType } from 'my-package';
import OtherType from 'other-package';

export class Product {
  attributes: MyType, my-package;
  otherAttributes: OtherType, other-package, default | null;
}

2. Two default imports from the same package raises error

/// @DtoCastType(MyType, my-package, default)
attributes  Json
/// @DtoCastType(MyType, my-package, default)
otherAttributes  Json?
Error: Can not merge import statements; both statements have set the 'default' preoperty

This could be bypassed by setting any following appearance just to @DtoCastType(MyType), but this is not very intuitive.

@zackdotcomputer
Copy link
Author

Sorry @Brakebein for the insufficient QA. Fixed the bugs you reported and added some usage to your sample schema.prisma so I could validate myself that it was generating as-expected.

@Brakebein
Copy link
Owner

Can you please also test and fix the following cases with relative paths:

/// @DtoCastType(OtherType, ../types)
otherAttributes Json
/// @DtoCastType(OtherType, ../types, default)
otherAttributes Json

In my case, I get following output:

import { Prisma } from '@prisma/client';
import { MyType } from 'my-package';

export class Product {
  attributes: MyType, my-package;
  otherAttributes: Prisma.JsonValue;
}

I think it's beacuse the following RegEx doesn't contain any dots (and slashes):

const ANNOTATION_PARAMS_REGEX = /(?:\(([_A-Za-z0-9\-\, ]*)\))?/;

@zackdotcomputer
Copy link
Author

@Brakebein Done! Added a proper unit test for this new and old behavior with isAnnotatedWith as well

@Brakebein Brakebein merged commit 42a251b into Brakebein:main Jan 6, 2023
@Brakebein
Copy link
Owner

Thanks! It looks okay now.

@zackdotcomputer
Copy link
Author

Thank you @Brakebein - this package is a real help to us integrating Prisma into Nest at work and I'm happy to contribute back things we would find useful. Really appreciate you taking the time to both listen to the suggestions and help make my PRs better and more sound.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants