feat(backend): add support for PG
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
"dockerode": "^3.2.0",
|
||||
"express": "^4.17.1",
|
||||
"knex": "^0.21.1",
|
||||
"pg": "^8.3.0",
|
||||
"sqlite3": "^4.2.0",
|
||||
"winston": "^3.2.1"
|
||||
},
|
||||
|
||||
@@ -45,11 +45,35 @@ function makeCreateEnv(loadedConfigs: AppConfig[]) {
|
||||
|
||||
return (plugin: string): PluginEnvironment => {
|
||||
const logger = getRootLogger().child({ type: 'plugin', plugin });
|
||||
const database = knex({
|
||||
client: 'sqlite3',
|
||||
connection: ':memory:',
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
// Supported DBs are sqlite and postgres
|
||||
const isPg = [
|
||||
'POSTGRES_USER',
|
||||
'POSTGRES_HOST',
|
||||
'POSTGRES_PASSWORD',
|
||||
].every(key => Object.keys(process.env).includes(key));
|
||||
|
||||
let knexConfig;
|
||||
|
||||
if (isPg) {
|
||||
knexConfig = {
|
||||
client: 'pg',
|
||||
useNullAsDefault: true,
|
||||
connection: {
|
||||
host: process.env.PG_HOST,
|
||||
user: process.env.PG_USER,
|
||||
password: process.env.PG_PASSWORD,
|
||||
database: `backstage_plugin_${plugin}`,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
knexConfig = {
|
||||
client: 'sqlite3',
|
||||
connection: ':memory:',
|
||||
useNullAsDefault: true,
|
||||
};
|
||||
}
|
||||
|
||||
const database = knex(knexConfig);
|
||||
database.client.pool.on('createSuccess', (_eventId: any, resource: any) => {
|
||||
resource.run('PRAGMA foreign_keys = ON', () => {});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user