Skip to content

Commit 6bb3bdd

Browse files
authored
perf(query-typeorm): Custom count implementation for better performance (#223)
This improvement decreases the time it takes for the query to complete with 50% on average. - [x] Fix linting - [ ] Add test
2 parents e4b8286 + 7c80d42 commit 6bb3bdd

File tree

29 files changed

+177
-97
lines changed

29 files changed

+177
-97
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11

2+
# [5.1.0-alpha.2](https://github.com/TriPSs/nestjs-query/compare/v5.1.0-alpha.1...v5.1.0-alpha.2) (2024-03-26)
3+
4+
5+
### Bug Fixes
6+
7+
* **query-graphql:** Fixed relations update/aggregate not respecting the given name ([c252b2a](https://github.com/TriPSs/nestjs-query/commit/c252b2af48eafa9be67665f9f61c1eb9c9ab6778)), closes [#239](https://github.com/TriPSs/nestjs-query/issues/239)
8+
9+
10+
11+
# [5.1.0-alpha.1](https://github.com/TriPSs/nestjs-query/compare/v5.1.0-alpha.0...v5.1.0-alpha.1) (2024-02-12)
12+
13+
14+
15+
# [5.1.0-alpha.0](https://github.com/TriPSs/nestjs-query/compare/v5.0.0...v5.1.0-alpha.0) (2024-02-12)
16+
17+
18+
### Performance Improvements
19+
20+
* **query-typeorm:** Custom count implementation for better performance ([eb89f32](https://github.com/TriPSs/nestjs-query/commit/eb89f32475875850062a635731ddfc0fd96d2455))
21+
22+
23+
224
# [5.0.0](https://github.com/TriPSs/nestjs-query/compare/v4.4.0...v5.0.0) (2024-01-19)
325

426

examples/basic/schema.gql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ type TodoItem {
163163
"""Specify to sort results."""
164164
sorting: [TagSort!]! = []
165165
): TodoItemTagsConnection!
166+
allSubTasks(
167+
"""Specify to filter the records returned."""
168+
filter: SubTaskFilter! = {}
169+
170+
"""Specify to sort results."""
171+
sorting: [SubTaskSort!]! = []
172+
): [SubTask!]!
166173
}
167174

168175
input SubTaskFilter {
@@ -403,7 +410,10 @@ type Mutation {
403410
setSubTasksOnTodoItem(input: SetSubTasksOnTodoItemInput!): TodoItem!
404411
addTagsToTodoItem(input: AddTagsToTodoItemInput!): TodoItem!
405412
setTagsOnTodoItem(input: SetTagsOnTodoItemInput!): TodoItem!
413+
addAllSubTasksToTodoItem(input: AddAllSubTasksToTodoItemInput!): TodoItem!
414+
setAllSubTasksOnTodoItem(input: SetAllSubTasksOnTodoItemInput!): TodoItem!
406415
removeTagsFromTodoItem(input: RemoveTagsFromTodoItemInput!): TodoItem!
416+
removeAllSubTasksFromTodoItem(input: RemoveAllSubTasksFromTodoItemInput!): TodoItem!
407417
createOneTodoItem(input: CreateOneTodoItemInput!): TodoItem!
408418
createManyTodoItems(input: CreateManyTodoItemsInput!): [TodoItem!]!
409419
updateOneTodoItem(input: UpdateOneTodoItemInput!): TodoItem!
@@ -535,6 +545,22 @@ input SetTagsOnTodoItemInput {
535545
relationIds: [ID!]!
536546
}
537547

548+
input AddAllSubTasksToTodoItemInput {
549+
"""The id of the record."""
550+
id: ID!
551+
552+
"""The ids of the relations."""
553+
relationIds: [ID!]!
554+
}
555+
556+
input SetAllSubTasksOnTodoItemInput {
557+
"""The id of the record."""
558+
id: ID!
559+
560+
"""The ids of the relations."""
561+
relationIds: [ID!]!
562+
}
563+
538564
input RemoveTagsFromTodoItemInput {
539565
"""The id of the record."""
540566
id: ID!
@@ -543,6 +569,14 @@ input RemoveTagsFromTodoItemInput {
543569
relationIds: [ID!]!
544570
}
545571

572+
input RemoveAllSubTasksFromTodoItemInput {
573+
"""The id of the record."""
574+
id: ID!
575+
576+
"""The ids of the relations."""
577+
relationIds: [ID!]!
578+
}
579+
546580
input CreateOneTodoItemInput {
547581
"""The record to create"""
548582
todoItem: TodoItemInput!

examples/basic/src/todo-item/dto/todo-item.dto.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GraphQLISODateTime, ID, ObjectType } from '@nestjs/graphql'
2-
import { CursorConnection, FilterableField } from '@ptc-org/nestjs-query-graphql'
2+
import { CursorConnection, FilterableField, UnPagedRelation } from '@ptc-org/nestjs-query-graphql'
33

44
import { SubTaskDTO } from '../../sub-task/dto/sub-task.dto'
55
import { TagDTO } from '../../tag/dto/tag.dto'
@@ -12,6 +12,11 @@ import { TagDTO } from '../../tag/dto/tag.dto'
1212
update: { enabled: true },
1313
remove: { enabled: true }
1414
})
15+
@UnPagedRelation('allSubTasks', () => SubTaskDTO, {
16+
relationName: 'subTasks',
17+
update: { enabled: true },
18+
remove: { enabled: true }
19+
})
1520
export class TodoItemDTO {
1621
@FilterableField(() => ID)
1722
id!: number

examples/dataloader-configuration/schema.gql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ input CursorPaging {
365365
}
366366

367367
type Mutation {
368-
addTodoItemsToSubTask(input: AddTodoItemsToSubTaskInput!): SubTask!
369-
setTodoItemsOnSubTask(input: SetTodoItemsOnSubTaskInput!): SubTask!
368+
addTodoItemToSubTask(input: AddTodoItemToSubTaskInput!): SubTask!
369+
setTodoItemOnSubTask(input: SetTodoItemOnSubTaskInput!): SubTask!
370370
createOneSubTask(input: CreateOneSubTaskInput!): SubTask!
371371
createManySubTasks(input: CreateManySubTasksInput!): [SubTask!]!
372372
updateOneSubTask(input: UpdateOneSubTaskInput!): SubTask!
@@ -395,15 +395,15 @@ type Mutation {
395395
deleteManyTags(input: DeleteManyTagsInput!): DeleteManyResponse!
396396
}
397397

398-
input AddTodoItemsToSubTaskInput {
398+
input AddTodoItemToSubTaskInput {
399399
"""The id of the record."""
400400
id: ID!
401401

402402
"""The ids of the relations."""
403403
relationIds: [ID!]!
404404
}
405405

406-
input SetTodoItemsOnSubTaskInput {
406+
input SetTodoItemOnSubTaskInput {
407407
"""The id of the record."""
408408
id: ID!
409409

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-query",
3-
"version": "5.0.0",
3+
"version": "5.1.0-alpha.2",
44
"private": true,
55
"workspaces": [
66
"packages/**"

packages/core/__tests__/assemblers/assembler.deserializer.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('AssemblerDeserializer decorator', () => {
1111
foo!: string
1212
}
1313

14-
expect(getAssemblerDeserializer(TestSerializer)!({ bar: 'bar' })).toEqual({ foo: 'bar' })
14+
expect(getAssemblerDeserializer(TestSerializer)({ bar: 'bar' })).toEqual({ foo: 'bar' })
1515
})
1616

1717
it('should throw an error if the serializer is registered twice', () => {

packages/core/__tests__/assemblers/assembler.serializer.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('AssemblerSerializer decorator', () => {
99
foo!: string
1010
}
1111

12-
expect(getAssemblerSerializer(TestSerializer)!({ foo: 'bar' })).toEqual({ bar: 'bar' })
12+
expect(getAssemblerSerializer(TestSerializer)({ foo: 'bar' })).toEqual({ bar: 'bar' })
1313
})
1414

1515
it('should throw an error if the serializer is registered twice', () => {

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ptc-org/nestjs-query-core",
3-
"version": "5.0.0",
3+
"version": "5.1.0-alpha.2",
44
"description": "Base query package",
55
"author": "doug-martin <[email protected]>",
66
"homepage": "https://github.com/tripss/nestjs-query#readme",

packages/query-graphql/__tests__/decorators/hook.decorator.spec.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('hook decorators', () => {
2424
@BeforeCreateOne(hookFn)
2525
class Test {}
2626

27-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_ONE, Test)!
27+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_ONE, Test)
2828
expect(new Stored[0]().run).toBe(hookFn)
2929
})
3030

@@ -36,7 +36,7 @@ describe('hook decorators', () => {
3636

3737
class Test extends Base {}
3838

39-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_ONE, Test)!
39+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_ONE, Test)
4040
expect(new Stored[0]().run).toBe(hookFn)
4141
})
4242

@@ -51,7 +51,7 @@ describe('hook decorators', () => {
5151
@BeforeCreateOne(childHookFn)
5252
class Test extends Base {}
5353

54-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_ONE, Test)!
54+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_ONE, Test)
5555
expect(new Stored[0]().run).toBe(childHookFn)
5656
})
5757

@@ -72,7 +72,7 @@ describe('hook decorators', () => {
7272
@BeforeCreateMany(hookFn)
7373
class Test {}
7474

75-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_MANY, Test)!
75+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_MANY, Test)
7676
expect(new Stored[0]().run).toBe(hookFn)
7777
})
7878

@@ -84,7 +84,7 @@ describe('hook decorators', () => {
8484

8585
class Test extends Base {}
8686

87-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_MANY, Test)!
87+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_MANY, Test)
8888
expect(new Stored[0]().run).toBe(hookFn)
8989
})
9090

@@ -99,7 +99,7 @@ describe('hook decorators', () => {
9999
@BeforeCreateMany(childHookFn)
100100
class Test extends Base {}
101101

102-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_MANY, Test)!
102+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_CREATE_MANY, Test)
103103
expect(new Stored[0]().run).toBe(childHookFn)
104104
})
105105

@@ -120,7 +120,7 @@ describe('hook decorators', () => {
120120
@BeforeUpdateOne(hookFn)
121121
class Test {}
122122

123-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_ONE, Test)!
123+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_ONE, Test)
124124
expect(new Stored[0]().run).toBe(hookFn)
125125
})
126126

