Merge pull request #12964 from ggazzo/fix/client-upsert

fix(minimongo): sync upsert doesn't create new documents
This commit is contained in:
Gabriel Grubba
2024-03-14 13:17:49 +01:00
committed by GitHub
2 changed files with 31 additions and 0 deletions

View File

@@ -660,6 +660,22 @@ export default class LocalCollection {
this._observeQueue.drain();
// If we are doing an upsert, and we didn't modify any documents yet, then
// it's time to do an insert. Figure out what document we are inserting, and
// generate an id for it.
let insertedId;
if (updateCount === 0 && options.upsert) {
const doc = LocalCollection._createUpsertDocument(selector, mod);
if (!doc._id && options.insertedId) {
doc._id = options.insertedId;
}
insertedId = this.insert(doc);
updateCount = 1;
}
return this.finishUpdate({
options,
updateCount,

View File

@@ -212,6 +212,21 @@ Tinytest.addAsync('minimongo - basics', async test => {
test.equal(after.d, undefined);
});
Tinytest.addAsync('minimongo - upsert', async test => {
const c = new LocalCollection();
await c.upsertAsync({ name: 'doc' }, { name: 'doc' });
test.equal(c.find({}).count(), 1);
await c.removeAsync({});
c.upsert({ name: 'doc' }, { name: 'doc' });
test.equal(c.find({}).count(), 1);
});
Tinytest.add('minimongo - error - no options', test => {
try {
throw MinimongoError('Not fun to have errors');