coresmart / persistencejs

persistence.js is an asynchronous Javascript database mapper library. You can use it in the browser, as well on the server (and you can share data models between them).
http://persistencejs.org
1.73k stars 240 forks source link

Persitencejs and Remote Ids #157

Open Bretto opened 9 years ago

Bretto commented 9 years ago

I have a DB with existing data and relations between objs, can I make PersistenceJs accept my remote DB ids and work with it? How do you use PersistenceJs with remote Data ? Any pointers would be appreciated thanks

chriscz commented 9 years ago

can I make PersistenceJs accept my remote DB ids and work with it?

Depending on the type of database you use, you could expose the _id or some other UUID based field over persistence.sync.js.

How do you use PersistenceJs with remote Data ?

I'll assume you are using a relational database. You could declare an extra column that contains a UUID and populate this column with unique id's for all of your data. This will allow persistence.js to use it as an id field instead of the integer PK.

What database are you using?

Bretto commented 9 years ago

Thanks for the feedback, its a MySQL DB, I would like to declare my schema and the relations, load the data and have PersistenceJS work with the data as it is in my DB, from my early testing it seams that it doesn't work this way (if the remote IDs where UUID would this work?). I would have to load the data, use the 'add' function to assign an Id to all the objects and then do the relations "hasMany" manually to work with the generated UUID, and then all my objects would have the 'ID' and the original 'Remote_ID'... All this does sound complicated, am I missing something ?

chriscz commented 9 years ago

When you say remote IDs are you referring to the id field that persistence is looking for when you set up the sync server? I haven't used the included sync server before since I wrote my own using Django and TastyPie.

Since ManyToMany syncing isn't supported by persistence, you'll have to manually manage it anyway. If you're using an ORM on the server, upgrading your database schema shouldn't be too difficult, presuming that your schema isn't too complex.