SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

Feature request: upsert

Open jdetter opened this issue 7 months ago • 1 comments

Something like this:

// Pseudocode
func upsert(ctx, primary_key_value, value):
  if ctx.primary_key_column.try_find(primary_key_value):
    // Row with a matching primary key column exists
    ctx.primary_key_column.delete(primary_key_value)
  ctx.primary_key_column.insert(primary_key_value, value)
  

In order to upsert on a column it needs to have a unique constraint on it.

jdetter avatar Apr 14 '25 21:04 jdetter

This can probably be implemented for now in module bindings as "try-update, insert on row-doesn't-exist" so that we don't have to add this in the host. If it turns out that this needs to be highly optimized we can add an ABI for it later on.

Centril avatar Apr 15 '25 14:04 Centril

Reopening, as the linked PR does not include C# support, and is currently marked unstable.

gefjon avatar May 05 '25 16:05 gefjon

If I may, perhaps the name "InsertOrUpdate" or similar would be better. Yes, it's longer, but there is then no ambiguity on first glance about what the function does.

sunkin351 avatar Jun 19 '25 00:06 sunkin351

If I may, perhaps the name "InsertOrUpdate" or similar would be better. Yes, it's longer, but there is then no ambiguity on first glance about what the function does.

@sunkin351 We did indeed settle on insert_or_update in the unstable Rust implementation, #2678 .

gefjon avatar Jun 19 '25 00:06 gefjon