Skip to content

Commit caf4a23

Browse files
authored
feat: support postgresql protocol in database URI (#7757)
1 parent 912edac commit caf4a23

File tree

3 files changed

+49
-55
lines changed

3 files changed

+49
-55
lines changed

Diff for: spec/PostgresInitOptions.spec.js

+38-54
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const PostgresStorageAdapter = require('../lib/Adapters/Storage/Postgres/Postgre
44
const postgresURI =
55
process.env.PARSE_SERVER_TEST_DATABASE_URI ||
66
'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
7-
const ParseServer = require('../lib/index');
8-
const express = require('express');
7+
98
//public schema
109
const databaseOptions1 = {
1110
initOptions: {
@@ -24,72 +23,57 @@ const GameScore = Parse.Object.extend({
2423
className: 'GameScore',
2524
});
2625

27-
function createParseServer(options) {
28-
return new Promise((resolve, reject) => {
29-
const parseServer = new ParseServer.default(
30-
Object.assign({}, defaultConfiguration, options, {
31-
serverURL: 'http://localhost:12668/parse',
32-
serverStartComplete: error => {
33-
if (error) {
34-
reject(error);
35-
} else {
36-
expect(Parse.applicationId).toEqual('test');
37-
const app = express();
38-
app.use('/parse', parseServer.app);
39-
40-
const server = app.listen(12668);
41-
Parse.serverURL = 'http://localhost:12668/parse';
42-
resolve(server);
43-
}
44-
},
45-
})
46-
);
47-
});
48-
}
49-
5026
describe_only_db('postgres')('Postgres database init options', () => {
51-
let server;
52-
53-
afterAll(done => {
54-
if (server) {
55-
Parse.serverURL = 'http://localhost:8378/1';
56-
server.close(done);
57-
}
58-
});
5927

60-
it('should create server with public schema databaseOptions', done => {
28+
it('should create server with public schema databaseOptions', async () => {
6129
const adapter = new PostgresStorageAdapter({
6230
uri: postgresURI,
6331
collectionPrefix: 'test_',
6432
databaseOptions: databaseOptions1,
6533
});
34+
await reconfigureServer({
35+
databaseAdapter: adapter,
36+
});
37+
const score = new GameScore({
38+
score: 1337,
39+
playerName: 'Sean Plott',
40+
cheatMode: false,
41+
});
42+
await score.save();
43+
});
6644

67-
createParseServer({ databaseAdapter: adapter })
68-
.then(newServer => {
69-
server = newServer;
70-
const score = new GameScore({
71-
score: 1337,
72-
playerName: 'Sean Plott',
73-
cheatMode: false,
74-
});
75-
return score.save();
76-
})
77-
.then(async () => {
78-
await reconfigureServer();
79-
done();
80-
}, done.fail);
45+
it('should create server using postgresql uri with public schema databaseOptions', async () => {
46+
const postgresURI2 = new URL(postgresURI);
47+
postgresURI2.protocol = 'postgresql:';
48+
const adapter = new PostgresStorageAdapter({
49+
uri: postgresURI2.toString(),
50+
collectionPrefix: 'test_',
51+
databaseOptions: databaseOptions1,
52+
});
53+
await reconfigureServer({
54+
databaseAdapter: adapter,
55+
});
56+
const score = new GameScore({
57+
score: 1337,
58+
playerName: 'Sean Plott',
59+
cheatMode: false,
60+
});
61+
await score.save();
8162
});
8263

83-
it('should fail to create server if schema databaseOptions does not exist', done => {
64+
it('should fail to create server if schema databaseOptions does not exist', async () => {
8465
const adapter = new PostgresStorageAdapter({
8566
uri: postgresURI,
8667
collectionPrefix: 'test_',
8768
databaseOptions: databaseOptions2,
8869
});
89-
90-
createParseServer({ databaseAdapter: adapter }).then(done.fail, async () => {
91-
await reconfigureServer();
92-
done();
93-
});
70+
try {
71+
await reconfigureServer({
72+
databaseAdapter: adapter,
73+
});
74+
fail("Should have thrown error");
75+
} catch(error) {
76+
expect(error).toBeDefined();
77+
}
9478
});
9579
});

Diff for: spec/PostgresStorageAdapter.spec.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
555555
},
556556
classLevelPermissions: undefined,
557557
});
558-
await new Promise(resolve => setTimeout(resolve, 500));
558+
await new Promise(resolve => setTimeout(resolve, 2000));
559559
expect(adapter._onchange).toHaveBeenCalled();
560560
});
561561
});
@@ -567,4 +567,13 @@ describe_only_db('postgres')('PostgresStorageAdapter shutdown', () => {
567567
adapter.handleShutdown();
568568
expect(adapter._client.$pool.ending).toEqual(true);
569569
});
570+
571+
it('handleShutdown, close connection of postgresql uri', () => {
572+
const databaseURI2 = new URL(databaseURI);
573+
databaseURI2.protocol = 'postgresql:';
574+
const adapter = new PostgresStorageAdapter({ uri: databaseURI2.toString() });
575+
expect(adapter._client.$pool.ending).toEqual(false);
576+
adapter.handleShutdown();
577+
expect(adapter._client.$pool.ending).toEqual(true);
578+
});
570579
});

Diff for: src/Controllers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption
227227
}
228228
switch (protocol) {
229229
case 'postgres:':
230+
case 'postgresql:':
230231
return new PostgresStorageAdapter({
231232
uri: databaseURI,
232233
collectionPrefix,

0 commit comments

Comments
 (0)