Skip to content

Commit

Permalink
get mongodb-memory-server work
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Aug 27, 2023
1 parent d241c2a commit 3e98905
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 40 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ghPages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
- nest
release:
types:
- published
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ lerna-debug.log*

# NotifyBC
**/*.local.*
/server/database/data.json
/server/database/**
!/server/database/.keep
/server/certs/**
!/server/certs/.keep
tsconfig.build.tsbuildinfo
18 changes: 8 additions & 10 deletions docs/docs/config/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ permalink: /docs/config-database/

# Database

By default _NotifyBC_ uses in-memory database backed up by file in _/server/database/data.json_ for local and docker deployment and MongoDB for Kubernetes deployment. To use MongoDB for non-Kubernetes deployment, add file _/src/datasources/db.datasource.local.json_ with MongoDB connection information such as following:
By default _NotifyBC_ uses in-memory database backed up by folder _/server/database/_ for local and docker deployment and MongoDB for Kubernetes deployment. To use MongoDB for non-Kubernetes deployment, add file _/src/datasources/db.datasource.(local|\<env\>).(json|js|ts)_ with MongoDB connection information such as following:

```json
{
"name": "db",
"connector": "mongodb",
"host": "127.0.0.1",
"database": "notifyBC",
"port": 27017
}
```js
module.exports = {
uri: 'mongodb://127.0.0.1:27017/notifyBC',
user: process.env.MONGODB_USER,
pass: process.env.MONGODB_PASSWORD,
};
```

See [LoopBack MongoDB data source](https://loopback.io/doc/en/lb4/MongoDB-connector.html#creating-a-mongodb-data-source) for more configurable properties.
See [Mongoose connection options](https://mongoosejs.com/docs/connections.html#options) for more configurable properties.
28 changes: 24 additions & 4 deletions docs/docs/miscellaneous/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,13 @@ Replace _v4.x.x_ with a v4 release, preferably latest, found in GitHub such as _
## v4 to v5
1. Update file _src/datasources/db.datasource.(local|<env>).(json|js|ts)_
v5 introduced following backward incompatible changes that need to be addressed in this order
1. remove _name_ property
2. remove _connector_ property
3. rename _url_ property to _uri_
1. If you use default in-memory database, data in _server/database/data.json_ will not be migrated automatically. Manually migrate if necessary.
2. Update file _src/datasources/db.datasource.local.json_
1. rename _url_ property to _uri_
2. for other properties, instead of following [LoopBack MongoDB data source](https://loopback.io/doc/en/lb4/MongoDB-connector.html#creating-a-mongodb-data-source), follow [Mongoose connection options](https://mongoosejs.com/docs/connections.html#options)
For example, change
Expand All @@ -290,3 +292,21 @@ Replace _v4.x.x_ with a v4 release, preferably latest, found in GitHub such as _
"uri": "mongodb://127.0.0.1:27017/notifyBC"
}
```
After above changes are addressed, upgrading to v5 is as simple as
```sh
git pull
git checkout tags/v5.x.x -b <branch_name>
yarn install && yarn build
```
or, if _NotifyBC_ is deployed to Kubernetes using Helm.
```sh
git pull
git checkout tags/v5.x.x -b <branch_name>
helm upgrade <release-name> -f helm/platform-specific/<platform>.yaml -f helm/values.local.yaml helm
```
Replace _v5.x.x_ with a v5 release, preferably latest, found in GitHub such as _v5.0.0_.
4 changes: 1 addition & 3 deletions helm/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ data:
};
db.datasource.production.js: |-
module.exports = {
name: 'db',
connector: 'mongodb',
url: `mongodb://${process.env.MONGODB_USER}:${process.env.MONGODB_PASSWORD}@${process.env.DATABASE_SERVICE_NAME}:${process.env.DB_PORT || 27017}/${process.env.MONGODB_DATABASE}?replicaSet=${process.env.MONGODB_REPLICA_SET_NAME}`
uri: `mongodb://${process.env.MONGODB_USER}:${process.env.MONGODB_PASSWORD}@${process.env.DATABASE_SERVICE_NAME}:${process.env.DB_PORT || 27017}/${process.env.MONGODB_DATABASE}?replicaSet=${process.env.MONGODB_REPLICA_SET_NAME}`
};
{{- range $key, $val := .Values.configMap }}
{{- $key | nindent 2 }}: |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notify-bc",
"version": "4.1.9",
"version": "5.0.0",
"dbSchemaVersion": "0.8.0",
"description": "A versatile notification API server",
"author": "",
Expand Down
Empty file added server/database/.keep
Empty file.
25 changes: 17 additions & 8 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from './config/config.module';
import { MongooseModule } from '@nestjs/mongoose';
import { AdministratorsModule } from './api/administrators/administrators.module';
import { BouncesModule } from './api/bounces/bounces.module';
import { ConfigurationsModule } from './api/configurations/configurations.module';
import { NotificationsModule } from './api/notifications/notifications.module';
import { SubscriptionsModule } from './api/subscriptions/subscriptions.module';
import { AdministratorsModule } from './api/administrators/administrators.module';
import { BouncesModule } from './api/bounces/bounces.module';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from './config/config.module';
import { DbConfigService } from './config/db-config.service';

@Module({
Expand All @@ -20,8 +20,17 @@ import { DbConfigService } from './config/db-config.service';
BouncesModule,
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (dbConfigService: DbConfigService) =>
dbConfigService.get(),
useFactory: async (dbConfigService: DbConfigService) => {
const dbConfig = dbConfigService.get();
if (dbConfig.uri) return dbConfig;
const mongod =
await require('mongodb-memory-server').MongoMemoryServer.create({
instance: dbConfig,
});
return {
uri: mongod.getUri(),
};
},
inject: [DbConfigService],
}),
],
Expand Down
12 changes: 6 additions & 6 deletions src/config/config.factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import * as fs from 'fs';
import { ConfigType } from './constants';
import fs from 'fs';
import { isArray, mergeWith } from 'lodash';
import path from 'path';
import { ConfigType } from './constants';

const config: Record<ConfigType, any> = {
[ConfigType.AppConfig]: undefined,
Expand Down Expand Up @@ -53,12 +53,12 @@ function init() {
config[ConfigType.AppConfig] = options;
config[ConfigType.MiddlewareConfig] = middlewareConfigs;

let dsFiles: string[] = ['db.datasource.js'];
let dsFiles: string[] = ['db.datasource.json', 'db.datasource.js'];
if (process.env.NODE_ENV) {
dsFiles = [
dsFiles = dsFiles.concat([
`db.datasource.${process.env.NODE_ENV}.json`,
`db.datasource.${process.env.NODE_ENV}.js`,
];
]);
}
if (process.env.NODE_ENV !== 'test') {
dsFiles = dsFiles.concat([
Expand Down
3 changes: 3 additions & 0 deletions src/datasources/db.datasource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dbPath": "server/database"
}
6 changes: 0 additions & 6 deletions src/datasources/db.datasource.ts

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"allowJs": true
"allowJs": true,
"esModuleInterop": true
}
}

0 comments on commit 3e98905

Please sign in to comment.