database-updates is a Node.js library designed to manage and apply database schema and data migration scripts in a versioned, reliable manner. It is currently at version 4.1.0 and appears to be actively maintained, though no explicit release cadence is stated beyond following semantic versioning for its own releases. The core differentiator is its reliance on SemVer (Semantic Versioning) compliant filenames for update scripts (e.g., `1.0.0-initial-setup.js`), which dictates the execution order and ensures each script runs exactly once per environment. This mechanism prevents inconsistencies across development, staging, and production environments. It supports both CommonJS and ES Module update scripts and offers experimental support for TypeScript update files, provided the runtime can dynamically `import()` them. It's particularly useful for applications built on MongoDB or similar NoSQL databases where schema changes are often handled via code-based migrations.
npm install database-updatesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `database-updates` with a MongoDB connection, define an update script, and execute the updates. It creates a temporary update file and ensures the database is updated according to the semver filename.
Ensure your Node.js runtime environment is configured to handle dynamic TypeScript imports for `.ts` update files. This often involves using a tool like `ts-node/register` or transpiling `.ts` files to `.js` before execution, or configuring Node.js to recognize `.ts` files as modules.
Verify all update script filenames conform to the `major.minor.patch-description.ext` format. The version part dictates the execution order.
For CommonJS, use `module.exports = function (db) { ... }`. For ES Modules, use `export default function (db) { ... }` or `export default (db) => { ... }`. Ensure the function signature is `(db) => { ... }`.For new changes, create a new update script with an incremented SemVer filename. To force a re-run of an existing script in a development environment, manually delete its entry from the `databaseUpdates` collection in your database.
If using `.ts` files, ensure `ts-node` is installed and registered (e.g., `node -r ts-node/register your-app.js`) or transpile your `.ts` files to `.js` before running. For ESM `.ts` files, ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and you're using dynamic `import()` where necessary.
Review the update script file to ensure it exports a function with the `(db)` signature correctly. Check for typos in `module.exports` or `export default`.
Ensure your database connection is directed to the primary replica set member for write operations, or configure appropriate write concerns and read preferences for your `MongoClient` instance if you intend to write to secondaries (which is generally not recommended for migrations).
No dependency data recorded yet.