Add postgres_fdw to database (#2854)

* Add postgres_fdw to database

* Add wrappers to database

* Add cp
This commit is contained in:
martmull
2023-12-08 14:43:52 +01:00
committed by GitHub
parent 7535c84e3d
commit 88abb11448
4 changed files with 71 additions and 13 deletions

View File

@ -1,6 +1,6 @@
import console from 'console';
import { connectionSource, performQuery } from './utils';
import { camelToSnakeCase, connectionSource, performQuery } from './utils';
connectionSource
.initialize()
@ -18,7 +18,7 @@ connectionSource
'create schema "core"',
);
await performQuery(
'CREATE EXTENSION IF NOT EXISTS pg_graphql',
'CREATE EXTENSION IF NOT EXISTS "pg_graphql"',
'create extension pg_graphql',
);
@ -27,6 +27,39 @@ connectionSource
'create extension "uuid-ossp"',
);
await performQuery(
'CREATE EXTENSION IF NOT EXISTS "postgres_fdw"',
'create extension "postgres_fdw"',
);
await performQuery(
'CREATE EXTENSION IF NOT EXISTS "wrappers"',
'create extension "wrappers"',
);
const supabaseWrappers = [
'airtable',
'bigQuery',
'clickHouse',
'firebase',
'logflare',
's3',
'stripe',
]; // See https://supabase.github.io/wrappers/
for (const wrapper of supabaseWrappers) {
await performQuery(
`
CREATE FOREIGN DATA WRAPPER "${wrapper.toLowerCase()}_fdw"
HANDLER "${camelToSnakeCase(wrapper)}_fdw_handler"
VALIDATOR "${camelToSnakeCase(wrapper)}_fdw_validator";
`,
`create ${wrapper} "wrappers"`,
true,
true,
);
}
await performQuery(
`COMMENT ON SCHEMA "core" IS '@graphql({"inflect_names": true})';`,
'inflect names for graphql',

View File

@ -7,22 +7,36 @@ import { DataSource } from 'typeorm';
config();
const configService = new ConfigService();
export const connectionSource = new DataSource({
type: 'postgres',
logging: false,
url: configService.get<string>('PG_DATABASE_URL'),
});
export const camelToSnakeCase = (str) =>
str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
export const performQuery = async (
query: string,
consoleDescription: string,
withLog = true,
ignoreAlreadyExistsError = false,
) => {
try {
const result = await connectionSource.query(query);
withLog && console.log(`Performed '${consoleDescription}' successfully`);
return result;
} catch (err) {
withLog && console.error(`Failed to perform '${consoleDescription}':`, err);
let message = '';
if (ignoreAlreadyExistsError && `${err}`.includes('already exists')) {
message = `Performed '${consoleDescription}' successfully`;
} else {
message = `Failed to perform '${consoleDescription}': ${err}`;
}
withLog && console.error(message);
}
};