GithubHelp home page GithubHelp logo

elfogre / lhsk Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 84 KB

LHS brackets parser for kotlin. Includes utility to generate sql queries via exposed framework.

License: GNU General Public License v3.0

Kotlin 100.00%

lhsk's Introduction

LHSK (LHS brackets parser and utilities for Kotlin and Exposed)

License Maven Central

Table of Contents

Introduction

LHS brackets is a flexible and powerful tool to add filters and sorts on REST requests as query params. Example:

/api/search/data?property_type[eq]=PARKING&deposit_amount[lte]=604.75&sort[0]=auction_value:asc    

This library provides utilities to parse LHS brackets syntax, generate it and also to easily apply them to database queries via Exposed library.

Features

  • Easy integration with Kotlin projects.
  • Comprehensive API for database interactions using Exposed.
  • Support for join queries in Exposed.
  • Supports Java SDK version 11 and Kotlin API version 1.9.

Installation

Gradle

Add the following dependencies to your build.gradle.kts file:

dependencies {
    implementation("io.github.elfogre:lhs-parser:1.0.1")
    implementation("io.github.elfogre:lhs-exposed:1.0.1")
}

Usage

Example 1: Basic Parser Usage (usin Ktor request params)

import io.github.elfogre.LHSBracketsParser

fun main() {
    val searchParams = LHSBracketsParser.parseSearchQueryParams(call.request.queryParameters.flattenEntries())
        ...
}

Example 2: Database Interaction with Exposed

Use exact table column names as property types on LHS.

import io.github.elfogre.LHSBracketsExposedExtension
import org.jetbrains.exposed.sql.transactions.transaction

fun main() {
    ...
    transaction {
        CarTable.
        selectAll().
        addWhereExpressionFromLHSFilter(parsedParams.filters).
        addOrderByFromLHSSort(parsedParams.sorts).
        map { Car.fromResultRow(it) }
    }
}

Filter and sort operations accepts a list of fields to exclude them from sorting and filtering.

Why Use Excluded Fields?

  • Prevent Performance Issues: Filtering by non-indexed fields can severely impact database performance.
  • Protect Internal Logic Fields: Avoid exposing and filtering by fields that are used for internal logic and should not be accessible externally.

Check for more examples in integration tests.

Example 3: LHS brackets syntax generation

import io.github.elfogre.LHSBracketsParser

fun main() {
    val queryParams = LHSBracketsParser.searchParamsToLHSBracketsQueryParams(searchParams)
        ...
}

Supported operators

  • eq: Equal to
  • neq: Not equal to
  • gt: Greater than
  • gte: Greater than or equal to
  • lt: Less than
  • lte: Less than or equal to
  • in: In a specified list of values (Use a comma separated list as value)
  • notin: Not in a specified list of values (Use a comma separated list as value)

Valid SQL types

Supported exposed column types for automatic casting in lhs-exposed are:

  • StringColumnType
  • LongColumnType
  • CustomEnumerationColumnType
  • IntegerColumnType
  • DecimalColumnType

Contributing

We welcome contributions! Please add issues or submit a pull request.

License

This project is licensed under the GNU GPL License - see the LICENSE file for details.

Acknowledgments

  • Thanks to the Exposed and Kotlin teams for their invaluable libraries.

lhsk's People

Contributors

elfogre avatar

Stargazers

Adrián Belmonte avatar  avatar Julio Muñoz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.