Skip to content

Custom type annotation #29

@anotheri

Description

@anotheri

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions