Skip to content
This repository was archived by the owner on Jun 26, 2021. It is now read-only.

Commit 1524d11

Browse files
kozikkamMichał Miszczyszyntypeofweb
authored
WIP: Support multiple where statements (#240)
* Support multiple where statements * Fix typing * Hide overloads * Fixes Co-authored-by: Michał Miszczyszyn <[email protected]> Co-authored-by: Michal Miszczyszyn <[email protected]>
1 parent 834396a commit 1524d11

File tree

7 files changed

+291
-49
lines changed

7 files changed

+291
-49
lines changed
File renamed without changes.

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
jest.config.js

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"tslint.run": "onSave",
44
"tslint.autoFixOnSave": true,
55
"prettier.semi": true,
6-
"prettier.trailingComma": "all"
6+
"prettier.trailingComma": "all",
7+
"typescript.tsdk": "node_modules/typescript/lib"
78
}

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
44
testRegex: '((\\.|/)(test|spec))\\.(ts?)$',
5+
setupFilesAfterEnv: ['jest-extended'],
56
};

src/__tests__/test.ts

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '../generator';
1010
import Path from 'path';
1111
import { compileTypeScriptCode } from './tsCompiler';
12-
import { Gostek, Op } from '../querybuilder/querybuilder';
12+
import { Gostek, Op, WhereOp } from '../querybuilder/querybuilder';
1313
import { User } from './generated/models';
1414

