jin-qu/jinqu-odata

OData date/time functions and Date literals

Closed this issue · 1 comments

jinqu-odata version

1.1.3

Proposed behavior

  1. Adding .getDatePart() and .getTimePart() pseudo-members (which are mapped to OData date() and time() respectively).
    OData accepts date() and time() functions in $filter which are very important for filtering purposes (especially date()).
    E.g. I have DeliveryDate(datetime2) in my DB which has both date and time parts, and I want to filter it by date part only.
    With this extensions I can make a query like
    query.where(`DeliveryDate.getDatePart() == value`, { value });
    which is converted to
    $filter=date(DeliveryDate) eq YYYY-MM-DD
    Otherwise I would need to check an interval between value and value+1 which would require a pile of extra code.
    If someone wants to be precise, he can extend Date prototype with this functions and use them in jinqu expression instead of string, e.g.
interface Date {
    getDatePart(): Date;
}

Date.prototype.getDatePart = function () {
    const ret = new Date(this);
    ret.setHours(0, 0, 0, 0);
    return ret;
}
  1. Providing date/time literals as Date('YYYY-MM-DDThh:mm:ss.sssZ') or Date('YYYY-MM-DD')
    OData accepts date/time literals as ISO 8601 strings without quotes, and currently jinqu-odata doesn't allow to pass such literals inside the string expression of 'where' method. Sometimes it is more convenient to pass Date literals instead of scope values.
    E.g. I use the following code to filter the grid:
  for (var field in filters) {
      if (field.match(/Date$/)) {
          query = query.where(whereDatePredicate(field, filters[field].matchMode!, filters[field].value));
      } else {
          query = query.where(wherePredicate(field, filters[field].matchMode!, filters[field].value));
      }
  }
...
function whereDatePredicate(property: string, operator: string, value: Date): string {
    return `${property}.getDatePart() ${operator} Date('${value.toDateString()}')`;
}

And this extension does not require modifying Tokenizer.

Actual behavior

  1. No ODate date() and time() functions
  2. No date/time literals in expressions

Again, all changes are already done in my repository, cannot create chained PRs