Skip to content

Commit

Permalink
Added a convenience method for finding the table name for a document …
Browse files Browse the repository at this point in the history
…type in Marten
  • Loading branch information
jeremydmiller committed Apr 10, 2024
1 parent a46f1c9 commit 4e1f28c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CoreTests/DocumentSchemaResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void ValidateRegisteredDocumentNames()

var schema = theSession.DocumentStore.Options.Schema;

schema.For<Account>().ShouldBe("documentschemaresolvertests.mt_doc_account");
schema.For(typeof(Account)).ShouldBe("documentschemaresolvertests.mt_doc_account");
schema.For<Company>().ShouldBe("documentschemaresolvertests.mt_doc_company");
schema.For<User>().ShouldBe("documentschemaresolvertests.mt_doc_user");

Expand Down
13 changes: 13 additions & 0 deletions src/Marten/IDocumentSchemaResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable enable
using System;

namespace Marten;

public interface IDocumentSchemaResolver
Expand All @@ -25,6 +27,17 @@ public interface IDocumentSchemaResolver
/// <returns>The name of <typeparamref name="TDocument"/> in the database.</returns>
string For<TDocument>(bool qualified = true);

/// <summary>
/// Find the database name of the table backing <typeparamref name="TDocument"/>. Supports documents and projections.
/// </summary>
/// <param name="documentType">The document type</param>
/// <param name="qualified" default="true">
/// When true (default) the qualified name is returned (schema and table name).
/// Otherwise only the table name is returned.
/// </param>
/// <returns>The name of <typeparamref name="TDocument"/> in the database.</returns>
string For(Type documentType, bool qualified = true);

/// <summary>
/// Find the database name of the table backing the events table. Supports documents and projections.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Marten/StoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,12 @@ string IDocumentSchemaResolver.For<TDocument>(bool qualified)
return qualified ? docType.TableName.QualifiedName : docType.TableName.Name;
}

public string For(Type documentType, bool qualified = true)
{
var docType = ((IReadOnlyStoreOptions)this).FindOrResolveDocumentType(documentType);
return qualified ? docType.TableName.QualifiedName : docType.TableName.Name;
}

string IDocumentSchemaResolver.ForEvents(bool qualified)
{
return qualified ? _eventGraph.Table.QualifiedName : _eventGraph.Table.Name;
Expand Down

0 comments on commit 4e1f28c

Please sign in to comment.