Documentation Index
Fetch the complete documentation index at: https://mintlify.com/abejarano/ts-mongo-criteria/llms.txt
Use this file to discover all available pages before exploring further.
@abejarano/ts-mongodb-criteria ships with a ts-mongo CLI that wraps migrate-mongo — a battle-tested migration runner for MongoDB. This lets you version-control index changes, collection renames, and document transformations right alongside your TypeScript application code, with a migrate-mongo-config.js file that is pre-wired to the same environment variables used by MongoClientFactory.
Setup
Installing the CLI
Thets-mongo binary is included when you install the package — no separate install step is required:
ts-mongo is available as a local binary through your package manager’s script runner.
Environment variables
The migration config reads the same environment variables used byMongoClientFactory. You can provide either a full connection string or the individual components:
.env file. If MONGO_URI is set, it takes precedence over the individual variables.
Config file
The CLI usesmigrate-mongo-config.js at the root of your project. Running ts-mongo migrate:init creates this file automatically. It stores migration state in a migrations collection inside your database.
When your
package.json contains "type": "module", running ts-mongo migrate:init automatically appends the -m esm flag to the underlying migrate-mongo init call. This tells migrate-mongo to generate an ESM-compatible config. Refer to the migrate-mongo documentation for the exact generated output.Commands reference
package.json for convenience:
migrate:create <name>
Creates a new timestamped migration file inside themigrations/ directory:
name argument becomes part of the filename and is used to identify the migration in the status output. Use a short, descriptive slug.
migrate:up
Reads themigrations changelog collection to determine which files have not yet been applied, then runs them in chronological order. Each migration’s up function receives the native Db object from the MongoDB driver.
migrate:down
Rolls back the last applied migration by calling itsdown function. Run it multiple times to roll back further.
migrate:status
Prints a table of all migration files and whether each one has been applied (up) or is pending:
Example migration file
The following migration adds a unique index onusers.email in up, and drops it again in down. The db argument is the raw Db instance from the MongoDB Node.js driver.
Migrations are app-specific, not library-specific. If you consume
@abejarano/ts-mongodb-criteria as a dependency in your own project, you need
to replicate this setup in your own repository: copy migrate-mongo-config.js
to your project root, create a migrations/ directory, and add the
migrate:* npm scripts. The library’s own migrations folder should not be
used for your application migrations.