Giter VIP home page Giter VIP logo

fluentdbtools's Introduction

FluentDbTools

Build Status MIT license API documentation

FluentDbTools provides a fluent SQL abstraction layer for creating database connections, building sql queries and migrating your database.

Following databases are currently supported:

  • Oracle
  • Postgres

Nugets

  • FluentDbTools.Abstractions: NuGet version
  • FluentDbTools.Common.Abstractions: NuGet version
  • FluentDbTools.Migration.Abstractions: NuGet version
  • FluentDbTools.SqlBuilder.Abstractions: NuGet version
  • FluentDbTools.Contracts: NuGet version
  • FluentDbTools.Migration.Contracts: NuGet version
  • FluentDbTools.Extensions.DbProvider: NuGet version
  • FluentDbTools.Extensions.SqlBuilder: NuGet version
  • FluentDbTools.Extensions.Migration: NuGet version
  • FluentDbTools.Extension.MSDependencyInjection: NuGet version
  • FluentDbTools.Extension.MSDependencyInjection.Oracle: NuGet version
  • FluentDbTools.Extension.MSDependencyInjection.Postgres: NuGet version
  • FluentDbTools.DbProviders: NuGet version
  • FluentDbTools.Migration: NuGet version
  • FluentDbTools.Migration.Common: NuGet version
  • FluentDbTools.Migration.Oracle: NuGet version
  • FluentDbTools.Migration.Postgres: NuGet version
  • FluentDbTools.SqlBuilder: NuGet version
  • FluentDbTools.SqlBuilder.Dapper: NuGet version

Example

The IDbConfig interface is the only member you need to instantiate. It provides a simple interface for providing which database type to use, and other necessary database settings.

Create a Database Connection

IDbConfig dbConfig = new DbConfig(..) // Implementantion of IDbConfig requested
OracleClientFactory.Instance.Register(SupportedDatabaseTypes.Oracle); // Register the database factories you see fit
NpgsqlFactory.Instance.Register(SupportedDatabaseTypes.Postgres);
var dbConnection = dbConfig.CreateDbConnection();

Register Database Providers with MSDependencyInjection

var serviceCollection = new ServiceCollection();       
var serviceProvider = serviceCollection
    .AddSingleton<IConfiguration>(serviceProvider => new ConfigurationBuilder()
        .AddJsonFile("config.json"))
    .AddOracleDbProvider()
    .AddPostgresDbProvider()
    .BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
    var dbFactory = scope.ServiceProvider.GetService<DbProviderFactory>();
    var dbConnection = scope.ServiceProvider.GetService<IDbConnection>();
}

Typical Json Configuration with MSDependencyInjection

"database": {
    "type": "postgres",
    "user": "dbuser",
    "password": "dbpassword",
    "adminUser": "admindbuser",
    "adminPassword": "admindbuser",
    "schema": "dbuser", 
    "databaseName": "dbuser", 
    "hostname": "localhost",
    "port": 5432,
    "pooling": true,
	// If you want all tables should be prefixed with EX 
	// i.e. Person will give tablename EXPerson in the database
	"schemaPrefix": {
        "Id": "EX", 
		"UniqueId": "excv", 
		"tables": {
			// ChangeLogContext for table "Person"
			"person": {
				"GlobalId": "abcd",
				"ShortName": "EXTST"
			}
		}
    }
    "migration": {
        "schemaPassword": "dbpassword" 
    }
}

// If database:schema not set, then it equals to 'database:user'
// If database:databaseName not set, then it equals to 'database:schema'
// If database:schemaPassword not set, then it equals to 'database:password'

Build SQL Query Fluently

IDbConfig dbConfig = DbConfigDatabaseTargets.Create(databaseTypes, schema);
var sql = dbConfig.CreateSqlBuilder().Select()
            .OnSchema()
            .Fields<Person>(x => x.F(item => item.Id))
            .Fields<Person>(x => x.F(item => item.SequenceNumber))
            .Fields<Person>(x => x.F(item => item.Username))
            .Fields<Person>(x => x.F(item => item.Password))
            .Where<Person>(x => x.WP(item => item.Id))
            .Build();

