Repository navigation
feat(postgrest): write rows from values that set only some columns - #1039
Merged
Merged
Conversation
…e-row-values # Conflicts: # packages/Postgrest/Postgrest/PublicAPI.Unshipped.txt
…e-row-values # Conflicts: # packages/Postgrest/Postgrest.Tests/Observability/TelemetryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
nullis written as SQLNULL. 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
The shape a generator emits (an
initaccessor records that a column was set;Valuesis implemented explicitly, so it stays out of IntelliSense on the record):Behavior
InsertAsync,UpsertAsync):POST /{table}with exactlyValuesas the JSON object, keyed as given and converted like model columns (dates, enums, ranges). Nocolumnsparameter; a key that is absent takes the column default.InsertManyAsync,UpsertManyAsync):POST /{table}?columns="a","b"with the union of every row's columns in first-seen order, andPrefer: missing=defaultalways, whateverQueryOptions.MissingFieldssays, so a column a row leaves out takes its default. Rows that set no column send[{},{}]withoutcolumns; PostgREST inserts rows of defaults.Prefer: resolution=merge-duplicatesandon_conflictfromQueryOptions.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 onUpsertManyAsync, as postgrest-js documents it.InsertAsync/InsertManyAsyncignoreQueryOptions.Upsert; upserts go throughUpsertAsync/UpsertManyAsynconly.UpdateAsync:PATCH /{table}?<filters>with exactlyValues. An update that sets no column is sent as{}: it changes nothing, and withReturning = RepresentationPostgREST answers with no rows whether or not the filter matched. The docs ofUpdateAsyncandIPostgrestUpdate<TModel>say so and tell callers to checkValuesfirst when that matters. An update without a filter is the service's to refuse.{}); PostgREST rejects a write without a body (PGRST102). Other writes keep their current body handling.ArgumentNullExceptionbefore any request for null values, a null collection, a null row or nullValues. Server failures throwPostgrestExceptionthrough the existing mapping (for exampleUniquenessViolation).ModeledResponse<TModel>like the neighbouring writes.Deviations from supabase-js
null(defaultToNull = true); here a column left out always takes its database default, single row or many, so "unset" means one thing everywhere.InsertManyAsyncandUpsertManyAsyncrather than overloads ofInsertAsync/UpsertAsync: overloads that both take optional parameters breakRS0026.Tests
RowValuesTestsNULLfornull, 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 rowRowValuesRequestTestscolumns,Prefer(missing=default, resolution, options),{}bodies, value conversion, pending state and the checks before any requestRowValuesResultTestsTelemetryTestsupdateoperationThe 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 answeredPGRST102.Diff coverage: 100% (40 lines). Mutation: 22 killed and 1 timeout on changed lines, 0 survivors.
Notes
BulkColumnsmoves fromBulkWrite/toCommon/, since the bulk write and the write from values both use it, and gains the overload over column names that both now share.InsertAsync,UpsertAsyncandUpdateAsyncare a second write path per table besideInsert,UpsertandUpdate(TModel); the model-based members are unchanged.