/Idam.EFTimestamps

A library for handling Timestamps and SoftDelete as UTC DateTime or Unix in EntityFramework.

Primary LanguageC#MIT LicenseMIT

Idam.EFTimestamps

NuGet .NET

A library for handling Timestamps and SoftDelete as UTC DateTime or Unix in EntityFramework.

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

Features

  • Soft delete (DeletedAt).
  • Timestamps (CreatedAt, UpdatedAt).

Both features support UTC DateTime and Unix Time Milliseconds format.

Example of Unix Time Milliseconds: currentmillis.com

Get started

Run this command to install

Install-Package Idam.EFTimestamps

or

dotnet add package Idam.EFTimestamps

Usage

Using Timestamps

  1. Add AddTimestamps() in your context.

    using Idam.EFTimestamps.Extensions;
    
    public class MyDbContext : DbContext
    {
        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChanges(acceptAllChangesOnSuccess);
        }
    
        public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
        }
    }
  2. Implement an Interface (ITimeStamps or ITimeStampsUnix) to your entity.

    using Idam.EFTimestamps.Interfaces;
    
    /// BaseEntity
    public abstract class BaseEntity
    {
        public int Id { get; set; }
        public string Name { get; set; } = default!;
        public string? Description { get; set; }
    }
    
    /// Using UTC DateTime Format
    public class Dt : BaseEntity, ITimeStamps
    {
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }
    
    /// Using Unix Format
    public class Unix : ITimeStampsUnix
    {
        public long CreatedAt { get; set; }
        public long UpdatedAt { get; set; }
    }

Using SoftDelete

  1. Add AddTimestamps() and AddSoftDeleteFilter() in your context.

    using Idam.EFTimestamps.Extensions;
    
    public class MyDbContext : DbContext
    {
        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChanges(acceptAllChangesOnSuccess);
        }
    
        public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
        }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.AddSoftDeleteFilter();
    
            base.OnModelCreating(modelBuilder);
        }
    }
  2. Implement an Interface (ISoftDelete or ISoftDeleteUnix) to your entity.

    using Idam.EFTimestamps.Interfaces;
    
    /// Using UTC DateTime Format
    public class Dt : BaseEntity, ISoftDelete
    {
        public DateTime? DeletedAt { get; set; }
    }
    
    /// Using Unix Format
    public class Unix : BaseEntity, ISoftDeleteUnix
    {
        public long? DeletedAt { get; set; }
    }

Restore

The SoftDelete has a Restore() function, so you can restore the deleted data.

using Idam.EFTimestamps.Extensions;

/// Your context
public class MyDbContext : DbContext
{
    public DbSet<Dt> Dts { get; set; }
}

/// Dt Controller
public class DtController
{
    readonly MyDbContext _context;

    public async Task<IActionResult> RestoreAsync(Dt dt)
    {
        var restored = _context.Dts.Restore(dt);
        await context.SaveChangesAsync();
        
        return Ok(restored);
    }
}

Force Remove

The SoftDelete has a ForceRemove() function, so you can permanently remove the data.

/// Dt Controller
public class DtController
{
    readonly MyDbContext _context;

    public async Task<IActionResult> ForceRemoveAsync(Dt dt)
    {
        _context.Dts.ForceRemove(dt);
        await context.SaveChangesAsync();
        
        return Ok();
    }
}

Trashed

The SoftDelete has a Trashed() function to check if current data is deleted.

/// Dt Controller
public class DtController
{
    public IActionResult IsDeleted(Dt dt)
    {
        bool isDeleted = dt.Trashed();
        
        return Ok(isDeleted);
    }
}

The Trashed() function only shows when your entity implements an interface ISoftDelete or ISoftDeleteUnix.

Ignore global softdelete filter

By default the deleted data filtered from the query, if you want to get the deleted data you can ignore the global softdelete filter by using IgnoreQueryFilters().

/// Dt Controller
public class DtController
{
    readonly MyDbContext _context;

    public async Task<IActionResult> GetAllDeletedAsync()
    {
        var deleteds = await _context.Dts
            .IgnoreQueryFilters()
            .Where(x => x.DeletedAt != null)
            .ToListAsync();

        return Ok(deleteds);
    }
}

Using Custom TimeStamps fields

By default, the TimeStamps interface uses CreatedAt, UpdatedAt, and DeletedAt as field names. To customize the TimeStamps fields simplify just add ColumnAttribute to the fields.

Migrating

Migrating from 7.0.0

  1. Remove [TimeStampsAttribute], [TimeStampsUnixAttribute], and [TimeStampsUtcAttribute].
  2. If you use custom field from TimeStampsAttribute, then add the ColumnAttribute to each TimeStamps fields.
  3. Remove/Create your own IGuidEntity.