Skip to content

NET Implementation of Filtex

License

Notifications You must be signed in to change notification settings

filtex/filtex-net

Repository files navigation

Filtex

The Filtex library is a versatile tool designed to filtering data across various sources like PostgreSQL, MongoDB, and in-memory datasets. This library empowers developers to create complex queries using both JSON and text formats, generating expressions compatible with the target data sources.

It allows to configure your dataset with some options and provides a metadata model to be able to use in UI components and then it accepts the query that is generated by UI and generates query for data sources like Postgres, Mongo etc.

Check the packages for other platforms.

Setup

dotnet add package FiltexNet

Usage

Configure

using FiltexNet;
using FiltexNet.Models;
using FiltexNet.Options;

// Create new filtex instance with options
var fx = Filtex.New(
    FieldOption.New().String().Name("name").Label("Name"),
    FieldOption.New().Number().Name("version").Label("Version"),
    FieldOption.New().Boolean().Name("status").Label("Status").Lookup("statuses"),
    LookupOption.New().Key("statuses").Values(new[]
    {
        new Lookup("Enabled", true),
        new Lookup("Disabled", false),
    })
);

Metadata

// Get metadata model to render components
var metadata = fx.Metadata();

Console.WriteLine(metadata);

Expression From Text

// Generate expression from the text input
var expression = fx.ExpressionFromText("Name Contain Filtex And Status Equal Enabled");

Expression From JSON

// Generate expression from the json input
var expression = fx.ExpressionFromJson(@"[
    ""And"",
    [
        [""Name"", ""Contain"", ""Filtex""],
        [""Status"", ""Equal"", ""Enabled""]
    ]
]");

Validate From Text

// Validate from the text input
fx.ValidateFromText("Name Contain Filtex And Status Equal Enabled");

Validate From JSON

// Validate from the json input
fx.ValidateFromJson(@"[
    ""And"",
    [
        [""Name"", ""Contain"", ""Filtex""],
        [""Status"", ""Equal"", ""Enabled""]
    ]
]");

Mongo Filter

using FiltexNet.Builders.Mongo;

// Generate filter from the expression for mongo
var mongoFilter = new MongoFilterBuilder().Build(expression);

Console.WriteLine(mongoFilter);

// Use generated mongo filter
var result = mongoClient.GetCollection("projects").Find(mongoFilter.Condition);

Console.WriteLine(result);

Postgres Filter

using FiltexNet.Builders.Postgres;

// Generate filter from the expression for postgres
var postgresFilter = new PostgresFilterBuilder().Build(expression);

Console.WriteLine(postgresFilter);

// Use generated postgres filter
var sql = "SELECT * FROM projects WHERE " + postgresFilter.Condition;
var result = postgresClient.Query(sql, postgresFilter.Args);

Console.WriteLine(result);

Memory Filter

using FiltexNet.Builders.Memory;

// Generate filter from the expression for memory
var memoryFilter = new MemoryFilterBuilder().Build(expression);

// Use generated memory function
var result = items.Where(memoryFilter.Fn);

Console.WriteLine(result);

License

This library is licensed under the MIT License.