···22import { ObjectId } from "mongodb";
33import {
44 connect,
55- defineModel,
65 disconnect,
76 type InferModel,
87 type InsertType,
···109} from "../mod.ts";
11101211// 1. Define your schema using Zod
1313-const userSchema = defineModel(z.object({
1212+const userSchema = z.object({
1413 name: z.string(),
1514 email: z.string().email(),
1615 age: z.number().int().positive().optional(),
1716 createdAt: z.date().default(() => new Date()),
1818-}));
1717+});
19182019// Infer the TypeScript type from the Zod schema
2120type User = InferModel<typeof userSchema>;
+1-1
mod.ts
···11-export { defineModel, type InferModel, type InsertType } from "./schema.ts";
11+export { type InferModel, type InsertType } from "./schema.ts";
22export { connect, disconnect } from "./client.ts";
33export { Model } from "./model.ts";
-4
schema.ts
···11import type { z } from "zod";
22import type { ObjectId } from "mongodb";
3344-export function defineModel<T extends z.ZodObject>(schema: T) {
55- return schema;
66-}
77-84export type InferModel<T extends z.ZodObject> = z.infer<T> & {
95 _id?: ObjectId;
106};
+3-11
tests/main_test.ts
···11import { assertEquals, assertExists, assertRejects } from "jsr:@std/assert";
22import { z } from "zod";
33-import {
44- connect,
55- defineModel,
66- disconnect,
77- type InferModel,
88- type InsertType,
99- Model,
1010-} from "../mod.ts";
33+import { connect, disconnect, type InsertType, Model } from "../mod.ts";
114import { ObjectId } from "mongodb";
1251313-const userSchema = defineModel(z.object({
66+const userSchema = z.object({
147 name: z.string(),
158 email: z.email(),
169 age: z.number().int().positive().optional(),
1710 createdAt: z.date().default(() => new Date()),
1818-}));
1111+});
19122020-type User = InferModel<typeof userSchema>;
2113type UserInsert = InsertType<typeof userSchema>;
22142315let UserModel: Model<typeof userSchema>;