Create custom db table and fields from plugin

plugins

#1

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?


New columns in directory (/users)
(Régis Hanol) #2

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:


#3

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


#4

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

            });

#5

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.


(Umer Mirza) #6

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.