Create custom db table and fields from plugin

Hi everyone, I’m working on developing a new plugin and I need to store some values in the database but I do not want to modify the discourse base system in any fashion. I’ve read through the beginners guide on plugin development and I did not see anything addressing this, essentially what i need is a table that has two fields that reference a single user that links another user or multiple users.

database → plugin_users → column 1 would be user_id and column 2 could be a comma separated list of user_ids.

Ideally I would also be able to query this data to pull those values from an XHR request, as well as set those values via an xhr request, is this possible with the current discourse plugin model and if so how would one go about integrating this?

1 Like

You might want to have a look at the custom fields (which are available for groups, users, categories, topics and posts) or the plugin store :wink:

1 Like

Is there any supporting documentation on how to make use of these? For example is the custom field helper exposed through the api/ endpoint?

Assuming I create a method within my plugin.rb file to set these values programmatically how could I access them through an xhr request?

Is there a way to expose internal plugin methods to the outside world?

For example something like

//plugin.rb file contents
def update_fields
//Save fields
end

//plugin.js contents

            Discourse.ajax("plugin#update_fields", {
              type: 'POST',
              dataType: 'json',
              data: {#}
            }).then(function(json) { //On success
            
             }, function(json) { //On error

            });

Managed to expose these routes by mimicking an existing plugin and how they went about doing it, I’m not sure if there is documentation out there for these things but it doesn’t seem very easy to find from google searches etc.

Are there any plans in the near future to improve the documentation for these things? Thanks for pointing me in the right general direction, though those files provide little context on how to make use of those methods.

1 Like

hi! I am in exact neer of what you described in your post. I see that you were able to accomplish that by mimicking sone exisiting plugin? May I ask exactly which plugin did you copy? Can you please give some pointers?
What I am trying to accomplish is to add some extra fields to the topics table in DB and then get/put data in those fields with REST api.

@deviousmachin3 I find myself in a similar situation and I’d really appreciate it if you could share your solution to the problem you faced.

Thanks in advance! :blush:

This should answer the questions for folks here

2 Likes

I think its better to do migrations like the polls plugin. I created a migration and model generator for plugins: GitHub - spirobel/discourse at plugin_model_and_migrations It puts the migration files at the same spot like in the polls plugin.