Skip to content

Commit

Permalink
feat: bootstrap mongodb (#136)
Browse files Browse the repository at this point in the history
This PR introduces MongoDB as the primary Database, as well as:
- Migration scripts that run after next deployments;
- A script and a data structure for importing development data (this
data can be used for development and for e2e tests);
- A CLI script that makes it easy for developers to start a MongoDB
locally with development data;
- An ADR about why we choose MongoDB.

---------

Co-authored-by: Jasper Herzberg <[email protected]>
  • Loading branch information
timonmasberg and JSPRH authored Apr 16, 2023
1 parent 01ff617 commit 0affebe
Show file tree
Hide file tree
Showing 17 changed files with 2,952 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ jobs:
run: envsubst < apps/api/src/.env.template > apps/api/src/.env
env:
PORT: 3000
MONGODB_URI: mongodb://127.0.0.1:27017/e2edb
- name: Build
run: npx nx affected --target=build --parallel=3

- name: Start and prepare MongoDB for E2Es
run: ./tools/db/kordis-db.sh init e2edb
- name: Run E2Es
run: npm run e2e
env:
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/next-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ jobs:
with:
slot: "next"
publishProfile: ${{ secrets.AZURE_WEBAPP_API_PUBLISH_PROFILE }}
- name: Apply Database Migrations
run: ./tools/db/kordis-db.sh apply-pending-migrations
env:
MONGODB_URI: ${{ secrets.DEV_MONGODB_URI }}

- name: Build and Deploy SPA
id: spa-deployment
uses: ./.github/actions/build-and-deploy-spa
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/.env.template
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PORT=$PORT
MONGODB_URI=$MONGODB_URI
18 changes: 15 additions & 3 deletions apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { MongooseModule } from '@nestjs/mongoose';
import * as path from 'path';

import { AuthModule } from '@kordis/api/auth';
Expand All @@ -11,8 +12,11 @@ import { AppService } from './app.service';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, cache: true }),
AuthModule,
ConfigModule.forRoot({
isGlobal: true,
cache: true,
envFilePath: path.resolve(__dirname, '.env'),
}),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile:
Expand All @@ -24,6 +28,14 @@ import { AppService } from './app.service';
},
playground: process.env.NODE_ENV !== 'production',
}),
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
uri: config.getOrThrow<string>('MONGODB_URI'),
}),
inject: [ConfigService],
}),
AuthModule,
],
providers: [AppService, AppResolver],
})
Expand Down
4 changes: 0 additions & 4 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* This is not a production server yet!
* This is only a minimal backend to get started.
*/
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
Expand Down
7 changes: 7 additions & 0 deletions apps/api/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const { composePlugins, withNx } = require('@nrwl/webpack');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = composePlugins(withNx(), (config) => {
config.plugins.push(
new CopyPlugin({
patterns: [{ from: 'src/.env', to: '.', noErrorOnMissing: true }],
}),
);

return config;
});
44 changes: 44 additions & 0 deletions docs/architecture-decisions/adr005-database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# ADR005: Database Selection - MongoDB

## Status

accepted

## Context

We are in the process of selecting a suitable database for the project. In
general, we need for a database that allows for fast development and changes,
integrates well with the NestJS framework and is cost-efficient without a vendor
lock-in (possibility to self-host).

## Decision

We have chosen MongoDB as primary database.

**Fast Development and Changes:** MongoDB's schemaless nature allows for rapid
development and changes without the need to define a fixed schema upfront. This
flexibility enables us to iterate quickly and adapt to changing requirements.

**Azure CosmosDB with MongoDB Adapter**: The availability of Azure CosmosDB with
the MongoDB adapter provides a convenient way to use cloud resources with the
option to easily opt out if needed. This aligns with the goal of leveraging the
available Azure credits and taking advantage of cloud-based features such as
managed backups.

**NestJS Integration**: MongoDB has first-tier support in the NestJS framework,
which means it is well-integrated and supported out of the box. This simplifies
the development process and reduces the effort required for integration.

**Widely Spread Database**: MongoDB is a popular NoSQL database with a large
community, which makes it easy to find resources and contribute to the project.

## Consequences

Next to positive factors already pointed out above, it might be complex to model
our data in a way that we have the complete safety of a Relational Database.
Even though MongoDB offers ACID transactions, this is not the "NoSQL way" of
managing related data. The possible consequence is, that we have to often
migrate our database, since it takes time to get into the mental model of NoSQL.
Furthermore, the danger of Azure CosmosDB not providing the latest features of
MongoDB is also given. The danger of having non-compliant queries is given, but
rare since Azure CosmosDB widely supports most of the MongoDB specifications.
Loading

0 comments on commit 0affebe

Please sign in to comment.