Skip to content

Commit 8ec565d

Browse files
fix: get mongodb url and database name from MONGODB_URI variable
1 parent 68fad91 commit 8ec565d

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

.env.example

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,4 @@ SITE=<SITE>
3434
SMTP_HOST=<SMTP_HOST>
3535
SMTP_MESSAGE_FROM=<SMTP_MESSAGE_FROM>
3636
SMTP_PORT=<SMTP_PORT>
37-
SMTP_SECURE=<SMTP_SECURE>
38-
DB_MIGRATE_MONGODB_URL="mongodb://<HOST>:<PORT>"
39-
DB_MIGRATE_DATABASE_NAME="<DATABASE_NAME>"
37+
SMTP_SECURE=<SMTP_SECURE>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Valid environment variables for the .env file. See [.env.example](/.env.example)
108108

109109
Where the [current SciCat Backend](https://github.com/SciCatProject/backend) accepts id fields in the database named `pid`, `doi`, or similar, this implementation requires there to be an id field of the form `_id` on every document. It is therefore necessary to run a database migration script towards MongoDB instance from a place where you have access to it and can install `migrate-mongo` package.
110110

111-
All database [migration scripts](https://github.com/SciCatProject/scicat-backend-next/tree/master/migrations) located in the [migrations](/migrations/) folder. Copy these together with [`migrate-mongo-config.js`](https://github.com/SciCatProject/scicat-backend-next/blob/master/migrate-mongo-config.js) file to a location were you can access your MongoDB instance and have `migrate-mongo` package installed either globally(which is preferred) or locally if global installation is not possible. Then modify the config file to contain your MongoDB instance details like database name and url. Once modified, start the migration by running
111+
All database [migration scripts](https://github.com/SciCatProject/scicat-backend-next/tree/master/migrations) located in the [migrations](/migrations/) folder. To start the migration use:
112112

113113
```sh
114114
npm run migrate:db:up

migrate-mongo-config.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
// In this file you can configure migrate-mongo
2+
const MONGODB_URI = require("dotenv").config().parsed.MONGODB_URI;
3+
4+
if (!MONGODB_URI) {
5+
throw new Error("Environment variable MONGODB_URI not set");
6+
}
7+
8+
const lastSlashIndex = MONGODB_URI.lastIndexOf("/");
9+
// NOTE: Get the database url and name from the MONGODB_URI and use them instead of defining new variables.
10+
const DATABASE_URL = MONGODB_URI.substring(0, lastSlashIndex);
11+
const DATABASE_NAME = MONGODB_URI.substring(lastSlashIndex + 1);
212

313
const config = {
414
mongodb: {
5-
// TODO Change (or review) the url to your MongoDB:
6-
url: process.env.DB_MIGRATE_MONGODB_URL,
15+
url: DATABASE_URL,
716

8-
// TODO Change this to your database name:
9-
databaseName: process.env.DB_MIGRATE_DATABASE_NAME,
17+
databaseName: DATABASE_NAME,
1018

1119
options: {
1220
useNewUrlParser: true, // removes a deprecation warning when connecting
1321
useUnifiedTopology: true, // removes a deprecating warning when connecting
1422
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
1523
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
16-
}
24+
},
1725
},
1826

1927
// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
@@ -22,15 +30,15 @@ const config = {
2230
// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
2331
changelogCollectionName: "changelog",
2432

25-
// The file extension to create migrations and search for in migration dir
33+
// The file extension to create migrations and search for in migration dir
2634
migrationFileExtension: ".js",
2735

2836
// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determine
2937
// if the file should be run. Requires that scripts are coded to be run multiple times.
3038
useFileHash: false,
3139

3240
// Don't change this, unless you know what you're doing
33-
moduleSystem: 'commonjs',
41+
moduleSystem: "commonjs",
3442
};
3543

3644
module.exports = config;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"prebuild": "rimraf dist",
1111
"build": "nest build",
1212
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
13-
"migrate:db:up": "node ./scripts/setVariables.js && node ./node_modules/migrate-mongo/bin/migrate-mongo.js up",
14-
"migrate:db:down": "node ./scripts/setVariables.js && node ./node_modules/migrate-mongo/bin/migrate-mongo.js down",
13+
"migrate:db:up": "node ./node_modules/migrate-mongo/bin/migrate-mongo.js up",
14+
"migrate:db:down": "node ./node_modules/migrate-mongo/bin/migrate-mongo.js down",
1515
"start": "nest start",
1616
"start:dev": "nest start --watch",
1717
"start:debug": "nest start --debug --watch",

scripts/setVariables.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)