Skip to content

Commit

Permalink
Bump version, update dependencies
Browse files Browse the repository at this point in the history
- Bump the version number to 1.5.0.
- Update devDependencies to latest versions.
- Switch to flat config for eslint.
- Change @prisma/generator-helper dependency version to "^5.0.0" since
  this generator should work with any 5.x version of that package.
- Remove second paragraph in the "Motivation" section of the readme, I
  felt it was unnecessary.
- Fix mistake in the "decimalType" description in the readme.
- Add test for the "object" enum type with prefix and suffix.
  • Loading branch information
mogzol committed Aug 27, 2024
1 parent 437b8aa commit 172a70f
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 551 deletions.
6 changes: 6 additions & 0 deletions .husky/install.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Skip Husky install in production and CI
if (process.env.NODE_ENV === "production" || process.env.CI === "true") {
process.exit(0);
}
const husky = (await import("husky")).default;
console.log(husky());
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "always"
},
"cSpell.words": ["datamodel", "datasource", "dmmf"],
"[prisma]": {
Expand Down
40 changes: 19 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

While Prisma client's generated types are sufficient for most use cases, there are some some scenarios where using them is not convenient or possible, due to the fact that they rely on both the `@prisma/client` package and on the client generated from your Prisma schema. That is where this generator comes into play. It generates a zero-dependency Typescript file containing type definitions for all your models. Zero-dependency in this case means that the file does not import any other packages, and can be used standalone in any Typescript app. By default the definitions are [type compatible](https://www.typescriptlang.org/docs/handbook/type-compatibility.html) with the Prisma client types, however this can be customized via the [options](#options), see below for more info.

As example of why this is useful, say you have an API which uses Prisma and responds with models from your DB, and you want to create a DTO package with Typescript definitions for all the data your API returns. You wouldn't really want to include your entire generated Prisma client in that package, and you don't want to require users to install `@prisma/client` just to use your DTO. So instead, you can use this generator to create a zero-dependency Typescript file containing definitions for all your models, and then use that in your DTO package.

## Usage

To use this generator, first install the package:
Expand Down Expand Up @@ -46,25 +44,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" \| "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. |
| **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 the Prisma client, `"enum"` will create TypeScript enums, `"stringUnion"` will create a string union type. |
| 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 `valueOf()` 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
4 changes: 2 additions & 2 deletions generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const SCALAR_TYPE_GETTERS: Record<string, (config: Config) => string> = {

// Since we want the output to have zero dependencies, define custom types which are compatible
// with the actual Prisma types. If users need the real Prisma types, they can cast to them.
const CUSTOM_TYPES: Record<string, string> = {
const CUSTOM_TYPES = {
BufferObject: 'type BufferObject = { type: "Buffer"; data: number[] };',
Decimal: "type Decimal = { valueOf(): string };",
JsonValue:
Expand Down Expand Up @@ -235,7 +235,7 @@ generatorHandler({
let prettier: typeof import("prettier");
try {
prettier = await import("prettier");
} catch (e) {
} catch {
throw new Error("Unable import Prettier. Is it installed?");
}

Expand Down
Loading

0 comments on commit 172a70f

Please sign in to comment.