Skip to content

Commit 2d28b35

Browse files
committed
refactor: rename additionalImports to filePrefix
1 parent fa25a77 commit 2d28b35

File tree

6 files changed

+30
-31
lines changed

6 files changed

+30
-31
lines changed

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,28 @@ without losing the safety of the TypeScript type system?
3939
fileName = "types.ts"
4040
// Optionally generate runtime enums to a separate file
4141
enumFileName = "enums.ts"
42-
// Optionally include a custom import section (single or multi-line)
43-
additionalImports = "import Decimal from 'decimal.js';"
42+
// Optionally add content at the start of generated files (imports, pragmas, comments, etc.)
43+
filePrefix = "import type { Decimal } from 'decimal.js';"
4444
// For multi-line, use Prisma's triple-quoted string syntax (see below)
4545
}
4646
```
4747

4848
3. Run `prisma migrate dev` or `prisma generate` and use your freshly generated
4949
types when instantiating Kysely!
5050

51-
### Advanced Import Configuration
51+
### File Prefix Configuration
5252

53-
The `additionalImports` option supports full import sections, allowing you to import multiple libraries with different syntaxes:
53+
The `filePrefix` option allows you to add custom content at the start of generated files. This is particularly useful for importing custom types and libraries:
5454

5555
```prisma
5656
generator kysely {
5757
provider = "prisma-kysely"
5858
output = "../src/db"
5959
fileName = "types.ts"
6060
61-
// Import multiple libraries with different syntaxes
62-
additionalImports = """
63-
import Decimal from 'decimal.js';
61+
// Add custom imports for libraries you need in generated types
62+
filePrefix = """
63+
import type { Decimal } from 'decimal.js';
6464
import { Big } from 'big.js';
6565
import * as moment from 'moment';
6666
import { v4 as uuid } from 'uuid';
@@ -72,14 +72,13 @@ import type { SomeType } from './custom-types';
7272
This will generate a file that starts with:
7373

7474
```typescript
75-
import { Big } from "big.js";
76-
import Decimal from "decimal.js";
77-
import type { ColumnType } from "kysely";
78-
import * as moment from "moment";
79-
import { v4 as uuid } from "uuid";
80-
81-
import type { SomeType } from "./custom-types";
75+
import type { Decimal } from 'decimal.js';
76+
import { Big } from 'big.js';
77+
import * as moment from 'moment';
78+
import { v4 as uuid } from 'uuid';
79+
import type { SomeType } from './custom-types';
8280

81+
import type { ColumnType } from "kysely";
8382
// ... rest of generated types
8483
```
8584

@@ -119,7 +118,7 @@ hope it's just as useful for you! 😎
119118
| `enumFileName` | The filename for the generated enums. Omitting this will generate enums and files in the same file. | |
120119
| `camelCase` | Enable support for Kysely's camelCase plugin | `false` |
121120
| `exportWrappedTypes` | Kysely wrapped types such as `Selectable<Model>` are also exported as described in the [Kysely documentation](https://kysely.dev/docs/getting-started#types). The exported types follow the naming conventions of the document. | `false` |
122-
| `additionalImports` | A full import section to prepend to the generated file. Supports any TS import syntax (default, named, namespace, renamed, and `import type`). Single-line strings or multi-line via Prisma triple-quoted strings (`""" ... """`). The content is inserted verbatim; no sorting or deduplication is performed. | |
121+
| `filePrefix` | Content to prepend to the start of generated file(s). Useful for custom imports, pragma directives (e.g., `// @ts-nocheck`), comments, or any other content. Supports single-line strings or multi-line via Prisma triple-quoted strings (`""" ... """`). The content is inserted verbatim at the top of the file(s). | |
123122
| `readOnlyIds` | Use Kysely's `GeneratedAlways` for `@id` fields with default values, preventing insert and update. | `false` |
124123
| `[typename]TypeOverride` | Allows you to override the resulting TypeScript type for any Prisma type. Useful when targeting a different environment than Node (e.g. WinterCG compatible runtimes that use UInt8Arrays instead of Buffers for binary types etc.) Check out the [config validator](https://github.com/valtyr/prisma-kysely/blob/main/src/utils/validateConfig.ts) for a complete list of options. | |
125124
| `dbTypeName` | Allows you to override the exported type with all tables | `DB` |

src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ generatorHandler({
107107
defaultSchema: config.defaultSchema,
108108
importExtension: config.importExtension,
109109
exportWrappedTypes: config.exportWrappedTypes,
110-
additionalImports: config.additionalImports,
110+
filePrefix: config.filePrefix,
111111
});
112112

113113
// And write it to a file!

src/helpers/generateFile.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ test("generates a file which imports Kysely wrapper types.", () => {
3737
);
3838
});
3939

40-
test("generates a file with single additional import when additionalImports is specified.", () => {
40+
test("generates a file with custom prefix when filePrefix is specified.", () => {
4141
const result = generateFile([], {
4242
withEnumImport: false,
4343
withLeader: true,
4444
exportWrappedTypes: false,
45-
additionalImports: "import Decimal from 'decimal.js';",
45+
filePrefix: "import Decimal from 'decimal.js';",
4646
});
4747
expect(result).toContain("import Decimal from 'decimal.js';");
4848
});
4949

50-
test("generates a file with multiple additional imports when additionalImports is specified.", () => {
50+
test("generates a file with multiple imports in filePrefix.", () => {
5151
const result = generateFile([], {
5252
withEnumImport: false,
5353
withLeader: true,
5454
exportWrappedTypes: false,
55-
additionalImports: `import Decimal from 'decimal.js';
55+
filePrefix: `import Decimal from 'decimal.js';
5656
import { Big } from 'big.js';
5757
import * as moment from 'moment';`,
5858
});
@@ -61,12 +61,12 @@ import * as moment from 'moment';`,
6161
expect(result).toContain("import * as moment from 'moment';");
6262
});
6363

64-
test("generates a file with named additional import when additionalImports is specified.", () => {
64+
test("generates a file with filePrefix containing renamed imports.", () => {
6565
const result = generateFile([], {
6666
withEnumImport: false,
6767
withLeader: true,
6868
exportWrappedTypes: false,
69-
additionalImports: "import { v4 as uuid } from 'uuid';",
69+
filePrefix: "import { v4 as uuid } from 'uuid';",
7070
});
7171
expect(result).toContain("import { v4 as uuid } from 'uuid';");
7272
});

src/helpers/generateFile.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ type Options = {
66
withEnumImport: false | { importPath: string; names: string[] };
77
withLeader: boolean;
88
exportWrappedTypes: boolean;
9-
additionalImports?: string;
9+
filePrefix?: string;
1010
};
1111

1212
export const generateFile = (
1313
statements: readonly ts.Statement[],
14-
{ withEnumImport, withLeader, exportWrappedTypes, additionalImports }: Options
14+
{ withEnumImport, withLeader, exportWrappedTypes, filePrefix }: Options
1515
) => {
1616
const file = ts.factory.createSourceFile(
1717
statements,
@@ -21,7 +21,7 @@ export const generateFile = (
2121

2222
const result = printer.printFile(file);
2323

24-
const leader = `${additionalImports ? `${additionalImports}\n` : ""}import type { ColumnType${
24+
const leader = `${filePrefix ? `${filePrefix}\n` : ""}import type { ColumnType${
2525
result.includes("GeneratedAlways") ? ", GeneratedAlways" : ""
2626
}${
2727
exportWrappedTypes ? ", Insertable, Selectable, Updateable" : ""

src/helpers/generateFiles.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function generateFiles(opts: {
2525
defaultSchema: string;
2626
importExtension: string;
2727
exportWrappedTypes: boolean;
28-
additionalImports?: string;
28+
filePrefix?: string;
2929
}) {
3030
const models = opts.models.map(
3131
({ definition, ...rest }: ModelType): MultiDefsModelType => ({
@@ -55,7 +55,7 @@ export function generateFiles(opts: {
5555
withEnumImport: false,
5656
withLeader: true,
5757
exportWrappedTypes: opts.exportWrappedTypes,
58-
additionalImports: opts.additionalImports,
58+
filePrefix: opts.filePrefix,
5959
}),
6060
};
6161

@@ -73,7 +73,7 @@ export function generateFiles(opts: {
7373
},
7474
withLeader: true,
7575
exportWrappedTypes: opts.exportWrappedTypes,
76-
additionalImports: opts.additionalImports,
76+
filePrefix: opts.filePrefix,
7777
}
7878
),
7979
};
@@ -88,7 +88,7 @@ export function generateFiles(opts: {
8888
withEnumImport: false,
8989
withLeader: false,
9090
exportWrappedTypes: opts.exportWrappedTypes,
91-
additionalImports: opts.additionalImports,
91+
filePrefix: opts.filePrefix,
9292
}
9393
),
9494
};

src/utils/validateConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export const configValidator = z
5757
// Export Kysely wrapped types such as `Selectable<Model>`
5858
exportWrappedTypes: booleanStringLiteral.default(false),
5959

60-
// Import any additional libraries. Set to the full import section you want (e.g., "import Decimal from 'decimal.js';", "import { Big } from 'big.js';", multiple imports separated by newlines, etc.)
61-
additionalImports: z.string().optional(),
60+
// Content to prepend to the start of the generated file(s). Useful for custom imports, pragmas, or comments.
61+
filePrefix: z.string().optional(),
6262
})
6363
.strict()
6464
.transform((config) => {

0 commit comments

Comments
 (0)