···55 connect,
56 disconnect,
57 InferModel,
58+ Input,
59 Model,
60} from "@nozzle/nozzle";
61import { userSchema } from "./schemas/user";
62import { ObjectId } from "mongodb"; // v6+ driver recommended
6364type User = InferModel<typeof userSchema>;
65+type UserInsert = Input<typeof userSchema>;
6667async function main() {
68 // Use the latest connection string format and options
···8384```ts
85// Insert one
86+// Note: createdAt has a default, so it's optional in the input type
87const newUser: UserInsert = {
88 name: "John Doe",
89 email: "john.doe@example.com",
90 age: 30,
91+ // createdAt is optional because of z.date().default(() => new Date())
92};
93const insertResult = await UserModel.insertOne(newUser);
94···142 { age: { $gte: 18 } },
143 { skip: 0, limit: 10, sort: { age: -1 } },
144);
145+146+// Index Management
147+// Create a unique index
148+await UserModel.createIndex({ email: 1 }, { unique: true });
149+150+// Create a compound index
151+await UserModel.createIndex({ name: 1, age: -1 });
152+153+// Create multiple indexes at once
154+await UserModel.createIndexes([
155+ { key: { email: 1 }, name: "email_idx", unique: true },
156+ { key: { name: 1, age: -1 }, name: "name_age_idx" },
157+]);
158+159+// List all indexes
160+const indexes = await UserModel.listIndexes();
161+console.log("Indexes:", indexes);
162+163+// Check if index exists
164+const exists = await UserModel.indexExists("email_idx");
165+166+// Drop an index
167+await UserModel.dropIndex("email_idx");
168+169+// Sync indexes (useful for migrations - creates missing, updates changed)
170+await UserModel.syncIndexes([
171+ { key: { email: 1 }, name: "email_idx", unique: true },
172+ { key: { createdAt: 1 }, name: "created_at_idx" },
173+]);
174```
175176---
+2-2
examples/user.ts
···4 connect,
5 disconnect,
6 type InferModel,
7- type InsertType,
8 Model,
9} from "../mod.ts";
10···1819// Infer the TypeScript type from the Zod schema
20type User = InferModel<typeof userSchema>;
21-type UserInsert = InsertType<typeof userSchema>;
2223async function runExample() {
24 try {
···4 connect,
5 disconnect,
6 type InferModel,
7+ type Input,
8 Model,
9} from "../mod.ts";
10···1819// Infer the TypeScript type from the Zod schema
20type User = InferModel<typeof userSchema>;
21+type UserInsert = Input<typeof userSchema>;
2223async function runExample() {
24 try {
+1-1
mod.ts
···1-export { type InferModel, type InsertType } from "./schema.ts";
2export { connect, disconnect } from "./client.ts";
3export { Model } from "./model.ts";
···1+export { type InferModel, type Input } from "./schema.ts";
2export { connect, disconnect } from "./client.ts";
3export { Model } from "./model.ts";