MNGDB.Extensions.Ledger
1.0.0
dotnet add package MNGDB.Extensions.Ledger --version 1.0.0
NuGet\Install-Package MNGDB.Extensions.Ledger -Version 1.0.0
<PackageReference Include="MNGDB.Extensions.Ledger" Version="1.0.0" />
paket add MNGDB.Extensions.Ledger --version 1.0.0
#r "nuget: MNGDB.Extensions.Ledger, 1.0.0"
// Install MNGDB.Extensions.Ledger as a Cake Addin #addin nuget:?package=MNGDB.Extensions.Ledger&version=1.0.0 // Install MNGDB.Extensions.Ledger as a Cake Tool #tool nuget:?package=MNGDB.Extensions.Ledger&version=1.0.0
MongoDB.Driver.Extensions.Ledger
MongoDB.Driver.Extensions.Ledger is a .NET library that extends the MongoDB driver to include ledger functionality, allowing you to maintain a "audit or history" log of document operations (insert, update, delete) within a MongoDB collection. All document operations are stored in append-only mode so that any change can be preserved.
Once the document has been stored in a ledger, it is aimed to not be changed later. Within this extension SHA256 hashed document is preserved within log collection so that if an unauthorized change is made to the document, the change can be determined.
The main motivation for this repository is to be able to use ledger approach within MongoDB for .NET workloads. It was inspired by this proof-of-concept python implemention by MongoDB-Labs.
Installation
Usage and How It Works
First, ensure you have the necessary using directives:
using MongoDB.Driver;
using MongoDB.Driver.Extensions.Ledger;
Insert a document
var collection = database.GetCollection<MyDocument>("myCollection");
var document = new MyDocument { /* Initialize your document */ };
await collection.InsertOneAsLedger(document);
InsertOneAsLedger
- Inserts the document into the specified collection.
- Logs the operation in a audit or history collection with a unique ID, SHA256 hash of the document, and metadata.
Replace a document
var collection = database.GetCollection<MyDocument>("myCollection");
var document = new MyDocument { /* Initialize your document */ };
var filter = Builders<MyDocument>.Filter.Eq(doc => doc.Id, document.Id);
await collection.ReplaceOneAsLedger(document, filter);
ReplaceOneAsLedger
- Replaces the document in the specified collection based on the provided filter.
- Logs the operation in a audit or history collection with a unique ID, SHA256 hash of the document, and metadata, including the previous hash and versioning.
Delete a document
var collection = database.GetCollection<MyDocument>("myCollection");
var filter = Builders<MyDocument>.Filter.Eq(doc => doc.Id, documentId);
await collection.DeleteOneAsLedger(filter);
DeleteOneAsLedger
- Deletes the document from the specified collection based on the provided filter.
- Logs the operation in a audit or history collection with a unique ID, SHA256 hash of the document, and metadata, including the previous hash and versioning.
Verify a document
var collection = database.GetCollection<MyDocument>("myCollection");
await collection.VerifyOneFromLedger(document); /* Fectched or updated document */
VerifyOneFromLedger
- Verifies a document's versions and hashes in audit or history log
Log Document Model
Log document has the following model to have audit or history log in the collection.
internal class LogRecord<T>
{
[BsonId]
public string Id { get; set; }
public T Data { get; set; }
public Metadata Metadata { get; set; }
}
Metadata
contains the meta information for the data. Hashed data and some reference properties are defined in metadata. Also every metadata has a version number and timestamp for changes.
internal class Metadata
{
public string OriginalId { get; set; }
public string Hash { get; set; }
public string? PreviousHash { get; set; }
public string Operation { get; set; }
public int Version { get; set; }
public DateTimeOffset Timestamp { get; set; }
}
Contributing
Contributions are more than welcome! Please submit a pull request or create an issue to discuss your ideas or feedbacks.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- MongoDB.Driver (>= 2.28.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 119 | 8/9/2024 |