-
-
Notifications
You must be signed in to change notification settings - Fork 626
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
Problems with Inbound primary key in some cases #1112
Comments
undefined is not a valid key in indexeddb. There is a difference between not having a prop set at all and to having it set to undefined. IndexedDB allows you to have auto-incremented primary keys in case you do not specify the inbound key on the object. If you do, the system will not provide the key for you. But in this case you specify the key as undefined, which is not supported. |
The issue here is, that no value is specified. I just define that id as a field exists (the same thing that is done in the example from the official docs. If no such way is provided, dexie is only usable with several workarounds for about everyone using a similiar build system then cra (like using @babel/plugin-transform-typescript) |
I see the issue which basically comes since JS class fields where introduced. If you transpile with typescript tsc to ES5 or ES6 you won't have this issue since a declaration will not do anything. But as babel-plugin-transform interprets the new JS spec correctly, the declaration will also mean an initializer to undefined. What Dexie could do here is possibly to workaround this new situation when declared fields correspond to an auto-incremented key and delete it in case it is undefined before passing it to IndexedDB. But it's a good question whether it is the right thing to do. On one hand this could be to intrusive but on the other hand, it would not be nice not being able to declare the indexed fields. Ideas and comments are welcome! |
A workaround for now is probably to use merged declarations: interface Entry {
id: string;
text: string;
}
class Entry {
constructor(text: string) {
this.text = text;
}
} |
...or how does plugin-transform-typescript handle the situation when property as optional? class Entry {
id?: string
text: string
} Will it still transpile it with a class field for id? |
I agree. It could be intrusive and subsequently lead to issues for others, that actually may rely on this behaviour... The workaround looks nice enough, i give it a try. An optional field doesn't work (see my example repository) Unfortunatly i do not see a solution that would not be intrusive in any way... |
This would be a problem for everyone that wants to store class instances in Dexie and have auto-incremented keys. |
I tried to use dexie in a create-react-app environment using typescript.
In this environment using an inbound primary key (
++id
) fails if the model class contains that key as a field. (Like shown in the typescript example in the docs)An example of this problem can be found in this repository
As far as i can tell, this might happen because the model class actually has a field named id. (so that it can be accessed)
Without the id field, the example works.
This is reflected in the compiled code.
In the create react app example, the compiled Entry class looks like this:
In this case, the id field is explicitly defined.
In this example the compiled Entry class looks like this:
In this case, the id field is not explicitly defined.
Changing this example in such a way that the id field is set to undefined in the constructor (see the Entry class in DB.ts) leads to the same issues like in the create-react-app example.
The text was updated successfully, but these errors were encountered: