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

Commit

Permalink
WIP: Support multiple where statements (#240)
Browse files Browse the repository at this point in the history
* Support multiple where statements

* Fix typing

* Hide overloads

* Fixes

Co-authored-by: Michał Miszczyszyn <[email protected]>
Co-authored-by: Michal Miszczyszyn <[email protected]>
  • Loading branch information
3 people authored May 26, 2020
1 parent 834396a commit 1524d11
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 49 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
jest.config.js
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"tslint.run": "onSave",
"tslint.autoFixOnSave": true,
"prettier.semi": true,
"prettier.trailingComma": "all"
"prettier.trailingComma": "all",
"typescript.tsdk": "node_modules/typescript/lib"
}
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testRegex: '((\\.|/)(test|spec))\\.(ts?)$',
setupFilesAfterEnv: ['jest-extended'],
};
115 changes: 101 additions & 14 deletions src/__tests__/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../generator';
import Path from 'path';
import { compileTypeScriptCode } from './tsCompiler';
import { Gostek, Op } from '../querybuilder/querybuilder';
import { Gostek, Op, WhereOp } from '../querybuilder/querybuilder';
import { User } from './generated/models';

describe('unit tests', () => {
Expand Down Expand Up @@ -252,7 +252,7 @@ export const User = {
Gostek.from(User)
.select(['id'])
.select('*')
.where(['name', Op.$eq, 'Michał'])
.where({ [WhereOp.$and]: [['name', Op.$eq, 'Michał']] })
.getQuery(),
).toEqual({
text: 'SELECT * FROM "user" WHERE "name" = $1',
Expand All @@ -262,13 +262,29 @@ export const User = {
expect(
Gostek.from(User)
.select(['id', 'name'])
.where(['id', Op.$in, [1, 2, 3]])
.where({ [WhereOp.$and]: [['id', Op.$in, [1, 2, 3]]] })
.getQuery(),
).toEqual({
text:
'SELECT "user"."id", "user"."name" FROM "user" WHERE "id" in ($1,$2,$3)',
values: [1, 2, 3],
});

expect(
Gostek.from(User)
.select(['id', 'name'])
.where({
[WhereOp.$and]: [
['id', Op.$in, [1, 2, 3]],
['name', Op.$eq, 'Kasia'],
],
})
.getQuery(),
).toEqual({
text:
'SELECT "user"."id", "user"."name" FROM "user" WHERE "id" in ($1,$2,$3) AND "name" = $4',
values: [1, 2, 3, 'Kasia'],
});
});

it('can insert a full new entity to database', async () => {
Expand All @@ -293,7 +309,7 @@ export const User = {
int2Column: 2,
int4Column: 11,
int8Column: 10n,
numericColumn: 50.5,
numericColumn: '50.5',
jsonbColumn: {
some: 'value',
is: 1,
Expand Down Expand Up @@ -337,7 +353,7 @@ export const User = {
int2Column: null,
int4Column: 11,
int8Column: null,
numericColumn: 50.5,
numericColumn: '50.5',
jsonbColumn: {
some: 'value',
is: 1,
Expand All @@ -357,7 +373,6 @@ export const User = {
expect(results[0]).toEqual({
...userObject,
dateColumn: nowWithoutTimezoneDatePrecision,
numericColumn: '50.5',
});
});

Expand Down Expand Up @@ -413,6 +428,8 @@ export const User = {
varcharColumn: '',
};

Gostek.to(User).insertOne(one);

await db.none(`INSERT INTO "user" VALUES (
1,
'[email protected]',
Expand Down Expand Up @@ -457,25 +474,95 @@ export const User = {
two,
]);

expect(await Gostek.from(User).select(['id']).execute(db)).toEqual([
{ id: one.id },
{ id: two.id },
]);
expect(await Gostek.from(User).select(['id']).execute(db)).toContainEqual(
{
id: expect.toBeNumber(),
},
);

expect(
await Gostek.from(User)
.select(['id'])
.select('*')
.where(['name', Op.$eq, 'Michał'])
.where({ [WhereOp.$and]: [['name', Op.$eq, 'Michał']] })
.execute(db),
).toSatisfyAll((el) => {
expect(el).toMatchObject({
id: expect.toBeNumber(),
name: 'Michał',
});
return true;
});

expect(
await Gostek.from(User)
.select(['id', 'name'])
.where({ [WhereOp.$and]: [['id', Op.$in, [1, 2, 3]]] })
.execute(db),
).toSatisfyAll((el) => {
expect(el).toEqual({
id: expect.toBeOneOf([1, 2, 3]),
name: expect.toBeString(),
});
return true;
});

expect(
await Gostek.from(User)
.select(['id', 'name'])
.where({
[WhereOp.$and]: [
['id', Op.$in, [1, 2, 3]],
['name', Op.$eq, 'Michał'],
],
})
.execute(db),
).toSatisfyAll((el) => {
expect(el).toEqual({
id: expect.toBeOneOf([1, 2, 3]),
name: 'Michał',
});
return true;
});

expect(
await Gostek.from(User)
.select(['id', 'name'])
.where({
[WhereOp.$or]: [
['id', Op.$in, [1, 2, 3]],
['name', Op.$eq, 'Michal'],
],
})
.execute(db),
).toEqual([one]);
).toSatisfyAll((el) => {
expect(el).toBeOneOf([
{
id: expect.toBeOneOf([1, 2, 3]),
name: expect.toBeString(),
},
{
id: expect.toBeNumber(),
name: 'Michał',
},
]);
return true;
});

expect(
await Gostek.from(User)
.select(['id', 'name'])
.where(['id', Op.$in, [1, 2, 3]])
.where({
[WhereOp.$or]: [
['name', Op.$eq, 'Michał'],
['name', Op.$eq, 'Kasia'],
],
})
.execute(db),
).toEqual([{ id: one.id, name: one.name }]);
).toEqual([
{ id: one.id, name: one.name },
{ id: two.id, name: two.name },
]);
});
});
});
47 changes: 32 additions & 15 deletions src/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Gostek, Op } from './querybuilder/querybuilder';
import { Gostek, Op, WhereOp } from './querybuilder/querybuilder';
// tslint:disable:no-magic-numbers

const User = {
Expand All @@ -12,39 +12,56 @@ const User = {
} as const;

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

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

// $ExpectError
Gostek.from(User).select(['id']).where(['id', Op.$in, null]);
Gostek.from(User)
.select(['id'])
// @ts-expect-error
.where({ [WhereOp.$and]: [['id', Op.$in, null]] });

// $ExpectError
// @ts-expect-error
Gostek.to(User).select();

// $ExpectError
// @ts-expect-error
Gostek.to(User).insertOne();

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

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

// $ExpectError
Gostek.from(User).select(['userData']).where(['userData', Op.$eq, undefined]);
Gostek.from(User)
.select(['userData'])
// @ts-expect-error
.where({ [WhereOp.$and]: [['userData', Op.$eq, undefined]] });

Gostek.from(User)
.select(['userData'])
.where({
// @ts-expect-error
[WhereOp.$and]: [['name', Op.$eq, 'name']],
// @ts-expect-error
[WhereOp.$or]: [['name', Op.$eq, 'name']],
});

Gostek.from(User)
.select(['id'])
// $ExpectError
.where(['id', Op.$in, ['a', 'b', 'c']]);
.where({
// @ts-expect-error
[WhereOp.$and]: [['id', Op.$in, ['a', 'b', 'c']]],
});

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

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

0 comments on commit 1524d11

Please sign in to comment.