Skip to content

Commit 6d6c9b6

Browse files
committedJan 6, 2019
Add lessThanOrEqual and moreThanOrEqual operators.
In typeorm#3126 @alfaproject asked about this two operators. I decide to implement and send the PR with this operators.
1 parent 0c85b84 commit 6d6c9b6

File tree

7 files changed

+162
-2
lines changed

7 files changed

+162
-2
lines changed
 

‎docs/find-options.md

+32
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ will execute following query:
171171
SELECT * FROM "post" WHERE "likes" < 10
172172
```
173173

174+
* `LessThanOrEqual`
175+
176+
```ts
177+
import {LessThanOrEqual} from "typeorm";
178+
179+
const loadedPosts = await connection.getRepository(Post).find({
180+
likes: LessThanOrEqual(10)
181+
});
182+
```
183+
184+
will execute following query:
185+
186+
```sql
187+
SELECT * FROM "post" WHERE "likes" <= 10
188+
```
189+
174190
* `MoreThan`
175191

176192
```ts
@@ -187,6 +203,22 @@ will execute following query:
187203
SELECT * FROM "post" WHERE "likes" > 10
188204
```
189205

206+
* `MoreThanOrEqual`
207+
208+
```ts
209+
import {MoreThanOrEqual} from "typeorm";
210+
211+
const loadedPosts = await connection.getRepository(Post).find({
212+
likes: MoreThanOrEqual(10)
213+
});
214+
```
215+
216+
will execute following query:
217+
218+
```sql
219+
SELECT * FROM "post" WHERE "likes" >= 10
220+
```
221+
190222
* `Equal`
191223

192224
```ts

‎src/find-options/FindOperator.ts

+4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ export class FindOperator<T> {
9494
}
9595
case "lessThan":
9696
return `${aliasPath} < ${parameters[0]}`;
97+
case "lessThanOrEqual":
98+
return `${aliasPath} <= ${parameters[0]}`;
9799
case "moreThan":
98100
return `${aliasPath} > ${parameters[0]}`;
101+
case "moreThanOrEqual":
102+
return `${aliasPath} >= ${parameters[0]}`;
99103
case "equal":
100104
return `${aliasPath} = ${parameters[0]}`;
101105
case "like":

‎src/find-options/FindOperatorType.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
*/
44
export type FindOperatorType = "not"
55
| "lessThan"
6+
| "lessThanOrEqual"
67
| "moreThan"
8+
| "moreThanOrEqual"
79
| "equal"
810
| "between"
911
| "in"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {FindOperator} from "../FindOperator";
2+
3+
/**
4+
* Find Options Operator.
5+
* Example: { someField: LessThanOrEqual(10) }
6+
*/
7+
export function LessThanOrEqual<T>(value: T|FindOperator<T>) {
8+
return new FindOperator("lessThanOrEqual", value);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {FindOperator} from "../FindOperator";
2+
3+
/**
4+
* Find Options Operator.
5+
* Example: { someField: MoreThanOrEqual(10) }
6+
*/
7+
export function MoreThanOrEqual<T>(value: T|FindOperator<T>) {
8+
return new FindOperator("moreThanOrEqual", value);
9+
}

‎src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ export * from "./find-options/operator/Equal";
7878
export * from "./find-options/operator/In";
7979
export * from "./find-options/operator/IsNull";
8080
export * from "./find-options/operator/LessThan";
81+
export * from "./find-options/operator/LessThanOrEqual";
8182
export * from "./find-options/operator/Like";
8283
export * from "./find-options/operator/MoreThan";
84+
export * from "./find-options/operator/MoreThanOrEqual";
8385
export * from "./find-options/operator/Not";
8486
export * from "./find-options/operator/Raw";
8587
export * from "./find-options/FindConditions";

‎test/functional/repository/find-options-operators/repository-find-operators.ts

+104-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "reflect-metadata";
22
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
3-
import {Any, Between, Connection, Equal, In, IsNull, LessThan, Like, MoreThan, Not} from "../../../../src";
3+
import {Any, Between, Connection, Equal, In, IsNull, LessThan, LessThanOrEqual, Like, MoreThan, MoreThanOrEqual, Not} from "../../../../src";
44
import {Post} from "./entity/Post";
55
import {PostgresDriver} from "../../../../src/driver/postgres/PostgresDriver";
66
import {Raw} from "../../../../src/find-options/operator/Raw";
@@ -54,6 +54,33 @@ describe("repository > find options > operators", () => {
5454

5555
})));
5656

