Coder Social home page Coder Social logo

juantarquino-ssb / dynamicodatatosql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dynamicodatatosql/dynamicodatatosql

0.0 0.0 0.0 69 KB

Dotnet NuGet package to convert OData query to SQL query when the data model is dynamic and hence entity framework or any other ORM with IQuerable support cannot be used. In a multi-tenant enterprise or Saas applications, the data model is usually not fixed (dynamic).

License: MIT License

C# 100.00%

dynamicodatatosql's Introduction

DynamicODataToSQL

Dotnet NuGet package to convert OData query to SQL query when the data model is dynamic and hence entity framework or any other ORM with IQuerable support cannot be used. In a multi-tenant enterprise or Saas applications, the data model is usually not fixed (dynamic).

License GitHub Actions Status GitHub release (latest SemVer) Nuget

Table of Contents

Example Scenario

Let's consider you are building a Saas application for project and issue tracking, similar to Jira. Development teams and organizations track issues differently, to personalize and measure their agile process.
Your Saas application provides this by allowing tenants to add properties(columns) to existing object types(tables) or create completely new object types, it further allows querying new object types and filtering using new properties through an OData service.

Contoso Inc, one of your tenant

  • Adds a boolean property Internal to Issue object. It is used to track internal vs customer reported issues.
  • Adds another object type called Customer to track which customer reported the issue or was affected by it. Customer object contains standard properties like Name, Email etc.

It is not trivial to expose a multi-tenant OData service in such a scenario using Entity Framework since DB schema/EF's DBContext can be different for each tenant and can be modified on the fly.

GET https://api.trackerOne.com/contoso/odata/Issues?$filter=Internal eq true
GET https://api.trackerOne.com/contoso/odata/Customers?$filter=contains(Email,'outlook.com')

This project aims to solve this issue by providing a simple API to convert an OData query to an SQL query when the data model is dynamic.

Getting Started

  • Install Nuget Package
    Install-Package DynamicODataToSQL
var converter = new ODataToSqlConverter(new EdmModelBuilder(), new SqlServerCompiler() { UseLegacyPagination = false });
var tableName = "Customers"; 
var odataQueryParams = new Dictionary<string, string>
                {
                    {"select", "Name, Email" },
                    {"filter", "contains(Email,'outlook.com')" },
                    {"orderby", "Name" },
                    {"top", "20" },
                    {"skip", "5" },
                };
 var result = converter.ConvertToSQL(
                tableName,
                odataQueryParams,
                false);

string sql = result.Item1;
// SELECT [Name], [Email] FROM [Customers] WHERE [Email] like @p0 ORDER BY [Name] ASC OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY

IDictionary<string, object> sqlParams = result.Item2; 
// {"@p0", "%outlook.com%"},{"@p1", 5}, {"@p2", 20}

See Unit tests for more examples

Example OData Service

See DynamicODataSampleService for and example OData service.

  1. Download AdventureWorks2019 Sample Database

  2. Restore AdventureWorks2019 database.

  3. Setup database user and permissions

    CREATE LOGIN odata_service WITH PASSWORD = 'Password123';   
    use AdventureWorks2019
    CREATE USER odata_service FOR LOGIN odata_service;
    GRANT SELECT ON DATABASE::AdventureWorks2019 TO odata_service;
    GO
  4. Run dotnet run --project .\Samples\DynamicODataSampleService\DynamicODataSampleService.csproj

  5. Use Powershell to query the service, Top 10 Persons by ModifiedDate

    Invoke-RestMethod 'https://localhost:5001/tables/Person.Person?orderby=ModifiedDate desc&skip=0&top=10&select=FirstName,LastName,ModifiedDate' | ConvertTo-Json

    Products with StockLevel less than 100

    Invoke-RestMethod 'https://localhost:5001/tables/Production.Product?filter=SafetyStockLevel lt 100' | ConvertTo-Json

Features

  • Supports basic OData syntax for select, filter, skip, top, orderby and now apply
  • Currently does NOT support expand and lambda operators.

filter support

apply support (aggregations)

Aggregations using Odata apply query option is supported. Spec: http://docs.oasis-open.org/odata/odata-data-aggregation-ext/v4.0/odata-data-aggregation-ext-v4.0.html

  • Transformations: filter, groupby and aggregate are supported. expand, concat, search, top, bottom are NOT supported.
  • sum, min, max, avg, countdistinct and count are supported.

Example

\orders?$apply=groupby((Country),aggregate(Amount with sum as Total,Amount with average as AvgAmt))

is converted to

SELECT [Country], Sum(Amount) AS Total, Avg(Amount) AS AvgAmt FROM [Orders] GROUP BY [Country]

For more advanced aggreagate scenarios supported, see unit tests.

Roadmap

  • [] Support for validating column names and column data types.

Contributing

We are always looking for people to contribute! To find out how to help out, have a look at our Contributing Guide.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copyright

Copyright MIT © 2020 Vaibhav Goyal. See LICENSE for details.

dynamicodatatosql's People

Contributors

vaibhav-goyal avatar goyalvaibhav 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.