@supernova-studio/model 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +17 -17
- package/dist/index.d.ts +17 -17
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1142 -1139
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/helpers/db.ts +59 -0
package/package.json
CHANGED
package/src/helpers/db.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UnknownKeysParam,
|
|
3
|
+
ZodNullable,
|
|
4
|
+
ZodObject,
|
|
5
|
+
ZodObjectDef,
|
|
6
|
+
ZodOptional,
|
|
7
|
+
ZodRawShape,
|
|
8
|
+
ZodSchema,
|
|
9
|
+
ZodTypeAny,
|
|
10
|
+
objectInputType,
|
|
11
|
+
objectOutputType,
|
|
12
|
+
z,
|
|
13
|
+
} from "zod";
|
|
14
|
+
|
|
1
15
|
export type DbCreateInputOmit<T> = Omit<T, "id" | "createdAt" | "updatedAt">;
|
|
2
16
|
|
|
3
17
|
export type DbUpdateInputOmit<T> = Omit<T, "createdAt" | "updatedAt" | "persistentId">;
|
|
@@ -35,3 +49,48 @@ export function zodUpdateInputOmit() {
|
|
|
35
49
|
persistentId: true,
|
|
36
50
|
} as const;
|
|
37
51
|
}
|
|
52
|
+
|
|
53
|
+
function zodCreate<
|
|
54
|
+
T extends ZodRawShape,
|
|
55
|
+
UnknownKeys extends UnknownKeysParam = UnknownKeysParam,
|
|
56
|
+
Catchall extends ZodTypeAny = ZodTypeAny,
|
|
57
|
+
O = objectOutputType<T, Catchall, UnknownKeys>,
|
|
58
|
+
I = objectInputType<T, Catchall, UnknownKeys>
|
|
59
|
+
>(schema: ZodObject<T, UnknownKeysParam, ZodTypeAny, I, O>) {
|
|
60
|
+
return schema.omit(zodCreateInputOmit());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Don't use it yet, produces too complex type
|
|
65
|
+
*/
|
|
66
|
+
function zodUpdate<
|
|
67
|
+
T extends ZodRawShape,
|
|
68
|
+
UnknownKeys extends UnknownKeysParam = UnknownKeysParam,
|
|
69
|
+
Catchall extends ZodTypeAny = ZodTypeAny,
|
|
70
|
+
O = objectOutputType<T, Catchall, UnknownKeys>,
|
|
71
|
+
I = objectInputType<T, Catchall, UnknownKeys>
|
|
72
|
+
>(schema: ZodObject<T, UnknownKeysParam, ZodTypeAny, I, O>) {
|
|
73
|
+
const hue = schema.omit(zodUpdateInputOmit());
|
|
74
|
+
return makeOptionalPropsNullable(hue).partial();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function makeOptionalPropsNullable<Schema extends z.AnyZodObject>(schema: Schema) {
|
|
78
|
+
type SchemaType = typeof schema;
|
|
79
|
+
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
81
|
+
const entries = Object.entries(schema.shape) as [keyof SchemaType["shape"], z.ZodTypeAny][];
|
|
82
|
+
const newProps = entries.reduce(
|
|
83
|
+
(acc, [key, value]) => {
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
85
|
+
acc[key] = value instanceof z.ZodOptional ? value.unwrap().nullable() : value;
|
|
86
|
+
return acc;
|
|
87
|
+
},
|
|
88
|
+
{} as {
|
|
89
|
+
[key in keyof SchemaType["shape"]]: SchemaType["shape"][key] extends z.ZodOptional<infer T>
|
|
90
|
+
? z.ZodNullable<T>
|
|
91
|
+
: SchemaType["shape"][key];
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return z.object(newProps);
|
|
96
|
+
}
|