Updated to allow upsert to take in a query