From f3f81b692bcb54da1090d945cf55a4890111a2a6 Mon Sep 17 00:00:00 2001 From: ren dong Date: Fri, 7 Apr 2017 19:50:55 +0800 Subject: [PATCH] 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 --- spec/PostgresInitOptions.spec.js | 60 +++++++++++++++++++ .../Storage/Postgres/PostgresClient.js | 6 +- { | 0 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 spec/PostgresInitOptions.spec.js create mode 100644 { diff --git a/spec/PostgresInitOptions.spec.js b/spec/PostgresInitOptions.spec.js new file mode 100644 index 00000000..825eacaf --- /dev/null +++ b/spec/PostgresInitOptions.spec.js @@ -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(); + }); + }); +}); diff --git a/src/Adapters/Storage/Postgres/PostgresClient.js b/src/Adapters/Storage/Postgres/PostgresClient.js index 0f4b3c8d..a94eb9c2 100644 --- a/src/Adapters/Storage/Postgres/PostgresClient.js +++ b/src/Adapters/Storage/Postgres/PostgresClient.js @@ -1,4 +1,4 @@ -const pgp = require('pg-promise')(); + const parser = require('./PostgresConfigParser'); export function createClient(uri, databaseOptions) { @@ -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]; } } diff --git a/{ b/{ new file mode 100644 index 00000000..e69de29b