Skip to content
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

Add enumType option that matches Prisma client's enum definitions #6

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ Note that `bigint` types don't have a default `toJSON` method, so the above assu

## Options

| **Option** | **Type** | **Default** | **Description** |
| ----------------- | :----------------------------------------------------: | :------------------------------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| output | `string` | `"interfaces.ts"` | The output location for the generated Typescript interfaces. |
| enumPrefix | `string` | `""` | Prefix to add to enum types. |
| enumSuffix | `string` | `""` | Suffix to add to enum types. |
| modelPrefix | `string` | `""` | Prefix to add to model types. |
| modelSuffix | `string` | `""` | Suffix to add to model types. |
| 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"`. |
| modelType | `"interface" \| "type"` | `"interface"` | Controls how model definitions are generated. `"interface"` will create Typescript interfaces, `"type"` will create Typescript types. If using mongodb, this also affects `type` definitions. |
| 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. |
| decimalType | `"Decimal" \| "string" \| "number"` | `"Decimal"` | The type to use for Decimal model fields. The `Decimal` type here is just an interface with a `getValue()` function. You will need to cast to an actual Decimal type if you want to use other methods. |
| bytesType | `"Buffer" \| "BufferObject" \| "string" \| "number[]"` | `"Buffer"` | The type to use for Bytes model fields. `BufferObject` is a type definition which matches the output of `Buffer.toJSON()`, which is called when running `JSON.stringify()` on a Buffer. |
| optionalRelations | `boolean` | `true` | Controls whether model relation fields are optional. If `true`, all model relation fields will use `?:` in the field definition. |
| omitRelations | `boolean` | `false` | Controls whether model relation fields are omitted. If `true`, model definitions will not include their relations. |
| prettier | `boolean` | `false` | Formats the output using Prettier. Setting this to `true` requires that the `prettier` package is available. |
| **Option** | **Type** | **Default** | **Description** |
| ----------------- | :----------------------------------------------------: | :------------------------------------------------------------------------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| output | `string` | `"interfaces.ts"` | The output location for the generated Typescript interfaces. |
| enumPrefix | `string` | `""` | Prefix to add to enum types. |
| enumSuffix | `string` | `""` | Suffix to add to enum types. |
| modelPrefix | `string` | `""` | Prefix to add to model types. |
| modelSuffix | `string` | `""` | Suffix to add to model types. |
| 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"`. |
| modelType | `"interface" \| "type"` | `"interface"` | Controls how model definitions are generated. `"interface"` will create Typescript interfaces, `"type"` will create Typescript types. If using mongodb, this also affects `type` definitions. |
| enumType | `"stringUnion" \| "enum" \| "object"` | `"stringUnion"` | Controls how enums are generated. `"object"` will create an object and type like Prisma client, `"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. |
| decimalType | `"Decimal" \| "string" \| "number"` | `"Decimal"` | The type to use for Decimal model fields. The `Decimal` type here is just an interface with a `getValue()` function. You will need to cast to an actual Decimal type if you want to use other methods. |
| bytesType | `"Buffer" \| "BufferObject" \| "string" \| "number[]"` | `"Buffer"` | The type to use for Bytes model fields. `BufferObject` is a type definition which matches the output of `Buffer.toJSON()`, which is called when running `JSON.stringify()` on a Buffer. |
| optionalRelations | `boolean` | `true` | Controls whether model relation fields are optional. If `true`, all model relation fields will use `?:` in the field definition. |
| omitRelations | `boolean` | `false` | Controls whether model relation fields are omitted. If `true`, model definitions will not include their relations. |
| prettier | `boolean` | `false` | Formats the output using Prettier. Setting this to `true` requires that the `prettier` package is available. |

## Example

Expand Down
9 changes: 7 additions & 2 deletions generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Config {
typeSuffix: string;
headerComment: string;
modelType: "interface" | "type";
enumType: "stringUnion" | "enum";
enumType: "stringUnion" | "enum" | "object";
dateType: "Date" | "string" | "number";
bigIntType: "bigint" | "string" | "number";
decimalType: "Decimal" | "string" | "number";
Expand Down Expand Up @@ -52,7 +52,7 @@ function validateConfig(config: Config) {
if (!["interface", "type"].includes(config.modelType)) {
errors.push(`Invalid modelType: ${config.modelType}`);
}
if (!["stringUnion", "enum"].includes(config.enumType)) {
if (!["stringUnion", "enum", "object"].includes(config.enumType)) {
errors.push(`Invalid enumType: ${config.enumType}`);
}
if (!["Date", "string", "number"].includes(config.dateType)) {
Expand Down Expand Up @@ -87,6 +87,11 @@ function getEnumTs(
const enumValues = enumData.values.map(({ name }) => `"${name}"`).join(" | ");
return `export type ${enumNameMap.get(enumData.name)} = ${enumValues};`;
}
case "object": {
const enumValues = enumData.values.map(({ name }) => ` ${name}: "${name}"`).join(",\n");
const enumName = enumNameMap.get(enumData.name);
return `export const ${enumName} = {\n${enumValues}\n};\n\nexport type ${enumName} = (typeof ${enumName})[keyof typeof ${enumName}];`;
}
default:
throw new Error(`Unknown enumType: ${config.enumType}`);
}
Expand Down
80 changes: 80 additions & 0 deletions tests/options-behavior/expected/enumTypeObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This file was auto-generated by prisma-generator-typescript-interfaces

export const Gender = {
Male: "Male",
Female: "Female",
Other: "Other"
};

export type Gender = (typeof Gender)[keyof typeof Gender];

export const DataTest = {
Apple: "Apple",
Banana: "Banana",
Orange: "Orange",
Pear: "Pear"
};

export type DataTest = (typeof DataTest)[keyof typeof DataTest];

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

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

export interface 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 @@ -97,6 +97,12 @@ generator enumTypeSuffixPrefix {
enumSuffix = "Enum"
}

generator enumTypeObject {
provider = "node --loader ts-node/esm generator.ts"
output = "enumTypeObject.ts"
enumType = "object"
}

generator modelType {
provider = "node --loader ts-node/esm generator.ts"
output = "modelType.ts"
Expand Down
Loading