Skip to content

Commit 2649b5c

Browse files
author
Eldar B
authored
Merge pull request #47 from PCOffline/feature/validate-db-uri
feat: add db uri validation
2 parents 0e68212 + a37dc52 commit 2649b5c

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [0.4.1](https://github.com/pcoffline/reset-db/compare/v0.4.0...v0.4.1) (2022-10-08)
6+
7+
8+
### Features
9+
10+
* add db uri validation ([fb81cd5](https://github.com/pcoffline/reset-db/commit/fb81cd59bf81d088db6d0e257fe4b59d3a1d6b03))
11+
12+
13+
### Bug Fixes
14+
15+
* package.json & package-lock.json to reduce vulnerabilities ([8570b4d](https://github.com/pcoffline/reset-db/commit/8570b4d89f6fb2147a4f876c7635032433ad8825))
16+
517
## [0.4.0](https://github.com/PCOffline/reset-db/compare/v0.3.0...v0.4.0) (2022-01-24)
618

719

index.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ async function evaluateCollectionValues({
162162
}
163163

164164
async function initialise() {
165+
// Validate Mongo URI values
166+
const mongoUriValues = new Set(
167+
['MONGO_URI', 'MONGODB_URI', 'mongoUri', 'DB_URI', 'DATABASE_URI'].filter(
168+
(name) => name in process.env,
169+
),
170+
);
171+
172+
if (!config.mongoUri && !mongoUriValues.size()) {
173+
logError('No MongoDB URI provided');
174+
throw new Error('No MongoDB URI provided');
175+
}
176+
177+
if (
178+
mongoUriValues.size() > 1 ||
179+
(mongoUriValues.size() &&
180+
config.mongoUri !== mongoUriValues.values().next())
181+
) {
182+
logError(
183+
'Mismatch between MongoDB URIs:',
184+
[config.mongoUri, ...Array.from(mongoUriValues)]
185+
.filter((uri) => uri)
186+
.join(', '),
187+
);
188+
throw new Error('Mismatch between MongoDB URIs');
189+
}
190+
191+
// If config.mongoUri is unset, that means that the value is in the process.env
192+
config.mongoUri ??= mongoUriValues.values().next();
193+
165194
// Connect to mongoose
166195
await loadPromise(
167196
mongoose.connect(
@@ -235,18 +264,18 @@ async function initialise() {
235264
}),
236265
);
237266

238-
return loadPromise(modelsPromise, {
267+
await loadPromise(modelsPromise, {
239268
text: 'Creating models',
240269
successText: 'Created models',
241270
failText: 'Failed to create models',
242-
})
243-
.then(logDebugData)
244-
.then(() => {
245-
if (standardCollections.length === 0) {
246-
logError('No valid collections found!');
247-
throw new Error('No valid collections found!');
248-
}
249-
});
271+
});
272+
273+
logDebugData();
274+
275+
if (standardCollections.length === 0) {
276+
logError('No valid collections found!');
277+
throw new Error('No valid collections found!');
278+
}
250279
}
251280

252281
async function dryRun() {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "reset-db",
33
"type": "module",
4-
"version": "0.4.0",
4+
"version": "0.4.1",
55
"description": "A fast and easy way to reset your collections",
66
"main": "index.js",
77
"scripts": {

0 commit comments

Comments
 (0)