Skip to content

Commit 7c8d460

Browse files
authored
fix: Reduces database queries by avoiding queries with empty ins. (#511)
1 parent 7948c7f commit 7c8d460

File tree

9 files changed

+34
-0
lines changed

9 files changed

+34
-0
lines changed

src/apps/statements/repo/modelsRepo/getStatementsByIds/memory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import Signature from './Signature';
55

66
export default (config: FacadeConfig): Signature => {
77
return async ({ client, ids }) => {
8+
if (ids.length === 0) {
9+
return [];
10+
}
811
const filteredModels = config.state.statements.filter((model) => {
912
return (
1013
includes(ids, model.statement.id) &&

src/apps/statements/repo/modelsRepo/getStatementsByIds/mongo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ interface Result {
1111

1212
export default (config: FacadeConfig): Signature => {
1313
return async ({ client, ids }) => {
14+
if (ids.length === 0) {
15+
return [];
16+
}
1417
const collection = (await config.db()).collection(STATEMENTS_COLLECTION_NAME);
1518

1619
const query = {

src/apps/statements/repo/modelsRepo/getUpRefsByIds/memory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const getTargetId = (model: StoredStatementModel) => {
1616

1717
export default (config: FacadeConfig): Signature => {
1818
return async ({ client, targetIds }) => {
19+
if (targetIds.length === 0) {
20+
return [];
21+
}
1922
const filteredModels = config.state.statements.filter((model) => {
2023
return (
2124
model.statement.object.objectType === 'StatementRef' &&

src/apps/statements/repo/modelsRepo/getUpRefsByIds/mongo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ interface Result {
1414

1515
export default (config: FacadeConfig): Signature => {
1616
return async ({ client, targetIds }) => {
17+
if (targetIds.length === 0) {
18+
return [];
19+
}
1720
const collection = (await config.db()).collection(STATEMENTS_COLLECTION_NAME);
1821

1922
const query = {

src/apps/statements/repo/modelsRepo/getVoidersByIds/memory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import Signature from './Signature';
77

88
export default (config: FacadeConfig): Signature => {
99
return async ({ client, ids }) => {
10+
// istanbul ignore next - this is checked higher in the code.
11+
if (ids.length === 0) {
12+
return [];
13+
}
1014
const query = (statement: Statement) => {
1115
return (
1216
statement.verb.id === voidVerbId &&

src/apps/statements/repo/modelsRepo/getVoidersByIds/mongo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ interface Result { readonly statement: { readonly id: string }; }
77

88
export default (config: FacadeConfig): Signature => {
99
return async ({ client, ids }) => {
10+
// istanbul ignore next - this is checked higher in the code.
11+
if (ids.length === 0) {
12+
return [];
13+
}
1014
const query = {
1115
'statement.id': { $in: ids },
1216
...voidQuery,

src/apps/statements/repo/modelsRepo/getVoidersByObjectIds/memory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import Signature from './Signature';
88

99
export default (config: FacadeConfig): Signature => {
1010
return async ({ client, ids }) => {
11+
if (ids.length === 0) {
12+
return [];
13+
}
1114
const query = (statement: Statement) => {
1215
return (
1316
statement.verb.id === voidVerbId &&

src/apps/statements/repo/modelsRepo/getVoidersByObjectIds/mongo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ interface Result { readonly statement: { readonly object: { readonly id: string
77

88
export default (config: FacadeConfig): Signature => {
99
return async ({ client, ids }) => {
10+
if (ids.length === 0) {
11+
return [];
12+
}
1013
const query = {
1114
'statement.object.id': { $in: ids },
1215
...voidQuery,

src/apps/statements/tests/store.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ describe('get statement', () => {
3030
assertNoStatements(actualStatements as Statement[]);
3131
});
3232

33+
it('should ensure that we do not make unnecessary database queries for refs', async () => {
34+
await storeStatements([{
35+
actor: { mbox: 'mailto:test@example.org' },
36+
verb: { id: 'https://example.org/verb' },
37+
object: { id: 'https://example.org/activity' },
38+
}]);
39+
});
40+
3341
it('should throw an error when the store does not match', async () => {
3442
const unknownClient = createClientModel({
3543
lrs_id: '5988ff000000000000000001',

0 commit comments

Comments
 (0)