forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseAdapter.js
More file actions
74 lines (63 loc) · 2.09 KB
/
DatabaseAdapter.js
File metadata and controls
74 lines (63 loc) · 2.09 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
/** @flow weak */
// Database Adapter
//
// Allows you to change the underlying database.
//
// Adapter classes must implement the following methods:
// * a constructor with signature (connectionString, optionsObject)
// * connect()
// * loadSchema()
// * create(className, object)
// * find(className, query, options)
// * update(className, query, update, options)
// * destroy(className, query, options)
// * This list is incomplete and the database process is not fully modularized.
//
// Default is MongoStorageAdapter.
import DatabaseController from './Controllers/DatabaseController';
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
let dbConnections = {};
let appDatabaseURIs = {};
let appDatabaseOptions = {};
function setAppDatabaseURI(appId, uri) {
appDatabaseURIs[appId] = uri;
}
function setAppDatabaseOptions(appId: string, options: Object) {
appDatabaseOptions[appId] = options;
}
//Used by tests
function clearDatabaseSettings() {
appDatabaseURIs = {};
dbConnections = {};
appDatabaseOptions = {};
}
//Used by tests
function destroyAllDataPermanently() {
if (process.env.TESTING) {
var promises = [];
for (var conn in dbConnections) {
promises.push(dbConnections[conn].deleteEverything());
}
return Promise.all(promises);
}
throw 'Only supported in test environment';
}
function getDatabaseConnection(appId: string, collectionPrefix: string) {
if (dbConnections[appId]) {
return dbConnections[appId];
}
let mongoAdapterOptions = {
collectionPrefix: collectionPrefix,
mongoOptions: appDatabaseOptions[appId],
uri: appDatabaseURIs[appId], //may be undefined if the user didn't supply a URI, in which case the default will be used
}
dbConnections[appId] = new DatabaseController(new MongoStorageAdapter(mongoAdapterOptions));
return dbConnections[appId];
}
module.exports = {
getDatabaseConnection: getDatabaseConnection,
setAppDatabaseOptions: setAppDatabaseOptions,
setAppDatabaseURI: setAppDatabaseURI,
clearDatabaseSettings: clearDatabaseSettings,
destroyAllDataPermanently: destroyAllDataPermanently,
};