techfort / LokiJS

javascript embeddable / in-memory database
http:/techfort.github.io/LokiJS
MIT License
6.73k stars 482 forks source link

How about using return value at Resultset.prototype.update? #837

Closed Einere closed 4 years ago

Einere commented 4 years ago

I try to update document

todoDB.findAndUpdate({
    'id': {
        '$eq': newTodoItem.id
    }
}, (data) => {
    return {
        ...data,
        ...todoItemForUpdate
    };
});

but it wan't working.

and I found that not using return value at Result.prototype.update()

Resultset.prototype.update = function (updateFunction) {
    ...
    // pass in each document object currently in resultset to user supplied updateFunction
    for (var idx = 0; idx < len; idx++) {
        // if we have cloning option specified or are doing differential delta changes, clone object first
        if (this.collection.cloneObjects || !this.collection.disableDeltaChangesApi) {
            obj = clone(rcd[this.filteredrows[idx]], this.collection.cloneMethod);
            updateFunction(obj); // here
            this.collection.update(obj);
        }
        else {
            // no need to clone, so just perform update on collection data object instance
            updateFunction(rcd[this.filteredrows[idx]]); // and here
            this.collection.update(rcd[this.filteredrows[idx]]);
        }
    }

    return this;
};

so, if you want update a lot of property, you should code below.

todoDB.findAndUpdate({
    'id': {
        '$eq': newTodoItem.id
    }
}, (data) => {
    data.foo = someData;
    data.bar = someData2;
    data.baz = someData3;
    ...
});

I think it's so exhausting. how about using return value like below?

Resultset.prototype.update = function (updateFunction) {
    ...
    // pass in each document object currently in resultset to user supplied updateFunction
    for (var idx = 0; idx < len; idx++) {
        // if we have cloning option specified or are doing differential delta changes, clone object first
        if (this.collection.cloneObjects || !this.collection.disableDeltaChangesApi) {
            obj = clone(rcd[this.filteredrows[idx]], this.collection.cloneMethod);
            var updatedObj = updateFunction(obj);
            this.collection.update(updatedObj);
        }
        else {
            // no need to clone, so just perform update on collection data object instance
            var updatedObj = updateFunction(rcd[this.filteredrows[idx]]);
            this.collection.update(updatedObj);
        }
    }

    return this;
};
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.