1.0.0-4 • Published 7 months ago

jsearchquery v1.0.0-4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

SearchQuery

SearchQuery is a library that extends LINQ search over a database using Entity Framework. It is not tied to any specific database or storage engine and is instead backed by your existing code and data. Search can be done in two interfaces: IQueryable and IEnumerable.

The main task is to make the request between Frontend - Backend more flexible and easier to search data.

Installation

.NET

To install the SearchQuery package, run the following command:

dotnet add package SearchQuery

JavaScript/TypeScript

To install the SearchQuery package, run the following command:

npm install jsearchquery

Requirements

  • Minimal version: .NET 6.0

Important Notes

  • Ensure to follow valid JSON format and type for request condition field type.

Types

A SearchQuery object type at some point those fields have to resolve to some concrete data.

SearchQuery JS/TSSearchQuery .NET.NET
stringstringstring
DatestringDateTime
numbernumberchar, byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal
booleanbooleanboolean
null/undefinednullnull

Examples

JavaScript/TypeScript

const search = new SearchQuery({
  operator: Operator.And,
  conditions: [
    {
      field: "fullName",
      operation: Operation.Equal,
      values: ["Dennis Ritchie"],
    },
    {
      field: "created",
      // Only two values for Between, NotBetween
      operation: Operation.Between,
      values: ["1990-12-31", "2025-12-31"],
    },
    {
      operator: Operator.Or,
      conditions: [
        {
          field: "fullName",
          operation: Operation.StartsWith,
          values: ["Dennis"],
          incase: Case.Lower,
        },
        {
          field: "fullName",
          operation: Operation.EndsWith,
          values: ["Ritchie"],
          incase: Case.Upper,
        },
      ],
    },
  ],
});

C

var searchQuery = new Search
{
    Operator = Operator.And,
    Conditions = new Conditions {
        new Condition
        {
            Field = nameof(TestEntity.FullName),
            Operation = Operation.Equal,
            Values = new Values
            {
                "Dennis Ritchie"
            }
        },
        new Condition
        {
            Field = nameof(TestEntity.Created),
            // Only two values for Between, NotBetween
            Operation = Operation.Between,
            Values = new Values
            {
                "1990-12-31", "2025-12-31"
            }
        },
        new Query {
            Operator = Operator.Or,
            Conditions = new Conditions {
                new Condition
                {
                    Field = nameof(TestEntity.FullName),
                    Operation = Operation.StartsWith,
                    Values = new Values
                    {
                        "Dennis"
                    },
                    Incase = Case.Lower
                },
                new Condition
                {
                    Field = nameof(TestEntity.FullName),
                    Operation = Operation.EndsWith,
                    Values = new Values
                    {
                        "Ritchie"
                    },
                    Incase = Case.Upper
                }
            }
        }
    }
};
using (var db = new Database()) {
    var query = db.Entities.Search(searchQuery).ToList();
}

API

Namespace: SearchQuery

Class: Extensions

Search Methods

Search for IEnumerable

public static IEnumerable<T> Search<T>(this IEnumerable<T> set, Search query) where T : class;
public static IEnumerable<T> Search<T>(this IEnumerable<T> set, Search query, int pageNumber, int pageSize) where T : class;
  • Description: Filters an IEnumerable collection based on the provided Search query object.
  • Parameters:
    • set: The collection to search.
    • query: The search query.
    • pageNumber: (Optional) The page number for paginated results.
    • pageSize: (Optional) The size of each page.
  • Returns: Filtered IEnumerable.

Search for IQueryable

public static IQueryable<T> Search<T>(this IQueryable<T> set, Search query) where T : class;
public static IQueryable<T> Search<T>(this IQueryable<T> set, Search query, int pageNumber, int pageSize) where T : class;
  • Description: Filters an IQueryable collection based on the provided Search query object.
  • Parameters: Same as above.
  • Returns: Filtered IQueryable.
Type Analysis Methods

InType

public static Type? InType(this Type? valueType, bool isColletion = false);
  • Description: Extracts the underlying type, especially for nullable or generic collection types.

IsNull

public static bool IsNull(this Type? valueType);
  • Description: Determines if the given type is null.

InNull

public static Type? InNull(this Type? valueType);
  • Description: Converts a type to a nullable type if applicable.

IsType

public static Types IsType(this Type type, bool isColletion = false);
  • Description: Maps the type to a specific Types enum value (e.g., String, Number, Boolean, Date, Null).

IsCollection

public static bool IsColletion(this Type type);
  • Description: Determines if the type represents a collection.

Type Checking Methods

public static bool IsBoolean(this Type? valueType, bool isColletion = false);
public static bool IsNumber(this Type? valueType, bool isColletion = false);
public static bool IsDate(this Type? valueType, bool isColletion = false);
public static bool IsString(this Type? valueType, bool isColletion = false);
  • Description: Determines if the type represents a specific data type or a collection.

