feat: added model to represent and construct filters when querying for records

This commit is contained in:
StevanFreeborn
2023-02-12 15:52:47 -06:00
parent abf571f415
commit d2edce6290
4 changed files with 257 additions and 67 deletions
+64 -12
View File
@@ -1,12 +1,64 @@
export enum FilterOperators {
Equal = 'eq',
NotEqual = 'ne',
Contains = 'contains',
IsNull = 'isnull',
NotNull = 'notnull',
GreaterThan = 'gt',
LessThan = 'lt',
And = 'and',
Or = 'or',
Not = 'not',
}
/**
* @enum FilterOperators - The operators that can be used in a query filter
*/
export enum FilterOperators {
/**
* @constant Equal - The equal operator
* @remarks Can be used with text, auto-number, date, and number fields as well as their formula equivalents.
*/
Equal = 'eq',
/**
* @constant NotEqual - The not equal operator
* @remarks Can be used with text, auto-number, date, and number fields as well as their formula equivalents.
*/
NotEqual = 'ne',
/**
* @constant Contains - The contains operator
* @remarks Can be used with list fields as well as their formula equivalents.
*/
Contains = 'contains',
/**
* @constant IsNull - The is null operator
* @remarks Can be used with text, date, and number fields as well as their formula equivalents.
*/
IsNull = 'isnull',
/**
* @constant NotNull - The not null operator
* @remarks Can be used with text, date, and number fields as well as their formula equivalents.
*/
NotNull = 'notnull',
/**
* @constant GreaterThan - The greater than operator
* @remarks Can be used with auto-number, date, and number fields as well as their formula equivalents.
*/
GreaterThan = 'gt',
/**
* @constant LessThan - The less than operator
* @remarks Can be used with auto-number, date, and number fields as well as their formula equivalents.
*/
LessThan = 'lt',
/**
* @constant And - The and operator
* @remarks Can be used to create compound filters.
*/
And = 'and',
/**
* @constant Or - The or operator
* @remarks Can be used to create compound filters.
*/
Or = 'or',
/**
* @constant Not - The not operator
* @remarks Can be used to negate a filter and create compound filters.
*/
Not = 'not',
}
+43
View File
@@ -0,0 +1,43 @@
import { FilterOperators } from '../enums/FilterOperators';
export class QueryFilter {
public fieldId: number;
public operator: FilterOperators;
public value: string | number | Date | null;
constructor(
fieldId: number,
operator: FilterOperators,
value: string | number | Date | null
) {
if (
value === null &&
operator !== FilterOperators.IsNull &&
operator !== FilterOperators.NotNull
) {
throw new Error('Value cannot be null for this operator');
}
this.fieldId = fieldId;
this.operator = operator;
this.value = value;
}
public toString(): string {
if (this.value == null) {
return `${this.fieldId} ${this.operator}`;
}
if (this.value instanceof Date) {
return `${this.fieldId} ${
this.operator
} datetime'${this.value.toISOString()}'`;
}
if (typeof this.value === 'string') {
return `${this.fieldId} ${this.operator} '${this.value}'`;
}
return `${this.fieldId} ${this.operator} ${this.value}`;
}
}
+56 -55
View File
@@ -1,55 +1,56 @@
import { DataFormat } from '../enums/DataFormat';
import { PagingRequest } from './PagingRequest';
/**
* @class QueryRecordsRequest - Request to query for records.
*/
export class QueryRecordsRequest {
/**
* @property {number} appId - The id of the app the records belong to.
*/
appId: number;
/**
* @property {string} filter - The filter to use to query for records.
*/
filter: string;
/**
* @property {number[]} fieldIds - The ids of the fields to include in the response.
*/
fieldIds: number[];
/**
* @property {DataFormat} dataFormat - The format of the data in the response.
*/
dataFormat: DataFormat;
/**
* @property {PagingRequest} pagingRequest - The paging request to use to query for records.
*/
pagingRequest: PagingRequest;
/**
* @constructor - Creates a new instance of QueryRecordsRequest
* @param {number} appId - The id of the app the records belong to.
* @param {string} filter - The filter to use to query for records.
* @param {number[]} fieldIds - The ids of the fields to include in the response.
* @param {DataFormat} dataFormat - The format of the data in the response.
* @param {PagingRequest} pagingRequest - The paging request to use to query for records.
* @returns {QueryRecordsRequest} - A new instance of QueryRecordsRequest
*/
constructor(
appId: number,
filter: string,
fieldIds: number[] = [],
dataFormat: DataFormat = DataFormat.Raw,
pagingRequest: PagingRequest = new PagingRequest(1, 50)
) {
this.appId = appId;
this.filter = filter;
this.fieldIds = fieldIds;
this.dataFormat = dataFormat;
this.pagingRequest = pagingRequest;
}
}
import { DataFormat } from '../enums/DataFormat';
import { PagingRequest } from './PagingRequest';
import { type QueryFilter } from './QueryFilter';
/**
* @class QueryRecordsRequest - Request to query for records.
*/
export class QueryRecordsRequest {
/**
* @property {number} appId - The id of the app the records belong to.
*/
appId: number;
/**
* @property {string | QueryFilter} filter - The filter to use to query for records.
*/
filter: string | QueryFilter;
/**
* @property {number[]} fieldIds - The ids of the fields to include in the response.
*/
fieldIds: number[];
/**
* @property {DataFormat} dataFormat - The format of the data in the response.
*/
dataFormat: DataFormat;
/**
* @property {PagingRequest} pagingRequest - The paging request to use to query for records.
*/
pagingRequest: PagingRequest;
/**
* @constructor - Creates a new instance of QueryRecordsRequest
* @param {number} appId - The id of the app the records belong to.
* @param {string | QueryFilter} filter - The filter to use to query for records.
* @param {number[]} fieldIds - The ids of the fields to include in the response.
* @param {DataFormat} dataFormat - The format of the data in the response.
* @param {PagingRequest} pagingRequest - The paging request to use to query for records.
* @returns {QueryRecordsRequest} - A new instance of QueryRecordsRequest
*/
constructor(
appId: number,
filter: string | QueryFilter,
fieldIds: number[] = [],
dataFormat: DataFormat = DataFormat.Raw,
pagingRequest: PagingRequest = new PagingRequest(1, 50)
) {
this.appId = appId;
this.filter = filter.toString();
this.fieldIds = fieldIds;
this.dataFormat = dataFormat;
this.pagingRequest = pagingRequest;
}
}