rethinkdb / horizon

Horizon is a realtime, open-source backend for JavaScript apps.
MIT License
6.78k stars 351 forks source link

collection.store(object) mutates date values #635

Open intellix opened 8 years ago

intellix commented 8 years ago

Server version: 1.1.3 Client version: 1.1.3

I'm using ng2 and piping a date object into a particular format within my view. Upon using store, the date is replaced by Horizon/RethinkDB metadata within the original object so my view dies. I'm currently getting around this by copying the object with: Object.assign({}, o)

Example:

var o = {
  name: 'test',
  created: new Date()
};
horizon('things').store(o).subscribe(() => {
  console.log(o.created); // {$reql_type$: "TIME", epoch_time: 1465318800, timezone: 'Z' }
});
deontologician commented 8 years ago

Shoot! Deserialize should definitely not mutate, that was a bad call on my part (trying to be prematurely efficient :/)

tailsu commented 7 years ago

How do we work around this? Is there an easy way to convert this object to a regular Date object? Like, maybe, new Date(o.created.epoch_time) ? Or do you suggest that we don't store Date objects, but instead store timestamps, date.getTime()?

tailsu commented 7 years ago

Maybe copy the object before storing it, and copy the returned id back to the original copy? Hairy...

tailsu commented 7 years ago

The same problem occurs with collection.update.