Thin MongoDB ODM built for Standard Schema
mongodb zod deno

fix example

+14 -251
+1 -3
README.md
··· 23 23 ## 📦 Installation 24 24 25 25 ```bash 26 - npm install mizzleorm mongodb zod 27 - # or 28 - yarn add mizzleorm mongodb zod 26 + deno add jsr:@nozzle/db 29 27 ``` 30 28 31 29 > If you need to upgrade your local MongoDB server, see:
+1 -1
deno.json
··· 9 9 "test:mock": "deno test tests/mock_test.ts", 10 10 "test:integration": "deno test --allow-net --allow-read --allow-write --allow-env --allow-sys tests/main_test.ts", 11 11 "test:watch": "deno run --allow-run --allow-read scripts/test.ts --mock --watch", 12 - "example": "ts-node examples/user.ts" 12 + "example": "deno run --allow-net --allow-read --allow-write --allow-env --allow-sys examples/user.ts" 13 13 }, 14 14 "imports": { 15 15 "zod": "jsr:@zod/zod@^4.0.17",
-238
examples/user.js
··· 1 - "use strict"; 2 - var __awaiter = (this && this.__awaiter) || 3 - function (thisArg, _arguments, P, generator) { 4 - function adopt(value) { 5 - return value instanceof P ? value : new P(function (resolve) { 6 - resolve(value); 7 - }); 8 - } 9 - return new (P || (P = Promise))(function (resolve, reject) { 10 - function fulfilled(value) { 11 - try { 12 - step(generator.next(value)); 13 - } catch (e) { 14 - reject(e); 15 - } 16 - } 17 - function rejected(value) { 18 - try { 19 - step(generator["throw"](value)); 20 - } catch (e) { 21 - reject(e); 22 - } 23 - } 24 - function step(result) { 25 - result.done 26 - ? resolve(result.value) 27 - : adopt(result.value).then(fulfilled, rejected); 28 - } 29 - step((generator = generator.apply(thisArg, _arguments || [])).next()); 30 - }); 31 - }; 32 - var __generator = (this && this.__generator) || function (thisArg, body) { 33 - var _ = { 34 - label: 0, 35 - sent: function () { 36 - if (t[0] & 1) throw t[1]; 37 - return t[1]; 38 - }, 39 - trys: [], 40 - ops: [], 41 - }, 42 - f, 43 - y, 44 - t, 45 - g = Object.create( 46 - (typeof Iterator === "function" ? Iterator : Object).prototype, 47 - ); 48 - return g.next = verb(0), 49 - g["throw"] = verb(1), 50 - g["return"] = verb(2), 51 - typeof Symbol === "function" && (g[Symbol.iterator] = function () { 52 - return this; 53 - }), 54 - g; 55 - function verb(n) { 56 - return function (v) { 57 - return step([n, v]); 58 - }; 59 - } 60 - function step(op) { 61 - if (f) throw new TypeError("Generator is already executing."); 62 - while (g && (g = 0, op[0] && (_ = 0)), _) { 63 - try { 64 - if ( 65 - f = 1, 66 - y && (t = op[0] & 2 67 - ? y["return"] 68 - : op[0] 69 - ? y["throw"] || ((t = y["return"]) && t.call(y), 0) 70 - : y.next) && 71 - !(t = t.call(y, op[1])).done 72 - ) return t; 73 - if (y = 0, t) op = [op[0] & 2, t.value]; 74 - switch (op[0]) { 75 - case 0: 76 - case 1: 77 - t = op; 78 - break; 79 - case 4: 80 - _.label++; 81 - return { value: op[1], done: false }; 82 - case 5: 83 - _.label++; 84 - y = op[1]; 85 - op = [0]; 86 - continue; 87 - case 7: 88 - op = _.ops.pop(); 89 - _.trys.pop(); 90 - continue; 91 - default: 92 - if ( 93 - !(t = _.trys, t = t.length > 0 && t[t.length - 1]) && 94 - (op[0] === 6 || op[0] === 2) 95 - ) { 96 - _ = 0; 97 - continue; 98 - } 99 - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { 100 - _.label = op[1]; 101 - break; 102 - } 103 - if (op[0] === 6 && _.label < t[1]) { 104 - _.label = t[1]; 105 - t = op; 106 - break; 107 - } 108 - if (t && _.label < t[2]) { 109 - _.label = t[2]; 110 - _.ops.push(op); 111 - break; 112 - } 113 - if (t[2]) _.ops.pop(); 114 - _.trys.pop(); 115 - continue; 116 - } 117 - op = body.call(thisArg, _); 118 - } catch (e) { 119 - op = [6, e]; 120 - y = 0; 121 - } finally { 122 - f = t = 0; 123 - } 124 - } 125 - if (op[0] & 5) throw op[1]; 126 - return { value: op[0] ? op[1] : void 0, done: true }; 127 - } 128 - }; 129 - Object.defineProperty(exports, "__esModule", { value: true }); 130 - var zod_1 = require("zod"); 131 - var src_1 = require("../src"); 132 - var mongodb_1 = require("mongodb"); 133 - // 1. Define your schema using Zod 134 - var userSchema = (0, src_1.defineModel)(zod_1.z.object({ 135 - name: zod_1.z.string(), 136 - email: zod_1.z.string().email(), 137 - age: zod_1.z.number().int().positive().optional(), 138 - createdAt: zod_1.z.date().default(function () { 139 - return new Date(); 140 - }), 141 - })); 142 - function runExample() { 143 - return __awaiter(this, void 0, void 0, function () { 144 - var UserModel, 145 - newUser, 146 - insertResult, 147 - users, 148 - foundUser, 149 - updateResult, 150 - updatedUser, 151 - deleteResult, 152 - error_1; 153 - return __generator(this, function (_a) { 154 - switch (_a.label) { 155 - case 0: 156 - _a.trys.push([0, 9, 10, 12]); 157 - // 3. Connect to MongoDB 158 - return [ 159 - 4, /*yield*/ 160 - (0, src_1.connect)( 161 - "mongodb://localhost:27017", 162 - "mizzleorm_example", 163 - ), 164 - ]; 165 - case 1: 166 - // 3. Connect to MongoDB 167 - _a.sent(); 168 - console.log("Connected to MongoDB"); 169 - UserModel = new src_1.MongoModel("users", userSchema); 170 - // Clean up previous data 171 - return [4, /*yield*/ UserModel.delete({})]; 172 - case 2: 173 - // Clean up previous data 174 - _a.sent(); 175 - newUser = { 176 - name: "Alice Smith", 177 - email: "alice@example.com", 178 - age: 30, 179 - }; 180 - return [4, /*yield*/ UserModel.insertOne(newUser)]; 181 - case 3: 182 - insertResult = _a.sent(); 183 - console.log("Inserted user:", insertResult.insertedId); 184 - return [4, /*yield*/ UserModel.find({ name: "Alice Smith" })]; 185 - case 4: 186 - users = _a.sent(); 187 - console.log("Found users:", users); 188 - return [ 189 - 4, /*yield*/ 190 - UserModel.findOne({ 191 - _id: new mongodb_1.ObjectId(insertResult.insertedId), 192 - }), 193 - ]; 194 - case 5: 195 - foundUser = _a.sent(); 196 - console.log("Found one user:", foundUser); 197 - return [ 198 - 4, /*yield*/ 199 - UserModel.update({ 200 - _id: new mongodb_1.ObjectId(insertResult.insertedId), 201 - }, { age: 31 }), 202 - ]; 203 - case 6: 204 - updateResult = _a.sent(); 205 - console.log("Updated user count:", updateResult.modifiedCount); 206 - return [ 207 - 4, /*yield*/ 208 - UserModel.findOne({ 209 - _id: new mongodb_1.ObjectId(insertResult.insertedId), 210 - }), 211 - ]; 212 - case 7: 213 - updatedUser = _a.sent(); 214 - console.log("Updated user data:", updatedUser); 215 - return [4, /*yield*/ UserModel.delete({ name: "Alice Smith" })]; 216 - case 8: 217 - deleteResult = _a.sent(); 218 - console.log("Deleted user count:", deleteResult.deletedCount); 219 - return [3, /*break*/ 12]; 220 - case 9: 221 - error_1 = _a.sent(); 222 - console.error("Error during example run:", error_1); 223 - return [3, /*break*/ 12]; 224 - case 10: 225 - // 9. Disconnect from MongoDB 226 - return [4, /*yield*/ (0, src_1.disconnect)()]; 227 - case 11: 228 - // 9. Disconnect from MongoDB 229 - _a.sent(); 230 - console.log("Disconnected from MongoDB"); 231 - return [7 /*endfinally*/]; 232 - case 12: 233 - return [2 /*return*/]; 234 - } 235 - }); 236 - }); 237 - } 238 - runExample();
+12 -9
examples/user.ts
··· 1 1 import { z } from "zod"; 2 + import { ObjectId } from "mongodb"; 2 3 import { 3 4 connect, 4 5 defineModel, 5 6 disconnect, 6 - InferModel, 7 - InsertType, 8 - MongoModel, 9 - } from "../src"; 10 - import { ObjectId } from "mongodb"; 7 + type InferModel, 8 + type InsertType, 9 + Model, 10 + } from "../mod.ts"; 11 11 12 12 // 1. Define your schema using Zod 13 13 const userSchema = defineModel(z.object({ ··· 24 24 async function runExample() { 25 25 try { 26 26 // 3. Connect to MongoDB 27 - await connect("mongodb://localhost:27017", "mizzleorm_example"); 27 + await connect("mongodb://localhost:27017", "nozzle_example"); 28 28 console.log("Connected to MongoDB"); 29 29 30 - // 2. Create a MongoModel for your collection 31 - const UserModel = new MongoModel("users", userSchema); 30 + // 2. Create a Model for your collection 31 + const UserModel = new Model("users", userSchema); 32 32 33 33 // Clean up previous data 34 34 await UserModel.delete({}); ··· 76 76 } 77 77 } 78 78 79 - runExample(); 79 + // Only run the example if this is the main module 80 + if (import.meta.main) { 81 + runExample(); 82 + }