Skip to content

Commit

Permalink
fix: Core handler services that have a dependency on central-services…
Browse files Browse the repository at this point in the history
…-database are not loading all tables on startup (#343)

Fix for mojaloop/project#1888. Fix issue by changing all `Db.<table>.*` syntax function operations to `Db.from('<table>').*`. The issue was caused by the central-services-database Database class on Db.connect() loading all tables via an SQL request, and creating a Class property (`Db.<table>`) to reference the Table object. The issue here being that the query to fetch all the tables from the database does not return all tables (to be investigated in future).  `Db.from('<table>').*` ensures that the table object is created properly.
  • Loading branch information
mdebarros authored Feb 12, 2021
1 parent 0c20c8f commit 74a2b2c
Show file tree
Hide file tree
Showing 33 changed files with 172 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ jspm_packages
.history
/.gitlab-ci.env
junit.xml

# Add ignores
*IGNORE*
*ignore*
48 changes: 44 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "central-settlement",
"description": "Central settlements hosted by a scheme to record and make settlements.",
"version": "12.0.1",
"version": "12.0.2",
"license": "Apache-2.0",
"private": false,
"author": "ModusBox",
Expand Down Expand Up @@ -38,7 +38,7 @@
"@mojaloop/central-services-shared": "11.5.5",
"@mojaloop/central-services-stream": "10.6.0",
"@mojaloop/ml-number": "11.0.0",
"@now-ims/hapi-now-auth": "2.0.2",
"@now-ims/hapi-now-auth": "2.0.3",
"async": "3.2.0",
"async-retry": "1.3.1",
"bignumber.js": "9.0.1",
Expand Down Expand Up @@ -83,18 +83,19 @@
"tapes": "4.1.0"
},
"pre-commit": [
"standard",
"lint",
"dep:check",
"test"
"test:unit"
],
"scripts": {
"start": "run-p start:api",
"start:api": "node src/api/index.js",
"watch:api": "nodemon src/api/index.js",
"start:handlers": "node src/handlers/index.js",
"regenerate": "yo swaggerize:test --framework hapi --apiPath './src/interface/swagger.yaml'",
"standard": "standard",
"lint": "eslint .",
"lint": "standard",
"lint:fix": "standard --fix",
"eslint": "eslint .",
"test": "npm run test:unit | faucet",
"test:all": "run-s test",
"test:unit": "tape 'test/unit/**/*.test.js'",
Expand Down
2 changes: 1 addition & 1 deletion src/models/ilpPackets/ilpPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const ErrorHandler = require('@mojaloop/central-services-error-handling')

exports.getById = async (id) => {
try {
return await Db.ilpPacket.find({ transferId: id })
return await Db.from('ilpPacket').find({ transferId: id })
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
Expand Down
28 changes: 14 additions & 14 deletions src/models/lib/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {

ledgerAccountTypes: async function () {
const ledgerAccountTypeEnum = {}
const ledgerAccountTypeEnumsList = await Db.ledgerAccountType.find({})
const ledgerAccountTypeEnumsList = await Db.from('ledgerAccountType').find({})
if (ledgerAccountTypeEnumsList) {
for (const state of ledgerAccountTypeEnumsList) {
ledgerAccountTypeEnum[`${state.name}`] = state.ledgerAccountTypeId
Expand All @@ -47,7 +47,7 @@ module.exports = {
},
ledgerEntryTypes: async function () {
const ledgerEntryTypeEnum = {}
const ledgerEntryTypeEnumsList = await Db.ledgerEntryType.find({})
const ledgerEntryTypeEnumsList = await Db.from('ledgerEntryType').find({})
if (ledgerEntryTypeEnumsList) {
for (const state of ledgerEntryTypeEnumsList) {
ledgerEntryTypeEnum[`${state.name}`] = state.ledgerEntryTypeId
Expand All @@ -57,7 +57,7 @@ module.exports = {
},
participantLimitTypes: async function () {
const participantLimitTypeEnum = {}
const participantLimitTypeEnumsList = await Db.participantLimitType.find({})
const participantLimitTypeEnumsList = await Db.from('participantLimitType').find({})
if (participantLimitTypeEnumsList) {
for (const state of participantLimitTypeEnumsList) {
participantLimitTypeEnum[`${state.name}`] = state.participantLimitTypeId
Expand All @@ -68,7 +68,7 @@ module.exports = {
settlementDelay: async function () {
const settlementDelayName = {}

const settlementDelayNamesList = await Db.settlementDelay.find({})
const settlementDelayNamesList = await Db.from('settlementDelay').find({})
if (settlementDelayNamesList) {
for (const record of settlementDelayNamesList) {
settlementDelayName[`${record.name}`] = record.settlementDelayId
Expand All @@ -79,7 +79,7 @@ module.exports = {
settlementDelayEnums: async function () {
const settlementDelayEnum = {}

const settlementDelayEnumsList = await Db.settlementDelay.find({})
const settlementDelayEnumsList = await Db.from('settlementDelay').find({})
if (settlementDelayEnumsList) {
for (const record of settlementDelayEnumsList) {
settlementDelayEnum[`${record.name}`] = record.name
Expand All @@ -90,7 +90,7 @@ module.exports = {
settlementGranularity: async function () {
const settlementGranularityName = {}

const settlementGranularityNamesList = await Db.settlementGranularity.find({})
const settlementGranularityNamesList = await Db.from('settlementGranularity').find({})
if (settlementGranularityNamesList) {
for (const record of settlementGranularityNamesList) {
settlementGranularityName[`${record.name}`] = record.settlementGranularityId
Expand All @@ -101,7 +101,7 @@ module.exports = {
settlementGranularityEnums: async function () {
const settlementGranularityEnum = {}

const settlementGranularityEnumsList = await Db.settlementGranularity.find({})
const settlementGranularityEnumsList = await Db.from('settlementGranularity').find({})
if (settlementGranularityEnumsList) {
for (const record of settlementGranularityEnumsList) {
settlementGranularityEnum[`${record.name}`] = record.name
Expand All @@ -112,7 +112,7 @@ module.exports = {
settlementInterchange: async function () {
const settlementInterchangeName = {}

const settlementInterchangeNamesList = await Db.settlementInterchange.find({})
const settlementInterchangeNamesList = await Db.from('settlementInterchange').find({})
if (settlementInterchangeNamesList) {
for (const record of settlementInterchangeNamesList) {
settlementInterchangeName[`${record.name}`] = record.settlementInterchangeId
Expand All @@ -123,7 +123,7 @@ module.exports = {
settlementInterchangeEnums: async function () {
const settlementInterchangeEnum = {}

const settlementInterchangeEnumsList = await Db.settlementInterchange.find({})
const settlementInterchangeEnumsList = await Db.from('settlementInterchange').find({})
if (settlementInterchangeEnumsList) {
for (const record of settlementInterchangeEnumsList) {
settlementInterchangeEnum[`${record.name}`] = record.name
Expand All @@ -134,7 +134,7 @@ module.exports = {
settlementStates: async function () {
const settlementStateEnum = {}

const settlementStateEnumsList = await Db.settlementState.find({})
const settlementStateEnumsList = await Db.from('settlementState').find({})
if (settlementStateEnumsList) {
for (const state of settlementStateEnumsList) {
settlementStateEnum[`${state.enumeration}`] = state.settlementStateId
Expand All @@ -144,7 +144,7 @@ module.exports = {
},
settlementWindowStates: async function () {
const settlementWindowStateEnum = {}
const settlementWindowStateEnumsList = await Db.settlementWindowState.find({})
const settlementWindowStateEnumsList = await Db.from('settlementWindowState').find({})
if (settlementWindowStateEnumsList) {
for (const state of settlementWindowStateEnumsList) {
settlementWindowStateEnum[`${state.enumeration}`] = state.settlementWindowStateId
Expand All @@ -154,7 +154,7 @@ module.exports = {
},
transferParticipantRoleTypes: async function () {
const transferParticipantRoleTypeEnum = {}
const transferParticipantRoleTypeEnumsList = await Db.transferParticipantRoleType.find({})
const transferParticipantRoleTypeEnumsList = await Db.from('transferParticipantRoleType').find({})
if (transferParticipantRoleTypeEnumsList) {
for (const state of transferParticipantRoleTypeEnumsList) {
transferParticipantRoleTypeEnum[`${state.name}`] = state.transferParticipantRoleTypeId
Expand All @@ -164,7 +164,7 @@ module.exports = {
},
transferStateEnums: async function () {
const transferStateEnum = {}
const transferStateEnumsList = await Db.transferState.find({})
const transferStateEnumsList = await Db.from('transferState').find({})
if (transferStateEnumsList) {
for (const state of transferStateEnumsList) {
// apply distinct even though final result would contain distinct values
Expand All @@ -177,7 +177,7 @@ module.exports = {
},
transferStates: async function () {
const transferStateEnum = {}
const transferStateEnumsList = await Db.transferState.find({})
const transferStateEnumsList = await Db.from('transferState').find({})
if (transferStateEnumsList) {
for (const state of transferStateEnumsList) {
transferStateEnum[`${state.transferStateId}`] = state.transferStateId
Expand Down
2 changes: 1 addition & 1 deletion src/models/misc/migrationLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Db = require('../../lib/db')
* @returns {Promise<boolean>} - true if locked, false if not. Rejects if an error occours
*/
const getIsMigrationLocked = async () => {
const result = await Db.migration_lock.query(async builder => {
const result = await Db.from('migration_lock').query(async builder => {
builder.select('is_locked AS isLocked')
.orderBy('index', 'desc')
.first()
Expand Down
20 changes: 10 additions & 10 deletions src/models/settlement/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ const Facade = {
},

getById: async function ({ settlementId }) {
return Db.settlement.query(builder => {
return Db.from('settlement').query(builder => {
return builder
.join('settlementStateChange AS ssc', 'ssc.settlementStateChangeId', 'settlement.currentStateChangeId')
.select('settlement.settlementId',
Expand All @@ -1345,7 +1345,7 @@ const Facade = {
},

getByParams: async function ({ state, fromDateTime, toDateTime, currency, settlementWindowId, fromSettlementWindowDateTime, toSettlementWindowDateTime, participantId, accountId }) {
return Db.settlement.query(builder => {
return Db.from('settlement').query(builder => {
const b = builder
.innerJoin('settlementStateChange AS ssc', 'ssc.settlementStateChangeId', 'settlement.currentStateChangeId')
.innerJoin('settlementSettlementWindow AS ssw', 'ssw.settlementId', 'settlement.settlementId')
Expand Down Expand Up @@ -1537,7 +1537,7 @@ const Facade = {

settlementParticipantCurrency: {
getByListOfIds: async function (listOfIds) {
return Db.settlementParticipantCurrency.query(builder => {
return Db.from('settlementParticipantCurrency').query(builder => {
return builder
.leftJoin('participantCurrency AS pc', 'pc.participantCurrencyId', 'settlementParticipantCurrency.participantCurrencyId')
.leftJoin('participant as p', 'p.participantCurrencyId', 'pc.participantCurrencyId')
Expand All @@ -1551,7 +1551,7 @@ const Facade = {
},

getAccountsInSettlementByIds: async function ({ settlementId, participantId }) {
return Db.settlementParticipantCurrency.query(builder => {
return Db.from('settlementParticipantCurrency').query(builder => {
return builder
.join('participantCurrency AS pc', 'pc.participantCurrencyId', 'settlementParticipantCurrency.participantCurrencyId')
.select('settlementParticipantCurrencyId')
Expand All @@ -1561,7 +1561,7 @@ const Facade = {
},

getParticipantCurrencyBySettlementId: async function ({ settlementId }) {
return Db.settlementParticipantCurrency.query(builder => {
return Db.from('settlementParticipantCurrency').query(builder => {
return builder
.leftJoin('settlementParticipantCurrencyStateChange AS spcsc', 'spcsc.settlementParticipantCurrencyStateChangeId', 'settlementParticipantCurrency.currentStateChangeId')
.join('participantCurrency AS pc', 'pc.participantCurrencyId', 'settlementParticipantCurrency.participantCurrencyId')
Expand All @@ -1579,7 +1579,7 @@ const Facade = {
},

getSettlementAccountById: async function (settlementParticipantCurrencyId) {
return Db.settlementParticipantCurrency.query(builder => {
return Db.from('settlementParticipantCurrency').query(builder => {
return builder
.join('settlementParticipantCurrencyStateChange AS spcsc', 'spcsc.settlementParticipantCurrencyStateChangeId', 'settlementParticipantCurrency.currentStateChangeId')
.join('participantCurrency AS pc', 'pc.participantCurrencyId', 'settlementParticipantCurrency.participantCurrencyId')
Expand All @@ -1596,7 +1596,7 @@ const Facade = {
},

getSettlementAccountsByListOfIds: async function (settlementParticipantCurrencyIdList) {
return Db.settlementParticipantCurrency.query(builder => {
return Db.from('settlementParticipantCurrency').query(builder => {
return builder
.join('settlementParticipantCurrencyStateChange AS spcsc', 'spcsc.settlementParticipantCurrencyStateChangeId', 'settlementParticipantCurrency.currentStateChangeId')
.join('participantCurrency AS pc', 'pc.participantCurrencyId', 'settlementParticipantCurrency.participantCurrencyId')
Expand All @@ -1614,7 +1614,7 @@ const Facade = {
},
settlementSettlementWindow: {
getWindowsBySettlementIdAndAccountId: async function ({ settlementId, accountId }) {
return Db.settlementSettlementWindow.query(builder => {
return Db.from('settlementSettlementWindow').query(builder => {
return builder
.join('settlementWindow', 'settlementWindow.settlementWindowId', 'settlementSettlementWindow.settlementWindowId')
.join('settlementWindowStateChange AS swsc', 'swsc.settlementWindowStateChangeId', 'settlementWindow.currentStateChangeId')
Expand All @@ -1635,8 +1635,8 @@ const Facade = {
},

getWindowsBySettlementIdAndParticipantId: async function ({ settlementId, participantId }, enums) {
const participantAccountList = (await Db.participantCurrency.find({ participantId, ledgerAccountTypeId: enums.ledgerAccountTypes.POSITION })).map(record => record.participantCurrencyId)
return Db.settlementSettlementWindow.query(builder => {
const participantAccountList = (await Db.from('participantCurrency').find({ participantId, ledgerAccountTypeId: enums.ledgerAccountTypes.POSITION })).map(record => record.participantCurrencyId)
return Db.from('settlementSettlementWindow').query(builder => {
return builder
.join('settlementWindow', 'settlementWindow.settlementWindowId', 'settlementSettlementWindow.settlementWindowId')
.join('settlementWindowStateChange AS swsc', 'swsc.settlementWindowStateChangeId', 'settlementWindow.currentStateChangeId')
Expand Down
2 changes: 1 addition & 1 deletion src/models/settlement/participantCurrency.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
const Db = require('../../lib/db')

const checkParticipantAccountExists = async ({ participantId, accountId }, enums = {}) => {
return Db.participantCurrency.query(builder => {
return Db.from('participantCurrency').query(builder => {
return builder
.select('participantCurrencyId')
.where({ participantId })
Expand Down
4 changes: 2 additions & 2 deletions src/models/settlement/settlement.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
const Db = require('../../lib/db')

const create = async (settlement) => {
return Db.settlement.insert({
return Db.from('settlement').insert({
reason: settlement.reason,
createdDate: settlement.createdDate
})
}

const getById = async (id) => {
return Db.settlement.findOne({ settlementId: id })
return Db.from('settlement').findOne({ settlementId: id })
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/models/settlement/settlementModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
const Db = require('../../lib/db')

const getByName = async (name) => {
return Db.settlementModel.findOne({ name, isActive: 1 })
return Db.from('settlementModel').findOne({ name, isActive: 1 })
}

module.exports = {
Expand Down
Loading

0 comments on commit 74a2b2c

Please sign in to comment.