Skip to content

Commit 7af3209

Browse files
kulshekharflovilmart
authored andcommitted
Use the Postgres Adapter with a Postgres URI (#2871)
* Use the Postgres Adapter with a Postgres URI * Handle malformed databaseURI
1 parent c253daa commit 7af3209

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/ParseServer.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var batch = require('./batch'),
77
multer = require('multer'),
88
Parse = require('parse/node').Parse,
99
path = require('path'),
10+
url = require('url'),
1011
authDataManager = require('./authDataManager');
1112

1213
import defaults from './defaults';
@@ -54,6 +55,7 @@ import DatabaseController from './Controllers/DatabaseController';
5455
import SchemaCache from './Controllers/SchemaCache';
5556
import ParsePushAdapter from 'parse-server-push-adapter';
5657
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
58+
import PostgresStorageAdapter from './Adapters/Storage/Postgres/PostgresStorageAdapter';
5759

5860
import { ParseServerRESTController } from './ParseServerRESTController';
5961
// Mutate the Parse object to add the Cloud Code handlers
@@ -145,11 +147,7 @@ class ParseServer {
145147
if ((databaseOptions || (databaseURI && databaseURI != defaults.DefaultMongoURI) || collectionPrefix !== '') && databaseAdapter) {
146148
throw 'You cannot specify both a databaseAdapter and a databaseURI/databaseOptions/collectionPrefix.';
147149
} else if (!databaseAdapter) {
148-
databaseAdapter = new MongoStorageAdapter({
149-
uri: databaseURI,
150-
collectionPrefix,
151-
mongoOptions: databaseOptions,
152-
});
150+
databaseAdapter = this.getDatabaseAdapter(databaseURI, collectionPrefix, databaseOptions)
153151
} else {
154152
databaseAdapter = loadAdapter(databaseAdapter)
155153
}
@@ -252,6 +250,27 @@ class ParseServer {
252250
}
253251
}
254252

253+
getDatabaseAdapter(databaseURI, collectionPrefix, databaseOptions) {
254+
let protocol;
255+
try{
256+
const parsedURI = url.parse(databaseURI);
257+
protocol = parsedURI.protocol ? parsedURI.protocol.toLowerCase() : null;
258+
}catch(e){}
259+
switch (protocol) {
260+
case 'postgres:':
261+
return new PostgresStorageAdapter({
262+
uri: databaseURI,
263+
collectionPrefix
264+
});
265+
default:
266+
return new MongoStorageAdapter({
267+
uri: databaseURI,
268+
collectionPrefix,
269+
mongoOptions: databaseOptions,
270+
});
271+
}
272+
}
273+
255274
get app() {
256275
return ParseServer.app(this.config);
257276
}

0 commit comments

Comments
 (0)