Skip to content

Commit f3f81b6

Browse files
rendongscflovilmart
authored andcommitted
support pg-promise init options (#3613)
* pg-promise init options * add database init options * Create PostgresInitOptions.spec.js * Update PostgresInitOptions.spec.js * Update PostgresInitOptions.spec.js * add PostgresInitOptions test * Add files via upload * linebreaks CRLF to LF * modify postgresURI to test environment * modify pg error code to 42P01 * fix reconfigureServer callback
1 parent 5282868 commit f3f81b6

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

spec/PostgresInitOptions.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const Parse = require('parse/node').Parse;
2+
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter');
3+
const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
4+
5+
//public schema
6+
const databaseOptions1 = {
7+
initOptions: {
8+
connect: function (client, dc, isFresh) {
9+
if (isFresh) {
10+
client.query('SET search_path = public');
11+
}
12+
}
13+
}
14+
};
15+
16+
//not exists schema
17+
const databaseOptions2 = {
18+
initOptions: {
19+
connect: function (client, dc, isFresh) {
20+
if (isFresh) {
21+
client.query('SET search_path = not_exists_schema');
22+
}
23+
}
24+
}
25+
};
26+
27+
const GameScore = Parse.Object.extend({
28+
className: "GameScore"
29+
});
30+
31+
describe('Postgres database init options', () => {
32+
it('create server with public schema databaseOptions,shoud be ok', (done) => {
33+
reconfigureServer({
34+
databaseAdapter: new PostgresStorageAdapter({
35+
uri: postgresURI, collectionPrefix: 'test_',
36+
databaseOptions: databaseOptions1
37+
})
38+
}).then(done, fail);
39+
});
40+
41+
it("save new GameScore in public schema", function (done) {
42+
var score = new GameScore({ "score": 1337, "playerName": "Sean Plott", "cheatMode": false });
43+
score.save().then(done, fail);
44+
});
45+
46+
it('create server with not exists schema databaseOptions,shoud be fail', (done) => {
47+
reconfigureServer({
48+
databaseAdapter: new PostgresStorageAdapter({
49+
uri: postgresURI, collectionPrefix: 'test_',
50+
databaseOptions: databaseOptions2
51+
})
52+
}).then(() => {
53+
done();
54+
})
55+
.catch(error => {
56+
expect(error.code).toEqual('42P01');
57+
done();
58+
});
59+
});
60+
});

src/Adapters/Storage/Postgres/PostgresClient.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const pgp = require('pg-promise')();
1+
22
const parser = require('./PostgresConfigParser');
33

44
export function createClient(uri, databaseOptions) {
@@ -13,11 +13,13 @@ export function createClient(uri, databaseOptions) {
1313
dbOptions[key] = databaseOptions[key];
1414
}
1515

16+
const initOptions = dbOptions.initOptions || {};
17+
const pgp = require('pg-promise')(initOptions);
1618
const client = pgp(dbOptions);
1719

1820
if (dbOptions.pgOptions) {
1921
for (const key in dbOptions.pgOptions) {
20-
client.pg.defaults[key] = dbOptions.pgOptions[key];
22+
pgp.pg.defaults[key] = dbOptions.pgOptions[key];
2123
}
2224
}
2325

{

Whitespace-only changes.

0 commit comments

Comments
 (0)