Filters

From SocialStack
Revision as of 17:57, 10 November 2021 by Luke (talk | contribs) (→‎Operators)

Filters are a query-like mechanism for filtering out results from a list endpoint. At their core, they are textual and support a set of arguments.

Filter examples

A textual filter with arguments is like so:

(UserId=? or UserId=?) and CreatedUtc>?

It is not case sensitive. Filters in the permission system do not support these ? arguments (as it wouldn't know where to bind the value from).

Typically you'll see this textual form of filter inside a Where() call in the C# API:

uint userId = 14;

var listOfThingsByThisUser = await Where("UserId=?").Bind(userId).ListAll(context);

.Bind() is used to bind the actual value of an argument. Values are bound in this way such that the filter string is a constant and can benefit from being precompiled. Indeed, the whole filter system is built to be entirely non-allocating, as the Bind calls internally set your provided value onto an compiled object field, with that object originating from a pool for the given cached filter.

JS filter notation

Filters can also be constructed by the frontend, most often by Loop. In this scenario, a convenience filter notation is also available:

{
    where: {
        userId: 14
    }
}

#Operators can be used by passing an object:

{
    where: {
        userId: {
            greaterThan: 14
        }
    }
}

Or statements are represented with an array:

{
    where: [{
        userId: 14
    },
    {
        userId: 16
    }]
}

And statements are performed by providing multiple field values:

{
    where: {
        userId: 14,
        createdUtc: {
            greaterThan: yesterday /* yesterday is a JS Date */
        }
    }
}

Operators

Filters support a variety of operators for comparisons:

  • Greater than
 * Field>?   (query notation)
 * {greaterThan: x}    (JS notation)
  • Less than
 * Field<?   (query notation)
 * {lessThan: x}    (JS notation)
  • Greater than or equal
 * Field>=?   (query notation)
 * {greaterThanOrEqual: x}    (JS notation)
 * {geq: x}    (JS notation)
  • Less than or equal
 * Field<=?   (query notation)
 * {lessThanOrEqual: x}    (JS notation)
 * {leq: x}    (JS notation)
  • Equals
 * Field=?   (query notation)
 * x    (JS notation)
 * {equals: x}    (JS notation)