Best convention for custom plugin DB data for 2021?

I’m building a site that is a rental real estate community, with the core functionality orienting around city-specific discussion. There will be user data that specifies cities they’re knowledgeable about and what their relationship is with those cities (currently live there, used to live there, etc) and users will also subscribe to cities they want to invest in but are not knowledgable about. The cities will all be categories, with a custom field to mark their geocoded data so that users can browse cities on a map.

Where I’m a bit torn is how I should structure this from a DB efficiency standpoint. When you’re on a city page, I’ll be showing a feed that shows the “member experts” of that city. If I have to query all users and map through their custom fields and “expert cities” custom field hash every time I render a category page, I feel like that would be pretty dang slow, especially as the amount of users and cities grows.

If I were to build this in my own rails app, this would be easily solved with some join tables and model hasmanythrough relationships or something. What I’m wondering here is what the recommended approach is for a plugin that needs a join table. It seems that custom migrations and tables are discouraged and that it’s generally better to either use custom fields or PluginStore – I haven’t been able to find any real documentation on the PluginStore stuff but am currently in the process of digging.

Just thought it’d be wise for me to ask about the “official Discourse-recommended” approach before I go too deep in any direction.

Thanks :slight_smile:
Zach

3 Likes

That may have been the case in early days, but nowadays it’s quite the opposite. We even migrated our own plugins from custom fields to their own tables were it made sense, like the poll plugin and the voting plugin.

Plugins with rich data models should create their own models, tables, relationships and migrations. It’s much maintainable and will provide great performance. Just watch for those N+1 :wink:

5 Likes

Ah ok, fantastic. Glad I asked then. :wink: Is there a recommended way to modify core models from within my plugin?

I would like to add some custom defs and relationships, e.g. a hasmanythrough with my new tables, and I’m not really sure how to best do that, other than calling functions on my new model where I have to pass in the IDs of the category and user, etc.

I found this post but it looks like he never got an answer.

2 Likes

There are some examples in the Poll plugin I mentioned above:

https://github.com/discourse/discourse/blob/d56b2e85aacb9744649562c5b8a8cba22f186c6c/plugins/poll/plugin.rb#L484

2 Likes

Awesome. I’m so excited that this is possible! Thanks.

3 Likes