Skip to content

Commit

Permalink
feat: support postgresql protocol in database URI (#7757)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 authored Jan 2, 2022
1 parent 912edac commit caf4a23
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 55 deletions.
92 changes: 38 additions & 54 deletions spec/PostgresInitOptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const PostgresStorageAdapter = require('../lib/Adapters/Storage/Postgres/Postgre
const postgresURI =
process.env.PARSE_SERVER_TEST_DATABASE_URI ||
'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
const ParseServer = require('../lib/index');
const express = require('express');

//public schema
const databaseOptions1 = {
initOptions: {
Expand All @@ -24,72 +23,57 @@ const GameScore = Parse.Object.extend({
className: 'GameScore',
});

function createParseServer(options) {
return new Promise((resolve, reject) => {
const parseServer = new ParseServer.default(
Object.assign({}, defaultConfiguration, options, {
serverURL: 'http://localhost:12668/parse',
serverStartComplete: error => {
if (error) {
reject(error);
} else {
expect(Parse.applicationId).toEqual('test');
const app = express();
app.use('/parse', parseServer.app);

const server = app.listen(12668);
Parse.serverURL = 'http://localhost:12668/parse';
resolve(server);
}
},
})
);
});
}

describe_only_db('postgres')('Postgres database init options', () => {
let server;

afterAll(done => {
if (server) {
Parse.serverURL = 'http://localhost:8378/1';
server.close(done);
}
});

it('should create server with public schema databaseOptions', done => {
it('should create server with public schema databaseOptions', async () => {
const adapter = new PostgresStorageAdapter({
uri: postgresURI,
collectionPrefix: 'test_',
databaseOptions: databaseOptions1,
});
await reconfigureServer({
databaseAdapter: adapter,
});
const score = new GameScore({
score: 1337,
playerName: 'Sean Plott',
cheatMode: false,
});
await score.save();
});

createParseServer({ databaseAdapter: adapter })
.then(newServer => {
server = newServer;
const score = new GameScore({
score: 1337,
playerName: 'Sean Plott',
cheatMode: false,
});
return score.save();
})
.then(async () => {
await reconfigureServer();
done();
}, done.fail);
it('should create server using postgresql uri with public schema databaseOptions', async () => {
const postgresURI2 = new URL(postgresURI);
postgresURI2.protocol = 'postgresql:';
const adapter = new PostgresStorageAdapter({
uri: postgresURI2.toString(),
collectionPrefix: 'test_',
databaseOptions: databaseOptions1,
});
await reconfigureServer({
databaseAdapter: adapter,
});
const score = new GameScore({
score: 1337,
playerName: 'Sean Plott',
cheatMode: false,
});
await score.save();
});

it('should fail to create server if schema databaseOptions does not exist', done => {
it('should fail to create server if schema databaseOptions does not exist', async () => {
const adapter = new PostgresStorageAdapter({
uri: postgresURI,
collectionPrefix: 'test_',
databaseOptions: databaseOptions2,
});

createParseServer({ databaseAdapter: adapter }).then(done.fail, async () => {
await reconfigureServer();
done();
});
try {
await reconfigureServer({
databaseAdapter: adapter,
});
fail("Should have thrown error");
} catch(error) {
expect(error).toBeDefined();
}
});
});
11 changes: 10 additions & 1 deletion spec/PostgresStorageAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
},
classLevelPermissions: undefined,
});
await new Promise(resolve => setTimeout(resolve, 500));
await new Promise(resolve => setTimeout(resolve, 2000));
expect(adapter._onchange).toHaveBeenCalled();
});
});
Expand All @@ -567,4 +567,13 @@ describe_only_db('postgres')('PostgresStorageAdapter shutdown', () => {
adapter.handleShutdown();
expect(adapter._client.$pool.ending).toEqual(true);
});

it('handleShutdown, close connection of postgresql uri', () => {
const databaseURI2 = new URL(databaseURI);
databaseURI2.protocol = 'postgresql:';
const adapter = new PostgresStorageAdapter({ uri: databaseURI2.toString() });
expect(adapter._client.$pool.ending).toEqual(false);
adapter.handleShutdown();
expect(adapter._client.$pool.ending).toEqual(true);
});
});
1 change: 1 addition & 0 deletions src/Controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption
}
switch (protocol) {
case 'postgres:':
case 'postgresql:':
return new PostgresStorageAdapter({
uri: databaseURI,
collectionPrefix,
Expand Down

0 comments on commit caf4a23

Please sign in to comment.