mirror of
https://github.com/bitwarden/server.git
synced 2025-05-28 23:04:50 -05:00
set up postgresql repository
This commit is contained in:
parent
8596ba2caa
commit
84800da1fb
@ -24,6 +24,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.3" />
|
||||
<PackageReference Include="Npgsql" Version="4.0.4" />
|
||||
<PackageReference Include="Quartz" Version="3.0.7" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />
|
||||
|
@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using Dapper;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
namespace Bit.Core.Repositories
|
||||
{
|
||||
public abstract class BaseRepository
|
||||
{
|
28
src/Core/Repositories/PostgreSql/BasePostgreSqlRepository.cs
Normal file
28
src/Core/Repositories/PostgreSql/BasePostgreSqlRepository.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Dapper;
|
||||
|
||||
namespace Bit.Core.Repositories.PostgreSql
|
||||
{
|
||||
public abstract class BasePostgreSqlRepository : BaseRepository
|
||||
{
|
||||
static BasePostgreSqlRepository()
|
||||
{
|
||||
// Support snake case property names
|
||||
DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||||
}
|
||||
|
||||
public BasePostgreSqlRepository(string connectionString, string readOnlyConnectionString)
|
||||
: base(connectionString, readOnlyConnectionString)
|
||||
{ }
|
||||
|
||||
protected static string SnakeCase(string input)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
return input;
|
||||
}
|
||||
var startUnderscores = Regex.Match(input, @"^_+");
|
||||
return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
}
|
89
src/Core/Repositories/PostgreSql/Repository.cs
Normal file
89
src/Core/Repositories/PostgreSql/Repository.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using Bit.Core.Models.Table;
|
||||
using Npgsql;
|
||||
|
||||
namespace Bit.Core.Repositories.PostgreSql
|
||||
{
|
||||
public abstract class Repository<T, TId> : BasePostgreSqlRepository, IRepository<T, TId>
|
||||
where TId : IEquatable<TId>
|
||||
where T : class, ITableObject<TId>
|
||||
{
|
||||
public Repository(string connectionString, string readOnlyConnectionString, string table)
|
||||
: base(connectionString, readOnlyConnectionString)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(table))
|
||||
{
|
||||
Table = table;
|
||||
}
|
||||
else
|
||||
{
|
||||
Table = SnakeCase(typeof(T).Name);
|
||||
}
|
||||
}
|
||||
|
||||
protected string Table { get; private set; }
|
||||
|
||||
public virtual async Task<T> GetByIdAsync(TId id)
|
||||
{
|
||||
using(var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<T>(
|
||||
$"{Table}_read_by_id",
|
||||
new { id = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.SingleOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task CreateAsync(T obj)
|
||||
{
|
||||
obj.SetNewId();
|
||||
using(var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
$"{Table}_create",
|
||||
obj,
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task ReplaceAsync(T obj)
|
||||
{
|
||||
using(var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
$"{Table}_update",
|
||||
obj,
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task UpsertAsync(T obj)
|
||||
{
|
||||
if(obj.Id.Equals(default(TId)))
|
||||
{
|
||||
await CreateAsync(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplaceAsync(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task DeleteAsync(T obj)
|
||||
{
|
||||
using(var connection = new NpgsqlConnection(ConnectionString))
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
$"{Table}_delete_by_id",
|
||||
new { id = obj.Id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
src/Sql/PostgreSQL/Functions/user_read_by_id.sql
Normal file
14
src/Sql/PostgreSQL/Functions/user_read_by_id.sql
Normal file
@ -0,0 +1,14 @@
|
||||
CREATE OR REPLACE FUNCTION user_read_by_id
|
||||
(
|
||||
id uuid
|
||||
)
|
||||
RETURNS SETOF "user"
|
||||
LANGUAGE 'sql'
|
||||
AS $BODY$
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
"user"
|
||||
WHERE
|
||||
"id" = id;
|
||||
$BODY$;
|
Loading…
x
Reference in New Issue
Block a user