@@ -132,7 +132,7 @@ describe('hook decorators', () => {
132132

133133
class Test extends Base {}
134134

135-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_ONE, Test)!
135+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_ONE, Test)
136136
expect(new Stored[0]().run).toBe(hookFn)
137137
})
138138

@@ -147,7 +147,7 @@ describe('hook decorators', () => {
147147
@BeforeUpdateOne(childHookFn)
148148
class Test extends Base {}
149149

150-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_ONE, Test)!
150+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_ONE, Test)
151151
expect(new Stored[0]().run).toBe(childHookFn)
152152
})
153153

@@ -168,7 +168,7 @@ describe('hook decorators', () => {
168168
@BeforeUpdateMany(hookFn)
169169
class Test {}
170170

171-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_MANY, Test)!
171+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_MANY, Test)
172172
expect(new Stored[0]().run).toBe(hookFn)
173173
})
174174

@@ -180,7 +180,7 @@ describe('hook decorators', () => {
180180

181181
class Test extends Base {}
182182

183-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_MANY, Test)!
183+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_MANY, Test)
184184
expect(new Stored[0]().run).toBe(hookFn)
185185
})
186186

@@ -195,7 +195,7 @@ describe('hook decorators', () => {
195195
@BeforeUpdateMany(childHookFn)
196196
class Test extends Base {}
197197

198-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_MANY, Test)!
198+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_UPDATE_MANY, Test)
199199
expect(new Stored[0]().run).toBe(childHookFn)
200200
})
201201

