Fix broken realm sorting

This fixes bugs #2520 and #2484
The cause of the bugs is the fact that calling `.sorted` method
on the realm resultset produces another resultset which is sorted
when iterated through `Iterator<T>` interface, but does absolutely
nothing to way how resultset object is serialised (printed out
as JSON).
So when we sorted, printed to json, parsed back through transit
and expected the order to be retained, it was not, and it was
failing quite randomly.
To be precise, it failed in a case where we sorted the resultset
according to some property and picked the top element from the result.
As the top element picking was done AFTER the sorted realm resultset
was converted into cljs datastructure, it was sometimes failing.

This fix just ensures that we select the single element from js
resultset (where sorting info is still retained) and only convert
that single element to cljs (it's also much more efficient).
This commit is contained in:
janherich 2017-11-28 01:04:12 +01:00 committed by Jan Herich
parent 0730420017
commit 56eb3f7a43
1 changed files with 11 additions and 9 deletions

View File

@ -73,7 +73,7 @@
(reset! account-realm (open-migrated-realm address account/schemas))
(handler nil)))))
; realm functions
;; realm functions
(defn and-query [queries]
(str/join " and " queries))
@ -128,17 +128,19 @@
(def reader (transit/reader :json))
(defn- internal-convert [js-object]
(->> js-object
(.stringify js/JSON)
(transit/read reader)
walk/keywordize-keys))
(defn js-object->clj
"Converts any js type/object into a map recursively
Performs 5 times better than iterating over the object keys
and that would require special care for collections"
[js-object]
(let [o (->> js-object
(.stringify js/JSON)
(transit/read reader))]
(walk/keywordize-keys (if (map? o)
(map->vec o)
o))))
(let [o (internal-convert js-object)]
(if (map? o) (map->vec o) o)))
(defn fix-map->vec
"Takes a map m and a keyword k
@ -161,8 +163,8 @@
(defn single [result]
(aget result 0))
(def single-clj
(comp first js-object->clj))
(defn single-clj [results]
(some-> results single internal-convert))
(defn- get-schema-by-name [opts]
(->> opts