Skip to content

Custom type annotation #29

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

Closed
anotheri opened this issue Jan 29, 2025 · 5 comments
Closed

Custom type annotation #29

anotheri opened this issue Jan 29, 2025 · 5 comments

Comments

@anotheri
Copy link

anotheri commented Jan 29, 2025

Now it's possible to redefine of existing default types as @m1212e shown in #25 (comment).

But instead of overriding the default type mapping for all the strings for example (or all the dates, whatever), i'd like to be able to define a new type with custom transformation and use it just for a couple of fields in Prisma schema and keep the default type mapping for other fields.

E.g. if i extend Type with new type:

import { Type as NativeType } from "@sinclair/typebox";

// e.g. in DB it's stored as string | null but API returns it as integer
const StringifiedInt = NativeType.Transform(NativeType.Integer())
  .Decode((value) => (value === 0 ? null : toString(value)))
  .Encode((value) => parseInt(value || '0', 10));

export const Type = {
  ...NativeType,
  StringifiedInt,
};

and then i'd like to apply this new custom type to a Prisma schema field via annotation:

model myModel {
  /// @prismabox.type=StringifiedInt
  myCustomField String? @db.TinyText
}

It's supposed to be quite useful for BigInt type fields which are still not supported good enough in JSON and require transformation to string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json

Thanks in advance!

@anotheri anotheri changed the title Custom types annotation Custom type annotation Jan 29, 2025
@m1212e
Copy link
Owner

m1212e commented Jan 29, 2025

Hey, please see https://www.npmjs.com/package/prismabox/v/1.1.18-prerelease.0

I released a version under the @dev tag which you can install via npm. Please see if this is what you need!

@anotheri
Copy link
Author

anotheri commented Jan 30, 2025

@m1212e on my codebase, i've noticed an issue with applying __nullable__ to typeOverwrite.

It's exactly the same StringifiedInt example i've mentioned above. So to clarify I'd expect that __nullable__ to be applied to a type inside the Transform but not to a whole StringifiedInt type. Although it might be quite complicated case for codegen, so it's probably better to bypass wrapping into __nullable__ for custom (overriten) types either as a default behaviour or optionally via another annotation (both options look good for me).

And other than this case, it works exactly as i'd expected. Great and really helpful! And thank you a lot for a quick reaction! 🙏

@m1212e
Copy link
Owner

m1212e commented Jan 30, 2025

Would you mind giving a full/more specific code example on that scenario?

@m1212e
Copy link
Owner

m1212e commented Mar 4, 2025

Hey, I'll close this for now, feel free to re-open if you want to continue on this!

@m1212e m1212e closed this as completed Mar 4, 2025
@anotheri
Copy link
Author

@m1212e sure! Unfortunately, i had no time to take a look into that branch recently, but I'll try with latest version soon and share an example here if something still doesn't seem right. And thanks for your support!

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

No branches or pull requests

2 participants