how about

    selector = @key ? { @key => attributes[@key].read(row) } : { model.primary_key => nil }

then below you get

    upsert.row selector, document