Skip to content

feat(postgrest): write rows from values that set only some columns - #1039

Merged
Tr00d merged 3 commits into
masterfrom
feat/postgrest-write-row-values
Dec 2, 2026
Merged

Tr00d merged 3 commits into
masterfrom
feat/postgrest-write-row-values

Conversation

@Tr00d

@Tr00d Tr00d commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary

A table can insert, upsert and update rows from values that set only some columns: a column left out takes its database default (on insert) or keeps its value (on update), and a column set to null is written as SQL NULL. This is groundwork for generated types (database.generated_types.typed_queries), whose insert and update records implement the new interfaces; it carries no capability annotation.

Public API

// Supabase.Postgrest.WriteRowValues
public interface IPostgrestInsert<TModel> where TModel : BaseModel, new() { IReadOnlyDictionary<string, object?> Values { get; } }
public interface IPostgrestUpdate<TModel> where TModel : BaseModel, new() { IReadOnlyDictionary<string, object?> Values { get; } }

// IPostgrestTable<TModel>
Task<ModeledResponse<TModel>> InsertAsync(IPostgrestInsert<TModel> values, QueryOptions? options = null, CancellationToken cancellationToken = default);
Task<ModeledResponse<TModel>> InsertManyAsync(IReadOnlyCollection<IPostgrestInsert<TModel>> values, QueryOptions? options = null, CancellationToken cancellationToken = default);
Task<ModeledResponse<TModel>> UpsertAsync(IPostgrestInsert<TModel> values, QueryOptions? options = null, CancellationToken cancellationToken = default);
Task<ModeledResponse<TModel>> UpsertManyAsync(IReadOnlyCollection<IPostgrestInsert<TModel>> values, QueryOptions? options = null, CancellationToken cancellationToken = default);
Task<ModeledResponse<TModel>> UpdateAsync(IPostgrestUpdate<TModel> changes, QueryOptions? options = null, CancellationToken cancellationToken = default);

The shape a generator emits (an init accessor records that a column was set; Values is implemented explicitly, so it stays out of IntelliSense on the record):

public sealed record BooksInsert(long AuthorId, string Title) : IPostgrestInsert<Books>
{
    private DateTime? publishedOn; private bool publishedOnSet;

    public DateTime? PublishedOn { get => this.publishedOn; init { this.publishedOn = value; this.publishedOnSet = true; } }

    IReadOnlyDictionary<string, object?> IPostgrestInsert<Books>.Values => this.SetColumns();

    private Dictionary<string, object?> SetColumns()
    {
        var values = new Dictionary<string, object?> { ["author_id"] = this.AuthorId, ["title"] = this.Title };
        if (this.publishedOnSet) values["published_on"] = this.publishedOn;
        return values;
    }
}

await client.Table<Books>().InsertAsync(new BooksInsert(2, "Dune") { PublishedOn = null });
await client.Table<Books>().Where(book => book.Id == 7).UpdateAsync(new BooksUpdate { Title = "Renamed" });

Behavior

  • One row (InsertAsync, UpsertAsync): POST /{table} with exactly Values as the JSON object, keyed as given and converted like model columns (dates, enums, ranges). No columns parameter; a key that is absent takes the column default.
  • Several rows (InsertManyAsync, UpsertManyAsync): POST /{table}?columns="a","b" with the union of every row's columns in first-seen order, and Prefer: missing=default always, whatever QueryOptions.MissingFields says, so a column a row leaves out takes its default. Rows that set no column send [{},{}] without columns; PostgREST inserts rows of defaults.
  • Upsert adds Prefer: resolution=merge-duplicates and on_conflict from QueryOptions.OnConflict. With several rows, a column a row leaves out takes its default even when the row already exists, replacing the stored value; this is documented on UpsertManyAsync, as postgrest-js documents it.
  • InsertAsync/InsertManyAsync ignore QueryOptions.Upsert; upserts go through UpsertAsync/UpsertManyAsync only.
  • UpdateAsync: PATCH /{table}?<filters> with exactly Values. An update that sets no column is sent as {}: it changes nothing, and with Returning = Representation PostgREST answers with no rows whether or not the filter matched. The docs of UpdateAsync and IPostgrestUpdate<TModel> say so and tell callers to check Values first when that matters. An update without a filter is the service's to refuse.
  • A write that sets no column is always sent with a JSON body ({}); PostgREST rejects a write without a body (PGRST102). Other writes keep their current body handling.
  • ArgumentNullException before any request for null values, a null collection, a null row or null Values. Server failures throw PostgrestException through the existing mapping (for example UniquenessViolation).
  • Returns ModeledResponse<TModel> like the neighbouring writes.

Deviations from supabase-js

  • supabase-js bulk writes default a missing field to null (defaultToNull = true); here a column left out always takes its database default, single row or many, so "unset" means one thing everywhere.
  • The several-row methods are named InsertManyAsync and UpsertManyAsync rather than overloads of InsertAsync/UpsertAsync: overloads that both take optional parameters break RS0026.

Tests

Tier Class Pins
E2E RowValuesTests Default for a left-out column, NULL for null, the per-row default of several rows, rows of defaults from [{},{}], upsert on the primary key, update of only the set columns, and an empty update that changes nothing and returns no row
Contract RowValuesRequestTests Exact bodies, columns, Prefer (missing=default, resolution, options), {} bodies, value conversion, pending state and the checks before any request
Contract RowValuesResultTests The written rows handed back and the failure mapping
Contract TelemetryTests An update from values is reported as the update operation

The tests failed before the change (the types did not exist); with the current empty-body handling, the empty insert and update were not sent with {} and the E2E update answered PGRST102.

Diff coverage: 100% (40 lines). Mutation: 22 killed and 1 timeout on changed lines, 0 survivors.

Notes

  • BulkColumns moves from BulkWrite/ to Common/, since the bulk write and the write from values both use it, and gains the overload over column names that both now share.
  • InsertAsync, UpsertAsync and UpdateAsync are a second write path per table beside Insert, Upsert and Update(TModel); the model-based members are unchanged.

Tr00d added 3 commits December 2, 2026 12:14
…e-row-values

# Conflicts:
#	packages/Postgrest/Postgrest/PublicAPI.Unshipped.txt
…e-row-values

# Conflicts:
#	packages/Postgrest/Postgrest.Tests/Observability/TelemetryTests.cs
@Tr00d
Tr00d marked this pull request as ready for review December 2, 2026 12:49
@Tr00d
Tr00d merged commit 8662227 into master Dec 2, 2026
10 of 11 checks passed
@Tr00d
Tr00d deleted the feat/postgrest-write-row-values branch December 2, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant