paldepind / synceddb

Makes it easy to write offline-first applications with realtime syncing and server side persistence.
MIT License
411 stars 34 forks source link

configurable keyPath for people having existing records #24

Open ghost opened 8 years ago

ghost commented 8 years ago

Hi,

Please read the edit below.

There is a bug which prevents deletion of records from the store: createTombstone gets called with the user defined record: https://github.com/paldepind/synceddb/blob/master/client/src/utils.js#L116

But it has a hard coded scheme, which expects properties like key and version: https://github.com/paldepind/synceddb/blob/master/client/src/utils.js#L162

So in my stores, my primary key is id and not key, so when the tombstone gets put into the store, a new record is created (with a new id), which prevents the deletion from being noticed.

Possible solution: Pass the primary key field name and its value to createTombstone.

Thank you for your great work!

Edit: I just noticed that a property with name key seems to be generated automatically for newly created records. I'm using a db with existing entities, so it doesn't have that.

piglovesyou commented 8 years ago

It is the expected behaviour for now, SyncedDB only supports key as a primary key. If we support arbitrary name for it, we have to identify the name for client and server, then we need a config file or something for that, which makes us confused. See also here.

But I agree that this can cause a migration problem for person like you who wants to use existing data.

ghost commented 8 years ago

Ah, I see. But isn't this rather unflexible?

Would you accept a PR which will still defaults to key but lets the user specify the key path, if desired? On the other hand, key seems to be a generated "unique" value, which probably has some other function I don't get yet... If key is essential for synceddb, maybe it could be a unique index instead, letting the user choose the key path. Just a few thoughts.

I'll read more into your code and IndexedDB the next few days before attempting any changes. :)

piglovesyou commented 8 years ago

Actually it's @paldepind 's code 😃 Yeah let's do that, it should encourage adoption for such people. I think all we have to do is to make it able to pass keyPath option to both the client and the backend, edit hardcoded parts, then it should work.

ghost commented 8 years ago

I'll close this, since synceddb doesn't play well with my personal preferences for syncing. Some personal reasons/things that I noticed:

Thanks for your support and respect for that sophisticated source code!

piglovesyou commented 8 years ago

@whatda That list definitely helps us to think core arch, thanks. As you said offline app should care uniqueness of id in each records (any clustering DB env can also say the same thing in the first place) so auto increment is never be an option.

I still feel configurable keyPath is good for some people so let me keep open it. Also let me change the summary