From f99e397699ccebda1cd085ba7e5b1d44012ba3d2 Mon Sep 17 00:00:00 2001 From: anon Date: Mon, 15 Apr 2019 14:17:33 -0400 Subject: [PATCH] Add schemas to config --- .gitignore | 1 + postgraphile/README.md | 9 ++++++++ postgraphile/package.json | 1 + postgraphile/spec/config/parse.spec.ts | 3 ++- postgraphile/spec/server/config.spec.ts | 8 +++++-- postgraphile/src/adapters/fs.ts | 2 +- postgraphile/src/adapters/postgraphile.ts | 2 +- postgraphile/src/config/parse.ts | 28 +++++++++++++++-------- postgraphile/src/server/config.ts | 2 +- postgraphile/src/server/interface.ts | 3 ++- 10 files changed, 42 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index d29ae381..78fb54c5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ postgraphile/package-lock.json vulcanizedb.log db/migrations/20*.sql plugins/*.so +postgraphile/*.toml diff --git a/postgraphile/README.md b/postgraphile/README.md index 0bd5958c..cd17c7d2 100644 --- a/postgraphile/README.md +++ b/postgraphile/README.md @@ -13,6 +13,15 @@ Build the docker image in this directory. Start the `GraphiQL` frontend by: * Run the container (ex. `docker run -e DATABASE_HOST=localhost -e DATABASE_NAME=vulcanize_public -e DATABASE_USER=vulcanize -e DATABASE_PASSWORD=vulcanize -d m0ar/images:postgraphile-alpine`) * GraphiQL is available at `:3000/graphiql` +By default, this build will expose only the "public" schema - to add other schemas, use a config file `config.toml` and set the env var `CONFIG_PATH` to point to its location. Example `toml`: + +``` +[database] + name = "vulcanize_public" + hostname = "localhost" + port = 5432 + schemas = ["public", "yourschema"] +``` ## Building diff --git a/postgraphile/package.json b/postgraphile/package.json index 42ef634b..c085ad20 100644 --- a/postgraphile/package.json +++ b/postgraphile/package.json @@ -7,6 +7,7 @@ "lint": "tslint --project ./tsconfig.json --config ./tslint.json", "postinstall": "rm -rf node_modules/@graphile && mkdir node_modules/@graphile && cp -R ./vendor/postgraphile-supporter/ ./node_modules/@graphile/plugin-supporter/", "start": "npm run build && node ./build/dist/vulcanize-postgraphile-server.js", + "dev": "./node_modules/typescript/bin/tsc && node build/dist/src/index.js", "test": "rm -rf ./build/spec && tsc --build ./tsconfig.test.json && jasmine --config=./spec/support/jasmine.json", "test:ci": "npm run lint && npm run test" }, diff --git a/postgraphile/spec/config/parse.spec.ts b/postgraphile/spec/config/parse.spec.ts index 4756cb3c..954cf599 100644 --- a/postgraphile/spec/config/parse.spec.ts +++ b/postgraphile/spec/config/parse.spec.ts @@ -33,7 +33,8 @@ describe('parseConfig', () => { const databaseConfig = parseConfig( readCallback, tomlParseCallback, configPath); - expect(databaseConfig.host).toEqual('postgres://user:password@example.com:1234'); + expect(databaseConfig.host) + .toEqual('postgres://user:password@example.com:1234'); }); it('provides the database name', () => { diff --git a/postgraphile/spec/server/config.spec.ts b/postgraphile/spec/server/config.spec.ts index 1a6c17f9..964bcf97 100644 --- a/postgraphile/spec/server/config.spec.ts +++ b/postgraphile/spec/server/config.spec.ts @@ -21,7 +21,11 @@ describe('buildServerConfig', () => { let databaseConfig: DatabaseConfig; beforeEach(() => { - databaseConfig = { host: 'example.com', database: 'example_database' }; + databaseConfig = { + host: 'example.com', + database: 'example_database', + schemas: ['public'] + }; postgraphileMiddleware = jasmine .createSpyObj(['call']), @@ -84,7 +88,7 @@ describe('buildServerConfig', () => { it('provides the database config to Postgraphile', () => { expect(serverUtilities.postgraphile).toHaveBeenCalledWith( `${databaseConfig.host}/${databaseConfig.database}`, - ["public"], + databaseConfig.schemas, jasmine.any(Object)); }); diff --git a/postgraphile/src/adapters/fs.ts b/postgraphile/src/adapters/fs.ts index 68716e55..7f42e893 100644 --- a/postgraphile/src/adapters/fs.ts +++ b/postgraphile/src/adapters/fs.ts @@ -8,4 +8,4 @@ export type ReadFileSyncCallback = ( ) => string | Buffer; export type TomlParseCallback - = (fileContents: string) => { [key: string]: { [key: string]: string } }; + = (fileContents: string) => { [key: string]: { [key: string]: any } }; diff --git a/postgraphile/src/adapters/postgraphile.ts b/postgraphile/src/adapters/postgraphile.ts index 17e4bf4e..37a0ecfe 100644 --- a/postgraphile/src/adapters/postgraphile.ts +++ b/postgraphile/src/adapters/postgraphile.ts @@ -9,7 +9,7 @@ import { PluginHookFn } from 'postgraphile/build/postgraphile/pluginHook'; export interface PostgraphileMiddleware extends RequestHandler {} export interface PostgraphileOptions { - pluginHook: PluginHookFn, + pluginHook: PluginHookFn; simpleSubscriptions: boolean; watchPg: boolean; enableCors: boolean; diff --git a/postgraphile/src/config/parse.ts b/postgraphile/src/config/parse.ts index 513aa4fe..30960bbc 100644 --- a/postgraphile/src/config/parse.ts +++ b/postgraphile/src/config/parse.ts @@ -6,8 +6,10 @@ export const MISSING_PATH_MESSAGE = `No path to config toml file provided, ` + `please check the value of ${CONFIG_PATH_KEY} in your environment`; export const MISSING_HOST_MESSAGE = 'No database host provided in config toml'; -export const MISSING_USER_MESSAGE = 'No database user & password provided in config toml'; -export const MISSING_DATABASE_MESSAGE = 'No database name provided in config toml'; +export const MISSING_USER_MESSAGE = 'No database user & password ' + + 'provided in config toml'; +export const MISSING_DATABASE_MESSAGE = 'No database name provided ' + + 'in config toml'; export function parseConfig( readCallback: ReadFileSyncCallback, @@ -19,16 +21,18 @@ export function parseConfig( let database = ''; let user = ''; let password = ''; + let schemas = ['public']; if (configPath) { - const tomlContents = readCallback(`${configPath}`).toString(); - const parsedToml = tomlParseCallback(tomlContents); + const tomlContents = readCallback(`${configPath}`).toString(); + const parsedToml = tomlParseCallback(tomlContents); - host = parsedToml['database']['hostname']; - port = parsedToml['database']['port']; - database = parsedToml['database']['name']; - user = parsedToml['database']['user']; - password = parsedToml['database']['password']; + host = parsedToml['database']['hostname']; + port = parsedToml['database']['port']; + database = parsedToml['database']['name']; + user = parsedToml['database']['user']; + password = parsedToml['database']['password']; + schemas = parsedToml['database']['schemas']; } // Overwrite config values with env. vars if such are set @@ -50,5 +54,9 @@ export function parseConfig( throw new Error(MISSING_USER_MESSAGE); } - return { host: `postgres://${user}:${password}@${host}:${port}`, database }; + return { + host: `postgres://${user}:${password}@${host}:${port}`, + database, + schemas + }; } diff --git a/postgraphile/src/server/config.ts b/postgraphile/src/server/config.ts index e2be53a1..c2a2bace 100644 --- a/postgraphile/src/server/config.ts +++ b/postgraphile/src/server/config.ts @@ -39,7 +39,7 @@ export function buildServerConfig( const middleware: PostgraphileMiddleware = utilities.postgraphile( `${databaseConfig.host}/${databaseConfig.database}`, - ["public"], + databaseConfig.schemas, options ); diff --git a/postgraphile/src/server/interface.ts b/postgraphile/src/server/interface.ts index b1d251b7..cd534d10 100644 --- a/postgraphile/src/server/interface.ts +++ b/postgraphile/src/server/interface.ts @@ -17,6 +17,7 @@ import { PluginHookFn } from 'postgraphile/build/postgraphile/pluginHook'; export interface DatabaseConfig { host: string; database: string; + schemas: string[]; } export interface ServerConfig { @@ -32,5 +33,5 @@ export interface ServerUtilities { httpServerFactory: CreateHttpServerCallback; passport: StaticPassportProvider; postgraphile: PostgraphileInitCallback; - pluginHook: PluginHookFn + pluginHook: PluginHookFn; }