mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Merge pull request #12964 from ggazzo/fix/client-upsert
fix(minimongo): sync upsert doesn't create new documents
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user