Clever TaglineC
Convex Community14mo ago
21 replies
Clever Tagline

Request: Is a document is referenced anywhere else?

I'm building an app for work, and in the front-end there are lots of things that can be added and deleted by the user. In a few cases, I'd like to prevent a given item from being deleted if it's referenced by any other document in any other table.

For example, I'm going to be tracking regions where our company has offices, so I made a regions table. There are many things that will reference a specific region—staff, contacts, deals, etc.—and I'd like to prevent a region from being removed if it's referenced by any of those things.

What I'd love to have is something like a documentHasReferences helper function:

* Its only argument would be the ID of a document ("Target Document")
* First the function would determine the table containing that document ("Target Table")
* Next it would scour all tables in the project, checking their schemas to find fields that store IDs for the Target Table
* If any such fields are found on a table, it would query the table to find any reference to the Target Document in those fields
* If a single reference is found on any table, it would return
true
otherwise it would return
false


I can obviously create a query that does something similar, but as I continue building the app I'll need to ensure that this query is updated as other tables are added or removed that interact with this region table. Other cases might also come up where something similar would be useful, which would require additional custom queries.

The helper function I'm dreaming about wouldn't need any such updates, as it would operate using internal access to a project's tables, their schemas, etc. One function could be used in any number of use cases with any document ID.

Is such a function even doable on the Convex end? If not a function, would this be possible as a component?
Was this page helpful?