···6565type UserInsert = Input<typeof userSchema>;
66666767async function main() {
6868- // Use the latest connection string format and options
6868+ // Basic connection
6969 await connect("mongodb://localhost:27017", "your_database_name");
7070+7171+ // Or with connection pooling options
7272+ await connect("mongodb://localhost:27017", "your_database_name", {
7373+ clientOptions: {
7474+ maxPoolSize: 10, // Maximum connections in pool
7575+ minPoolSize: 2, // Minimum connections in pool
7676+ maxIdleTimeMS: 30000, // Close idle connections after 30s
7777+ connectTimeoutMS: 10000, // Connection timeout
7878+ socketTimeoutMS: 45000, // Socket timeout
7979+ }
8080+ });
8181+7082 const UserModel = new Model("users", userSchema);
71837284 // Your operations go here
+30-2
client.ts
···11-import { type Db, MongoClient } from "mongodb";
11+import { type Db, type MongoClientOptions, MongoClient } from "mongodb";
2233interface Connection {
44 client: MongoClient;
···7788let connection: Connection | null = null;
991010+export interface ConnectOptions {
1111+ /**
1212+ * MongoDB connection options (pooling, timeouts, etc.)
1313+ * See: https://mongodb.github.io/node-mongodb-native/6.18/interfaces/MongoClientOptions.html
1414+ */
1515+ clientOptions?: MongoClientOptions;
1616+}
1717+1818+/**
1919+ * Connect to MongoDB with connection pooling and other options
2020+ *
2121+ * The MongoDB driver handles connection pooling automatically.
2222+ * Configure pooling via `clientOptions`:
2323+ *
2424+ * @example
2525+ * ```ts
2626+ * await connect("mongodb://localhost:27017", "mydb", {
2727+ * clientOptions: {
2828+ * maxPoolSize: 10, // Maximum connections in pool
2929+ * minPoolSize: 2, // Minimum connections in pool
3030+ * maxIdleTimeMS: 30000, // Close idle connections after 30s
3131+ * connectTimeoutMS: 10000, // Connection timeout
3232+ * socketTimeoutMS: 45000, // Socket timeout
3333+ * }
3434+ * });
3535+ * ```
3636+ */
1037export async function connect(
1138 uri: string,
1239 dbName: string,
4040+ options?: ConnectOptions,
1341): Promise<Connection> {
1442 if (connection) {
1543 return connection;
1644 }
17451818- const client = new MongoClient(uri);
4646+ const client = new MongoClient(uri, options?.clientOptions);
1947 await client.connect();
2048 const db = client.db(dbName);
2149
+1-1
mod.ts
···11export { type InferModel, type Input } from "./schema.ts";
22-export { connect, disconnect } from "./client.ts";
22+export { connect, disconnect, type ConnectOptions } from "./client.ts";
33export { Model } from "./model.ts";