Packages / Mongoose

Mongoose

MongoDB integration via Mongoose with injectable models, multi-connection support, and standard `inject()` API.

Overview

@miiajs/mongoose wires Mongoose into MiiaJS. Declare models with defineModel(), register them per feature module, and inject them in services as plain DI tokens with inject(User). The raw mongoose.Connection is available through inject(mongooseConnection()) for migrations, transactions, and direct collection-level access.

The package owns the connection lifecycle (onInit/onDestroy), retry logic for transient errors, and model compilation. The internal MongooseService is an implementation detail - users never inject it directly.

Installation

bun add @miiajs/mongoose mongoose

Peer dependencies:

PackageVersion
mongoose^9.4.1

Setup

typescript
import { Module } from '@miiajs/core'
import { MongooseModule } from '@miiajs/mongoose'

@Module({
  imports: [
    MongooseModule.configure(
      { uri: 'mongodb://localhost:27017/myapp' },
      { global: true },
    ),
  ],
})
class AppModule {}

MongooseModule.configure() exports mongooseConnection() and the connection internals the models of register() resolve. { global: true } makes them visible to every module; without it, a module that injects mongooseConnection() or calls register() imports MongooseModule bare (imports: [MongooseModule]) and gets this configuration.

With ConfigService (from @miiajs/config):

typescript
import { ConfigService } from '@miiajs/config'

MongooseModule.configure((resolve) => {
  const config = resolve(ConfigService)
  return {
    uri: config.getOrThrow('MONGODB_URL'),
    retry: { attempts: 5, delay: 2_000 },
  }
})

Configuration options

OptionTypeDefaultDescription
uristringrequiredMongoDB connection URI
connectionOptionsmongoose.ConnectOptions-Forwarded to createConnection()
retry.attemptsnumber3Connection attempts before giving up
retry.delaynumber2_000Delay (ms) between attempts

Only network-level errors (ECONNREFUSED, ECONNRESET, ETIMEDOUT, ENOTFOUND, EAI_AGAIN) are retried. Auth and schema errors abort immediately.

Define models

defineModel(name, schema) returns a typed DI token for the compiled mongoose.Model<T> - a frozen Token object that also carries the model name and schema. Pass it to MongooseModule.register() and to inject():

typescript
import { Schema } from 'mongoose'
import { defineModel } from '@miiajs/mongoose'

export interface IUser {
  name: string
  email: string
  role: string
}

const userSchema = new Schema<IUser>(
  {
    name: { type: String, required: true },
    email: { type: String, required: true },
    role: { type: String, default: 'user' },
  },
  { timestamps: true },
)

export const User = defineModel<IUser>('User', userSchema)

For schemas with plugins or middleware, compose the schema before passing it:

typescript
const userSchema = new Schema<IUser>({ ... })
userSchema.plugin(autopopulate)
userSchema.pre('save', async function () { /* ... */ })

export const User = defineModel('User', userSchema)

Register models

Register models in the feature module that owns them:

typescript
import { Module } from '@miiajs/core'
import { MongooseModule } from '@miiajs/mongoose'
import { User } from './user.model.js'

@Module({
  imports: [MongooseModule.register([User])],
  controllers: [UserController],
  providers: [UserService],
})
class UserModule {}

Each registered token becomes its own DI provider keyed by the token object. Mongoose compiles the model during onInit.

A register() result exports its models to the module that imports it. That module must see MongooseModule itself - global, or a bare imports: [MongooseModule, MongooseModule.register([User])] - because the models reach the connection through it. Several modules may each register() the same model and share one instance; every one of them must see MongooseModule that way, whatever the order of imports - one that does not is named in the startup report. Re-exporting one register() result is the other way to let several modules inject a model:

typescript
const userModels = MongooseModule.register([User])

@Module({ imports: [userModels], exports: [userModels] })
class UserModelsModule {}

Use in services

Inject models with the standard inject() from @miiajs/core:

typescript
import { Injectable, inject } from '@miiajs/core'
import { User } from './user.model.js'

@Injectable()
class UserService {
  private users = inject(User)

  async findAll() {
    return this.users.find()
  }

  async findById(id: string) {
    return this.users.findById(id)
  }

  async create(data: { name: string; email: string }) {
    return this.users.create(data)
  }

