Skip to content

Commit

Permalink
✨ feat: allow for more precise selection of transformer feature
Browse files Browse the repository at this point in the history
  • Loading branch information
m1212e committed Nov 18, 2024
1 parent 9e90b83 commit f7b1405
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/generators/primitiveField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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})`;
}

Expand Down

0 comments on commit f7b1405

Please sign in to comment.