erikolson186 / zangodb

MongoDB-like interface for HTML5 IndexedDB
https://erikolson186.github.io/zangodb/
MIT License
1.07k stars 72 forks source link

updated value doesn't take effect immediately #12

Closed naivefun closed 7 years ago

naivefun commented 7 years ago

I write the upsert method like this:

    public upsert(doc: Object, collectionName: string) {
        if (!doc) return Promise.reject('invalid doc');
        doc['updatedAt'] = Date.now();
        let id = doc['_id'];
        if (id) {
            return this.get(id, collectionName)
                .toArray((err: Error, docs: Object[]) => {
                    if (!err && docs && docs.length === 1) {
                        // found one, update
                        return this.collection(collectionName).update({ _id: id }, doc);
                    } else {
                        // found no record, insert
                        return this.insert(doc, collectionName);
                    }
                });
        } else {
            // missing _id, insert directly
            return this.insert(doc, collectionName);
        }
    }

And I call it like this:

    public try() {
        this.db.upsert({ _id: '12345', name: '123' }, DbService.COL_MAIN)
            .then(insertResult => {
                this.db.find({}, DbService.COL_MAIN)
                    .forEach(doc => {
                        console.log('result:', doc);
                    });
            })
            .catch((err: Error) => console.error(err, err.name, err.message));
    }

The value of name 123 is not updated in the inner find and forEach. That's to say when I call find the updates are not guaranteed?

naivefun commented 7 years ago

Hum false alarm ...