@lastshotlabs/bunshot 0.0.18 → 0.0.19
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Schema } from "mongoose";
|
|
1
|
+
import type { Schema as SchemaType } from "mongoose";
|
|
2
2
|
type ZodSchema = any;
|
|
3
3
|
export type ZodToMongooseRefConfig = {
|
|
4
4
|
/** DB field name (e.g., "account") */
|
|
@@ -14,7 +14,7 @@ export type ZodToMongooseConfig = {
|
|
|
14
14
|
/** Override Mongoose type for specific fields (e.g., { date: { type: Date, required: true } }) */
|
|
15
15
|
typeOverrides?: Record<string, unknown>;
|
|
16
16
|
/** Subdocument array fields: { items: mongooseSubSchema } */
|
|
17
|
-
subdocSchemas?: Record<string,
|
|
17
|
+
subdocSchemas?: Record<string, SchemaType>;
|
|
18
18
|
};
|
|
19
19
|
/**
|
|
20
20
|
* Derive a Mongoose SchemaDefinition from a Zod object schema.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mongoose } from "./mongo";
|
|
2
2
|
/** Unwrap nullable, optional, and default wrappers to get the core Zod type */
|
|
3
3
|
function unwrap(zodType) {
|
|
4
4
|
let t = zodType;
|
|
@@ -22,6 +22,10 @@ function unwrap(zodType) {
|
|
|
22
22
|
}
|
|
23
23
|
return { core: t, required };
|
|
24
24
|
}
|
|
25
|
+
/** Lazily access the Mongoose Schema class (avoids top-level require of mongoose) */
|
|
26
|
+
function getSchema() {
|
|
27
|
+
return mongoose.Schema;
|
|
28
|
+
}
|
|
25
29
|
/** Convert a single Zod type to a Mongoose field definition */
|
|
26
30
|
function toMongooseField(zodType) {
|
|
27
31
|
const { core, required } = unwrap(zodType);
|
|
@@ -36,7 +40,7 @@ function toMongooseField(zodType) {
|
|
|
36
40
|
return { type: Date, required };
|
|
37
41
|
if (defType === "enum")
|
|
38
42
|
return { type: String, enum: core.options, required };
|
|
39
|
-
return { type:
|
|
43
|
+
return { type: getSchema().Types.Mixed, required };
|
|
40
44
|
}
|
|
41
45
|
/**
|
|
42
46
|
* Derive a Mongoose SchemaDefinition from a Zod object schema.
|
|
@@ -64,7 +68,7 @@ export function zodToMongoose(zodSchema, config = {}) {
|
|
|
64
68
|
continue;
|
|
65
69
|
if (config.refs?.[apiField]) {
|
|
66
70
|
const { dbField, ref } = config.refs[apiField];
|
|
67
|
-
fields[dbField] = { type:
|
|
71
|
+
fields[dbField] = { type: getSchema().Types.ObjectId, ref, required: true };
|
|
68
72
|
continue;
|
|
69
73
|
}
|
|
70
74
|
if (config.typeOverrides?.[apiField]) {
|
package/package.json
CHANGED