Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying types #8

Open
MxAshUp opened this issue Jul 26, 2017 · 5 comments
Open

Support specifying types #8

MxAshUp opened this issue Jul 26, 2017 · 5 comments

Comments

@MxAshUp
Copy link

MxAshUp commented Jul 26, 2017

I think this is an oData v4 thing. Sample code: https://github.com/devnixs/ODataAngularResources/blob/master/src/odatavalue.js

@techniq
Copy link
Owner

techniq commented Jul 27, 2017

I've been debating myself how to handle specifying the types within odata-query. So far I've been considering 2 approaches:

  1. Have the formatting occur ahead of time by exposing a named function (or functions), such as:
import buildQuery, { format } from 'odata-query';
const query = { SomeProp: format(value, 'decimal') }
buildQuery({ filter })

I like this approach but it might be difficult to detect when passing in a "pre-typed" value as a string (ex. datetime'2017-01-01T00:00:00') and when just passing in a simple string value (ex. test) as not all types follow the type'value' format (ex. decimal is 12.345M). When passing in a simple string value, we need to format it by wrapping it in single quotes ' but if we pass in the string 12.345M we would not want to add the wrapping quotes.

  1. Pass in some structure as part of the query and have the formatting occur during query build time. For example:
import buildQuery from 'odata-query';
const query = { SomeProp: { decimal: value }}
buildQuery({ filter })

I'm not sure what the structure of this object should be, and I'm not overly keen on the key being the name of the formatter (although this somewhat aligns with the equality operators and boolean string functions). We would need a known list of formatters ahead of time, which would break supporting custom types (although I've not used them myself). It also seems like this approach adds more "magic" to the query format of odata-query.

For reference, here is the list of types we need to support (there might be more):

  • guid
  • datetime
  • datetimeoffset
  • byte
  • single
  • double
  • decimal
  • boolean
  • int32
  • custom types

@toxik
Copy link
Contributor

toxik commented May 24, 2018

We could also have an explicit value format: { cast: 'guid', value: '312312-3123-12-3123-123' }, see #18

@techniq
Copy link
Owner

techniq commented Jun 7, 2018

Starting in 5.4.0 you can now pass the value as an object { type: 'guid', value: '312312-3123-12-3123-123' }. We currently support type as guid and raw (raw is provided if you want to perform all the formatting externally and not have the value quoted/escaped/etc (like a string normally would be)).

I would still like to support most of the types listed above (ex. datetime'....') before closing the issue, but they should be pretty trivial to implement now. OData/WebApi does not support these from my testing (and is predominately what I use although I'm investigating https://github.com/voronov-maxim/OdataToEntity)

@samuelportz
Copy link
Contributor

How is it supposed to work with the in-Operator? Am I right that currently only eq-Operator is supported?

The current implementation works with the foo and foobar filter, but not the bar filter

buildQuery({
    foo: {
        eq: {
            type: 'guid',
            value: 'value'
        }
    },
    bar: { in: {
            type: 'guid',
            value: ['bar1', 'bar2']
        }
    },
    foobar: { in: [{
            type: 'guid',
            value: 'foobar'
        }, {
            type: 'guid',
            value: 'barfoo'
        }]
    }
})

I implemented a solution for the in-operator (Commit). I don't know how you want to handle this case.

@techniq
Copy link
Owner

techniq commented Jun 28, 2018

@samuelportz all comparison operators except in should be supported, as in is a special convenience operator to explode into an or statement.

Your referenced fix looks good to me and would merge it in if you would send a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants