Merge pull request #79 from vulcanize/postgraphileConfig

Postgraphile config
This commit is contained in:
ana0 2019-04-16 14:29:19 -04:00 committed by GitHub
commit dbd2208d31
10 changed files with 42 additions and 17 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ postgraphile/package-lock.json
vulcanizedb.log vulcanizedb.log
db/migrations/20*.sql db/migrations/20*.sql
plugins/*.so plugins/*.so
postgraphile/*.toml

View File

@ -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`) * 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` * 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 ## Building

View File

@ -7,6 +7,7 @@
"lint": "tslint --project ./tsconfig.json --config ./tslint.json", "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/", "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", "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": "rm -rf ./build/spec && tsc --build ./tsconfig.test.json && jasmine --config=./spec/support/jasmine.json",
"test:ci": "npm run lint && npm run test" "test:ci": "npm run lint && npm run test"
}, },

View File

@ -33,7 +33,8 @@ describe('parseConfig', () => {
const databaseConfig = parseConfig( const databaseConfig = parseConfig(
readCallback, tomlParseCallback, configPath); 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', () => { it('provides the database name', () => {

View File

@ -21,7 +21,11 @@ describe('buildServerConfig', () => {
let databaseConfig: DatabaseConfig; let databaseConfig: DatabaseConfig;
beforeEach(() => { beforeEach(() => {
databaseConfig = { host: 'example.com', database: 'example_database' }; databaseConfig = {
host: 'example.com',
database: 'example_database',
schemas: ['public']
};
postgraphileMiddleware = jasmine postgraphileMiddleware = jasmine
.createSpyObj<PostgraphileMiddleware>(['call']), .createSpyObj<PostgraphileMiddleware>(['call']),
@ -84,7 +88,7 @@ describe('buildServerConfig', () => {
it('provides the database config to Postgraphile', () => { it('provides the database config to Postgraphile', () => {
expect(serverUtilities.postgraphile).toHaveBeenCalledWith( expect(serverUtilities.postgraphile).toHaveBeenCalledWith(
`${databaseConfig.host}/${databaseConfig.database}`, `${databaseConfig.host}/${databaseConfig.database}`,
["public"], databaseConfig.schemas,
jasmine.any(Object)); jasmine.any(Object));
}); });

View File

@ -8,4 +8,4 @@ export type ReadFileSyncCallback = (
) => string | Buffer; ) => string | Buffer;
export type TomlParseCallback export type TomlParseCallback
= (fileContents: string) => { [key: string]: { [key: string]: string } }; = (fileContents: string) => { [key: string]: { [key: string]: any } };

View File

@ -9,7 +9,7 @@ import { PluginHookFn } from 'postgraphile/build/postgraphile/pluginHook';
export interface PostgraphileMiddleware extends RequestHandler {} export interface PostgraphileMiddleware extends RequestHandler {}
export interface PostgraphileOptions { export interface PostgraphileOptions {
pluginHook: PluginHookFn, pluginHook: PluginHookFn;
simpleSubscriptions: boolean; simpleSubscriptions: boolean;
watchPg: boolean; watchPg: boolean;
enableCors: boolean; enableCors: boolean;

View File

@ -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`; + `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_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_USER_MESSAGE = 'No database user & password '
export const MISSING_DATABASE_MESSAGE = 'No database name provided in config toml'; + 'provided in config toml';
export const MISSING_DATABASE_MESSAGE = 'No database name provided '
+ 'in config toml';
export function parseConfig( export function parseConfig(
readCallback: ReadFileSyncCallback, readCallback: ReadFileSyncCallback,
@ -19,6 +21,7 @@ export function parseConfig(
let database = ''; let database = '';
let user = ''; let user = '';
let password = ''; let password = '';
let schemas = ['public'];
if (configPath) { if (configPath) {
const tomlContents = readCallback(`${configPath}`).toString(); const tomlContents = readCallback(`${configPath}`).toString();
@ -29,6 +32,7 @@ export function parseConfig(
database = parsedToml['database']['name']; database = parsedToml['database']['name'];
user = parsedToml['database']['user']; user = parsedToml['database']['user'];
password = parsedToml['database']['password']; password = parsedToml['database']['password'];
schemas = parsedToml['database']['schemas'];
} }
// Overwrite config values with env. vars if such are set // Overwrite config values with env. vars if such are set
@ -50,5 +54,9 @@ export function parseConfig(
throw new Error(MISSING_USER_MESSAGE); throw new Error(MISSING_USER_MESSAGE);
} }
return { host: `postgres://${user}:${password}@${host}:${port}`, database }; return {
host: `postgres://${user}:${password}@${host}:${port}`,
database,
schemas
};
} }

View File

@ -39,7 +39,7 @@ export function buildServerConfig(
const middleware: PostgraphileMiddleware = utilities.postgraphile( const middleware: PostgraphileMiddleware = utilities.postgraphile(
`${databaseConfig.host}/${databaseConfig.database}`, `${databaseConfig.host}/${databaseConfig.database}`,
["public"], databaseConfig.schemas,
options options
); );

View File

@ -17,6 +17,7 @@ import { PluginHookFn } from 'postgraphile/build/postgraphile/pluginHook';
export interface DatabaseConfig { export interface DatabaseConfig {
host: string; host: string;
database: string; database: string;
schemas: string[];
} }
export interface ServerConfig { export interface ServerConfig {
@ -32,5 +33,5 @@ export interface ServerUtilities {
httpServerFactory: CreateHttpServerCallback; httpServerFactory: CreateHttpServerCallback;
passport: StaticPassportProvider; passport: StaticPassportProvider;
postgraphile: PostgraphileInitCallback; postgraphile: PostgraphileInitCallback;
pluginHook: PluginHookFn pluginHook: PluginHookFn;
} }