Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
feat(db): add vector db
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed May 12, 2024
1 parent 7c114ad commit e22b2d4
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageVersion Include="EFCore.NamingConventions" Version="8.0.3" />
<PackageVersion Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="8.1.2" />
<!-- Vector -->
<PackageVersion Include="Pgvector" Version="0.2.0" />
<PackageVersion Include="Pgvector.EntityFrameworkCore" Version="0.2.0" />
<!-- Semantic Kernel -->
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.11.1" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.OpenAI" Version="1.11.1" />
<!-- OpenTelemetry -->
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ x-default-logging: &logging

services:
rookie.db:
image: postgres:16.2-alpine3.19
image: ankane/pgvector:v0.5.1
container_name: rookie.db
restart: unless-stopped
ports:
Expand Down
5 changes: 5 additions & 0 deletions src/RookieShop.Domain/RookieShop.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<PackageReference Include="StronglyTypedId" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Pgvector" />
<PackageReference Include="Pgvector.EntityFrameworkCore" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Ardalis.SmartEnum" />
<PackageReference Include="Ardalis.GuardClauses" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<PackageReference Include="FluentEmail.Mailtrap" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.OpenAI" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.Redis" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" />
Expand Down
1 change: 1 addition & 0 deletions src/RookieShop.Persistence/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.ConfigureSmartEnum();
modelBuilder.HasPostgresExtension(UniqueType.Extension);
modelBuilder.HasPostgresExtension(VectorType.Extension);
modelBuilder.ApplyConfigurationsFromAssembly(AssemblyReference.DbContextAssembly);
}
}
7 changes: 7 additions & 0 deletions src/RookieShop.Persistence/Constants/VectorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RookieShop.Persistence.Constants;

public static class VectorType
{
public const string Extension = "vector";
public const string DataType = "vector(384)";
}
11 changes: 8 additions & 3 deletions src/RookieShop.Persistence/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Npgsql;
using RookieShop.Domain.SharedKernel;
using RookieShop.Persistence.Interceptors;

Expand All @@ -24,11 +25,15 @@ public static IHostApplicationBuilder AddPersistence(this IHostApplicationBuilde

builder.Services.AddDbContextPool<ApplicationDbContext>((sp, options) =>
{
options.UseNpgsql(connectionString, sqlOptions =>
var npgsqlDataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
npgsqlDataSourceBuilder.UseVector();
options.UseNpgsql(npgsqlDataSourceBuilder.ConnectionString, sqlOptions =>
{
sqlOptions.UseVector();
sqlOptions.MigrationsAssembly(AssemblyReference.DbContextAssembly.FullName);
sqlOptions.EnableRetryOnFailure(15, TimeSpan.FromSeconds(30),
null);
sqlOptions.EnableRetryOnFailure(15, TimeSpan.FromSeconds(30), null);
})
.UseExceptionProcessor()
.EnableServiceProviderCaching()
Expand Down
4 changes: 0 additions & 4 deletions src/RookieShop.Persistence/RookieShop.Persistence.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,4 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Constants\" />
</ItemGroup>

</Project>

0 comments on commit e22b2d4

Please sign in to comment.