Skip to content

eksotama/FluentDbTools

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FluentDbTools

Build Status MIT license

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.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

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
    .AddScoped<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": "admin",
    "adminPassword": "admin",
    "schema": "dbuser", 
    "databaseName": "dbuser", 
    "hostname": "localhost",
    "port": 5433,
    "pooling": true,
    "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()
    .AddScoped<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")]
public class AddPersonTable : MigrationModel
{
    public override void Up()
    {
        Create.Table(Table.Person)
            .InSchema(SchemaName)
            .WithColumn(Column.Id).AsGuid().PrimaryKey()
            .WithColumn(Column.SequenceNumber).AsInt32().NotNullable()
            .WithColumn(Column.Username).AsString()
            .WithColumn(Column.Password).AsString()
            .WithTableSequence(this);
    }

    public override void Down()
    {
        Delete.Table(Table.Person).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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.2%
  • Other 0.8%