datascript icon indicating copy to clipboard operation
datascript copied to clipboard

js entity api returns clj object

Open pedroteixeira opened this issue 6 years ago • 2 comments

I have "a test case" that reproduces the problem over here: https://runkit.com/pedroteixeira/5b3d293e2362ea0012159119

We would expect it to return the value converted to native js, right?

Any workarounds for this? Thanks

pedroteixeira avatar Jul 04 '18 20:07 pedroteixeira

Hm, so you’re trying storing JS object as a value. Not gonna work at the moment — DS does total JS to CLJ conversion under the hood of JS API. It wouldn’t be that way if it was JS-first but unfortunately it’s CLJS-oriented. Don’t have any ideas how to work around that too. If you store plain values (not objects) all should be fine though

tonsky avatar Jul 04 '18 20:07 tonsky

Ok, I thought it could be a regression. In a JS-only environment, I managed to use datascript-mori to wrap the entity API, to try workaround the js API returning different objects than inputted. Perhaps something more efficient could be done internally? Perhaps just letting values as they were inputted (without conversion)?

Not ideal, but the following is a temptative solution using a wrapper class (typescript) -- if it helps anyone in the future:

import { mori } from 'datascript-mori';

class DBEntity implements IDatabaseEntity {
  entity: any;

  constructor(dsentity) {
    this.entity = dsentity;
  }

  get(key) {
    const value = this.entity.get(key);

    if (value && value.eid) {
      return new DBEntity(value);
      
    } else if (value != null && mori.isSeqable(value)) {
      return mori.toJs(value);
    }

    return value;
  }
}

pedroteixeira avatar Jul 04 '18 20:07 pedroteixeira