57+
it("lessThanOrEqual", () => Promise.all(connections.map(async connection => {
58+
59+
// insert some fake data
60+
const post1 = new Post();
61+
post1.title = "About #1";
62+
post1.likes = 12;
63+
await connection.manager.save(post1);
64+
const post2 = new Post();
65+
post2.title = "About #2";
66+
post2.likes = 3;
67+
await connection.manager.save(post2);
68+
const post3 = new Post();
69+
post3.title = "About #3";
70+
post3.likes = 13;
71+
await connection.manager.save(post3);
72+
73+
// check operator
74+
const loadedPosts = await connection.getRepository(Post).find({
75+
likes: LessThanOrEqual(12)
76+
});
77+
loadedPosts.should.be.eql([
78+
{ id: 1, likes: 12, title: "About #1" },
79+
{ id: 2, likes: 3, title: "About #2" }
80+
]);
81+
82+
})));
83+
5784
it("not(lessThan)", () => Promise.all(connections.map(async connection => {
5885

5986
// insert some fake data
@@ -74,6 +101,30 @@ describe("repository > find options > operators", () => {
74101

75102
})));
76103

104+
it("not(lessThanOrEqual)", () => Promise.all(connections.map(async connection => {
105+
106+
// insert some fake data
107+
const post1 = new Post();
108+
post1.title = "About #1";
109+
post1.likes = 12;
110+
await connection.manager.save(post1);
111+
const post2 = new Post();
112+
post2.title = "About #2";
113+
post2.likes = 3;
114+
await connection.manager.save(post2);
115+
const post3 = new Post();
116+
post3.title = "About #3";
117+
post3.likes = 13;
118+
await connection.manager.save(post3);
119+
120+
// check operator
121+
const loadedPosts = await connection.getRepository(Post).find({
122+
likes: Not(LessThanOrEqual(12))
123+
});
124+
loadedPosts.should.be.eql([{ id: 3, likes: 13, title: "About #3" }]);
125+
126+
})));
127+
77128
it("moreThan", () => Promise.all(connections.map(async connection => {
78129

79130
// insert some fake data
@@ -94,6 +145,33 @@ describe("repository > find options > operators", () => {
94145

95146
})));
96147

148+
it("moreThanOrEqual", () => Promise.all(connections.map(async connection => {
149+
150+
// insert some fake data
151+
const post1 = new Post();
152+
post1.title = "About #1";
153+
post1.likes = 12;
154+
await connection.manager.save(post1);
155+
const post2 = new Post();
156+
post2.title = "About #2";
157+
post2.likes = 3;
158+
await connection.manager.save(post2);
159+
const post3 = new Post();
160+
post3.title = "About #3";
161+
post3.likes = 13;
162+
await connection.manager.save(post3);
163+
164+
// check operator
165+
const loadedPosts = await connection.getRepository(Post).find({
166+
likes: MoreThanOrEqual(12)
167+
});
168+
loadedPosts.should.be.eql([
169+
{ id: 1, likes: 12, title: "About #1" },
170+
{ id: 3, likes: 13, title: "About #3" }
171+
]);
172+
173+
})));
174+
97175
it("not(moreThan)", () => Promise.all(connections.map(async connection => {
98176

99177
// insert some fake data
@@ -114,6 +192,30 @@ describe("repository > find options > operators", () => {
114192

115193
})));
116194

195+
it("not(moreThanOrEqual)", () => Promise.all(connections.map(async connection => {
196+
197+
// insert some fake data
198+
const post1 = new Post();
199+
post1.title = "About #1";
200+
post1.likes = 12;
201+
await connection.manager.save(post1);
202+
const post2 = new Post();
203+
post2.title = "About #2";
204+
post2.likes = 3;
205+
await connection.manager.save(post2);
206+
const post3 = new Post();
207+
post3.title = "About #3";
208+
post3.likes = 13;
209+
await connection.manager.save(post3);
210+
211+
// check operator
212+
const loadedPosts = await connection.getRepository(Post).find({
213+
likes: Not(MoreThanOrEqual(12))
214+
});
215+
loadedPosts.should.be.eql([{ id: 2, likes: 3, title: "About #2" }]);
216+
217+
})));
218+
117219
it("equal", () => Promise.all(connections.map(async connection => {
118220

119221
// insert some fake data
@@ -417,4 +519,4 @@ describe("repository > find options > operators", () => {
417519

418520
})));
419521

420-
});
522+
});

0 commit comments

Comments
 (0)
Please sign in to comment.