diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljc index 725ed46f31c0a2fc6352c703ce91f25609eaf724..74c58448a56da0e337adf52d1d6d8af7bf4659d4 100644 --- a/src/metabase/lib/core.cljc +++ b/src/metabase/lib/core.cljc @@ -142,6 +142,7 @@ upper lower] [lib.field + field-id fields with-fields fieldable-columns] diff --git a/src/metabase/lib/field.cljc b/src/metabase/lib/field.cljc index 1147e4ad87b58ddc1d55253f8595087548123ce5..a9c4e5b86e9d595655eed6254ae6d30b67bb8c29 100644 --- a/src/metabase/lib/field.cljc +++ b/src/metabase/lib/field.cljc @@ -529,3 +529,8 @@ (assoc col :selected? true)) visible-columns) (lib.equality/mark-selected-columns visible-columns selected-fields))))) + +(mu/defn field-id :- [:maybe ::lib.schema.common/int-greater-than-or-equal-to-zero] + "Find the field id for something or nil." + [field-metadata :- lib.metadata/ColumnMetadata] + (:id field-metadata)) diff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljs index ca91fe8d89761e4cdac88f41aa129d4904bda447..8951f93b74509e47308aefadf6ecbe24a8dd3d9a 100644 --- a/src/metabase/lib/js.cljs +++ b/src/metabase/lib/js.cljs @@ -124,6 +124,11 @@ (update :table update-keys u/->camelCaseEn) (clj->js :keyword-fn u/qualified-name))) +(defn ^:export field-id + "Find the field id for something or nil." + [field-metadata] + (lib.core/field-id field-metadata)) + (defn ^:export order-by-clause "Create an order-by clause independently of a query, e.g. for `replace` or whatever." ([orderable] diff --git a/test/metabase/lib/field_test.cljc b/test/metabase/lib/field_test.cljc index 9fd9da2b90bcdc74a1d8a24b3ca07e41c9e97610..9ca7d6233b8f88476ce9aea1fcacd9cd8a52ee6a 100644 --- a/test/metabase/lib/field_test.cljc +++ b/test/metabase/lib/field_test.cljc @@ -735,3 +735,15 @@ query (lib/with-binning (m/find-first (comp #{"PRICE"} :name) breakoutables) (first (lib.binning/numeric-binning-strategies))))))))) + +(deftest ^:parallel field-id-test + (let [id-meta (meta/field-metadata :venues :id) + query (-> (lib/query meta/metadata-provider (meta/table-metadata :venues)) + (lib/with-fields [id-meta]) + (lib/expression "foo" (lib/+ id-meta 10))) + venues-id (:id id-meta) + cols (lib/orderable-columns query)] + (is (= venues-id (lib/field-id id-meta))) + (is (=? {"foo" nil + "ID" venues-id} + (into {} (map (juxt :lib/desired-column-alias lib/field-id)) cols)))))