This repository was archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmongo.js
More file actions
87 lines (75 loc) · 2.55 KB
/
Copy pathmongo.js
File metadata and controls
87 lines (75 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// provides a service to the API server, making mongo available for data storage
// also provides the mongo collections (according to configuration) available as a data source
'use strict';
const APIServerModule = require(process.env.CSSVC_BACKEND_ROOT + '/api_server/lib/api_server/api_server_module.js');
const MongoClient = require(process.env.CSSVC_BACKEND_ROOT + '/shared/server_utils/mongo/mongo_client.js');
const DEPENDENCIES = [
'access_logger' // since we do query logging, we need the access logger module
];
const ROUTES = [
{
method: 'delete',
path: 'no-auth/--clear-mock-cache',
func: 'handleClearMockCache'
},
{
method: 'delete',
path: 'no-auth/--delete-test-data',
func: 'handleDeleteTestData'
}
];
class Mongo extends APIServerModule {
getDependencies () {
return DEPENDENCIES;
}
getRoutes () {
return ROUTES;
}
services () {
// return a function that, when invoked, will return a service structure with our
// mongo client as a service to the API server app
return async () => {
if (!this.api.config.storage.mongo) {
this.api.warn('Will not connect to mongo, no mongo configuration supplied');
return;
}
this.mongoClientFactory = new MongoClient({
tryIndefinitely: true,
mockMode: this.api.config.apiServer.mockMode,
logger: this.api.logger,
queryLogging: this.api.config.storage.mongo.queryLogging,
collections: this.api.serverOptions.rawCollections,
hintsRequired: this.api.config.storage.mongo.hintsRequired
});
this.mongoClient = await this.mongoClientFactory.openMongoClient(this.api.config.storage.mongo);
return { mongoClient: this.mongoClient };
};
}
dataSources () {
// return the mongo collections as a data source, for implementation-immune reading and writing of data
return () => {
return this.mongoClient.mongoCollections;
};
}
handleClearMockCache (request, response) {
if (this.api.config.apiServer.mockMode) {
this.mongoClient.clearMockCache();
response.status(200).send({});
}
else {
response.status(401).send('NOT IN MOCK MODE');
}
}
handleDeleteTestData (request, response) {
(async function() {
if (request.headers['x-cs-delete-cheat'] !== this.api.config.sharedSecrets.subscriptionCheat) {
response.status(401).send('UNAUTHORIZED');
}
await Promise.all(Object.keys(this.mongoClient.mongoCollections).map(async collection => {
await this.mongoClient.mongoCollections[collection].deleteByQuery({_forTesting:true}, {overrideHintRequired:true});
}));
response.status(200).send();
}).call(this);
}
}
module.exports = Mongo;