Skip to content

support pg-promise init options #3613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 7, 2017
60 changes: 60 additions & 0 deletions spec/PostgresInitOptions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const Parse = require('parse/node').Parse;
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter');
const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';

//public schema
const databaseOptions1 = {
initOptions: {
connect: function (client, dc, isFresh) {
if (isFresh) {
client.query('SET search_path = public');
}
}
}
};

//not exists schema
const databaseOptions2 = {
initOptions: {
connect: function (client, dc, isFresh) {
if (isFresh) {
client.query('SET search_path = not_exists_schema');
}
}
}
};

const GameScore = Parse.Object.extend({
className: "GameScore"
});

describe('Postgres database init options', () => {
it('create server with public schema databaseOptions,shoud be ok', (done) => {
reconfigureServer({
databaseAdapter: new PostgresStorageAdapter({
uri: postgresURI, collectionPrefix: 'test_',
databaseOptions: databaseOptions1
})
}).then(done, fail);
});

it("save new GameScore in public schema", function (done) {
var score = new GameScore({ "score": 1337, "playerName": "Sean Plott", "cheatMode": false });
score.save().then(done, fail);
});

it('create server with not exists schema databaseOptions,shoud be fail', (done) => {
reconfigureServer({
databaseAdapter: new PostgresStorageAdapter({
uri: postgresURI, collectionPrefix: 'test_',
databaseOptions: databaseOptions2
})
}).then(() => {
done();
})
.catch(error => {
expect(error.code).toEqual('42P01');
done();
});
});
});
6 changes: 4 additions & 2 deletions src/Adapters/Storage/Postgres/PostgresClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pgp = require('pg-promise')();

const parser = require('./PostgresConfigParser');

export function createClient(uri, databaseOptions) {
Expand All @@ -13,11 +13,13 @@ export function createClient(uri, databaseOptions) {
dbOptions[key] = databaseOptions[key];
}

const initOptions = dbOptions.initOptions || {};
const pgp = require('pg-promise')(initOptions);
const client = pgp(dbOptions);

if (dbOptions.pgOptions) {
for (const key in dbOptions.pgOptions) {
client.pg.defaults[key] = dbOptions.pgOptions[key];
pgp.pg.defaults[key] = dbOptions.pgOptions[key];
}
}

Expand Down
Empty file added {
Empty file.