This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
generated from KennethTrecy/web_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intrn(query): make query to filter posts by time range
- Loading branch information
1 parent
256f37a
commit db24373
Showing
2 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Model from "%/models/post" | ||
import Factory from "~/factories/post" | ||
|
||
import pipe from "./sift_by_range" | ||
|
||
describe("Database Pipe: Sift by range", () => { | ||
it("can find inside time span", async() => { | ||
const CURRENT_TIME = new Date() | ||
const TIME_OFFSET = 10 | ||
const BEGIN_TIME = new Date() | ||
BEGIN_TIME.setSeconds(CURRENT_TIME.getSeconds() - TIME_OFFSET) | ||
const END_TIME = new Date() | ||
END_TIME.setSeconds(CURRENT_TIME.getSeconds() + TIME_OFFSET * 2) | ||
const model = await new Factory().insertOne() | ||
|
||
const options = pipe({}, { | ||
"filter": { | ||
"dateTimeRange": { | ||
"begin": BEGIN_TIME, | ||
"end": END_TIME | ||
} | ||
} | ||
}) | ||
|
||
const foundModels = await Model.findAll(options) | ||
|
||
expect(foundModels).toHaveLength(1) | ||
expect(foundModels).toHaveProperty("0.id", model.id) | ||
}) | ||
|
||
it("cannot find outside time span", async() => { | ||
const CURRENT_TIME = new Date() | ||
const TIME_OFFSET = 5 | ||
const BEGIN_TIME = new Date() | ||
BEGIN_TIME.setSeconds(CURRENT_TIME.getSeconds() - TIME_OFFSET * 2) | ||
const END_TIME = new Date() | ||
END_TIME.setSeconds(CURRENT_TIME.getSeconds() - TIME_OFFSET) | ||
await new Factory().insertOne() | ||
|
||
const options = pipe({}, { | ||
"filter": { | ||
"dateTimeRange": { | ||
"begin": BEGIN_TIME, | ||
"end": END_TIME | ||
} | ||
} | ||
}) | ||
const foundModels = await Model.findAll(options) | ||
|
||
expect(foundModels).toHaveLength(0) | ||
}) | ||
}) |
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,38 @@ | ||
import type { FindOptions } from "%/types/dependent" | ||
import type { DateTimeRangeFilter } from "$/types/query" | ||
|
||
import Log from "$!/singletons/log" | ||
|
||
import Condition from "%/helpers/condition" | ||
import isUndefined from "$/type_guards/is_undefined" | ||
|
||
/** | ||
* Sift post models which within a certain range. | ||
*/ | ||
export default function<T>( | ||
currentState: FindOptions<T>, | ||
constraints: DateTimeRangeFilter | ||
): FindOptions<T> { | ||
const newState = { ...currentState } | ||
|
||
const { dateTimeRange } = constraints.filter | ||
|
||
if (isUndefined(newState.where)) { | ||
newState.where = {} | ||
} | ||
|
||
const condition = new Condition() | ||
condition.and( | ||
new Condition().greaterThanOrEqual("createdAt", dateTimeRange.begin), | ||
new Condition().lessThanOrEqual("createdAt", dateTimeRange.end) | ||
) | ||
|
||
newState.where = new Condition().and( | ||
new Condition(newState.where), | ||
condition | ||
).build() | ||
|
||
Log.trace("pipeline", "sift by range") | ||
|
||
return newState | ||
} |