Skip to content

Commit df81070

Browse files
committed
Check if collection exists in database
1 parent 29e316d commit df81070

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class MongoClientFactory {
2525
const client = new MongoClient(config.get('mongo.url'), { useUnifiedTopology: true });
2626

2727
await client.connect();
28-
client.db(config.get('mongo.name'));
2928

3029
return client;
3130
}

src/apps/mooc_backend/config/config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ const convictConfig = convict({
1212
doc: 'The Mongo connection URL',
1313
format: String,
1414
env: 'MONGO_URL'
15-
},
16-
name: {
17-
doc: 'The Mongo database name',
18-
format: String,
19-
env: 'MONGO_DB_NAME'
2015
}
2116
}
2217
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"mongo": { "url": "mongodb://localhost:27017", "name": "test" }
2+
"mongo": { "url": "mongodb://localhost:27017/test" }
33
}

tests/Contexts/Shared/infrastructure/arranger/MongoEnvironmentArranger.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ export class MongoEnvironmentArranger extends EnvironmentArranger {
77
}
88

99
protected async clean(name: string): Promise<void> {
10-
await (await this.client()).db().dropCollection(name);
10+
const hasCollection = await this.hasCollection(name);
11+
12+
if (hasCollection) {
13+
await (await this.client()).db().dropCollection(name);
14+
}
15+
}
16+
17+
private async hasCollection(name: string): Promise<boolean> {
18+
const client = await this.client();
19+
return await client
20+
.db()
21+
.listCollections({ name }, { nameOnly: true })
22+
.hasNext();
1123
}
1224

1325
protected client(): Promise<MongoClient> {

0 commit comments

Comments
 (0)