Skip to content

Commit

Permalink
Manually enableable flag added for converting ids within filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
loeiks committed Jul 24, 2024
1 parent f455e9e commit 23bd394
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
20 changes: 10 additions & 10 deletions app/src/Filter/data_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export class WeivDataFilter {
}

private memoizedBetween!: Function;
between(propertyName: string, rangeStart: any, rangeEnd: any): WeivDataFilter {
between(propertyName: string, rangeStart: any, rangeEnd: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || typeof propertyName !== "string" || !rangeStart || !rangeEnd) {
kaptanLogar("00020", `propertyName, rangeStart and rangeEnd must have valid values to work with between method!`);
}

if (!this.memoizedBetween) {
this.memoizedBetween = memoize((propertyName, rangeStart, rangeEnd) => {
if (propertyName === "_id") {
if (propertyName === "_id" || convertIds) {
return this.addFilter(propertyName, {
$gte: convertIdToObjectId(rangeStart),
$lte: convertIdToObjectId(rangeEnd),
Expand Down Expand Up @@ -85,14 +85,14 @@ export class WeivDataFilter {
}

private memoizedEq!: Function;
eq(propertyName: string, value: any): WeivDataFilter {
eq(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || value === undefined || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with eq method!`);
}

if (!this.memoizedEq) {
this.memoizedEq = memoize((propertyName, value) => {
if (propertyName === "_id") {
if (propertyName === "_id" || convertIds) {
return this.addFilter(propertyName, {
$eq: convertIdToObjectId(value),
});
Expand Down Expand Up @@ -141,14 +141,14 @@ export class WeivDataFilter {
}

private memoizedHasAll!: Function;
hasAll(propertyName: string, value: any): WeivDataFilter {
hasAll(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || !value || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with hasAll method!`);
}

if (!Array.isArray(value)) { value = [value]; }

if (propertyName === "_id") {
if (propertyName === "_id" || convertIds) {
let values = [];
for (const v of value) {
values.push(convertIdToObjectId(v));
Expand All @@ -172,14 +172,14 @@ export class WeivDataFilter {
}

private memoizedHasSome!: Function;
hasSome(propertyName: string, value: any): WeivDataFilter {
hasSome(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || !value || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with hasSome method!`);
}

if (!Array.isArray(value)) { value = [value]; }

if (propertyName === "_id") {
if (propertyName === "_id" || convertIds) {
let values = [];
for (const v of value) {
values.push(convertIdToObjectId(v));
Expand Down Expand Up @@ -267,14 +267,14 @@ export class WeivDataFilter {
}

private memoizedNe!: Function;
ne(propertyName: string, value: any): WeivDataFilter {
ne(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || value === undefined || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with ne method!`);
}

if (!this.memoizedNe) {
this.memoizedNe = memoize((propertyName, value) => {
if (propertyName === "_id") {
if (propertyName === "_id" || convertIds) {
return this.addFilter(propertyName, {
$ne: convertIdToObjectId(value),
});
Expand Down
15 changes: 10 additions & 5 deletions app/weivdata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,10 @@ declare module '@exweiv/weiv-data' {
* @param propertyName The property whose value will be compared with `rangeStart` and `rangeEnd`.
* @param rangeStart The beginning value of the range to match against.
* @param rangeEnd The ending value of the range to match against.
* @param convertIds When enabled passed value will be converted to ObjectId from string. Defaults to false.
* @returns A `WeivDataFilter` cursor representing the refined filters.
*/
between<K extends Internal.CItemKeys<CItem>>(propertyName: K, rangeStart: CItem[K], rangeEnd: CItem[K]): WeivDataFilter<CItem>;
between<K extends Internal.CItemKeys<CItem>>(propertyName: K, rangeStart: CItem[K], rangeEnd: CItem[K], convertIds?: boolean): WeivDataFilter<CItem>;

/**
* @description
Expand Down Expand Up @@ -550,9 +551,10 @@ declare module '@exweiv/weiv-data' {
*
* @param propertyName The property whose value will be compared with `value`.
* @param value The value to match against.
* @param convertIds When enabled passed value will be converted to ObjectId from string. Defaults to false.
* @returns A `WeivDataFilter` cursor representing the refined filters.
*/
eq<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K]): WeivDataFilter<CItem>;
eq<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K], convertIds?: boolean): WeivDataFilter<CItem>;

/**
* @description
Expand Down Expand Up @@ -580,19 +582,21 @@ declare module '@exweiv/weiv-data' {
*
* @param propertyName The property whose value will be compared with `value`.
* @param value The values to match against.
* @param convertIds When enabled passed values will be converted to ObjectId from string. Defaults to false.
* @returns A `WeivDataFilter` cursor representing the refined filters.
*/
hasAll<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K][]): WeivDataFilter<CItem>;
hasAll<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K][], convertIds?: boolean): WeivDataFilter<CItem>;

/**
* @description
* Refines a query or filter to match items whose specified property value equals any of the specified `value` parameters.
*
* @param propertyName The property whose value will be compared with `value`.
* @param value The values to match against.
* @param convertIds When enabled passed values will be converted to ObjectId from string. Defaults to false.
* @returns A `WeivDataFilter` cursor representing the refined filters.
*/
hasSome<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K][]): WeivDataFilter<CItem>;
hasSome<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K][], convertIds?: boolean): WeivDataFilter<CItem>;

/**
* @description
Expand Down Expand Up @@ -638,9 +642,10 @@ declare module '@exweiv/weiv-data' {
*
* @param propertyName The property whose value will be compared with `value`.
* @param value The value to match against.
* @param convertIds When enabled passed value will be converted to ObjectId from string. Defaults to false.
* @returns A `WeivDataFilter` cursor representing the refined filters.
*/
ne<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K]): WeivDataFilter<CItem>;
ne<K extends Internal.CItemKeys<CItem>>(propertyName: K, value: CItem[K], convertIds?: boolean): WeivDataFilter<CItem>;

/**
* @description
Expand Down

0 comments on commit 23bd394

Please sign in to comment.