QueryKit icon indicating copy to clipboard operation
QueryKit copied to clipboard

Support for custom sort filter

Open ChasakisD opened this issue 1 year ago • 4 comments

Hey!

Sieve feature of custom sort/filter methods is a pretty handy feature where it comes to support custom or complex queries. https://github.com/Biarity/Sieve?tab=readme-ov-file#add-custom-sortfilter-methods

Is there any plan to support such a feature?

ChasakisD avatar Jul 01 '24 16:07 ChasakisD

First, I'm seeing this honestly. What api might you expect with QueryKit? Could you show an example of it in use?

pdevito3 avatar Jul 01 '24 17:07 pdevito3

Here is an example using where I would like to query Books table and sort/fitler the sold units.

It may become very complex to fit all the cases for filtering (nested, complex queries), so providing custom operators could be very handy.

In the example below, let's say that we want to filter on the property SoldUnits which is the sum of the quantity of ordered books in each order. Or let's say we want to filter on the sum of the quantity of ordered books in each order in the past 10 days.

public class Book
{
    public int Id { get; set; }

    public string Title { get; set; }
    public string Author { get; set; }

    public List<BookOrder> Orders { get; set; }
}

public class BookOrder
{
    public int Id { get; set; }
    public Book Book { get; set; }

    public int Quantity { get; set; }

    public DateTime OrderDate { get; set; }
}

// Lets say that we map it to a dto as follows
public class BookDto
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }

    public int TotalOrders { get; set; }
    public int SoldUnits { get; set; }
}

// Now we can use the following query to get the data
await DataContext.Books
    .Select(b => new BookDto
    {
        Id = b.Id,
        Title = b.Title,
        Author = b.Author,
        TotalOrders = b.Orders.Count(),
        SoldUnits = b.Orders.Sum(o => o.Quantity)
    })
    .ToListAsync();

// So lets say that we want to filter on the soldUnits
// We can do it as follows
private static readonly QueryKitConfiguration QueryConfiguration = new(
    config =>
    {
        config.CustomProperty<Book>(x => x.Orders.Sum(o => o.Quantity));
            .HasQueryName("SoldUnits");

        // or a more complex one
        config.CustomOperation<Book>((x, op, value) => x.Orders.Where(y => y.OrderDate > DateTime.UtcNow.AddDays(-10)).Sum(o => o.Quantity) > value);
            .HasQueryName("SoldUnitsMore10InTheLastDays");
    });

ChasakisD avatar Jul 01 '24 17:07 ChasakisD

So as the application scales, and more data needed, it would be very useful to have the ability to define your custom operations

ChasakisD avatar Jul 01 '24 17:07 ChasakisD

Got it. Thanks for the examples! I could see adding this but probably isn't something I'm going to do immediately. Will put it in the backlog though and I'm open to a PR if you're interested. The recent derivation property PRs below might give a bit of idea a similar flavor of change for reference

  • https://github.com/pdevito3/QueryKit/commit/512f14864642d846152c0cfb1df4c3bfc0e1797e
  • https://github.com/pdevito3/QueryKit/commit/a4bdc22f3c6f140d111befbf320790eaa8ea7082

pdevito3 avatar Jul 02 '24 03:07 pdevito3