  async update(id: string, data: Record<string, any>) {
    return this.users.findByIdAndUpdate(id, data, { new: true })
  }

  async delete(id: string) {
    const result = await this.users.deleteOne({ _id: id })
    return result.deletedCount > 0
  }
}

inject(User) returns a typed mongoose.Model<T> proxy. The proxy preserves Mongoose's document construction (new this.users(data)) and instanceof checks (doc instanceof this.users); the underlying compiled model is created during app.init(). Use the injected model for both - the User token is an object, not a class, so new User(data) and doc instanceof User throw a TypeError.

Raw connection access

For aggregations, transactions, or direct collection access, inject the raw mongoose.Connection:

typescript
import { Injectable, inject } from '@miiajs/core'
import { mongooseConnection } from '@miiajs/mongoose'

@Injectable()
class MigrationService {
  private connection = inject(mongooseConnection())

  async listCollections() {
    return (await this.connection.db?.listCollections().toArray()) ?? []
  }

  async runTransaction() {
    const session = await this.connection.startSession()
    try {
      await session.withTransaction(async () => {
        // multi-document writes
      })
    } finally {
      await session.endSession()
    }
  }
}

mongooseConnection(name?) is memoized: the same name (including the default empty name) always returns the same token, so DI identity is stable across files.

Multiple connections

Pass { name } as the second argument of configure() and register() for each additional database:

typescript
@Module({
  imports: [
    MongooseModule.configure({ uri: MAIN_DB }, { global: true }),
    MongooseModule.configure({ uri: ANALYTICS_DB }, { name: 'analytics', global: true }),
    UserModule,
    EventModule,
  ],
})
class AppModule {}

A bare imports: [MongooseModule] reaches only the configuration without a name. A named connection is either global, as here, or its configure() result is imported by one module that re-exports it.

Register each model under the connection it belongs to:

typescript
// UserModule - default connection
MongooseModule.register([User])

// EventModule - named connection
MongooseModule.register([Event], { name: 'analytics' })

If you need the same model on two connections, declare two distinct tokens. They look identical to the user but DI sees them as separate providers:

typescript
export const User = defineModel<IUser>('User', userSchema)
export const AnalyticsUser = defineModel<IUser>('User', userSchema)

MongooseModule.register([User])                                 // default
MongooseModule.register([AnalyticsUser], { name: 'analytics' })

// In services:
private users = inject(User)
private analyticsUsers = inject(AnalyticsUser)

For raw connection access on the named connection:

typescript
private analyticsConn = inject(mongooseConnection('analytics'))

Connection lifecycle

  • onInit - validates that no two distinct tokens claim the same model name, connects to MongoDB with retry on transient errors, compiles all registered models. If model compilation fails, the connection is closed before the error is rethrown (no leaked sockets).
  • onDestroy - closes the connection and clears all model references.

A provider that injects the connection or a model gets its onInit() after the connection opens and its onDestroy() before the connection closes, wherever its module sits in imports.

Testing

Because models are DI tokens, you override them directly in tests with TestApp.override():

typescript
import { TestApp } from '@miiajs/testing'
import { AppModule } from './app.module.js'
import { User } from './user/user.model.js'

const fakeUserModel = {
  find: () => Promise.resolve([{ _id: '1', name: 'Alice' }]),
  create: async (doc: any) => ({ _id: 'fake', ...doc }),
} as any

const app = await TestApp.create(AppModule)
  .override(User, fakeUserModel)
  .compile()

const res = await app.request('GET', '/users')
expect(res.status).toBe(200)

await app.close()

For higher-level overrides (replace the whole service), use the standard TestApp.override(UserService, mock) pattern from @miiajs/core.

Exports

typescript
import {
  MongooseModule,
  defineModel,
  mongooseConnection,
  type MongooseModuleOptions,
  type ModelToken,
  type MongooseConnectionToken,
} from '@miiajs/mongoose'

Both token types are built on Token from @miiajs/core: MongooseConnectionToken is Token<mongoose.Connection>, and ModelToken<T> is Token<mongoose.Model<T>> with the name and schema it was defined with. User.name is the mongoose model name, so a reference to the model can be written ref: User.name.

See also

  • @miiajs/papr - Papr-based MongoDB integration with the same shape
  • @miiajs/drizzle - SQL ORM integration following a similar pattern
  • Modules - DI scoping and module composition
Edit this page on GitHub