   [:v4])
 
 (defn query-factsets [& cols]
-  (->> (query-to-vec "select certname, (stable||volatile) as facts, environment, producer_timestamp from factsets")
+  (->> (str "select certname,"
+            "       (stable||volatile) as facts,"
+            "       environment,"
+            "       producer_timestamp"
+            "  from factsets
+                 inner join environments on factsets.environment_id=environments.id")
+       query-to-vec
        (map #(update % :facts (comp json/parse-string str)))
        (map #(select-keys % cols))))
+
+(defn query-facts
+        [& cols]
+        (->> (str "select certname,"
+                  "       environment,"
+                  "       value #>> '{}' as value,"
+                  "       key as name,"
+                  "       producer_timestamp"
+                  "  from (select certname,"
+                  "               producer_timestamp,"
+                  "               environment_id,"
+                  "               (jsonb_each((stable||volatile))).*"
+                  "          from factsets) fs"
+                  ;; Q: is this wrong?  fs.key outside subselect?
+                  "    left join environments env on fs.environment_id = env.id"
+                  "    order by fs.key")
+             query-to-vec
+             (map #(select-keys % cols))))
 
 (let [certname  "foo.example.com"
       facts     {:certname certname

          (is (= [{:certname certname
                   :facts {"x" "24", "y" "25", "z" "26"}}]
                 (query-factsets :certname :facts)))
          #_(is (= (query-to-vec
                  "SELECT fp.path as name,
                          COALESCE(fv.value_string,
                                   cast(fv.value_integer as text),
                                   cast(fv.value_boolean as text),
                                   cast(fv.value_float as text),
                                   '') as value,
                          fs.certname
                   FROM factsets fs
                     INNER JOIN facts as f on fs.id = f.factset_id
                     INNER JOIN fact_values as fv on f.fact_value_id = fv.id
                     INNER JOIN fact_paths as fp on f.fact_path_id = fp.id
                   WHERE fp.depth = 0
                   ORDER BY name ASC")
                 [{:certname certname :name "x" :value "24"}
                  {:certname certname :name "y" :value "25"}
                  {:certname certname :name "z" :value "26"}]))


      (is (= [{:certname certname
               :facts {"a" "1", "b" "2", "c" "3"}
               :producer_timestamp producer-time
               :environment "DEV"}]
             (query-factsets :certname :facts :producer_timestamp :environment)))
      #_(is (= (query-to-vec (str "select certname,"
                                  "       environment,"
                                  "       value,"
                                  "       key as name,"
                                  "       producer_timestamp"
                                  "  from (select certname,"
                                  "               producer_timestamp,"
                                  "               environment_id,"
                                  "               (jsonb_each((stable||volatile))).*"
                                  "          from factsets) fs"
                                  "            left join environments env on fs.environment_id = env.id"
                                  "          order by fs.key"))
             [{:certname certname :name "a" :value "1" :producer_timestamp producer-time :environment "DEV"}
              {:certname certname :name "b" :value "2" :producer_timestamp producer-time :environment "DEV"}
              {:certname certname :name "c" :value "3" :producer_timestamp producer-time :environment "DEV"}]))
