forked from adeyahya/prisma-typebox-generator
-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.ts
100 lines (97 loc) · 3.31 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { type Static, Type } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
const configSchema = Type.Object(
{
/**
* Where to output the generated files
*/
output: Type.String({ default: "./prisma/prismabox" }),
/**
* The name of the variable to import the Type from typebox
*/
typeboxImportVariableName: Type.String({ default: "Type" }),
/**
* The name of the dependency to import the Type from typebox
*/
typeboxImportDependencyName: Type.String({ default: "@sinclair/typebox" }),
/**
* Whether to allow additional properties in the generated schemes
*/
additionalProperties: Type.Boolean({ default: false }),
/**
* Should the input schemes be generated
*/
inputModel: Type.Boolean({ default: false }),
/**
* Prevents the ID field from being generated in the input model
*/
ignoreIdOnInputModel: Type.Boolean({ default: true }),
/**
* Prevents the createdAt field from being generated in the input model
*/
ignoreCreatedAtOnInputModel: Type.Boolean({ default: true }),
/**
* Prevents the updatedAt field from being generated in the input model
*/
ignoreUpdatedAtOnInputModel: Type.Boolean({ default: true }),
/**
* How the nullable union should be named
*/
nullableName: Type.String({ default: "__nullable__" }),
/**
* Whether to allow recursion in the generated schemes (enabling reduces code size)
*/
allowRecursion: Type.Boolean({ default: true }),
/**
* Additional fields to add to the generated schemes (must be valid strings in the context of usage)
* @example
* ```prisma
* generator prismabox {
provider = "node ./dist/cli.js"
inputModel = true
output = "./generated/schema"
additionalFieldsPlain = ["additional: Type.Optional(Type.String())"]
}
```
*/
additionalFieldsPlain: Type.Optional(Type.Array(Type.String())),
/**
* How the transform date type should be named
*/
transformDateName: Type.String({ default: "__transformDate__" }),
/**
* When enabled, this option ensures that only primitive types are generated as JSON types.
* This ensures compatibility with tooling that only supports the serilization to JSON primitive types.
*
* E.g. Date will be of Type String when enabled.
*/
useJsonTypes: Type.Union([Type.Boolean(), Type.Literal("transformer")], {
default: false,
}),
/**
* What file extension, if any, to add to src file imports. Set to ".js" to support nodenext module resolution
*/
importFileExtension: Type.String({ default: "" }),
/**
* The prefix to add to exported types
*/
exportedTypePrefix: Type.String({ default: "" }),
},
{ additionalProperties: false },
);
// biome-ignore lint/suspicious/noExplicitAny: we want to set the default value
let config: Static<typeof configSchema> = {} as unknown as any;
export function setConfig(input: unknown) {
try {
Value.Clean(configSchema, input);
Value.Default(configSchema, input);
config = Value.Decode(configSchema, Value.Convert(configSchema, input));
Object.freeze(config);
} catch (error) {
console.error(Value.Errors(configSchema, input).First);
throw error;
}
}
export function getConfig() {
return config;
}