@tulipes/mongoose
v0.1.1
Published
Mongoose models provider for Tulipes apps: connection, model registration and bootstrap behind core's models capability
Maintainers
Readme
@tulipes/mongoose
The Mongoose models provider for Tulipes
apps. It connects, compiles every module's models/*.model.ts on the boot's
own connection, runs bootstrap/*.bootstrap.ts, and closes the connection
under core's shutdown deadline. Core itself no longer installs or imports
Mongoose; an app without a database declares no provider and never pays for one.
Requires Node 24.x, mongoose@^8 as the app's own dependency (peer — the app's
schemas and this provider must share one instance) and a core that ships the
provider contract.
Select it
One line in the app's root package.json:
{ "tulipes": { "providers": { "models": "@tulipes/mongoose" } } }and MONGO_URI declared by the app's sys core module (meta.variables.json)
and set. Modules with models/ or bootstrap/ directories require the
provider automatically; a module that only reads models declares
"tulipes": { "requires": ["models"] } in its own manifest. Boot refuses an
unmet requirement before any module code is imported. Installing this package
without the declaration selects nothing; a MONGO_URI on its own connects
nothing.
Use it
// modules/users/models/user.model.ts
import { Schema } from "mongoose";
import type { ModelDef } from "@tulipes/mongoose";
const userSchema = new Schema({ email: { type: String, required: true } });
export default { name: "User", schema: userSchema } satisfies ModelDef;
// anywhere after phase 8
const User = requireModels(ctx).get<UserDoc>("User"); // typed once this package is in the compilation
ctx.models.connection; // the boot's own mongoose ConnectionModelDef, BootstrapFn and ModelStore moved here from @tulipes/core/db;
ctx.models, requireModels and DatabaseCtx stay in @tulipes/core/boot.
Behavior
- One connection per boot,
serverSelectionTimeoutMS: 5000; ownership is registered with core before the connection is awaited, so a database that never answers is still closed by the failed-boot cleanup. - Models register in module load order; a second module registering the same
name is a boot error naming both.
def.schemamust be aSchemafrom the samemongooseinstance. - Bootstrap tasks run in backend and worker processes, never in
scriptmode or runtime inspection, and only when registration reported nothing. - Global plugins (
mongoose.plugin(...)) remain application code and apply because the app and this provider share the instance.
Versions
0.1.0-rc.2 (no source change since rc.1; republished with core 0.10.0-rc.2)
requires core ^0.10.0-rc.1, the candidate that introduced the provider seam;
install both with @next. See core's
MIGRATING.md.
