Skip to content

Commit

Permalink
PII removal in the event sourcing. Closes GH-3409
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Oct 24, 2024
1 parent e49aa70 commit a9019e8
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 34 deletions.
6 changes: 5 additions & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ const config: UserConfig<DefaultTheme.Config> = {
{
text: 'Multitenancy',
link: '/events/multitenancy'
},
},
{
text: 'Removing Protected Information',
link: '/events/protection'
},
{
text: 'Aggregates, events and repositories',
link: '/scenarios/aggregates-events-repositories'
Expand Down
6 changes: 3 additions & 3 deletions docs/configuration/hostbuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ services.AddMarten()
.UseLightweightSessions()
.UseNpgsqlDataSource();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L292-L300' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_usenpgsqldatasource' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L299-L307' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_usenpgsqldatasource' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

If you're on .NET 8 (and above), you can also use a dedicated [keyed registration](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#keyed-di-services). This can be useful for scenarios where you need more than one data source registered:
Expand All @@ -145,7 +145,7 @@ services.AddMarten()
.UseLightweightSessions()
.UseNpgsqlDataSource(dataSourceKey);
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L343-L353' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_usenpgsqldatasource_keyed' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L350-L360' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_usenpgsqldatasource_keyed' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Using a Multi-Host Data Source <Badge type="tip" text="7.11" />
Expand All @@ -167,7 +167,7 @@ services.AddMarten(x =>
.UseLightweightSessions()
.UseNpgsqlDataSource();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L314-L326' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_usenpgsqldatasourcemultihost' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L321-L333' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_usenpgsqldatasourcemultihost' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

::: warning
Expand Down
2 changes: 1 addition & 1 deletion docs/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,5 +605,5 @@ Marten provides a helper method to fetch the PostgreSQL server version exposed v
```cs
var pgVersion = theStore.Diagnostics.GetPostgresVersion();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/Diagnostics/ability_to_fetch_postgres_server_version.cs#L11-L13' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_get_postgres_version' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/Diagnostics/ability_to_fetch_postgres_server_version.cs#L12-L14' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_get_postgres_version' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion docs/documents/indexing/foreign-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,5 @@ public class Issue
public Guid? OtherUserId { get; set; }
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/ForeignKeys/configuring_foreign_key_fields.cs#L68-L84' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_issue-with-fk-attribute' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/ForeignKeys/configuring_foreign_key_fields.cs#L69-L85' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_issue-with-fk-attribute' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion docs/documents/partial-updates-patching.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public async Task rename_deep_prop()
{
var target2 = query.Load<Target>(target.Id);
target2.Inner.AnotherString.ShouldBe("Foo");
SpecificationExtensions.ShouldBeNull(target2.Inner.String);
target2.Inner.String.ShouldBeNull();
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/documents/plv8.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public async Task rename_deep_prop()
{
var target2 = query.Load<Target>(target.Id);
target2.Inner.AnotherString.ShouldBe("Foo");
SpecificationExtensions.ShouldBeNull(target2.Inner.String);
target2.Inner.String.ShouldBeNull();
}
}
```
Expand Down
26 changes: 13 additions & 13 deletions docs/documents/querying/compiled-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public class UsersByFirstName: ICompiledListQuery<User>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L591-L604' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_usersbyfirstname-query' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L601-L614' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_usersbyfirstname-query' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

If you do want to use a `Select()` transform, use this interface:
Expand Down Expand Up @@ -230,7 +230,7 @@ public class UserNamesForFirstName: ICompiledListQuery<User, string>
public string FirstName { get; set; }
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L616-L630' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_usernamesforfirstname' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L626-L640' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_usernamesforfirstname' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Querying for Related Documents with Include()
Expand All @@ -255,10 +255,10 @@ public async Task simple_compiled_include_for_a_single_document()
var issueQuery = new IssueByTitleWithAssignee { Title = issue.Title };
var issue2 = query.Query(issueQuery);

SpecificationExtensions.ShouldNotBeNull(issueQuery.Included);
issueQuery.Included.ShouldNotBeNull();
issueQuery.Included.Single().Id.ShouldBe(user.Id);

SpecificationExtensions.ShouldNotBeNull(issue2);
issue2.ShouldNotBeNull();
}

public class IssueByTitleWithAssignee: ICompiledQuery<Issue>
Expand Down Expand Up @@ -455,7 +455,7 @@ public class FindUserByAllTheThings: ICompiledQuery<User>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L375-L392' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_finduserbyallthethings' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L385-L402' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_finduserbyallthethings' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Querying for Multiple Results as JSON
Expand All @@ -477,7 +477,7 @@ public class FindJsonOrderedUsersByUsername: ICompiledListQuery<User>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L409-L423' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledtojsonarray' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L419-L433' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledtojsonarray' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

If you wish to do it asynchronously, you can use the `ToJsonArrayAsync()` method.
Expand All @@ -499,7 +499,7 @@ public class FindJsonOrderedUsersByUsername: ICompiledListQuery<User>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L409-L423' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledtojsonarray' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L419-L433' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledtojsonarray' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Note that the result has the documents comma separated and wrapped in angle brackets (as per the Json notation).
Expand All @@ -522,7 +522,7 @@ public class FindJsonUserByUsername: ICompiledQuery<User>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L394-L407' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledasjson' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L404-L417' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledasjson' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

And an example:
Expand All @@ -541,7 +541,7 @@ public class FindJsonUserByUsername: ICompiledQuery<User>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L394-L407' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledasjson' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L404-L417' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_compiledasjson' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

(our `ToJson()` method simply returns a string representation of the `User` instance in Json notation)
Expand Down Expand Up @@ -572,7 +572,7 @@ public class TargetsInOrder: ICompiledListQuery<Target>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L527-L545' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_targetsinorder' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Compiled/compiled_queries.cs#L537-L555' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_targetsinorder' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

And when used in the actual test:
Expand Down Expand Up @@ -666,7 +666,7 @@ public class LonghandColorTargets: IQueryPlan<IReadOnlyList<Target>>, IBatchQuer
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_plans.cs#L78-L128' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_color_targets' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_plans.cs#L74-L124' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_color_targets' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

And then use that like so:
Expand All @@ -680,7 +680,7 @@ public static async Task use_query_plan(IQuerySession session, CancellationToken
.QueryByPlanAsync(new ColorTargets(Colors.Blue), token);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_plans.cs#L132-L140' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_query_plan' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_plans.cs#L128-L136' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_query_plan' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

There is also a similar interface for usage with [batch querying](/documents/querying/batched-queries):
Expand Down Expand Up @@ -741,5 +741,5 @@ public async Task use_as_batch()
greens.Select(x => x.Id).ShouldBe(expectedReds.Select(x => x.Id));
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_plans.cs#L38-L75' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_query_plan_in_batch_query' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_plans.cs#L34-L71' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_query_plan_in_batch_query' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
8 changes: 4 additions & 4 deletions docs/documents/querying/linq/child-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var results = theSession
.Where(x => x.Children.Any(_ => _.Number == 6 && _.Double == -1))
.ToArray();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L131-L138' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_any-query-through-child-collection-with-and' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L132-L139' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_any-query-through-child-collection-with-and' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Finally, you can query for child collections that do **not** contain a value:
Expand Down Expand Up @@ -112,7 +112,7 @@ public async Task query_against_string_array()
.Select(x => x.Id).ShouldHaveTheSameElementsAs(doc1.Id, doc2.Id);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L473-L491' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_against_string_array' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L474-L492' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_against_string_array' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Marten also allows you to query over IEnumerables using the Any method for equality (similar to Contains):
Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task query_against_number_list_with_any()
.Count(x => x.Numbers.Any()).ShouldBe(3);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L589-L613' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_any_string_array' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L590-L614' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_any_string_array' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

As of 1.2, you can also query against the `Count()` or `Length` of a child collection with the normal comparison
Expand Down Expand Up @@ -170,7 +170,7 @@ public async Task query_against_number_list_with_count_method()
.Single(x => x.Numbers.Count() == 4).Id.ShouldBe(doc3.Id);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L615-L636' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_against_number_list_with_count_method' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/ChildCollections/query_against_child_collections.cs#L616-L637' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_against_number_list_with_count_method' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## IsOneOf
Expand Down
2 changes: 1 addition & 1 deletion docs/documents/querying/linq/paging.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ If you want to create you own paged queries, just use the `Take()` and `Skip()`
public void can_get_the_total_in_results()
{
var count = theSession.Query<Target>().Count(x => x.Number > 10);
SpecificationExtensions.ShouldBeGreaterThan(count, 0);
count.ShouldBeGreaterThan(0);

// We're going to use stats as an output
// parameter to the call below, so we
Expand Down
4 changes: 2 additions & 2 deletions docs/documents/querying/linq/projections.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public async Task use_select_with_multiple_fields_to_other_type()

users.Each(x =>
{
SpecificationExtensions.ShouldNotBeNull(x.First);
SpecificationExtensions.ShouldNotBeNull(x.Last);
x.First.ShouldNotBeNull();
x.Last.ShouldNotBeNull();
});
}
```
Expand Down
8 changes: 4 additions & 4 deletions docs/documents/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ theStore.BulkInsert(new[] { user });
// Open a document session with the identity map
using var session = theStore.IdentitySession();
session.Load<User>(user.Id)
.ShouldBeTheSameAs(session.Load<User>(user.Id));
.ShouldBeSameAs(session.Load<User>(user.Id));
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/IdentityMapTests.cs#L10-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-identity-map' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/IdentityMapTests.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-identity-map' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Do note that using the identity map functionality can be wasteful if you aren't able to take advantage of the identity map caching in a session. In those cases, you may want to either use the `IDocumentStore.LightweightSession()` which forgos the identity map functionality, or use the read only `IQuerySession` alternative. RavenDb users will note that Marten does not (yet) support any notion of `Evict()` to manually remove documents from identity map tracking to avoid memory usage problems. Our hope is that the existence of the lightweight session and the read only interface will alleviate the memory explosion problems that you can run into with naive usage of identity maps or the dirty checking when fetching a large number of documents.
Expand All @@ -166,8 +166,8 @@ using (var session = theStore.IdentitySession())
session.Store(target1, target2);

// Both documents are in the identity map
session.Load<Target>(target1.Id).ShouldBeTheSameAs(target1);
session.Load<Target>(target2.Id).ShouldBeTheSameAs(target2);
session.Load<Target>(target1.Id).ShouldBeSameAs(target1);
session.Load<Target>(target2.Id).ShouldBeSameAs(target2);

// Eject the 2nd document
session.Eject(target2);
Expand Down
2 changes: 1 addition & 1 deletion docs/events/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ You can enable global projections for conjoined tenancy.
```cs
opts.Events.EnableGlobalProjectionsForConjoinedTenancy = true;
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/Aggregation/aggregation_projection_validation_rules.cs#L94-L98' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_enabling_global_projections_for_conjoined_tenancy' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/Aggregation/aggregation_projection_validation_rules.cs#L89-L93' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_enabling_global_projections_for_conjoined_tenancy' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

::: warning
Expand Down
Loading

0 comments on commit a9019e8

Please sign in to comment.