Code Monkey home page Code Monkey logo

rest-api-from-sql's Introduction

REST API Generator

This is a simple, unambitious code generation tool. It looks at the schema of a SQL database and generates a REST API for it, with typical CRUD endpoints that support both filtering on primary key and (optionally) on unique key, as additional endpoints.

See the samples folder for an example of how to call the executable and the output of a successful invocation.

Example PowerShell invocation:

.\apigen.exe `
-c "Server=.,1401;Initial Catalog=AdventureWorks;Persist Security Info=False;User ID=username;Password=password;" `
-s "AdventureWorksApi" `
-o "c:\temp\generated code\AdventureWorks" `
-p "AdventureWorksApi" 

The apigen.exe executable can be found in the releases page, or compiled using dotnet build. The executable will then be in the rest-api-from-sql\src\RestApiFromSqlSchema.Console\bin\Debug\net5.0 folder.

Quick summary of the generated API:

  • ASP.NET 5.0 (C#)
  • Swagger support
  • Generates one class per table
  • Generates an EntityFramework DbContext with support for both primary and composite keys
  • Generates an API controller for each entity (typical CRUD, i.e.. paginated index, get by id/unique key, create, edit, delete)
  • Maps schema/table name to namespace/type name, e.g. HumanResources.Shift becomes namespace HumanResources { public class Shift { ... } }
  • Always includes both schema and table name when naming or referencing things (to avoid ambiguity errors)
  • Modifies class/property names as necessary for syntax/conflict purposes (annotates using data annotations to keep the ORM happy)

Limitations:

  • Does not support tables without primary keys
  • Does not support generating navigation properties
  • Does not support the SQL hierarchyid system data type (currently maps to string, but untested for create/edit)
  • No tests

Planned:

  • Generate FluentValidation validators and plug them into the MVC request pipeline with a filter to map ModelState validation errors to an error schema
  • Avoid IDENTITY_INSERT errors on write (e.g. separate object with the IDENTITY column removed on generated POST endpoints)

Maybes:

  • Support generating an alternative API - CQRS/Mediator/ProjectTo
  • Implement support for other database engines
  • Desktop UI for flexibility in what to generate and configuration
  • Use Roslyn instead of string templates
  • Nicer looking generated code
  • More than just REST (probably not graphql, but maybe grpc - code first? grpc first? both?)

rest-api-from-sql's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar kane-armstrong 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.