Add schemas to config

This commit is contained in:
anon 2019-04-15 14:17:33 -04:00
parent 0f425ac76b
commit f99e397699
10 changed files with 42 additions and 17 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ postgraphile/package-lock.json
vulcanizedb.log
db/migrations/20*.sql
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`)
* 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

View File

@ -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"
},

View File

@ -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', () => {

View File

@ -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<PostgraphileMiddleware>(['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));
});

View File

@ -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 } };

View File

@ -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;

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`;
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
};
}

View File

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

View File

@ -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;
}