Search Class

Inheritance Query Class

PropertyTypeDefaultDescription
FormatFormatISODateTimeISO Formats, DateOnly Formats, TimeOnly Formats

Query Class

Inheritance ISearch Interface

PropertyTypeDefaultDescription
OperatorOperatorAndAnd, Or
ConditionsConditionsnew Conditions()Inherit from List with ISearch generic type

Condition Class

Inheritance ISearch Interface

PropertyTypeDefaultDescription
Fieldstring""Entity property name
OperationOperationOperation.EqualAction operation
ValuesValuesnew Values()Inherit from List with object generic type
IncaseCaseCase.DefaultTransform string for Contains, NotContains, StartsWith, NotStartsWith, EndsWith, NotEndsWith operation
FormatFormatISODateTimeISO Formats, DateOnly Formats, TimeOnly Formats

Conditions Class

Inheritance Base is generic List

Namespace: SearchQuery.NewtonsoftJson, SearchQuery.SystemTextJson

Class: Extensions

JSON Search Methods

Convert JSON to Search Query

public static JSearch ToSearch(this string json);
  • Description: Converts a JSON string to a JSearch object.

Search for IEnumerable

public static IEnumerable<T> Search<T>(this IEnumerable<T> set, string query) where T : class;
public static IEnumerable<T> Search<T>(this IEnumerable<T> set, string query, int pageNumber, int pageSize) where T : class;
  • Description: Filters an IEnumerable collection using a JSON query string.

Search for IQueryable

public static IQueryable<T> Search<T>(this IQueryable<T> set, string query) where T : class;
public static IQueryable<T> Search<T>(this IQueryable<T> set, string query, int pageNumber, int pageSize) where T : class;
  • Description: Filters an IQueryable collection using a JSON query string.

Class: JSearch

Attributes

  • JsonConverter
    [System.Text.Json.Serialization.JsonConverter(typeof(SearchConverter))]
    • Description: Indicates that the JSearch class uses a custom JSON converter for serialization and deserialization.

Methods

  1. ToJson

    public string ToJson();
    • Description: Serializes the JSearch object into a JSON string using System.Text.Json.
  2. FromJson

    public static JSearch FromJson(string json);
    • Description: Deserializes a JSON string into a JSearch object.

Constructor

  • JSearch()
    public JSearch() : base()
    • Description: Initializes a new instance of the JSearch class.

Operation Enum

KeyValueStringDateNumberBooleanNull
Equal0By TypeBy TypeBy TypeBy TypeBy Type
NotEqual1By TypeBy TypeBy TypeBy TypeBy Type
LessThan2CompareBy TypeBy TypeCompareIgnore
LessThanOrEqual3CompareBy TypeBy TypeCompareIgnore
GreaterThan4CompareBy TypeBy TypeCompareIgnore
GreaterThanOrEqual5CompareBy TypeBy TypeCompareIgnore
Contains6By TypeAs StringAs StringAs StringIgnore
NotContains7By TypeAs StringAs StringAs StringIgnore
StartsWith8By TypeAs StringAs StringAs StringIgnore
NotStartsWith9By TypeAs StringAs StringAs StringIgnore
EndsWith10By TypeAs StringAs StringAs StringIgnore
NotEndsWith11By TypeAs StringAs StringAs StringIgnore
Between12CompareCompareBy TypeCompareIgnore
NotBetween13CompareCompareBy TypeCompareIgnore

Case Enum

KeyValue
Default0
Lower1
Upper2

Operator Enum

KeyValue
And0
Or1

Types Enum

KeyValue
Null0
String1
Number2
Boolean3
Date4

Format Enum

KeyValueFormat
DateOnly1yyyy-MM-dd
TimeOnly2HH:mm:ss
ISODateTime3yyyy-MM-ddTHH:mm:ss
ISODateTimeWithMilliseconds4yyyy-MM-ddTHH:mm:ss.fff
ISODateTimeWithOffset5yyyy-MM-ddTHH:mm:sszzz
ISODateTimeUTC6yyyy-MM-ddTHH:mm:ssZ
ISODateTimeWithMillisecondsUTC7yyyy-MM-ddTHH:mm:ss.fffZ
DateDay11dd
DateMonth12MM
DateYear13yyyy
TimeOn21HH:mm
TimeHours22HH
TimeMinutes23mm
TimeSeconds24ss
TimeMilliseconds25fff
TimeFull26HH:mm:ss.fff
1.0.0-4

7 months ago

1.0.0-3

7 months ago

1.0.0-2

7 months ago

1.0.0-1

7 months ago

1.0.0

7 months ago