1515
describe('unit tests', () => {
@@ -252,7 +252,7 @@ export const User = {
252252
Gostek.from(User)
253253
.select(['id'])
254254
.select('*')
255-
.where(['name', Op.$eq, 'Michał'])
255+
.where({ [WhereOp.$and]: [['name', Op.$eq, 'Michał']] })
256256
.getQuery(),
257257
).toEqual({
258258
text: 'SELECT * FROM "user" WHERE "name" = $1',
@@ -262,13 +262,29 @@ export const User = {
262262
expect(
263263
Gostek.from(User)
264264
.select(['id', 'name'])
265-
.where(['id', Op.$in, [1, 2, 3]])
265+
.where({ [WhereOp.$and]: [['id', Op.$in, [1, 2, 3]]] })
266266
.getQuery(),
267267
).toEqual({
268268
text:
269269
'SELECT "user"."id", "user"."name" FROM "user" WHERE "id" in ($1,$2,$3)',
270270
values: [1, 2, 3],
271271
});
272+
273+
expect(
274+
Gostek.from(User)
275+
.select(['id', 'name'])
276+
.where({
277+
[WhereOp.$and]: [
278+
['id', Op.$in, [1, 2, 3]],
279+
['name', Op.$eq, 'Kasia'],
280+
],
281+
})
282+
.getQuery(),
283+
).toEqual({
284+
text:
285+
'SELECT "user"."id", "user"."name" FROM "user" WHERE "id" in ($1,$2,$3) AND "name" = $4',
286+
values: [1, 2, 3, 'Kasia'],
287+
});
272288
});
273289

274290
it('can insert a full new entity to database', async () => {
@@ -293,7 +309,7 @@ export const User = {
293309
int2Column: 2,
294310
int4Column: 11,
295311
int8Column: 10n,
296-
numericColumn: 50.5,
312+
numericColumn: '50.5',
297313
jsonbColumn: {
298314
some: 'value',
299315
is: 1,
@@ -337,7 +353,7 @@ export const User = {
337353
int2Column: null,
338354
int4Column: 11,
339355
int8Column: null,
340-
numericColumn: 50.5,
356+
numericColumn: '50.5',
341357
jsonbColumn: {
342358
some: 'value',
343359
is: 1,
@@ -357,7 +373,6 @@ export const User = {
357373
expect(results[0]).toEqual({
358374
...userObject,
359375
dateColumn: nowWithoutTimezoneDatePrecision,
360-
numericColumn: '50.5',
361376
});
362377
});
363378

@@ -413,6 +428,8 @@ export const User = {
413428
varcharColumn: '',
414429
};
415430

431+
Gostek.to(User).insertOne(one);
432+
416433
await db.none(`INSERT INTO "user" VALUES (
417434
1,
418435
@@ -457,25 +474,95 @@ export const User = {
457474
two,
458475
]);
459476

460-
expect(await Gostek.from(User).select(['id']).execute(db)).toEqual([
461-
{ id: one.id },
462-
{ id: two.id },
463-
]);
477+
expect(await Gostek.from(User).select(['id']).execute(db)).toContainEqual(
478+
{
479+
id: expect.toBeNumber(),
480+
},
481+
);
464482

465483
expect(
466484
await Gostek.from(User)
467485
.select(['id'])
468486
.select('*')
469-
.where(['name', Op.$eq, 'Michał'])
487+
.where({ [WhereOp.$and]: [['name', Op.$eq, 'Michał']] })
488+
.execute(db),
489+
).toSatisfyAll((el) => {
490+
expect(el).toMatchObject({
491+
id: expect.toBeNumber(),
492+
name: 'Michał',
493+
});
494+
return true;
495+
});
496+
497+
expect(
498+
await Gostek.from(User)
499+
.select(['id', 'name'])
500+
.where({ [WhereOp.$and]: [['id', Op.$in, [1, 2, 3]]] })
501+
.execute(db),
502+
).toSatisfyAll((el) => {
503+
expect(el).toEqual({
504+
id: expect.toBeOneOf([1, 2, 3]),
505+
name: expect.toBeString(),
506+
});
507+
return true;
508+
});
509+
510+
expect(
511+
await Gostek.from(User)
512+
.select(['id', 'name'])
513+
.where({
514+
[WhereOp.$and]: [
515+
['id', Op.$in, [1, 2, 3]],
516+
['name', Op.$eq, 'Michał'],
517+
],
518+
})
519+
.execute(db),
520+
).toSatisfyAll((el) => {
521+
expect(el).toEqual({
522+
id: expect.toBeOneOf([1, 2, 3]),
523+
name: 'Michał',
524+
});
525+
return true;
526+
});
527+
528+
expect(
529+
await Gostek.from(User)
530+
.select(['id', 'name'])
531+
.where({
532+
[WhereOp.$or]: [
533+
['id', Op.$in, [1, 2, 3]],
534+
['name', Op.$eq, 'Michal'],
535+
],
536+
})
470537
.execute(db),
471-
).toEqual([one]);
538+
).toSatisfyAll((el) => {
539+
expect(el).toBeOneOf([
540+
{
541+
id: expect.toBeOneOf([1, 2, 3]),
542+
name: expect.toBeString(),
543+
},
544+
{
545+
id: expect.toBeNumber(),
546+
name: 'Michał',
547+
},
548+
]);
549+
return true;
550+
});
472551

473552
expect(
474553
await Gostek.from(User)
475554
.select(['id', 'name'])
476-
.where(['id', Op.$in, [1, 2, 3]])
555+
.where({
556+
[WhereOp.$or]: [
557+
['name', Op.$eq, 'Michał'],
558+
['name', Op.$eq, 'Kasia'],
559+
],
560+
})
477561
.execute(db),
478-
).toEqual([{ id: one.id, name: one.name }]);
562+
).toEqual([
563+
{ id: one.id, name: one.name },
564+
{ id: two.id, name: two.name },
565+
]);
479566
});
480567
});
481568
});

src/index.test-d.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Gostek, Op } from './querybuilder/querybuilder';
1+
import { Gostek, Op, WhereOp } from './querybuilder/querybuilder';
22
// tslint:disable:no-magic-numbers
33

44
const User = {
@@ -12,39 +12,56 @@ const User = {
1212
} as const;
1313

1414
async () => {
15-
// $ExpectError
15+
// @ts-expect-error
1616
Gostek.from(User).select('foo');
1717

18-
// $ExpectError
19-
Gostek.from(User).select('*').where(['id', Op.$eq, null]);
18+
Gostek.from(User)
19+
.select('*')
20+
// @ts-expect-error
21+
.where({ [WhereOp.$and]: [['id', Op.$eq, null]] });
2022

21-
// $ExpectError
22-
Gostek.from(User).select(['id']).where(['id', Op.$in, null]);
23+
Gostek.from(User)
24+
.select(['id'])
25+
// @ts-expect-error
26+
.where({ [WhereOp.$and]: [['id', Op.$in, null]] });
2327

24-
// $ExpectError
28+
// @ts-expect-error
2529
Gostek.to(User).select();
2630

27-
// $ExpectError
31+
// @ts-expect-error
2832
Gostek.to(User).insertOne();
2933

30-
// $ExpectError
34+
// @ts-expect-error
3135
Gostek.to(User).insertOne({ notExisting: 'field' });
3236

33-
// $ExpectError
37+
// @ts-expect-error
3438
Gostek.to(User).insertOne({ name: 1 });
3539

36-
// $ExpectError
37-
Gostek.from(User).select(['userData']).where(['userData', Op.$eq, undefined]);
40+
Gostek.from(User)
41+
.select(['userData'])
42+
// @ts-expect-error
43+
.where({ [WhereOp.$and]: [['userData', Op.$eq, undefined]] });
44+
45+
Gostek.from(User)
46+
.select(['userData'])
47+
.where({
48+
// @ts-expect-error
49+
[WhereOp.$and]: [['name', Op.$eq, 'name']],
50+
// @ts-expect-error
51+
[WhereOp.$or]: [['name', Op.$eq, 'name']],
52+
});
3853

3954
Gostek.from(User)
4055
.select(['id'])
41-
// $ExpectError
42-
.where(['id', Op.$in, ['a', 'b', 'c']]);
56+
.where({
57+
// @ts-expect-error
58+
[WhereOp.$and]: [['id', Op.$in, ['a', 'b', 'c']]],
59+
});
4360

4461
// $ExpectType { readonly name: string | null; }[]
4562
await Gostek.from(User)
4663
.select(['name'])
47-
.where(['id', Op.$in, [1, 2, 3]])
64+
.where({ [WhereOp.$and]: [['id', Op.$in, [1, 2, 3]]] })
4865
.execute({} as any);
4966

5067
// $ExpectType { readonly id: number; readonly name: string | null; readonly userData: Json; readonly int8Column: BigInt; }[]

0 commit comments

Comments
 (0)