# Database ## MongoDatabase MongoDB connection manager with Mongoose integration. ```typescript new MongoDatabase(client: Vimcord, options?: MongooseOptions): MongoDatabase ``` ### Static Methods ```typescript // Get existing instance MongoDatabase.getInstance(clientId?: number | Vimcord): MongoDatabase | undefined // Wait for instance to be ready MongoDatabase.getReadyInstance(clientId?: number | Vimcord, timeoutMs?: number): Promise // Start a new session MongoDatabase.startSession(options?: ClientSessionOptions, clientId?: number | Vimcord): Promise ``` ### Properties ```typescript database.connection; // Mongoose Connection database.isReady; // boolean - connection status database.mongoose; // Mongoose instance database.clientId; // number - client identifier ``` ### Methods ```typescript // Connect database.connect(uri?: string, connectionOptions?: ConnectOptions, options?: MongoConnectionOptions): Promise // Disconnect database.disconnect(): Promise // Sessions database.startSession(options?: ClientSessionOptions): Promise // Transactions database.useTransaction(fn: (session: ClientSession) => Promise): Promise // Wait for ready database.waitForReady(): Promise ``` ### MongoConnectionOptions ```typescript interface MongoConnectionOptions { maxRetries?: number; // default: 3 } ``` --- ## Usage ### Connect to MongoDB ```typescript import { createClient, defineClientOptions, MongoDatabase } from "vimcord"; const client = createClient( defineClientOptions({ intents: [...] }) ); client.useDatabase(new MongoDatabase(client)); ``` ### With Connection URI ```typescript client.useDatabase(new MongoDatabase(client)); await client.db.connect(process.env.MONGO_URI); ``` ### With Custom Options ```typescript await client.db.connect( process.env.MONGO_URI, { maxPoolSize: 10, serverSelectionTimeoutMS: 5000 }, { maxRetries: 5 } ); ``` --- ## Transactions ```typescript await client.db.useTransaction(async session => { await UserSchema.findByIdAndUpdate(userId, { $inc: { balance: 100 } }, { session }); await TransactionSchema.create([{ userId, amount: 100 }], { session }); }); ``` --- ## Schema Helpers ### MongoSchemaBuilder ```typescript new MongoSchemaBuilder(name: string, schema: SchemaDefinition, options?: MongoSchemaOptions): MongoSchemaBuilder ``` ```typescript interface MongoSchemaOptions { collection?: string; timestamps?: boolean; timeseries?: { timeField: string; metaField?: string; granularity?: "seconds" | "minutes" | "hours" }; indexes?: Array<{ fields: Record; options?: Record; }>; } ``` ### Methods ```typescript // Indexes schema.addIndex(fields: Record, options?: Record): this schema.addUniqueIndex(fields: Record): this schema.addTTLIndex(field: string, options?: number): this // Middleware schema.pre(method: "save" | "validate" | "remove" | "deleteOne" | "updateOne", fn: (this: any, next: (err?: Error) => void) => void): this // Methods schema.method(name: string, fn: (this: any, ...args: any[]) => any): this schema.static(name: string, fn: (this: Model, ...args: any[]) => any): this // Virtuals schema.virtual(name: string, options?: Record): this // Compile schema.toModel(name: string): Model> ```