Register Migration With MSDependencyInjection & Migrate the Database With Extended FluentMigrator

IEnumerable<Assembly> migrationAssemblies => new[] { typeof(AddPersonTable).Assembly };
var serviceProvider = new ServiceCollection()
    .AddSingleton<IConfiguration>(serviceProvider => new ConfigurationBuilder()
        .AddJsonFile("config.json"))
    .ConfigureWithMigrationAndScanForVersionTable(migrationAssemblies)
    .BuildServiceProvider();

using (var scope = provider.CreateScope())
{
    var migrationRunner = scope.ServiceProvider.GetService<IMigrationRunner>();

    migrationRunner.MigrateUp();
}
[Migration(1, "Migration Example With SchemaPrefix")]
public class AddPersonTable : MigrationModel
{
    public override void Up()
    {
        // Create the ChangeLogContext with values from this.GetMigrationConfig()
        // -
        // SchemaPrefixId is fetched from database:migration:schemaPrefix:id or database:schemaPrefix:id<br/>
        // SchemaPrefixUniqueId is fetched from database:migration:schemaPrefix:UniqueId or database:schemaPrefix:UniqueId<br/>
        // -
        // GlobalId is fetched from database:migration:schemaPrefix:tables:person:GlobalId or database:schemaPrefix:tables:person:GlobalId<br/>
        // ShortName is fetched from database:migration:schemaPrefix:tables:person:ShortName or database:schemaPrefix:tables:person:ShortName<br/>
        var personChangeLogContext = new ChangeLogContext(this, Table.Person);

        // When "database:schemaPrefix:Id" or "database:migration:schemaPrefix:Id" have a value,
        // the tableName Person will be created as {SchemaPrefixId}Person.
		//
        // i.e: "database:schemaPrefix:Id" = "EX" => a table with name EXPerson will be created.
        // If both "database:schemaPrefix:Id" and "database:migration:schemaPrefix:Id" is missing,
        // a table with name Person will be created.
        Create.Table(Table.Person.GetPrefixedName(SchemaPrefixId)).InSchema(personChangeLogContext)
            .WithChangeLog(PersonLogContext) // ChangeLog activation for Create.Table(..)
            .WithColumn(Column.Id).AsGuid().PrimaryKey()
            .WithColumn(Column.SequenceNumber).AsInt32().NotNullable()
            .WithColumn(Column.Username).AsString()
            .WithColumn(Column.Password).AsString()
            .WithDefaultColumns() // Enable DefaultColumns functionality
            .WithTableSequence(this);
    }

    public override void Down()
    {
        var personChangeLogContext = new ChangeLogContext(this, Table.Person);
        Delete.Table(GetPrefixedName(Table.Person))
            .WithChangeLog(personChangeLogContext,this)
            .InSchema(SchemaName);

    }
}

More Examples

Please have a look in the example folder:

Get Started

  1. Install Docker
  2. Install Python and pip
  3. Install python dependencies:
    • pip install DockerBuildManagement
  4. See available commands:
    • dbm -help

Build & Run

  1. Start domain development by deploying service dependencies:
    • dbm -swarm -start
  2. Test solution in containers:
    • dbm -test
  3. Open solution and continue development:
  4. Publish new nuget version:
    • Bump version in CHANGELOG.md
    • Build and publish nugets: dbm -build -run -publish
  5. Stop development when you feel like it:
    • dbm -swarm -stop

Buildsystem

fluentdbtools's People

Contributors

aho-dips avatar hansehe avatar jaalu avatar kyrrem avatar traviscibot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fluentdbtools's Issues

CreateAliasForType does not account for SQL keywords

Attempting to construct a query for all fields of an object AggressiveStork

var sql = SqlBuilder
                .Select()
                .Fields<AggressiveStork>(x => x.All())
                .OnSchema(DbConfig.Schema)
                .Build();

leads to SqlBuilder constructing the query

SELECT as.* FROM AggressiveStork as

In Postgres, the final as is interpreted as the SQL keyword AS, causing a syntax error.

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.