@@ -216,7 +216,7 @@ describe('hook decorators', () => {
216216
@BeforeDeleteOne(hookFn)
217217
class Test {}
218218

219-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_ONE, Test)!
219+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_ONE, Test)
220220
expect(new Stored[0]().run).toBe(hookFn)
221221
})
222222

@@ -228,7 +228,7 @@ describe('hook decorators', () => {
228228

229229
class Test extends Base {}
230230

231-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_ONE, Test)!
231+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_ONE, Test)
232232
expect(new Stored[0]().run).toBe(hookFn)
233233
})
234234

@@ -243,7 +243,7 @@ describe('hook decorators', () => {
243243
@BeforeDeleteOne(childHookFn)
244244
class Test extends Base {}
245245

246-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_ONE, Test)!
246+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_ONE, Test)
247247
expect(new Stored[0]().run).toBe(childHookFn)
248248
})
249249

@@ -264,7 +264,7 @@ describe('hook decorators', () => {
264264
@BeforeDeleteMany(hookFn)
265265
class Test {}
266266

267-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_MANY, Test)!
267+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_MANY, Test)
268268
expect(new Stored[0]().run).toBe(hookFn)
269269
})
270270

@@ -276,7 +276,7 @@ describe('hook decorators', () => {
276276

277277
class Test extends Base {}
278278

279-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_MANY, Test)!
279+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_MANY, Test)
280280
expect(new Stored[0]().run).toBe(hookFn)
281281
})
282282

