upsert<T> method

Future<T> upsert<T>({
  1. required List<SupaInsert<B>> records,
  2. required SupaModifier<B, R, T, PostgrestBuilder, PostgrestBuilder> modifier,
  3. SupaFilter<B>? filter,
  4. Set<SupaColumnBase<B>>? columns,
})

Updates or inserts records in the Supabase table.

records: The list of records to update or insert.

filter: The filter to apply to the query. This targets the records fetched after the upsert.

columns: The set of columns to fetch. If null, all columns are fetched.

modifier: The modifier to apply to the query.

Implementation

Future<T> upsert<T>({
  required List<SupaInsert<B>> records,
  required SupaModifier<B, R, T, PostgrestBuilder<dynamic, dynamic, dynamic>,
          PostgrestBuilder<dynamic, dynamic, dynamic>>
      modifier,
  SupaFilter<B>? filter,
  Set<SupaColumnBase<B>>? columns,
}) async {
  var request = supabaseClient
      .schema(schema)
      .from(tableName)
      .upsert(records.map((r) => r.toJSON()).toList());

  if (filter != null) request = request.supaApplyFilter(filter);

  if (modifier is! SupaNoneModifier<B, R>) {
    final response = await request
        .select(_generateColumnsPattern(columns))
        .supaApplyModifier(modifier);
    return _castResponse(modifier, response);
  }

  return (await request) as T;
}