JCC Express

Database

Mongoose

Introduction

JCC Express MVC supports MongoDB through a Mongoose driver (MongooseDriver) selected by database config/env.

When enabled, the framework resolves and registers the Mongoose connection as:

  • database.connection
  • mongoose

Enable Mongoose

Set env values for mongo mode:

env
DB_ORM=mongoose
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=your_db
DB_USERNAME=
DB_PASSWORD=

app/Config/database.ts includes the expected mongoose block:

TypeScript
mongoose: {
  host,
  port,
  database,
  username,
  password,
  options: {},
}

Required packages

Install Mongoose:

Bash
npm install mongoose

Do not manually write model files. Use ArtisanNode:

Bash
bun artisanNode make:model User

Keep your Mongo-oriented model definitions in app/Models so the project structure stays consistent.


Mongoose model example

In Mongoose mode, a model file can follow this shape:

TypeScript
import mongoose, { HydratedDocument, InferSchemaType, Schema } from "mongoose";

const UserSchema = new Schema(
  {
    name: { type: String, required: true },
    email: { type: String, unique: true, required: true },
    password: { type: String, required: true },
  },
  { timestamps: true },
);

export const User = mongoose.model("User", UserSchema);
export type User = HydratedDocument<InferSchemaType<typeof UserSchema>>;

The exported model name User is important in this project for route model binding conventions.


Connection behavior

From MongooseDriver:

  • builds URI as mongodb://[username:password@]host:port/database
  • runs mongoose.connect(uri, options)
  • keeps a shared connection reference for the app lifecycle

Disconnect path uses mongoose.disconnect().


Access the Mongoose connection

TypeScript
const mongooseConn = app.resolve("mongoose");

The instance is registered by DatabaseServiceProvider.


Notes

  • Driver selection is triggered by DB_ORM=mongoose or DB_CONNECTION=mongodb.
  • If host/database are missing, URI construction fails and connect throws.
  • Keep Mongo-specific schema/model logic separate from SQL/JCC query-builder code paths.

Summary

  • Set DB_ORM=mongoose.
  • Configure the mongoose section in app/Config/database.ts.
  • Install mongoose.
  • Generate models with bun artisanNode make:model User.
  • Resolve connection via app.resolve("mongoose").