-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make selects optionally lazy (#101)
In a effort to make `workers-qb` more efficient for databases that can support cursors/iterables it now supports lazy selects so that, a query like this doesn't potentially OoM the worker: ```sql SELECT * FROM table ``` The API for this is backwards-compatible and you can enable lazy selects by specifying the lazy parameter: ```ts // this will now return a iterable instead of a list this.db .select('table') .execute({lazy: true}) ``` It also works for `.fetchAll` too ```ts // it will also return a iterable this.db .fetchAll({tableName: "table", lazy: true}) .execute() ``` Because of TS type-system, lazy must be statically known at compile otherwise this won't work properly and must always be true or undefined.
- Loading branch information
1 parent
d021135
commit 99181b4
Showing
8 changed files
with
157 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, expectTypeOf, it } from 'vitest' | ||
import { DefaultReturnObject } from '../../src' | ||
import { QuerybuilderTest, QuerybuilderTestSync } from '../utils' | ||
|
||
describe('Select builder', () => { | ||
it('should return a iterable if using async and lazy', async () => { | ||
const results = [ | ||
(await new QuerybuilderTest().select('table').execute({ lazy: true })).results!, | ||
(await new QuerybuilderTest().fetchAll({ tableName: 'table', lazy: true }).execute()).results!, | ||
] | ||
|
||
results.forEach((result) => expectTypeOf(result).toEqualTypeOf<AsyncIterable<DefaultReturnObject>>()) | ||
}) | ||
|
||
it('should return a list if using async and not lazy', async () => { | ||
const results = [ | ||
(await new QuerybuilderTest().select('table').execute()).results!, | ||
(await new QuerybuilderTest().fetchAll({ tableName: 'table' }).execute()).results!, | ||
] | ||
|
||
results.forEach((result) => expectTypeOf(result).toEqualTypeOf<DefaultReturnObject[]>()) | ||
}) | ||
|
||
it('should return a iterable if using sync and lazy', async () => { | ||
const results = [ | ||
new QuerybuilderTestSync().select('table').execute({ lazy: true }).results!, | ||
new QuerybuilderTestSync().fetchAll({ tableName: 'table', lazy: true }).execute().results!, | ||
] | ||
|
||
results.forEach((result) => expectTypeOf(result).toEqualTypeOf<Iterable<DefaultReturnObject>>()) | ||
}) | ||
|
||
it('should return a list if using sync and not lazy', async () => { | ||
const results = [ | ||
new QuerybuilderTestSync().select('table').execute().results!, | ||
new QuerybuilderTestSync().fetchAll({ tableName: 'table' }).execute().results!, | ||
] | ||
|
||
results.forEach((result) => expectTypeOf(result).toEqualTypeOf<DefaultReturnObject[]>()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters