feat: support postgresql protocol in database URI (#7757)
This commit is contained in:
@@ -4,8 +4,7 @@ const PostgresStorageAdapter = require('../lib/Adapters/Storage/Postgres/Postgre
|
|||||||
const postgresURI =
|
const postgresURI =
|
||||||
process.env.PARSE_SERVER_TEST_DATABASE_URI ||
|
process.env.PARSE_SERVER_TEST_DATABASE_URI ||
|
||||||
'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
|
'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
|
||||||
const ParseServer = require('../lib/index');
|
|
||||||
const express = require('express');
|
|
||||||
//public schema
|
//public schema
|
||||||
const databaseOptions1 = {
|
const databaseOptions1 = {
|
||||||
initOptions: {
|
initOptions: {
|
||||||
@@ -24,72 +23,57 @@ const GameScore = Parse.Object.extend({
|
|||||||
className: 'GameScore',
|
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', () => {
|
describe_only_db('postgres')('Postgres database init options', () => {
|
||||||
let server;
|
|
||||||
|
|
||||||
afterAll(done => {
|
it('should create server with public schema databaseOptions', async () => {
|
||||||
if (server) {
|
|
||||||
Parse.serverURL = 'http://localhost:8378/1';
|
|
||||||
server.close(done);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should create server with public schema databaseOptions', done => {
|
|
||||||
const adapter = new PostgresStorageAdapter({
|
const adapter = new PostgresStorageAdapter({
|
||||||
uri: postgresURI,
|
uri: postgresURI,
|
||||||
collectionPrefix: 'test_',
|
collectionPrefix: 'test_',
|
||||||
databaseOptions: databaseOptions1,
|
databaseOptions: databaseOptions1,
|
||||||
});
|
});
|
||||||
|
await reconfigureServer({
|
||||||
createParseServer({ databaseAdapter: adapter })
|
databaseAdapter: adapter,
|
||||||
.then(newServer => {
|
});
|
||||||
server = newServer;
|
const score = new GameScore({
|
||||||
const score = new GameScore({
|
score: 1337,
|
||||||
score: 1337,
|
playerName: 'Sean Plott',
|
||||||
playerName: 'Sean Plott',
|
cheatMode: false,
|
||||||
cheatMode: false,
|
});
|
||||||
});
|
await score.save();
|
||||||
return score.save();
|
|
||||||
})
|
|
||||||
.then(async () => {
|
|
||||||
await reconfigureServer();
|
|
||||||
done();
|
|
||||||
}, done.fail);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail to create server if schema databaseOptions does not exist', done => {
|
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', async () => {
|
||||||
const adapter = new PostgresStorageAdapter({
|
const adapter = new PostgresStorageAdapter({
|
||||||
uri: postgresURI,
|
uri: postgresURI,
|
||||||
collectionPrefix: 'test_',
|
collectionPrefix: 'test_',
|
||||||
databaseOptions: databaseOptions2,
|
databaseOptions: databaseOptions2,
|
||||||
});
|
});
|
||||||
|
try {
|
||||||
createParseServer({ databaseAdapter: adapter }).then(done.fail, async () => {
|
await reconfigureServer({
|
||||||
await reconfigureServer();
|
databaseAdapter: adapter,
|
||||||
done();
|
});
|
||||||
});
|
fail("Should have thrown error");
|
||||||
|
} catch(error) {
|
||||||
|
expect(error).toBeDefined();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -555,7 +555,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
|
|||||||
},
|
},
|
||||||
classLevelPermissions: undefined,
|
classLevelPermissions: undefined,
|
||||||
});
|
});
|
||||||
await new Promise(resolve => setTimeout(resolve, 500));
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||||
expect(adapter._onchange).toHaveBeenCalled();
|
expect(adapter._onchange).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -567,4 +567,13 @@ describe_only_db('postgres')('PostgresStorageAdapter shutdown', () => {
|
|||||||
adapter.handleShutdown();
|
adapter.handleShutdown();
|
||||||
expect(adapter._client.$pool.ending).toEqual(true);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -227,6 +227,7 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption
|
|||||||
}
|
}
|
||||||
switch (protocol) {
|
switch (protocol) {
|
||||||
case 'postgres:':
|
case 'postgres:':
|
||||||
|
case 'postgresql:':
|
||||||
return new PostgresStorageAdapter({
|
return new PostgresStorageAdapter({
|
||||||
uri: databaseURI,
|
uri: databaseURI,
|
||||||
collectionPrefix,
|
collectionPrefix,
|
||||||
|
|||||||
Reference in New Issue
Block a user