-
Notifications
You must be signed in to change notification settings - Fork 122
/
generators.ts
139 lines (126 loc) · 4.47 KB
/
generators.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { ORMType } from "../../../../../types.js";
import { Schema } from "../../../types.js";
import {
ZodMapping,
formatTableName,
getNonStringFields,
getZodMappings,
toCamelCase,
} from "../../../utils.js";
const createInsertZodSchema = (
schema: Schema,
orm: ORMType,
zodMappings: ZodMapping[]
) => {
const {
tableNameSingularCapitalised,
tableNameCamelCase,
tableNameSingular,
} = formatTableName(schema.tableName);
const baseSchema = `const baseSchema = ${
orm === "drizzle"
? `createSelectSchema(${tableNameCamelCase})`
: `${tableNameSingular}Schema`
}${schema.includeTimestamps ? ".omit(timestamps)" : ""}`;
const insertSchema = `export const insert${tableNameSingularCapitalised}Schema = ${
orm === "drizzle"
? `createInsertSchema(${tableNameCamelCase})${
schema.includeTimestamps ? ".omit(timestamps)" : ""
}`
: `baseSchema.omit({ id: true })`
};`;
const insertParams = `export const insert${tableNameSingularCapitalised}Params = ${
orm === "drizzle" ? `baseSchema.extend(` : `baseSchema.extend(`
}{${
zodMappings.length > 0
? `\n ${zodMappings
.map(
(field) =>
`${toCamelCase(field.name)}: z.coerce.${field.type}()${
field.type === "string" ? ".min(1)" : ""
}`
)
.join(`,\n `)}\n`
: ""
}}).omit({
id: true${schema.belongsToUser ? ",\n userId: true" : ""}
});`;
return `${baseSchema}\n\n${insertSchema}\n${insertParams}\n`;
};
const createUpdateZodSchema = (
schema: Schema,
orm: ORMType,
zodMappings: ZodMapping[]
) => {
const {
tableNameSingular,
tableNameCamelCase,
tableNameSingularCapitalised,
} = formatTableName(schema.tableName);
const updateSchema = `export const update${tableNameSingularCapitalised}Schema = ${
orm === "drizzle" ? `baseSchema` : `baseSchema`
};`;
const updateParams = `export const update${tableNameSingularCapitalised}Params = ${
orm === "drizzle"
? `baseSchema.extend(`
: `update${tableNameSingularCapitalised}Schema.extend(`
}{${
zodMappings.length > 0
? `\n ${zodMappings
.map(
(field) =>
`${toCamelCase(field.name)}: z.coerce.${field.type}()${
field.type === "string" ? ".min(1)" : ""
}`
)
.join(`,\n `)}\n`
: ""
}})${
schema.belongsToUser
? `.omit({
userId: true
});`
: ""
}`;
return `${updateSchema}\n${updateParams}`;
};
const createIdZodSchema = (schema: Schema) => {
const { tableNameSingular, tableNameSingularCapitalised } = formatTableName(
schema.tableName
);
return `export const ${tableNameSingular}IdSchema = baseSchema.pick({ id: true });`;
};
const createTypesForSchema = (schema: Schema, orm: ORMType) => {
const {
tableNameSingularCapitalised,
tableNameCamelCase,
tableNameCapitalised,
tableNameSingular,
} = formatTableName(schema.tableName);
return `// Types for ${tableNameCamelCase} - used to type API request params and within Components
export type ${tableNameSingularCapitalised} = ${
orm === "drizzle"
? `typeof ${tableNameCamelCase}.$inferSelect`
: `z.infer<typeof ${tableNameSingular}Schema>`
};
export type New${tableNameSingularCapitalised} = z.infer<typeof insert${tableNameSingularCapitalised}Schema>;
export type New${tableNameSingularCapitalised}Params = z.infer<typeof insert${tableNameSingularCapitalised}Params>;
export type Update${tableNameSingularCapitalised}Params = z.infer<typeof update${tableNameSingularCapitalised}Params>;
export type ${tableNameSingularCapitalised}Id = z.infer<typeof ${tableNameSingular}IdSchema>["id"];
// this type infers the return from get${tableNameCapitalised}() - meaning it will include any joins
export type Complete${tableNameSingularCapitalised} = Awaited<ReturnType<typeof get${tableNameCapitalised}>>["${tableNameCamelCase}"][number];
`;
};
export const createZodSchemas = (schema: Schema, orm: ORMType) => {
const { fields, tableName } = schema;
// get non string fields
const nonStringFields = getNonStringFields(fields);
const zodMappings = getZodMappings(nonStringFields);
const { tableNameCamelCase } = formatTableName(tableName);
return `// Schema for ${tableNameCamelCase} - used to validate API requests
${createInsertZodSchema(schema, orm, zodMappings)}
${createUpdateZodSchema(schema, orm, zodMappings)}
${createIdZodSchema(schema)}
${createTypesForSchema(schema, orm)}
`;
};