Skip to content

Commit

Permalink
fix: Reduce insert queries for deep update (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs authored Apr 10, 2024
1 parent 0c427be commit 55e5114
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
17 changes: 15 additions & 2 deletions db-service/lib/deep-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,23 @@ const _getDeepQueries = (diff, target, root = false) => {
queries.push(...subQueries)
}

queries.forEach(q => {
const insertQueries = new Map()

return queries.map(q => {
// Merge all INSERT statements for each target
if (q.INSERT) {
const target = q.target
if (insertQueries.has(target)) {
insertQueries.get(target).INSERT.entries.push(...q.INSERT.entries)
return
} else {
insertQueries.set(target, q)
}
}
Object.defineProperty(q, handledDeep, { value: true })
return q
})
return queries
.filter(a => a)
}

module.exports = {
Expand Down
22 changes: 6 additions & 16 deletions db-service/test/deep/deep.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,22 +787,12 @@ describe('test deep query generation', () => {
const deepQueries = getDeepQueries(query, [], model.definitions.Root)

const expectedInserts = [
INSERT.into(model.definitions.Root).entries({ ID: 1 }),
INSERT.into(model.definitions.Root).entries({ ID: 2 }),
INSERT.into(model.definitions.Root).entries({ ID: 3 }),
INSERT.into(model.definitions.Child).entries({ ID: 1 }),
INSERT.into(model.definitions.Child).entries({ ID: 2 }),
INSERT.into(model.definitions.Child).entries({ ID: 3 }),
INSERT.into(model.definitions.Child).entries({ ID: 4 }),
INSERT.into(model.definitions.Child).entries({ ID: 5 }),
INSERT.into(model.definitions.Child).entries({ ID: 6 }),
INSERT.into(model.definitions.Child).entries({ ID: 7 }),
INSERT.into(model.definitions.Child).entries({ ID: 8 }),
INSERT.into(model.definitions.Child).entries({ ID: 9 }),
INSERT.into(model.definitions.SubChild).entries({ ID: 10 }),
INSERT.into(model.definitions.SubChild).entries({ ID: 11 }),
INSERT.into(model.definitions.SubChild).entries({ ID: 12 }),
INSERT.into(model.definitions.SubChild).entries({ ID: 13 }),
INSERT.into(model.definitions.Root)
.entries([{ ID: 1 }, { ID: 2 }, { ID: 3 }]),
INSERT.into(model.definitions.Child)
.entries([{ ID: 1 }, { ID: 2 }, { ID: 3 }, { ID: 4 }, { ID: 5 }, { ID: 6 }, { ID: 7 }, { ID: 8 }, { ID: 9 }]),
INSERT.into(model.definitions.SubChild)
.entries([{ ID: 10 }, { ID: 11 }, { ID: 12 }, { ID: 13 }]),
]

expectedInserts.forEach(insert => {
Expand Down

0 comments on commit 55e5114

Please sign in to comment.