@@ -291,7 +291,7 @@ describe('hook decorators', () => {
291291
@BeforeDeleteMany(childHookFn)
292292
class Test extends Base {}
293293

294-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_MANY, Test)!
294+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_DELETE_MANY, Test)
295295
expect(new Stored[0]().run).toBe(childHookFn)
296296
})
297297

@@ -312,7 +312,7 @@ describe('hook decorators', () => {
312312
@BeforeQueryMany(hookFn)
313313
class Test {}
314314

315-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_QUERY_MANY, Test)!
315+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_QUERY_MANY, Test)
316316
expect(new Stored[0]().run).toBe(hookFn)
317317
})
318318

@@ -324,7 +324,7 @@ describe('hook decorators', () => {
324324

325325
class Test extends Base {}
326326

327-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_QUERY_MANY, Test)!
327+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_QUERY_MANY, Test)
328328
expect(new Stored[0]().run).toBe(hookFn)
329329
})
330330

@@ -339,7 +339,7 @@ describe('hook decorators', () => {
339339
@BeforeQueryMany(childHookFn)
340340
class Test extends Base {}
341341

342-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_QUERY_MANY, Test)!
342+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_QUERY_MANY, Test)
343343
expect(new Stored[0]().run).toBe(childHookFn)
344344
})
345345

@@ -360,7 +360,7 @@ describe('hook decorators', () => {
360360
@BeforeFindOne(hookFn)
361361
class Test {}
362362

363-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_FIND_ONE, Test)!
363+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_FIND_ONE, Test)
364364
expect(new Stored[0]().run).toBe(hookFn)
365365
})
366366

@@ -372,7 +372,7 @@ describe('hook decorators', () => {
372372

373373
class Test extends Base {}
374374

375-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_FIND_ONE, Test)!
375+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_FIND_ONE, Test)
376376
expect(new Stored[0]().run).toBe(hookFn)
377377
})
378378

@@ -387,7 +387,7 @@ describe('hook decorators', () => {
387387
@BeforeFindOne(childHookFn)
388388
class Test extends Base {}
389389

390-
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_FIND_ONE, Test)!
390+
const Stored: Class<Hook<any>>[] = getHooksForType(HookTypes.BEFORE_FIND_ONE, Test)
391391
expect(new Stored[0]().run).toBe(childHookFn)
392392
})
393393

packages/query-graphql/__tests__/decorators/resolver-field.decorator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('ResolverField decorator', (): void => {
3636
it('should call ResolveField with the correct mutation arguments', () => {
3737
const opts: resolverDecorator.ResolverMethodOpts[] = [{}]
3838
createTestResolver('test', () => Boolean, { nullable: true }, ...opts)
39-
const [n, rt, ao] = propertySpy.mock.calls[0]!
39+
const [n, rt, ao] = propertySpy.mock.calls[0]
4040
expect(n).toBe('test')
4141
expect(rt ? rt() : null).toEqual(Boolean)
4242
expect(ao).toEqual({ nullable: true })

0 commit comments

Comments
 (0)