diff --git a/src/config.ts b/src/config.ts index e025999..892f977 100644 --- a/src/config.ts +++ b/src/config.ts @@ -66,7 +66,9 @@ const configSchema = Type.Object( * * E.g. Date will be of Type String when enabled. */ - useJsonTypes: Type.Boolean({ default: false }), + useJsonTypes: Type.Union([Type.Boolean(), Type.Literal("transformer")], { + default: false, + }), /** * What file extension, if any, to add to src file imports. Set to ".js" to support nodenext module resolution */ @@ -76,7 +78,7 @@ const configSchema = Type.Object( */ exportedTypePrefix: Type.String({ default: "" }), }, - { additionalProperties: false }, + { additionalProperties: false } ); // biome-ignore lint/suspicious/noExplicitAny: we want to set the default value diff --git a/src/generators/primitiveField.ts b/src/generators/primitiveField.ts index 9772301..20d3efa 100644 --- a/src/generators/primitiveField.ts +++ b/src/generators/primitiveField.ts @@ -15,7 +15,7 @@ const PrimitiveFields = [ export type PrimitivePrismaFieldType = (typeof PrimitiveFields)[number]; export function isPrimitivePrismaFieldType( - str: string, + str: string ): str is PrimitivePrismaFieldType { // biome-ignore lint/suspicious/noExplicitAny: we want to check if the string is a valid primitive field return PrimitiveFields.includes(str as any); @@ -42,10 +42,20 @@ export function stringifyPrimitiveType({ if (["DateTime"].includes(fieldType)) { const config = getConfig(); - if (config.useJsonTypes) { + if (config.useJsonTypes === "transformer") { return `${getConfig().transformDateName}(${options})`; } + if (config.useJsonTypes) { + let opts = options; + if (opts.includes("{") && opts.includes("}")) { + opts = opts.replace("{", "{ format: 'date-time', "); + } else { + opts = `{ format: 'date-time' }`; + } + return `${config.typeboxImportVariableName}.String(${opts})`; + } + return `${getConfig().typeboxImportVariableName}.Date(${options})`; }