Skip to content

Running fake database triggers using MongoDB change streams

Notifications You must be signed in to change notification settings

mickaelpham/fake-triggers

Repository files navigation

Database Triggers

Playing with a fake database trigger runner on my local dev environment.

Setup

This assumes that you have Docker running locally.

npm install           # install dependencies
docker compose up -d  # start the services
cp .env.sample .env   # configure the application

Then edit the .env file with a MongoDB URL, e.g.:

# MongoDB instance
DATABASE_URL=http://localhost:27019/some_database

Finally, prepare the database so it's configured for change streams pre and post images, and has some indexes:

npm run db:prepare

Listen for change stream events:

npm run listen

In another terminal, run some scenarios, or modify the data in MongoDB directly, using Compass or whatever tool you fancy.

npm run exec <PATH TO SCENARIO>

Testing

For now, this is using the same database as the development environment, and you need to run the listener process before the test suite:

npm run listen

# in another shell
npm test

Configuration

I need to update the collection to provide the pre and post images. See this document for more information.

db.runCommand({ collMod: 'workspace_members', changeStreamPreAndPostImages: { enabled: true } })

About

Running fake database triggers using MongoDB change streams

Topics

Resources

Stars

Watchers

Forks