Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an IRecord interface and have Record satisfy it. #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions SurrealDb.Net/Internals/SurrealDbEngine.Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public async Task Connect(CancellationToken cancellationToken)
}

public async Task<T> Create<T>(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");
Expand Down Expand Up @@ -310,7 +310,7 @@ public async Task<TOutput> Merge<TMerge, TOutput>(
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");
Expand Down Expand Up @@ -679,7 +679,7 @@ CancellationToken cancellationToken
}

public async Task<T> Upsert<T>(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");
Expand Down
10 changes: 5 additions & 5 deletions SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public interface ISurrealDbEngine : IDisposable
void Configure(string? ns, string? db, string? token = null);
Task Connect(CancellationToken cancellationToken);
Task<T> Create<T>(T data, CancellationToken cancellationToken)
where T : Record;
where T : IRecord;
Task<T> Create<T>(string table, T? data, CancellationToken cancellationToken);
Task<TOutput> Create<TData, TOutput>(
StringRecordId recordId,
TData? data,
CancellationToken cancellationToken
)
where TOutput : Record;
where TOutput : IRecord;
Task Delete(string table, CancellationToken cancellationToken);
Task<bool> Delete(Thing thing, CancellationToken cancellationToken);
Task<bool> Delete(StringRecordId recordId, CancellationToken cancellationToken);
Expand Down Expand Up @@ -51,7 +51,7 @@ Task<SurrealDbLiveQuery<T>> LiveTable<T>(
CancellationToken cancellationToken
);
Task<TOutput> Merge<TMerge, TOutput>(TMerge data, CancellationToken cancellationToken)
where TMerge : Record;
where TMerge : IRecord;
Task<T> Merge<T>(
Thing thing,
Dictionary<string, object> data,
Expand Down Expand Up @@ -125,13 +125,13 @@ Task<Jwt> SignUp<T>(T scopeAuth, CancellationToken cancellationToken)
Task<IEnumerable<T>> UpdateAll<T>(string table, T data, CancellationToken cancellationToken)
where T : class;
Task<T> Upsert<T>(T data, CancellationToken cancellationToken)
where T : Record;
where T : IRecord;
Task<TOutput> Upsert<TData, TOutput>(
StringRecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record;
where TOutput : IRecord;
Task Use(string ns, string db, CancellationToken cancellationToken);
Task<string> Version(CancellationToken cancellationToken);
}
Expand Down
6 changes: 3 additions & 3 deletions SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ await Authenticate(new Jwt { Token = bearerAuth.Token }, cancellationToken)
}

public async Task<T> Create<T>(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");
Expand Down Expand Up @@ -607,7 +607,7 @@ public async Task<TOutput> Merge<TMerge, TOutput>(
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");
Expand Down Expand Up @@ -923,7 +923,7 @@ CancellationToken cancellationToken
}

public async Task<T> Upsert<T>(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");
Expand Down
18 changes: 16 additions & 2 deletions SurrealDb.Net/Models/Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@
namespace SurrealDb.Net.Models;

/// <summary>
/// The base record type.
/// The abstract class for Record type.
/// </summary>
public abstract class Record
public abstract class Record : IRecord
{
/// <summary>
/// The id of the record
/// </summary>
[JsonConverter(typeof(ThingConverter))]
[JsonPropertyName("id")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] // 💡 ignore null value to prevent failure on Create operation
public Thing? Id { get; set; }
}

/// <summary>
/// The interface for Record type.
/// </summary>
public interface IRecord
{
/// <summary>
/// The id of the record
Expand Down
6 changes: 3 additions & 3 deletions SurrealDb.Net/SurrealDbClient.Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public interface ISurrealDbClient : IDisposable
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SurrealDbException"></exception>
Task<T> Create<T>(T data, CancellationToken cancellationToken = default)
where T : Record;
where T : IRecord;

/// <summary>
/// Creates a record in a table in the database.
Expand Down Expand Up @@ -276,7 +276,7 @@ Task<SurrealDbLiveQuery<T>> LiveTable<T>(
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SurrealDbException"></exception>
Task<TOutput> Merge<TMerge, TOutput>(TMerge data, CancellationToken cancellationToken = default)
where TMerge : Record;
where TMerge : IRecord;

/// <summary>
/// Modifies the specified record in the database.
Expand Down Expand Up @@ -772,7 +772,7 @@ Task<IEnumerable<T>> UpdateAll<T>(
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SurrealDbException"></exception>
Task<T> Upsert<T>(T data, CancellationToken cancellationToken = default)
where T : Record;
where T : IRecord;

/// <summary>
/// Updates or creates the specified record in the database.
Expand Down
6 changes: 3 additions & 3 deletions SurrealDb.Net/SurrealDbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Task Connect(CancellationToken cancellationToken = default)
}

public Task<T> Create<T>(T data, CancellationToken cancellationToken = default)
where T : Record
where T : IRecord
{
return _engine.Create(data, cancellationToken);
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public Task<TOutput> Merge<TMerge, TOutput>(
TMerge data,
CancellationToken cancellationToken = default
)
where TMerge : Record
where TMerge : IRecord
{
return _engine.Merge<TMerge, TOutput>(data, cancellationToken);
}
Expand Down Expand Up @@ -573,7 +573,7 @@ public Task<IEnumerable<T>> UpdateAll<T>(
}

public Task<T> Upsert<T>(T data, CancellationToken cancellationToken = default)
where T : Record
where T : IRecord
{
return _engine.Upsert(data, cancellationToken);
}
Expand Down