Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updated latest queries
  • Loading branch information
radicaldrew committed Nov 14, 2024
commit 7d319af72d40c29e6699a3db607a6aa0d6d3b759
68 changes: 43 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ const fs = require('fs');
const cacheActivator = require('./lib/cache-activator');

const pingDb = async(pool) => {
return new Promise((resolve, reject) => {
pool.authenticate()
.then(function() {
resolve();
})
.catch(function (err) {
reject(err);
});
});
await pool.authenticate();
};

module.exports = function(mysqlConfig, logger) {
let writePool = null;
const { host, user, port, password, database, connectionLimit } = mysqlConfig;

const dialect = process.env.JAMBONES_DB_DIALECT || 'mysql';
const config = {
host,
Expand All @@ -43,20 +35,20 @@ module.exports = function(mysqlConfig, logger) {

pool.promise = () => pool;
pool.execute = () => pool.query;
if (process.env.JAMBONES_DB_WRITE_HOST &&
process.env.JAMBONES_DB_WRITE_USER &&
process.env.JAMBONES_DB_WRITE_PASSWORD &&
process.env.JAMBONES_DB_WRITE_DATABASE) {
const user = process.env.JAMBONES_DB_WRITE_USER;
const password = process.env.JAMBONES_DB_WRITE_PASSWORD;
const database = process.env.JAMBONES_DB_WRITE_DATABASE;
writeConfiguration = {
host: process.env.JAMBONES_DB_WRITE_HOST,
dialect: process.env.JAMBONES_DB_WRITE_DIALECT || 'mysql',
port: process.env.JAMBONES_DB_WRITE_PORT || 3306,

if (process.env.JAMBONES_MYSQL_WRITE_HOST &&
process.env.JAMBONES_MYSQL_WRITE_USER &&
process.env.JAMBONES_MYSQL_WRITE_PASSWORD &&
process.env.JAMBONES_MYSQL_WRITE_DATABASE) {
const user = process.env.JAMBONES_MYSQL_WRITE_USER;
const password = process.env.JAMBONES_MYSQL_WRITE_PASSWORD;
const database = process.env.JAMBONES_MYSQL_WRITE_DATABASE;
const writeConfiguration = {
host: process.env.JAMBONES_MYSQL_WRITE_HOST,
dialect: 'mysql',
port: process.env.JAMBONES_MYSQL_WRITE_PORT || 3306,
pool: {
max: process.env.JAMBONES_DB_WRITE_CONNECTION_LIMIT || 10,
max: process.env.JAMBONES_MYSQL_WRITE_CONNECTION_LIMIT || 10,
min: 0,
acquire: 30000,
idle: 10000
Expand All @@ -67,13 +59,39 @@ module.exports = function(mysqlConfig, logger) {
// test connection to database write pool
writePool.authenticate().catch((err) => { throw err; });
}

if (process.env.JAMBONES_POSTGRES_WRITE_HOST &&
process.env.JAMBONES_POSTGRES_WRITE_USER &&
process.env.JAMBONES_POSTGRES_WRITE_PASSWORD &&
process.env.JAMBONES_POSTGRES_WRITE_DATABASE) {
const user = process.env.JAMBONES_POSTGRES_WRITE_USER;
const password = process.env.JAMBONES_POSTGRES_WRITE_PASSWORD;
const database = process.env.JAMBONES_POSTGRES_WRITE_DATABASE;
const writeConfiguration = {
host: process.env.JAMBONES_POSTGRES_WRITE_HOST,
dialect: 'postgres',
port: process.env.JAMBONES_POSTGRES_WRITE_PORT || 3306,
pool: {
max: process.env.JAMBONES_POSTGRES_WRITE_CONNECTION_LIMIT || 10,
min: 0,
acquire: 30000,
idle: 10000
}
};
// create new Sequelize connection for write pool
writePool = new Sequelize(database, user, password, writeConfiguration);
// test connection to database write pool
writePool.authenticate().catch((err) => { throw err; });
}


// Cache activation for read only.
if (process.env.JAMBONES_DB_REFRESH_TTL)
cacheActivator.activate(pool);

logger = logger || {info: () => {}, error: () => {}, debug: () => {}};
// test connection to the database

// test connection to the database
pool.authenticate().catch((err) => { throw err; });

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/add-sbc-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function addSbcAddress(pool, logger, ipv4, port = 5060, tls_port = null, w
const r2 = await pool.query(sql, {
replacements: [uuid(), ipv4, port, tls_port, wss_port],
type: QueryTypes.INSERT
});
});
debug(`results from inserting sbc address ${ipv4} port: ${port}
tls_port: ${tls_port} wss_port:${wss_port} : ${JSON.stringify(r2)}`);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/lookup-account-by-sip-realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ async function lookupAccountBySipRealm(pool, logger, realm) {
nest: true,
replacements: [realm],
type: QueryTypes.SELECT
});
});
debug(`results: ${JSON.stringify(r)}`);
if (r.length > 0) {
const obj = r[0] //r[0].acc;
const obj = r[0]; //r[0].acc;
//Object.assign(obj, {registration_hook: r[0].rh});
//if (!obj.registration_hook.url) delete obj.registration_hook;
logger.debug(`retrieved account: ${JSON.stringify(obj)}`);
Expand All @@ -41,7 +41,7 @@ async function lookupAccountBySipRealm(pool, logger, realm) {
nest: true,
replacements: [superDomain],
type: QueryTypes.SELECT
});
});
debug(`results: ${JSON.stringify(r)}`);
if (r.length === 1) {
const obj = r[0]; //r[0].acc;
Expand Down
8 changes: 4 additions & 4 deletions lib/lookup-app-by-phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ async function lookupAppByPhoneNumber(pool, logger, phoneNumber, voip_carrier_si
const lookupAppBySid = require('./lookup-app-by-sid').bind(null, pool, logger);
let query;
let replacements;
if(voip_carrier_sid){
if (voip_carrier_sid) {
query = sqlCallRoutesWithCarrier;
replacements = [phoneNumber, voip_carrier_sid]
}else{
replacements = [phoneNumber, voip_carrier_sid];
} else {
query = sqlCallRoutes;
replacements = [phoneNumber]
replacements = [phoneNumber];
}

const callRoutes = await pool.query(query, {
Expand Down
4 changes: 2 additions & 2 deletions lib/lookup-app-by-realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ async function lookupAppByRealm(pool, logger, sip_realm) {
nest: true,
replacements: [sip_realm],
type: QueryTypes.SELECT
});
});
debug(`results: ${JSON.stringify(r)}`);
if (r.length > 0) {
const obj = r[0];
const obj = r[0];
debug(`retrieved application: ${JSON.stringify(obj)}`);
return obj;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/lookup-app-by-teams-tenant.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function lookupAppByTeamsTenant(pool, logger, tenant_fqdn) {
nest: true,
replacements: [tenant_fqdn],
type: QueryTypes.SELECT
});
});
debug(`results: ${JSON.stringify(r)}`);
if (r.length > 0) {
const obj = r[0];
Expand Down
2 changes: 1 addition & 1 deletion lib/lookup-auth-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function lookupAuthHook(pool, logger, sipRealm) {
replacements: [superDomain]
});
debug(`results from querying account for sip realm ${superDomain}: ${JSON.stringify(r)}`);
if (r.length > 0 ) {
if (r.length > 0) {
return r[0];
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/lookup-outbound-carrier-for-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function lookupOutboundCarrierForAccount(pool, logger, account_sid) {
console.log('---------------------------------------------');
console.log(gws);
console.log('---------------------------------------------');

carriers.push(...gws);

console.log('---------------------------------------------');
Expand Down
2 changes: 1 addition & 1 deletion lib/lookup-sbc-address-by-ipv4.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const sql = 'SELECT * FROM sbc_addresses where ipv4 = ?';
* @param {*} logger
*/
async function lookUpSbcAddressesbyIpv4(pool, logger, ipv4) {
try {
try {
debug(`select with ${ipv4}`);
const r = await pool.query(sql, {
raw: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/lookup-sip-gateway-by-sid.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function lookupSipGatewayBySid(pool, logger, sid) {
raw: true,
replacements: [sid],
type: QueryTypes.SELECT
});
});
debug(`results: ${JSON.stringify(r)}`);
if (r.length > 0) {
return r[0];
Expand Down
2 changes: 1 addition & 1 deletion lib/lookup-smpp-gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function lookupSmppGateways(pool, logger, account_sid) {
type: QueryTypes.SELECT
});
if (r.length > 0) return r;

const r2 = await pool.query(sqlServiceProvider, {
raw: true,
nest: true,
Expand Down
1 change: 0 additions & 1 deletion lib/update-carrier-register-status-by-sid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const debug = require('debug')('jambonz:db-helpers');
const { QueryTypes } = require('sequelize');

const sql = 'UPDATE voip_carriers SET register_status = ? where voip_carrier_sid = ?';

Expand Down
4 changes: 2 additions & 2 deletions lib/update-sip-gateway-by-sid.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { QueryTypes } = require('sequelize');
* @param {Object} values - An object where each key-value pair represents a column name and a new value.
*/
async function updateSipGatewayBySid(pool, logger, sip_gateway_sid, values) {

// Begin building the SQL query string and parameters.
let sql = 'UPDATE sip_gateways SET ';
const params = [];
Expand All @@ -25,7 +25,7 @@ async function updateSipGatewayBySid(pool, logger, sip_gateway_sid, values) {
sql += ' WHERE sip_gateway_sid = ?';
params.push(sip_gateway_sid);

const r = await pool.query(sql, {
const r = await pool.query(sql, {
replacements: params,
type: QueryTypes.UPDATE
});
Expand Down