Skip to content

Commit

Permalink
Add headerComment option, consolidate tests & use ts-node
Browse files Browse the repository at this point in the history
Adds a header comment to generated output, configurable with the new
headerComment option.

Also consolidates many of the tests by using multiple generator blocks
within a single schema file. The tests also now run using ts-node, and
test.ts no longer needs to be built to run tests. Because of this, the
tsconfig has also been split into two files. The base tsconfig.json
targets all ts files, so that type checking in the editor and running
with ts-node works properly. The tsconfig.build.json is used when
building the generator, and targets only generator.ts.
  • Loading branch information
mogzol committed Nov 18, 2023
1 parent 0727026 commit 7cf6719
Show file tree
Hide file tree
Showing 42 changed files with 727 additions and 347 deletions.
1 change: 1 addition & 0 deletions .github/workflows/npm-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ jobs:
cache: "npm"
- run: npm ci
- run: npm run lint
- run: npm run build
- run: npm test
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@ 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). |
| 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"` | `"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"` | `"Decimal"` | The type to use for Decimal model fields. Note that 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 or not. If `true`, all model relation fields will use `?:` in the field definition. |
| prettier | `boolean` | `false` | Formats the output using Prettier. Setting this to `true` requires that the `prettier` package is available. [Prettier settings files](https://prettier.io/docs/en/configuration.html) will be respected. |
| **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"`. |
| 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"` | `"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"` | `"Decimal"` | The type to use for Decimal model fields. Note that 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 or not. If `true`, all model relation fields will use `?:` in the field definition. |
| prettier | `boolean` | `false` | Formats the output using Prettier. Setting this to `true` requires that the `prettier` package is available. [Prettier settings files](https://prettier.io/docs/en/configuration.html) will be respected. |

## Example

Expand Down Expand Up @@ -169,9 +170,11 @@ model Data {
#### Output

<details>
<summary>src/interfaces.ts</summary>
<summary>src/dto/interfaces.ts</summary>

```typescript
// This file was auto-generated by prisma-generator-typescript-interfaces

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

export interface RelationA {
Expand Down Expand Up @@ -243,9 +246,11 @@ type JsonValue =
</details>

<details>
<summary>src/json-interfaces.ts</summary>
<summary>src/dto/json-interfaces.ts</summary>

```typescript
// This file was auto-generated by prisma-generator-typescript-interfaces

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

export interface RelationAJson {
Expand Down Expand Up @@ -326,20 +331,14 @@ All the code for this generator is contained within the `generator.ts` file. You

### Tests

You can run tests with `npm run test`. Tests are run using a custom script, see `test.ts` for details. You can add new tests by placing a Prisma schema and the expected output in a folder under the `tests` directory, you may want to look at the `tests/no-options` test as an example.
You can run tests with `npm run test`. Tests are run using a custom script, see `test.ts` for details. You can add new tests by placing a Prisma schema and the expected output in a folder under the `tests` directory, you may want to look at the `tests/options-behavior` test as an example.

You can run specific tests by passing them as arguments to the test command:

```
npm run test -- buffer-array-type mongo-types required-relations
npm run test -- options-behavior validation-errors
```

When a test fails, you can see the generated output in the `__TEST_TMP__` folder inside that test's directory. Compare this with the expected output to see why it failed.

By default the test runner will quit when it encounters it's first failure. If you want it to continue after failures, use the `-c` (or `--continue`) option:

```
npm run test -- -c
```

Please ensure all tests are passing and that the code is properly linted (`npm run lint`) before submitting a PR, thanks!
10 changes: 10 additions & 0 deletions generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Config {
modelSuffix: string;
typePrefix: string;
typeSuffix: string;
headerComment: string;
enumType: "stringUnion" | "enum";
dateType: "Date" | "string";
bigIntType: "bigint" | "string" | "number";
Expand Down Expand Up @@ -156,6 +157,7 @@ generatorHandler({
modelSuffix: "",
typePrefix: "",
typeSuffix: "",
headerComment: "This file was auto-generated by prisma-generator-typescript-interfaces",
enumType: "stringUnion",
dateType: "Date",
bigIntType: "bigint",
Expand Down Expand Up @@ -195,6 +197,14 @@ generatorHandler({

let ts = [...enumsTs, ...modelsTs, ...customTypesTs].join("\n\n") + "\n";

if (config.headerComment) {
const headerContent = config.headerComment
.split("\n")
.map((line) => `// ${line}`)
.join("\n");
ts = `${headerContent}\n\n${ts}`;
}

if (config.prettier) {
// Prettier is imported inside this if so that it's not a required dependency
let prettier: typeof import("prettier");
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "prisma-generator-typescript-interfaces",
"version": "1.0.1",
"version": "1.1.0",
"description": "Generate zero-dependency Typescript interfaces from Prisma schema",
"main": "generator.js",
"bin": {
"prisma-generator-typescript-interfaces": "generator.js"
},
"scripts": {
"build": "tsc",
"build": "tsc -p tsconfig.build.json",
"clean": "rimraf --glob *.js **/__TEST_TMP__",
"generate": "prisma generate --schema=prisma/example.prisma",
"lint": "prettier --check . && eslint . --ext .ts",
"lint:fix": "prettier --write . && eslint . --ext .ts --fix",
"test": "npm run build && node test.js",
"test": "node --loader ts-node/esm test.ts",
"prepare": "husky install"
},
"files": [
Expand Down
Loading

0 comments on commit 7cf6719

Please sign in to comment.