Skip to content

Commit

Permalink
Add definitionType option
Browse files Browse the repository at this point in the history
This option allows users to control whether the output definition are
typescript types or interfaces
  • Loading branch information
mogzol committed Dec 12, 2023
1 parent bc6eddc commit 2a89ba2
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Note that `bigint` types don't have a default `toJSON` method, so the above assu
| typePrefix | `string` | `""` | Prefix to add to [type](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-composite-types) types (mongodb only). |
| typeSuffix | `string` | `""` | Suffix to add to [type](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-composite-types) types (mongodb only). |
| headerComment | `string` | `"This file was auto-generated by prisma-generator-typescript-interfaces"` | Sets the header comment added to the top of the generated file. Set this to an empty string to disable the header comment. Supports multiple lines with `"\n"`. |
| definitionType | `"interface" \| "type"` | `"interface"` | Controls how the model definitions are generated. `"interface"` will create Typescript interfaces, `"type"` will create Typescript types. |
| enumType | `"stringUnion" \| "enum"` | `"stringUnion"` | Controls how enums are generated. `"enum"` will create Typescript enums, `"stringUnion"` will create union types with all the enum values. |
| dateType | `"Date" \| "string" \| "number"` | `"Date"` | The type to use for DateTime model fields. |
| bigIntType | `"bigint" \| "string" \| "number"` | `"bigint"` | The type to use for BigInt model fields. |
Expand Down
15 changes: 14 additions & 1 deletion generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Config {
typePrefix: string;
typeSuffix: string;
headerComment: string;
definitionType: "interface" | "type";
enumType: "stringUnion" | "enum";
dateType: "Date" | "string" | "number";
bigIntType: "bigint" | "string" | "number";
Expand Down Expand Up @@ -48,6 +49,9 @@ const CUSTOM_TYPES: Record<string, string> = {

function validateConfig(config: Config) {
const errors: string[] = [];
if (!["interface", "type"].includes(config.definitionType)) {
errors.push(`Invalid definitionType: ${config.definitionType}`);
}
if (!["stringUnion", "enum"].includes(config.enumType)) {
errors.push(`Invalid enumType: ${config.enumType}`);
}
Expand Down Expand Up @@ -143,7 +147,15 @@ function getModelTs(
.join("\n");

const name = modelNameMap.get(modelData.name) ?? typeNameMap.get(modelData.name);
return `export interface ${name} {\n${fields}\n}`;

switch (config.definitionType) {
case "interface":
return `export interface ${name} {\n${fields}\n}`;
case "type":
return `export type ${name} = {\n${fields}\n};`;
default:
throw new Error(`Unknown definitionType: ${config.definitionType}`);
}
}

generatorHandler({
Expand All @@ -163,6 +175,7 @@ generatorHandler({
typePrefix: "",
typeSuffix: "",
headerComment: "This file was auto-generated by prisma-generator-typescript-interfaces",
definitionType: "interface",
enumType: "stringUnion",
dateType: "Date",
bigIntType: "bigint",
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prisma-generator-typescript-interfaces",
"version": "1.2.0",
"version": "1.3.0",
"description": "Generate zero-dependency Typescript interfaces from Prisma schema",
"author": "Morgan Zolob",
"license": "MIT",
Expand Down
67 changes: 67 additions & 0 deletions tests/options-behavior/expected/typeDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file was auto-generated by prisma-generator-typescript-interfaces

export type Gender = "Male" | "Female" | "Other";

export type DataTest = "Apple" | "Banana" | "Orange" | "Pear";

export type Person = {
id: number;
name: string;
age: number;
email: string | null;
gender: Gender;
addressId: number;
address?: Address;
friends?: Person[];
friendsOf?: Person[];
data?: Data | null;
};

export type Address = {
id: number;
streetNumber: number;
streetName: string;
city: string;
isBilling: boolean;
people?: Person[];
};

export type Data = {
id: string;
stringField: string;
booleanField: boolean;
intField: number;
bigIntField: bigint;
floatField: number;
decimalField: Decimal;
dateField: Date;
jsonField: JsonValue;
bytesField: Buffer;
enumField: DataTest;
optionalStringField: string | null;
optionalBooleanField: boolean | null;
optionalIntField: number | null;
optionalBigIntField: bigint | null;
optionalFloatField: number | null;
optionalDecimalField: Decimal | null;
optionalDateField: Date | null;
optionalJsonField: JsonValue | null;
optionalBytesField: Buffer | null;
optionalEnumField: DataTest | null;
stringArrayField: string[];
booleanArrayField: boolean[];
intArrayField: number[];
bigIntArrayField: bigint[];
floatArrayField: number[];
decimalArrayField: Decimal[];
dateArrayField: Date[];
jsonArrayField: JsonValue[];
bytesArrayField: Buffer[];
enumArrayField: DataTest[];
personId: number;
person?: Person;
};

type Decimal = { valueOf(): string };

type JsonValue = string | number | boolean | { [key in string]?: JsonValue } | Array<JsonValue> | null;
6 changes: 6 additions & 0 deletions tests/options-behavior/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ generator enumTypeSuffixPrefix {
enumSuffix = "Enum"
}

generator typeDefinitions {
provider = "node --loader ts-node/esm generator.ts"
output = "typeDefinitions.ts"
definitionType = "type"
}

// ========================
// Prisma Schema
// ========================
Expand Down
1 change: 1 addition & 0 deletions tests/validation-errors/expected-error.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Error:
Invalid definitionType: bad
Invalid enumType: incorrect
Invalid dateType: wrong
Invalid bigIntType: values
Expand Down
14 changes: 7 additions & 7 deletions tests/validation-errors/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// ========================

generator typescriptInterfaces {
provider = "node --loader ts-node/esm generator.ts"
enumType = "incorrect"
dateType = "wrong"
bigIntType = "values"
decimalType = "go"
bytesType = "here"
prettier = "bad"
provider = "node --loader ts-node/esm generator.ts"
definitionType = "bad"
enumType = "incorrect"
dateType = "wrong"
bigIntType = "values"
decimalType = "go"
bytesType = "here"
}

// ========================
Expand Down

0 comments on commit 2a89ba2

Please sign in to comment.