···2import { ObjectId } from "mongodb";
3import {
4 connect,
5- defineModel,
6 disconnect,
7 type InferModel,
8 type InsertType,
···10} from "../mod.ts";
1112// 1. Define your schema using Zod
13-const userSchema = defineModel(z.object({
14 name: z.string(),
15 email: z.string().email(),
16 age: z.number().int().positive().optional(),
17 createdAt: z.date().default(() => new Date()),
18-}));
1920// Infer the TypeScript type from the Zod schema
21type User = InferModel<typeof userSchema>;
···2import { ObjectId } from "mongodb";
3import {
4 connect,
05 disconnect,
6 type InferModel,
7 type InsertType,
···9} from "../mod.ts";
1011// 1. Define your schema using Zod
12+const userSchema = z.object({
13 name: z.string(),
14 email: z.string().email(),
15 age: z.number().int().positive().optional(),
16 createdAt: z.date().default(() => new Date()),
17+});
1819// Infer the TypeScript type from the Zod schema
20type User = InferModel<typeof userSchema>;
+1-1
mod.ts
···1-export { defineModel, type InferModel, type InsertType } from "./schema.ts";
2export { connect, disconnect } from "./client.ts";
3export { Model } from "./model.ts";
···1+export { type InferModel, type InsertType } from "./schema.ts";
2export { connect, disconnect } from "./client.ts";
3export { Model } from "./model.ts";
-4
schema.ts
···1import type { z } from "zod";
2import type { ObjectId } from "mongodb";
34-export function defineModel<T extends z.ZodObject>(schema: T) {
5- return schema;
6-}
7-8export type InferModel<T extends z.ZodObject> = z.infer<T> & {
9 _id?: ObjectId;
10};
···1import type { z } from "zod";
2import type { ObjectId } from "mongodb";
300004export type InferModel<T extends z.ZodObject> = z.infer<T> & {
5 _id?: ObjectId;
6};
+3-11
tests/main_test.ts
···1import { assertEquals, assertExists, assertRejects } from "jsr:@std/assert";
2import { z } from "zod";
3-import {
4- connect,
5- defineModel,
6- disconnect,
7- type InferModel,
8- type InsertType,
9- Model,
10-} from "../mod.ts";
11import { ObjectId } from "mongodb";
1213-const userSchema = defineModel(z.object({
14 name: z.string(),
15 email: z.email(),
16 age: z.number().int().positive().optional(),
17 createdAt: z.date().default(() => new Date()),
18-}));
1920-type User = InferModel<typeof userSchema>;
21type UserInsert = InsertType<typeof userSchema>;
2223let UserModel: Model<typeof userSchema>;
···1import { assertEquals, assertExists, assertRejects } from "jsr:@std/assert";
2import { z } from "zod";
3+import { connect, disconnect, type InsertType, Model } from "../mod.ts";
00000004import { ObjectId } from "mongodb";
56+const userSchema = z.object({
7 name: z.string(),
8 email: z.email(),
9 age: z.number().int().positive().optional(),
10 createdAt: z.date().default(() => new Date()),
11+});
12013type UserInsert = InsertType<typeof userSchema>;
1415let UserModel: Model<typeof userSchema>;