-
Notifications
You must be signed in to change notification settings - Fork 74
MongoDB
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) withmongoose(npm --workspace api install mongoose)-
Update
api/utils/config.cjs(to useMONGODB_URIinstead ofDATABASE_URL, and strip out the unneeded Postgres configuration) andapi/server.jsaccordingly -
Update
api/messages/messageRepository.jsto use a Mongoose model to access the collectionNote: this shows the benefit of having a repository abstraction layer;
messageRouter.jsandmessageService.jsdon't have to change, even though we've swapped out the whole DB.
-
-
Replacing
node-pg-migrate(npm --workspace api uninstall node-pg-migrate) withmigrate-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.cjstoapi/migrate-mongo-config.js -
Migration template moves from
api/migrations/template.cjstoapi/migrations/sample-migration.js -
Command in the
api/package.jsonscripts changes to"migration": "migrate-mongo". -
New migrations can then be created in
api/migrations/
-
-
Updating the test setup in
api/setupTests.jsto start, connect to and migrate a MongoDB container instead of Postgres- Change the name of the env var set in the
api/package.json'stestscript toMONGODB_URI(the actual connection URI will be provided using the test container anyway)
- Change the name of the env var set in the
-
Updating workflows in
.github/workflowsto create a MongoDB instead of Postgres service container, and configure the env vars accordingly -
Updating
eslint.config.jsto correspond to the above changes