Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Improved error messaging for conflicting link defs in schema.ts #296

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client/sandbox/cli-nodejs/instant.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const graph = i.graph(
},
postsTags: {
forward: {
on: "posts",
on: "tags",
has: "many",
label: "tags",
label: "posts",
},
reverse: {
on: "tags",
on: "posts",
has: "many",
label: "posts",
label: "tags",
},
},
},
Expand Down
38 changes: 28 additions & 10 deletions server/src/instant/model/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,39 @@
". "
"Check your schema file for duplicate link definitions."))

(defn backwards-link-message [etype label]
(str "Conflicting link found for attribute: "
etype
"->"
label
". "
"It's possible that you already have a link with the same label names, but in the reverse direction. "
"We cannot automatically swap the direction of the link, so you will need to do this manually. "
"Check your schema in https://www.instantdb.com/dash for a link with the same label names. "
"If you find one, you can either delete the existing link in the dashboard, or swap the `forward` and `reverse` parameters for this link in your schema file."))

(defn assert-unique-idents! [current-attrs steps]
(let [current-ident-names (->> current-attrs
(mapcat attr-ident-names)
(map vec))
ident-names (->>
steps
(mapcat (fn [[op data]]
(when (= op :add-attr)
(attr-ident-names data)))))
dups (->> (concat current-ident-names ident-names)
(let [current-fwd-ident-names (->> current-attrs
(map attr-model/fwd-ident-name)
(map vec))
current-all-ident-names (->> current-attrs
(mapcat attr-ident-names)
(map vec))
new-ident-names (->>
steps
(mapcat (fn [[op data]]
(when (= op :add-attr)
(attr-ident-names data)))))
dups (->> (concat current-all-ident-names new-ident-names)
(frequencies)
(filter (fn [[_ freq]] (> freq 1))))
errors (map (fn [[[etype label]]]
{:in [:schema]
:message (dup-message etype label)}) dups)]
:message (if
(some #(= % [etype label]) current-fwd-ident-names)
(dup-message etype label)
(backwards-link-message etype label))})
dups)]
(ex/assert-valid! :schema
:steps
errors)))
Expand Down
Loading