more strategic connection closing in pg

This commit is contained in:
Martin McKeaveney 2022-05-10 11:13:40 +01:00
parent d17cbb011a
commit db0ffe4ac7
1 changed files with 45 additions and 37 deletions

View File

@ -147,7 +147,7 @@ module PostgresModule {
return parts.join(" || ")
}
async internalQuery(query: SqlQuery) {
async internalQuery(query: SqlQuery, close: boolean = true) {
const client = this.client
this.index = 1
// need to handle a specific issue with json data types in postgres,
@ -164,10 +164,11 @@ module PostgresModule {
try {
return await client.query(query.sql, query.bindings || [])
} catch (err) {
await this.client.end()
// @ts-ignore
throw new Error(err)
} finally {
await this.client.end()
if (close) await this.client.end()
}
}
@ -204,48 +205,54 @@ module PostgresModule {
}
} catch (err) {
tableKeys = {}
} finally {
await this.client.close()
}
const columnsResponse = await this.client.query(this.COLUMNS_SQL)
const tables: { [key: string]: Table } = {}
try {
const columnsResponse = await this.client.query(this.COLUMNS_SQL)
for (let column of columnsResponse.rows) {
const tableName: string = column.table_name
const columnName: string = column.column_name
const tables: { [key: string]: Table } = {}
// table key doesn't exist yet
if (!tables[tableName] || !tables[tableName].schema) {
tables[tableName] = {
_id: buildExternalTableId(datasourceId, tableName),
primary: tableKeys[tableName] || [],
name: tableName,
schema: {},
for (let column of columnsResponse.rows) {
const tableName: string = column.table_name
const columnName: string = column.column_name
// table key doesn't exist yet
if (!tables[tableName] || !tables[tableName].schema) {
tables[tableName] = {
_id: buildExternalTableId(datasourceId, tableName),
primary: tableKeys[tableName] || [],
name: tableName,
schema: {},
}
}
const identity = !!(
column.identity_generation ||
column.identity_start ||
column.identity_increment
)
const hasDefault =
typeof column.column_default === "string" &&
column.column_default.startsWith("nextval")
const isGenerated =
column.is_generated && column.is_generated !== "NEVER"
const isAuto: boolean = hasDefault || identity || isGenerated
tables[tableName].schema[columnName] = {
autocolumn: isAuto,
name: columnName,
...convertSqlType(column.data_type),
}
}
const identity = !!(
column.identity_generation ||
column.identity_start ||
column.identity_increment
)
const hasDefault =
typeof column.column_default === "string" &&
column.column_default.startsWith("nextval")
const isGenerated =
column.is_generated && column.is_generated !== "NEVER"
const isAuto: boolean = hasDefault || identity || isGenerated
tables[tableName].schema[columnName] = {
autocolumn: isAuto,
name: columnName,
...convertSqlType(column.data_type),
}
const final = finaliseExternalTables(tables, entities)
this.tables = final.tables
this.schemaErrors = final.errors
} catch (err) {
// @ts-ignore
throw new Error(err)
} finally {
await this.client.end()
}
const final = finaliseExternalTables(tables, entities)
this.tables = final.tables
this.schemaErrors = final.errors
}
async create(query: SqlQuery | string) {
@ -274,8 +281,9 @@ module PostgresModule {
if (Array.isArray(input)) {
const responses = []
for (let query of input) {
responses.push(await this.internalQuery(query))
responses.push(await this.internalQuery(query, false))
}
await this.client.end()
return responses
} else {
const response = await this.internalQuery(input)