MCPcopy Create free account
hub / github.com/subquery/subql / establishConnectionSequelize

Function establishConnectionSequelize

packages/node-core/src/db/db.module.ts:33–52  ·  view source on GitHub ↗
(option: SequelizeOption, numRetries: number)

Source from the content-addressed store, hash-verified

31const CONNECTION_SSL_ERROR_REGEX = 'not support SSL';
32
33async function establishConnectionSequelize(option: SequelizeOption, numRetries: number): Promise<Sequelize> {
34 const uri = `postgresql://${option.username}:${option.password}@${option.host}:${option.port}/${option.database}`;
35
36 const sequelize = new Sequelize(uri, option);
37 try {
38 await sequelize.authenticate();
39 } catch (error: any) {
40 if (JSON.stringify(error.message).includes(CONNECTION_SSL_ERROR_REGEX)) {
41 logger.warn('Database does not support SSL connection, will try to connect without it');
42 option.dialectOptions = undefined;
43 }
44 if (numRetries > 0) {
45 await delay(3);
46 return establishConnectionSequelize(option, numRetries - 1);
47 } else {
48 exitWithError(new Error(`Unable to connect to the database`, {cause: error}), logger);
49 }
50 }
51 return sequelize;
52}
53
54const sequelizeFactory = (option: SequelizeOption) => async () => {
55 const numRetries = 5;

Callers 1

sequelizeFactoryFunction · 0.85

Calls 3

delayFunction · 0.90
exitWithErrorFunction · 0.90
warnMethod · 0.45

Tested by

no test coverage detected