Giter Club home page Giter Club logo

magicodes.dynamicsqlapi's Introduction

Magicodes.DynamicSqlApi

根据SQL自动解析生成动态API。

Build Status

特点

总体说明

TODO

  • 使用模板语法生成API控制器:scriban/scriban
  • API注释
  • 基于数据表配置CURD
  • 多个数据库连接配置
  • 命名空间隔离(由于不支持命名空间定义,使用子类完成)
  • 全局配置

配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<SqlApis Name="Students" Comment="学生(增删改查等处理)">
		<SqlApi Name="GetStudentsByCount" SqlTpl="SELECT TOP (@Count) *
        FROM [dbo].[Students]" Comment="获取前N条学生数据">
			<Input>
				<!--会根据参数名称自动合并字段配置-->
				<Parameter Name="Count" DefaultValue="2" Comment="获取记录数"></Parameter>
			</Input>
		</SqlApi>
		<SqlApi Name="GetStudentById">
			<SqlTpl>
				SELECT TOP 1 * FROM [dbo].[Students]
				WHERE [Id] = @Id
			</SqlTpl>
			<Output>
				<Parameter Name="Name" DefaultValue="2" Comment="名称"></Parameter>
			</Output>
		</SqlApi>
		<SqlApi Name="Insert" HttpRoute='[HttpPost("")]'>
			<SqlTpl>
				INSERT INTO [dbo].[Students]
				([Name],[IdCard],[StudentCode],[Phone],[Nation],[Guardian],[GuardianPhone],[Address])
				VALUES
				(@Name,@IdCard,@StudentCode,@Phone,@Nation,@Guardian,@GuardianPhone,@Address)
			</SqlTpl>
			<Input>
				<Parameter Name="Name" Comment="名称"></Parameter>
			</Input>
		</SqlApi>
		<SqlApi Name="GetStudents" Comment="分页查询">
			<SqlTpl>
				select top (select (@PageSize)) *
				from (select row_number() over(order by id) as rownumber,*
				from [dbo].[Students]) temp_row
				where rownumber>(@SkipCount)
			</SqlTpl>
		</SqlApi>
	</SqlApis>
	<SqlApis Name="Classes">
		<SqlApi Name="GetAll" HttpRoute='[HttpGet("")]'>
			<SqlTpl>
				SELECT TOP (1000) *
				FROM [dbo].[Classes]
			</SqlTpl>
		</SqlApi>
	</SqlApis>
	<SqlApis Name="Iot">
		<SqlApi Name="GetAll" ConnectionString="Server=(localdb)\MSSQLLocalDB;Database=eschool-iot;Trusted_Connection=True;MultipleActiveResultSets=true" HttpRoute='[HttpGet("")]'>
			<SqlTpl>
				SELECT TOP (@PageSize) *
				FROM [dbo].[Devices]
			</SqlTpl>
		</SqlApi>
	</SqlApis>
</configuration>

结果

结果

相关官方Nuget包

名称 Nuget
Magicodes.DynamicSqlApi.Core NuGet
Magicodes.DynamicSqlApi.All NuGet
Magicodes.DynamicSqlApi.CsScript NuGet
Magicodes.DynamicSqlApi.Dapper NuGet
Magicodes.DynamicSqlApi.SqlServer NuGet

VNext

以下内容均已有思路,但是缺乏精力,因此虚席待PR,有兴趣的朋友可以参与进来,多多交流。

见上图:

  • API分组
  • API权限控制
  • API文档以及注释
  • 导入导出
  • HTTP状态码
  • 日志
  • 数据验证
  • 数组支持

开始使用

  1. 引用Nuget包"Magicodes.DynamicSqlApi.All"
名称 说明 Nuget
Magicodes.DynamicSqlApi.All Magicodes.DynamicSqlApi 默认实现 NuGet
  1. 添加配置文件“sqlMapper.xml”

配置文件默认为“sqlMapper.xml”,配置参考下文:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <SqlApis Name="AuditLogs">
    <SqlApi Name="GetAbpAuditLogsList" SqlTpl="SELECT TOP (@Count) *
        FROM [dbo].[AbpAuditLogs]">
      <Input>
        <!--会根据参数名称自动合并字段配置-->
        <Parameter Name="Count" DefaultValue="2"></Parameter>
      </Input>
    </SqlApi>
    <SqlApi Name="GetAbpAuditLogById">
      <SqlTpl>
        SELECT TOP 1 * FROM [dbo].[AbpAuditLogs]
        WHERE [Id] = @Id
      </SqlTpl>
    </SqlApi>
  </SqlApis>
  <SqlApis Name="Users">
    <SqlApi Name="GetAbpUsers">
      <SqlTpl>
        SELECT TOP (1000) *
        FROM [dbo].[AbpUsers]
      </SqlTpl>
    </SqlApi>
  </SqlApis>
</configuration>

如上述配置所示,仅需配置SQL语句即可,参数和结果列表全由Magicodes.DynamicSqlApi自动解析生成。Name是必须的。

  1. 配置ASP.NET Core工程

添加配置:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;
                    //根据环境变量加载不同的JSON配置
                    config.AddJsonFile("appsettings.json", true, true)
                        .AddJsonFile($"appsettings.{env.EnvironmentName}.json",
                            true, true);
                    //从环境变量添加配置
                    config.AddEnvironmentVariables();
                    config.AddXmlFile("sqlMapper.xml", true, false);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

启用DynamicSqlApi:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAllDynamicSqlApi(Configuration["ConnectionStrings:Default"]);
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseDynamicSqlApi();
        }

联系我们

订阅号

关注“麦扣聊技术”订阅号可以获得最新文章、教程、文档:

QQ群

  • 编程交流群<85318032>

  • 产品交流群<897857351>

文档官网&官方博客

其他开源库

magicodes.dynamicsqlapi's People

Contributors

codelove1314 avatar xl-wenqiang avatar

Stargazers

Jamal Jameel avatar weicong avatar dusdong avatar 老三的古代 avatar  avatar maskx avatar 于斯人也 avatar A Bruce avatar DY avatar Fuq188 avatar  avatar Kamal Alseisy avatar  avatar idkook avatar  avatar eric fong avatar  avatar  avatar ConanYao avatar IGeekFan avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar 李文强 avatar Regina-pxk avatar  avatar  avatar

magicodes.dynamicsqlapi's Issues

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.