diff --git a/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs b/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs index 42cbb4fe..b6597d35 100644 --- a/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs +++ b/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs @@ -133,7 +133,7 @@ public async Task Connect(CancellationToken cancellationToken) } public async Task Create(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); @@ -310,7 +310,7 @@ public async Task Merge( TMerge data, CancellationToken cancellationToken ) - where TMerge : Record + where TMerge : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); @@ -679,7 +679,7 @@ CancellationToken cancellationToken } public async Task Upsert(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); diff --git a/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs b/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs index ea73f540..d515c770 100644 --- a/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs +++ b/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs @@ -16,14 +16,14 @@ public interface ISurrealDbEngine : IDisposable void Configure(string? ns, string? db, string? token = null); Task Connect(CancellationToken cancellationToken); Task Create(T data, CancellationToken cancellationToken) - where T : Record; + where T : IRecord; Task Create(string table, T? data, CancellationToken cancellationToken); Task Create( StringRecordId recordId, TData? data, CancellationToken cancellationToken ) - where TOutput : Record; + where TOutput : IRecord; Task Delete(string table, CancellationToken cancellationToken); Task Delete(Thing thing, CancellationToken cancellationToken); Task Delete(StringRecordId recordId, CancellationToken cancellationToken); @@ -51,7 +51,7 @@ Task> LiveTable( CancellationToken cancellationToken ); Task Merge(TMerge data, CancellationToken cancellationToken) - where TMerge : Record; + where TMerge : IRecord; Task Merge( Thing thing, Dictionary data, @@ -125,13 +125,13 @@ Task SignUp(T scopeAuth, CancellationToken cancellationToken) Task> UpdateAll(string table, T data, CancellationToken cancellationToken) where T : class; Task Upsert(T data, CancellationToken cancellationToken) - where T : Record; + where T : IRecord; Task Upsert( StringRecordId recordId, TData data, CancellationToken cancellationToken ) - where TOutput : Record; + where TOutput : IRecord; Task Use(string ns, string db, CancellationToken cancellationToken); Task Version(CancellationToken cancellationToken); } diff --git a/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs b/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs index 675e8dad..f0c782d1 100644 --- a/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs +++ b/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs @@ -345,7 +345,7 @@ await Authenticate(new Jwt { Token = bearerAuth.Token }, cancellationToken) } public async Task Create(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); @@ -607,7 +607,7 @@ public async Task Merge( TMerge data, CancellationToken cancellationToken ) - where TMerge : Record + where TMerge : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); @@ -923,7 +923,7 @@ CancellationToken cancellationToken } public async Task Upsert(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); diff --git a/SurrealDb.Net/Models/Record.cs b/SurrealDb.Net/Models/Record.cs index 9faec854..0118c47c 100644 --- a/SurrealDb.Net/Models/Record.cs +++ b/SurrealDb.Net/Models/Record.cs @@ -5,9 +5,23 @@ namespace SurrealDb.Net.Models; /// -/// The base record type. +/// The abstract class for Record type. /// -public abstract class Record +public abstract class Record : IRecord +{ + /// + /// The id of the record + /// + [JsonConverter(typeof(ThingConverter))] + [JsonPropertyName("id")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] // 💡 ignore null value to prevent failure on Create operation + public Thing? Id { get; set; } +} + +/// +/// The interface for Record type. +/// +public interface IRecord { /// /// The id of the record diff --git a/SurrealDb.Net/SurrealDbClient.Interface.cs b/SurrealDb.Net/SurrealDbClient.Interface.cs index 87213b49..00bad8b1 100644 --- a/SurrealDb.Net/SurrealDbClient.Interface.cs +++ b/SurrealDb.Net/SurrealDbClient.Interface.cs @@ -76,7 +76,7 @@ public interface ISurrealDbClient : IDisposable /// /// Task Create(T data, CancellationToken cancellationToken = default) - where T : Record; + where T : IRecord; /// /// Creates a record in a table in the database. @@ -276,7 +276,7 @@ Task> LiveTable( /// /// Task Merge(TMerge data, CancellationToken cancellationToken = default) - where TMerge : Record; + where TMerge : IRecord; /// /// Modifies the specified record in the database. @@ -772,7 +772,7 @@ Task> UpdateAll( /// /// Task Upsert(T data, CancellationToken cancellationToken = default) - where T : Record; + where T : IRecord; /// /// Updates or creates the specified record in the database. diff --git a/SurrealDb.Net/SurrealDbClient.cs b/SurrealDb.Net/SurrealDbClient.cs index d699c67d..f9d01e32 100644 --- a/SurrealDb.Net/SurrealDbClient.cs +++ b/SurrealDb.Net/SurrealDbClient.cs @@ -181,7 +181,7 @@ public Task Connect(CancellationToken cancellationToken = default) } public Task Create(T data, CancellationToken cancellationToken = default) - where T : Record + where T : IRecord { return _engine.Create(data, cancellationToken); } @@ -288,7 +288,7 @@ public Task Merge( TMerge data, CancellationToken cancellationToken = default ) - where TMerge : Record + where TMerge : IRecord { return _engine.Merge(data, cancellationToken); } @@ -573,7 +573,7 @@ public Task> UpdateAll( } public Task Upsert(T data, CancellationToken cancellationToken = default) - where T : Record + where T : IRecord { return _engine.Upsert(data, cancellationToken); }