Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add uuid and enum support to malli schema generator #1

Merged
1 commit merged into from
Apr 2, 2023

Conversation

ghost
Copy link

@ghost ghost commented Mar 30, 2023

With this patch malli-schema will generate correct entries for uuid columns and produce correct enum definitions for custom types under the condition that you have to pass in your type definitions.

It would be great if we could have the xray extract all the present type definitions for the database like it does with the table information. I tried that by adding something like:

(defn get-types
  [{:keys [metadata dbadapter catalog]}]
  (binding [njdf/*datafy-failure* :omit]
    (-> metadata
        (.getSuperTypes catalog (:schema-pattern dbadapter) nil)
        datafy-result-set)))

and have the results saved within the xray besides :tables as :types, but when invoked getSuperTypes says it is not implemented. Might have used the wrong Java function, but that one looked to be the best fit.

So for the enums you would have to do something like this:

(def malli-schema-defs
   (dbx/malli-schema (merge xray {:enums {"fm.contact_status_type"
                                          db-schema/FMContactStatusType}})))

Interface could be prettier, but for a first version it does the trick.

@ghost ghost requested a review from jamiwissman March 30, 2023 23:12
Copy link
Author

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇🏻

@@ -9,7 +10,8 @@
:clob 'string?
:text 'string?
:varchar 'string?
:timestamp 'inst?})
:timestamp 'inst?
:uuid 'uuid?})
Copy link
Author

@ghost ghost Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one-liner for uuid support

(and (= :integer column-type) primary-key?)
(:integer-pk column-types)

;; :enum type definition
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main change for enum support:

I'm assuming here that types will always be identifyable by the pattern [schema].[name] which might not be 100% accurate? I think it will for for our case in PSQL but I'm not sure about other databases. Not sure if we can really upstream this later.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this doesn't seem like the best way to identify enums, but I currently don't have any better ideas either. Maybe add a comment here to make it clear that we are saying any column type with a . is an enum.

(and (= :integer column-type) primary-key?)
(:integer-pk column-types)

;; :enum type definition

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this doesn't seem like the best way to identify enums, but I currently don't have any better ideas either. Maybe add a comment here to make it clear that we are saying any column type with a . is an enum.

@ghost ghost merged commit 3c1a411 into main Apr 2, 2023
@ghost ghost deleted the feature/ixffxi/improve-malli-schema branch April 2, 2023 23:23
ghost pushed a commit that referenced this pull request Apr 2, 2023
add a generator for datapotato schema
This pull request was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant