Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use findOne().$ to subscribe a document, but the document's reference is not equal to the document's reference which created by newDocument #3777

Closed
wants to merge 1 commit into from

Conversation

qinyang912
Copy link
Contributor

This PR contains:

bug report

Describe the problem you have without this PR

When I use findOne().$ before newDocument, the reference of document which get from findOne().$ is not equal to the reference of document which created by newDocument.

But if I use findOne().$ after newDocument, the reference is right

stackblitz: https://stackblitz.com/edit/typescript-6usdjm?file=index.ts

[email protected] can also reproduce the bug

[email protected] is right

@pubkey
Copy link
Owner

pubkey commented May 9, 2022

I can reproduce the problem, thank you for reporting.

In the RxStorage interface, it is expected that an event is emitted before the write operation is finished.
Therefore the query emits the data before RxDB can know that the write succeeded and that is why a new RxDocument will be created for the emitted insert event.

In the past, this was not a problem because pouchdb emitted the event much time after the write finished.

I do not see a way to fix this. Also I think the whole temporary document topic should be deprecated.
Many things are not clear, for example what happens if multiple temporary documents (with the same primary key) are inserted at the same time.

Can you explain your use case on why you have to use .newDocument() instead of just storing the json data an later insert that into RxDB?

@pubkey
Copy link
Owner

pubkey commented May 9, 2022

Rel.

it('#3777 use findOne().$ to subscribe a document, the reference should equal to document which created by newDocument', async () => {
            const c = await humansCollection.create(0);
            const docId = randomString(10);
            let docOuter;
            c.findOne(docId).$.subscribe((doc) => (docOuter = doc));
            const docNew = c.newDocument({
                passportId: docId,
                age: 20,
                firstName: randomString(10),
                lastName: randomString(10)
            });
            await docNew.save();
            assert.ok(docOuter === docNew);
            c.database.destroy();
        })

@pubkey
Copy link
Owner

pubkey commented May 24, 2022

Closing this since now answer.

Temporary documents will be removed in the next major version.
https://github.com/pubkey/rxdb/blob/master/orga/before-next-major.md#remove-temporary-documents

@pubkey pubkey closed this May 24, 2022
@qinyang912
Copy link
Contributor Author

Sorry for the late reply, I found that the code that used .newDocument can be implemented with .insert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants