-
Notifications
You must be signed in to change notification settings - Fork 729
Bugfix/job generator db connections lfx 1830 #2709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,10 +9,9 @@ export async function databaseInit( | |||||||||||||||||||||||||||||||||||||
queryTimeoutMilliseconds: number = 60000, | ||||||||||||||||||||||||||||||||||||||
forceNewInstance: boolean = false, | ||||||||||||||||||||||||||||||||||||||
databaseHostnameOverride: string = null, | ||||||||||||||||||||||||||||||||||||||
profileQueries: boolean = false, | ||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||
if (profileQueries || forceNewInstance) { | ||||||||||||||||||||||||||||||||||||||
return models(queryTimeoutMilliseconds, databaseHostnameOverride, profileQueries) | ||||||||||||||||||||||||||||||||||||||
if (forceNewInstance) { | ||||||||||||||||||||||||||||||||||||||
return models(queryTimeoutMilliseconds, databaseHostnameOverride) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if (!cached) { | ||||||||||||||||||||||||||||||||||||||
|
@@ -21,3 +20,7 @@ export async function databaseInit( | |||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
return cached | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export async function databaseClose(database) { | ||||||||||||||||||||||||||||||||||||||
await database.sequelize.close() | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance databaseClose with type safety and error handling The Consider this implementation: - export async function databaseClose(database) {
+ interface Database {
+ sequelize: {
+ close(): Promise<void>;
+ };
+ }
+ export async function databaseClose(database: Database) {
+ if (!database?.sequelize) {
+ throw new Error('Invalid database instance provided');
+ }
try {
await database.sequelize.close();
+ } catch (error) {
+ throw new Error(`Failed to close database connection: ${error.message}`);
+ }
} 📝 Committable suggestion
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import { IS_CLOUD_ENV } from '@crowd/common' | |
* This module creates the Sequelize to the database and | ||
* exports all the models. | ||
*/ | ||
import { getServiceChildLogger, logExecutionTimeV2 } from '@crowd/logging' | ||
import { getServiceChildLogger } from '@crowd/logging' | ||
|
||
import { DB_CONFIG, SERVICE } from '../../conf' | ||
import * as configTypes from '../../conf/configTypes' | ||
|
@@ -47,11 +47,7 @@ function getCredentials(): Credentials { | |
} | ||
} | ||
|
||
async function models( | ||
queryTimeoutMilliseconds: number, | ||
databaseHostnameOverride = null, | ||
profileQueries = false, | ||
) { | ||
async function models(queryTimeoutMilliseconds: number, databaseHostnameOverride = null) { | ||
log.info('Initializing sequelize database connection!') | ||
const database = {} as any | ||
|
||
|
@@ -106,19 +102,19 @@ async function models( | |
}, | ||
) | ||
|
||
if (profileQueries) { | ||
const oldQuery = sequelize.query | ||
sequelize.query = async (query, options) => { | ||
const { replacements } = options || {} | ||
const result = await logExecutionTimeV2( | ||
() => oldQuery.apply(sequelize, [query, options]), | ||
log, | ||
`DB Query:\n${query}\n${replacements ? `Params: ${JSON.stringify(replacements)}` : ''}`, | ||
) | ||
|
||
return result | ||
} | ||
} | ||
// if (profileQueries) { | ||
// const oldQuery = sequelize.query | ||
// sequelize.query = async (query, options) => { | ||
// const { replacements } = options || {} | ||
// const result = await logExecutionTimeV2( | ||
// () => oldQuery.apply(sequelize, [query, options]), | ||
// log, | ||
// `DB Query:\n${query}\n${replacements ? `Params: ${JSON.stringify(replacements)}` : ''}`, | ||
// ) | ||
|
||
// return result | ||
// } | ||
// } | ||
Comment on lines
+105
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Remove commented out code Instead of keeping commented out code in the codebase, it should be removed as it's preserved in version control history. This improves code maintainability and reduces confusion. - // if (profileQueries) {
- // const oldQuery = sequelize.query
- // sequelize.query = async (query, options) => {
- // const { replacements } = options || {}
- // const result = await logExecutionTimeV2(
- // () => oldQuery.apply(sequelize, [query, options]),
- // log,
- // `DB Query:\n${query}\n${replacements ? `Params: ${JSON.stringify(replacements)}` : ''}`,
- // )
-
- // return result
- // }
- // } |
||
|
||
const modelClasses = [ | ||
require('./auditLog').default, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve error handling granularity and connection timeout
While the resource cleanup is good, consider these improvements:
Consider this implementation: