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

✨ implement Upsert and Update methods using a RecordId #139

Merged
merged 6 commits into from
Nov 27, 2024
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
jobs:
build:
needs: [embedded-build]
runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:

test-v1:
needs: [embedded-build]
runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -96,7 +96,7 @@ jobs:

test-v2:
needs: [embedded-build]
runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -152,7 +152,7 @@ jobs:
directory: .coverage

embedded-build:
runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ jobs:

create-package:
needs: [embedded-builds]
runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores

steps:
- name: Checkout repository
Expand Down Expand Up @@ -181,7 +181,7 @@ jobs:

validate-package:
needs: [create-package]
runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores

steps:
- name: Setup dotnet 8.0
Expand Down
31 changes: 22 additions & 9 deletions SurrealDb.Embedded.Internals/SurrealDbEmbeddedEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,6 @@ CancellationToken cancellationToken
.ConfigureAwait(false);
}

public async Task<SurrealDbResponse> Query(
FormattableString query,
CancellationToken cancellationToken
)
{
var (formattedQuery, parameters) = query.ExtractRawQueryParams();
return await RawQuery(formattedQuery, parameters, cancellationToken).ConfigureAwait(false);
}

public async Task<SurrealDbResponse> RawQuery(
string query,
IReadOnlyDictionary<string, object?> parameters,
Expand Down Expand Up @@ -630,6 +621,17 @@ CancellationToken cancellationToken
.ConfigureAwait(false);
}

public async Task<TOutput> Update<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record
{
return await SendRequestAsync<TOutput>(Method.Update, [recordId, data], cancellationToken)
.ConfigureAwait(false);
}

public async Task<IEnumerable<T>> Update<T>(
string table,
T data,
Expand Down Expand Up @@ -681,6 +683,17 @@ CancellationToken cancellationToken
.ConfigureAwait(false);
}

public async Task<TOutput> Upsert<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record
{
return await SendRequestAsync<TOutput>(Method.Upsert, [recordId, data], cancellationToken)
.ConfigureAwait(false);
}

public async Task Use(string ns, string db, CancellationToken cancellationToken)
{
await SendRequestAsync<Unit>(Method.Use, [ns, db], cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public async Task ShouldSerializeAndThenDeserialize(string connectionString)
("3", isWindowsOS ? new(3) : new()),
("4", isWindowsOS ? new(4) : new()),
("715615451235648454L", new(715615451235648454L)),
#if NET7_0_OR_GREATER
(
"(1981, 1, 1, 10, 58, 23, 122, 82, DateTimeKind.Unspecified)",
new(1981, 1, 1, 10, 58, 23, 122, 82, DateTimeKind.Unspecified)
),
#endif
("(1913, 6, 3, 14, 38, 30, 164)", new(1913, 6, 3, 14, 38, 30, 164)),
("2024-11-08 12:29:58.6625489", DateTime.Parse("2024-11-08 12:29:58.6625489Z"))
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public async Task ShouldSerializeAndThenDeserialize(string connectionString)
("TimeSpan.FromMinutes(1)", TimeSpan.FromMinutes(1)),
("TimeSpan.FromHours(1)", TimeSpan.FromHours(1)),
("TimeSpan.FromMilliseconds(1)", TimeSpan.FromMilliseconds(1)),
#if NET7_0_OR_GREATER
("TimeSpan.FromMicroseconds(1)", TimeSpan.FromMicroseconds(1)),
#endif
("TimeSpan.FromTicks(100)", TimeSpan.FromTicks(100)),
("TimeSpan.FromTicks(4715615451235648454L)", TimeSpan.FromTicks(4715615451235648454L)),
];
Expand Down
9 changes: 4 additions & 5 deletions SurrealDb.Net/Internals/Formatters/TimeSpanFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ public static (long seconds, int nanos) Convert(TimeSpan value)
+ (value.Microseconds * TimeConstants.NANOS_PER_MICROSECOND)
+ (value.Milliseconds * TimeConstants.NANOS_PER_MILLISECOND);
#else
double fractionedMilliseconds =
value.TotalMilliseconds - Math.Truncate(value.TotalMilliseconds);
int nanos =
(int)(fractionedMilliseconds * TimeConstants.NANOS_PER_MILLISECOND)
+ (value.Milliseconds * TimeConstants.NANOS_PER_MILLISECOND);
long ticksInSeconds = seconds * TimeSpan.TicksPerSecond;
long remainingTicks = value.Ticks - ticksInSeconds;

int nanos = (int)(remainingTicks * TimeConstants.NanosecondsPerTick);
#endif

return (seconds, nanos);
Expand Down
34 changes: 33 additions & 1 deletion SurrealDb.Net/Internals/SurrealDbEngine.Http.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Buffers;
using System.Buffers;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Net.Http.Headers;
Expand Down Expand Up @@ -778,6 +778,23 @@ CancellationToken cancellationToken
return dbResponse.DeserializeEnumerable<T>();
}

public async Task<TOutput> Update<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record
{
if (_version?.Major < 2)
throw new NotImplementedException();

var request = new SurrealDbHttpRequest { Method = "update", Parameters = [recordId, data] };

var dbResponse = await ExecuteRequestAsync(request, cancellationToken)
.ConfigureAwait(false);
return dbResponse.GetValue<TOutput>()!;
}

public async Task<T> Upsert<T>(T data, CancellationToken cancellationToken)
where T : Record
{
Expand Down Expand Up @@ -822,6 +839,21 @@ CancellationToken cancellationToken
return dbResponse.DeserializeEnumerable<T>();
}

public async Task<TOutput> Upsert<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record
{
string method = _version?.Major > 1 ? "upsert" : "update";
var request = new SurrealDbHttpRequest { Method = method, Parameters = [recordId, data] };

var dbResponse = await ExecuteRequestAsync(request, cancellationToken)
.ConfigureAwait(false);
return dbResponse.GetValue<TOutput>()!;
}

public async Task Use(string ns, string db, CancellationToken cancellationToken)
{
var request = new SurrealDbHttpRequest { Method = "use", Parameters = [ns, db] };
Expand Down
12 changes: 12 additions & 0 deletions SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ CancellationToken cancellationToken
where TOutput : Record;
Task<IEnumerable<T>> Update<T>(string table, T data, CancellationToken cancellationToken)
where T : class;
Task<TOutput> Update<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record;
Task<T> Upsert<T>(T data, CancellationToken cancellationToken)
where T : Record;
Task<TOutput> Upsert<TData, TOutput>(
Expand All @@ -160,6 +166,12 @@ CancellationToken cancellationToken
where TOutput : Record;
Task<IEnumerable<T>> Upsert<T>(string table, T data, CancellationToken cancellationToken)
where T : class;
Task<TOutput> Upsert<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record;
Task Use(string ns, string db, CancellationToken cancellationToken);
Task<string> Version(CancellationToken cancellationToken);
}
Expand Down
40 changes: 39 additions & 1 deletion SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net.WebSockets;
using System.Reactive.Concurrency;
Expand Down Expand Up @@ -1077,6 +1077,26 @@ CancellationToken cancellationToken
return dbResponse.DeserializeEnumerable<T>();
}

public async Task<TOutput> Update<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record
{
if (_version?.Major < 2)
throw new NotImplementedException();

var dbResponse = await SendRequestAsync(
"update",
[recordId, data],
SurrealDbWsRequestPriority.Normal,
cancellationToken
)
.ConfigureAwait(false);
return dbResponse.GetValue<TOutput>()!;
}

public async Task<T> Upsert<T>(T data, CancellationToken cancellationToken)
where T : Record
{
Expand Down Expand Up @@ -1130,6 +1150,24 @@ CancellationToken cancellationToken
return dbResponse.DeserializeEnumerable<T>();
}

public async Task<TOutput> Upsert<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken
)
where TOutput : Record
{
string method = _version?.Major > 1 ? "upsert" : "update";
var dbResponse = await SendRequestAsync(
method,
[recordId, data],
SurrealDbWsRequestPriority.Normal,
cancellationToken
)
.ConfigureAwait(false);
return dbResponse.GetValue<TOutput>()!;
}

public Task Use(string ns, string db, CancellationToken cancellationToken)
{
return Use(ns, db, SurrealDbWsRequestPriority.Normal, cancellationToken);
Expand Down
44 changes: 42 additions & 2 deletions SurrealDb.Net/SurrealDbClient.Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ Task<T> Update<T>(T data, CancellationToken cancellationToken = default)
/// <typeparam name="TData">The type of data contained in the record.</typeparam>
/// <typeparam name="TOutput">The type of the record updated.</typeparam>
/// <param name="recordId">The record id.</param>
/// <param name="data">The record to update.</param>
/// <param name="data">The record data to update.</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
/// <returns>The updated record.</returns>
/// <exception cref="OperationCanceledException"></exception>
Expand All @@ -869,7 +869,7 @@ Task<TOutput> Update<TData, TOutput>(
/// </summary>
/// <typeparam name="T">The type of the record to update.</typeparam>
/// <param name="table">The name of the database table.</param>
/// <param name="data">The record to create or update.</param>
/// <param name="data">The record data to update.</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
/// <returns>The list of updated records.</returns>
/// <exception cref="OperationCanceledException"></exception>
Expand All @@ -883,6 +883,26 @@ Task<IEnumerable<T>> Update<T>(
)
where T : class;

/// <summary>
/// Updates the specified record in the database.
/// </summary>
/// <typeparam name="TData">The type of data contained in the record.</typeparam>
/// <typeparam name="TOutput">The type of the record updated.</typeparam>
/// <param name="recordId">The record id.</param>
/// <param name="data">The record data to update.</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
/// <returns>The updated record.</returns>
/// <exception cref="OperationCanceledException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SurrealDbException"></exception>
Task<TOutput> Update<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken = default
)
where TOutput : Record;

/// <summary>
/// Updates or creates the specified record in the database.
/// </summary>
Expand Down Expand Up @@ -936,6 +956,26 @@ Task<IEnumerable<T>> Upsert<T>(
)
where T : class;

/// <summary>
/// Updates or creates the specified record in the database.
/// </summary>
/// <typeparam name="TData">The type of data contained in the record.</typeparam>
/// <typeparam name="TOutput">The type of the record created.</typeparam>
/// <param name="recordId">The record id.</param>
/// <param name="data">The record to create or update.</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
/// <returns>The record created or updated.</returns>
/// <exception cref="OperationCanceledException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SurrealDbException"></exception>
Task<TOutput> Upsert<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken = default
)
where TOutput : Record;

/// <summary>
/// Switch to a specific namespace and database.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions SurrealDb.Net/SurrealDbClient.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,16 @@ public Task<IEnumerable<T>> Update<T>(
return _engine.Update(table, data, cancellationToken);
}

public Task<TOutput> Update<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken = default
)
where TOutput : Record
{
return _engine.Update<TData, TOutput>(recordId, data, cancellationToken);
}

public Task<T> Upsert<T>(T data, CancellationToken cancellationToken = default)
where T : Record
{
Expand All @@ -538,6 +548,16 @@ public Task<IEnumerable<T>> Upsert<T>(
return _engine.Upsert(table, data, cancellationToken);
}

public Task<TOutput> Upsert<TData, TOutput>(
RecordId recordId,
TData data,
CancellationToken cancellationToken = default
)
where TOutput : Record
{
return _engine.Upsert<TData, TOutput>(recordId, data, cancellationToken);
}

public Task Use(string ns, string db, CancellationToken cancellationToken = default)
{
return _engine.Use(ns, db, cancellationToken);
Expand Down
Loading