Skip to content

MongoDB

Jonathan Sharpe edited this page Sep 17, 2024 · 3 revisions

The v2 starter kit now supports Postgres out of the box. However the same structure can also be used with other databases, for example MongoDB (via Mongoose).

PR#1767 shows what's needed, but it's basically a matter of:

  • Replacing pg (npm --workspace api uninstall pg) with mongoose (npm --workspace api install mongoose)

    • Update api/utils/config.cjs (to use MONGODB_URI instead of DATABASE_URL, and strip out the unneeded Postgres configuration) and api/server.js accordingly

    • Update api/messages/messageRepository.js to use a Mongoose model to access the collection

      Note: this shows the benefit of having a repository abstraction layer; messageRouter.js and messageService.js don't have to change, even though we've swapped out the whole DB.

  • Replacing node-pg-migrate (npm --workspace api uninstall node-pg-migrate) with migrate-mongo (npm --workspace api install migrate-mongo)

    • Use the ESM options, which also means you can migrate api/utils/config.cjs (CommonJS) -> api/utils/config.js (ESM)

    • Configuration moves from api/migrations/config.cjs to api/migrate-mongo-config.js

    • Migration template moves from api/migrations/template.cjs to api/migrations/sample-migration.js

    • Command in the api/package.json scripts changes to "migration": "migrate-mongo".

    • New migrations can then be created in api/migrations/

  • Updating the test setup in api/setupTests.js to start, connect to and migrate a MongoDB container instead of Postgres

    • Change the name of the env var set in the api/package.json's test script to MONGODB_URI (the actual connection URI will be provided using the test container anyway)
  • Updating workflows in .github/workflows to create a MongoDB instead of Postgres service container, and configure the env vars accordingly

  • Updating eslint.config.js to correspond to the above changes

Clone this wiki locally