Skip to content

Commit

Permalink
48 modified time on relation (#49)
Browse files Browse the repository at this point in the history
* CreatedOn and ModifiedOn set successfully on node merge

* Creating nodes with repo method, also sets the CreatedOn or ModifiedOn (only if enforced)

* default timestamp keys updated

* Single relation timestamps set

* Merge/Create multiple relations now adds timestamp()

* Merge/Create now sets timstamps on group relations

* added tests for saving timestamps to the database

* Updated the example

* EnforceIdentifier fixed

---------

Co-authored-by: Farhad Nowzari <[email protected]>
  • Loading branch information
farhadnowzari and Farhad Nowzari authored Jul 18, 2024
1 parent bbe9e1d commit 52a65a8
Show file tree
Hide file tree
Showing 16 changed files with 741 additions and 69 deletions.
4 changes: 2 additions & 2 deletions example/MovieGraph/Controllers/MoviesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public async Task<long> Create()
LastName = f.Name.LastName()
});
var stopwatch = new Stopwatch();
var movies = fake.Generate(100);
var movies = fake.Generate(1);
stopwatch.Start();
graphContext.Movies.AddRange(movies);
graphContext.Movies.MergeRange(movies);
await graphContext.SaveChangesAsync();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public void Configure(NodeTypeBuilder<Movie> builder)
{
builder.HasRelationWithSingle(x => x.Director, "DIRECTED", RelationDirection.In);
builder.HasRelationWithMultiple(x => x.Actors, "ACTED_IN", RelationDirection.In);
builder.HasIdentifier(x => x.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public void Configure(NodeTypeBuilder<Person> builder)
{
builder.HasRelationWithMultiple(x => x.DirectedMovies, "DIRECTED", RelationDirection.Out);
builder.HasRelationWithMultiple(x => x.ActedInMovies, "ACTED_IN", RelationDirection.Out);
builder.HasIdentifier(x => x.Id);
}
}
4 changes: 2 additions & 2 deletions example/MovieGraph/MovieGraph.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Neo4j.Berries.OGM\Neo4j.Berries.OGM.csproj" />
<ItemGroup>
<ProjectReference Include="..\..\src\Neo4j.Berries.OGM\Neo4j.Berries.OGM.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions example/MovieGraph/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{
options
.ConfigureFromAssemblies(typeof(Program).Assembly);
options.EnableTimestamps();
options.EnforceIdentifiers = true;
});
var app = builder.Build();
Expand Down
2 changes: 2 additions & 0 deletions src/Neo4j.Berries.OGM/Contexts/Neo4jSingletonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal class Neo4jSingletonContext
private readonly Assembly[] _assemblies;

public static bool EnforceIdentifiers { get; internal set; }
internal static TimestampConfiguration TimestampConfiguration { get; set; } = new TimestampConfiguration();
internal static Dictionary<string, NodeConfiguration> Configs { get; private set; } = [];
internal static Func<string, string> PropertyCaseConverter { get; set; } = (x) => x;
public Neo4jSingletonContext(params Assembly[] assemblies)
Expand All @@ -21,6 +22,7 @@ public Neo4jSingletonContext(OGMConfigurationBuilder builder)
_assemblies = builder.Assemblies;
EnforceIdentifiers = builder.EnforceIdentifiers;
PropertyCaseConverter = builder.PropertyCaseConverter ?? PropertyCaseConverter;
TimestampConfiguration = builder.TimestampConfiguration ?? TimestampConfiguration;
ParseAssemblyForConfigurations();
foreach (var config in builder.NodeSetConfigurations)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Neo4j.Berries.OGM/Models/Config/OGMConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ public class OGMConfigurationBuilder(IServiceProvider serviceProvider)
{
internal Assembly[] Assemblies { get; private set; } = [];
internal Dictionary<string, NodeConfiguration> NodeSetConfigurations { get; private set; } = [];
internal TimestampConfiguration TimestampConfiguration { get; private set; } = null;
public IServiceProvider ServiceProvider { get; } = serviceProvider;



/// <summary>
/// Reads the INodeConfiguration implementations from the given assemblies
/// </summary>
Expand All @@ -30,6 +32,14 @@ public OGMConfigurationBuilder Configure(Action<NodeSetConfigurationBuilder> bui
NodeSetConfigurations = nodeSetConfigurationBuilder.NodeSetConfigurations;
return this;
}
/// <summary>
/// Enabling timestamps will add CreatedOn and ModifiedOn properties to the nodes and relations.
/// </summary>
public void EnableTimestamps(TimestampConfiguration timestampConfiguration = null)
{
TimestampConfiguration = timestampConfiguration ?? new();
TimestampConfiguration.Enabled = true;
}

/// <summary>
/// Enforces the identifiers for the nodes.
Expand Down
18 changes: 18 additions & 0 deletions src/Neo4j.Berries.OGM/Models/Config/TimestampConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Neo4j.Berries.OGM.Models.Config;

public class TimestampConfiguration()
{
internal bool Enabled { get; set; }
/// <summary>
/// Enforce the created timestamp key. When true, ModifiedTimestampKey will be also set on creating.
/// </summary>
public bool EnforceModifiedTimestampKey { get; set; }
/// <summary>
/// The key to use for setting the created timestamp. Default: createdOn
/// </summary>
public string CreatedTimestampKey { get; set; } = "createdOn";
/// <summary>
/// The key to use for setting the updated timestamp. Default: modifiedOn
/// </summary>
public string ModifiedTimestampKey { get; set; } = "modifiedOn";
}
Loading

0 comments on commit 52a65a8

Please sign in to comment.