diff --git a/.github/workflows/ghPages.yml b/.github/workflows/ghPages.yml index 32a8d64cc..26d247d4a 100644 --- a/.github/workflows/ghPages.yml +++ b/.github/workflows/ghPages.yml @@ -3,6 +3,7 @@ on: push: branches: - main + - nest release: types: - published diff --git a/.gitignore b/.gitignore index ef54d4d3e..7a306e84c 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/docs/docs/config/database.md b/docs/docs/config/database.md index 0f91e9f08..3c99360ec 100644 --- a/docs/docs/config/database.md +++ b/docs/docs/config/database.md @@ -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|\).(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. diff --git a/docs/docs/miscellaneous/upgrade.md b/docs/docs/miscellaneous/upgrade.md index 6d783c24a..26d32912b 100644 --- a/docs/docs/miscellaneous/upgrade.md +++ b/docs/docs/miscellaneous/upgrade.md @@ -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|).(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 @@ -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 +yarn install && yarn build +``` + +or, if _NotifyBC_ is deployed to Kubernetes using Helm. + +```sh +git pull +git checkout tags/v5.x.x -b +helm upgrade -f helm/platform-specific/.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_. diff --git a/helm/templates/configmap.yaml b/helm/templates/configmap.yaml index 045545c43..1b041c26c 100644 --- a/helm/templates/configmap.yaml +++ b/helm/templates/configmap.yaml @@ -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 }}: | diff --git a/package.json b/package.json index bb9016de4..faaefa9d5 100644 --- a/package.json +++ b/package.json @@ -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": "", diff --git a/server/database/.keep b/server/database/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/src/app.module.ts b/src/app.module.ts index cbf6f13eb..a5bddf1fe 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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({ @@ -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], }), ], diff --git a/src/config/config.factory.ts b/src/config/config.factory.ts index caf993b49..2e4048131 100644 --- a/src/config/config.factory.ts +++ b/src/config/config.factory.ts @@ -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.AppConfig]: undefined, @@ -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([ diff --git a/src/datasources/db.datasource.json b/src/datasources/db.datasource.json new file mode 100644 index 000000000..fefee0e04 --- /dev/null +++ b/src/datasources/db.datasource.json @@ -0,0 +1,3 @@ +{ + "dbPath": "server/database" +} diff --git a/src/datasources/db.datasource.ts b/src/datasources/db.datasource.ts deleted file mode 100644 index 5aeeb4c25..000000000 --- a/src/datasources/db.datasource.ts +++ /dev/null @@ -1,6 +0,0 @@ -export default { - name: 'db', - localStorage: '', - file: 'server/database/data.json', - connector: 'memory', -}; diff --git a/tsconfig.json b/tsconfig.json index f572c2abc..5e429f85d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,7 @@ "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false, - "allowJs": true + "allowJs": true, + "esModuleInterop": true } }