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