Skip to content
Snippets Groups Projects
Unverified Commit 7daf9d6a authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Fix `required-native-extras` for Cljs and add TypeScript wrappers (#33552)

* Fix `required-native-extras` for Cljs and add TypeScript wrappers

* Oops add missing TS function impls
parent 74be152f
Branches
Tags
No related merge requests found
import * as ML from "cljs/metabase.lib.js";
import type { DatabaseId } from "metabase-types/api";
import type { MetadataProvider, Query } from "./types";
/**
* Returns the extra keys that are required for this database's native queries, for example `:collection` name is
* needed for MongoDB queries.
*/
export function requiredNativeExtras(
databaseId: DatabaseId,
metadata: MetadataProvider,
): string[] {
return ML.required_native_extras(databaseId, metadata);
}
type NativeExtras = {
collection?: string | null;
};
/**
* Returns the extra keys for native queries associated with this query.
*/
export function nativeExtras(query: Query): NativeExtras | null {
return ML.native_extras(query);
}
/**
* Updates the extras required for the db to run this query. The first stage must be a native type. Will ignore extras
* not in `required-native-extras`.
*/
export function withNativeExtras(
query: Query,
nativeExtras: NativeExtras | null,
): Query {
return ML.with_native_extras(query, nativeExtras);
}
......@@ -14,6 +14,7 @@ export * from "./filter";
export * from "./join";
export * from "./limit";
export * from "./metadata";
export * from "./native";
export * from "./segments";
export * from "./metrics";
export * from "./order_by";
......
......@@ -657,9 +657,12 @@
(lib.core/TemplateTags-> (lib.core/template-tags a-query)))
(defn ^:export required-native-extras
"Returns whether the extra keys required by the database."
"Returns the extra keys that are required for this database's native queries, for example `:collection` name is
needed for MongoDB queries."
[database-id metadata]
(to-array (lib.core/required-native-extras (metadataProvider database-id metadata))))
(to-array
(map u/qualified-name
(lib.core/required-native-extras (metadataProvider database-id metadata)))))
(defn ^:export with-different-database
"Changes the database for this query. The first stage must be a native type.
......@@ -670,8 +673,8 @@
(lib.core/with-different-database a-query (metadataProvider database-id metadata) (js->clj native-extras :keywordize-keys true))))
(defn ^:export with-native-extras
"Updates the extras required for the db to run this query.
The first stage must be a native type. Will ignore extras not in `required-native-extras`"
"Updates the extras required for the db to run this query. The first stage must be a native type. Will ignore extras
not in `required-native-extras`."
[a-query native-extras]
(lib.core/with-native-extras a-query (js->clj native-extras :keywordize-keys true)))
......
......@@ -148,7 +148,8 @@
[:collection {:optional true} ::common/non-blank-string]])
(mu/defn required-native-extras :- set?
"Returns the extra keys that are required for this database's native queries."
"Returns the extra keys that are required for this database's native queries, for example `:collection` name is
needed for MongoDB queries."
[metadata-provider :- lib.metadata/MetadataProviderable]
(let [db (lib.metadata/database metadata-provider)]
(cond-> #{}
......
......@@ -2,6 +2,7 @@
(:require
[clojure.test :refer [deftest is testing]]
[metabase.lib.js :as lib.js]
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]))
(deftest ^:parallel query=-test
......@@ -60,8 +61,7 @@
basic-query #js {"type" "query"
"query" #js {"joins" #js [join]}}
classy-query #js {"type" "query"
"query" #js {"joins" #js [join-class]}}
]
"query" #js {"joins" #js [join-class]}}]
(is (not= join join-class))
(is (not= (js->clj join) (js->clj join-class)))
(is (lib.js/query= basic-query classy-query)))))
......@@ -74,3 +74,14 @@
{:lib/type :option/join.strategy, :strategy :right-join}
{:lib/type :option/join.strategy, :strategy :inner-join}]
(vec strategies))))))
(deftest ^:parallel required-native-extras-test
(let [db (update meta/database :features conj :native-requires-specified-collection)
metadata-provider (lib.tu/mock-metadata-provider {:database db})
extras (lib.js/required-native-extras (:id db) metadata-provider)]
;; apparently #js ["collection"] is not equal to #js ["collection"]
(is (= js/Array
(type extras))
"should be a JS array")
(is (= ["collection"]
(js->clj extras)))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment