I’m creating a website where user can add some custom tags to their posts.
I’m using “Multiselect Dropdown” plugin which is great.
When user hits “Create post” button i save “list of tags” (generated via Multiselect Dropdown) to this post - so post contains list of tags.
The problem is - i want to separate this list to elements and add each tag to “Tags” database.
So for example, user is making his/her post with tags “beauty” and “photo”. When they publish it, datatype post hold list “beauty, photo” and i’m okay with it. Now, i want to add those things to separate database called “tags” so i will have two separate entries there “beauty” and “photo”. How can i do that?
I’m assuming your dropdown is displaying ‘texts’ and you’re allowing your users to add new items to the dropdown by typing them in if they don’t already exist in your database?
And you want to create new Tag entries for these new items?
If that’s the case there are a couple of options…
1 option is to create a backend workflow to create a new tag and then add it to the Post. Then in the browser workflow trigger that workflow using ‘schedule an API workflow on a list’, with the list being the list of texts that were added to the dropdown (you’ll need to filter them so as to only include those that don’t already exist in the database).
Another option is to use the list shifter plugin to iterate on the list, so you can do it directly in your browser workflow.
In either case, once the ‘Tags’ are created you can add them to the post (assuming the Post field for list of tags is of type Tag).
added to Tags database separately with this post unique_id
You mean you want to create a separate ‘Tag’ entry for each tag/post (i.e. an entry for tag1/post1, another entry for tag1/post2 etc.) with a 1-to-1 relationship between tags and posts?
Or just one entry per tag? (i.e. 1 entry for tag1, another for tag2 etc.)?
Personally I’d suggest having a datatype for your ‘Tags’ and then, depending on your estimated number of tags and posts, either saving tags as a list on the Post (assuming a post will only have a handful of tags), or even creating a separate connecting datatype of tag/post to connect tags to posts, rather than having a list of posts on your tag datatype or a single post entry saved on the tag.
In any case, the easiest way to create new tags is to use a backend workflow and run it on a list.
Firstly, make sure your ‘Tag’ datatype has a text field for ‘tag name’ or something similar, and your ‘Post’ datatype has a list field of ‘Tags’.
Create a backend workflow that can accept 2 parameters: One, a text, for the name of the tag, and another, a Post, for the post you want to assign the tag to.
Create 2 actions on this workflow: 1 to create a new tag (set the name of the tag to be the text parameter), and 1 to make changes to the Post (the Post parameter) by adding the result of step 1 (the new tag) to the list of tags on the post.
In your browser workflow, when ‘Create post button’ is clicked you’ll need a few actions.
Create a new post, and use ‘Set List’ to add existing tags to the post by doing a search for tags who’s name ‘is in’ the multidropdown’s value. This should add tags that already exist to the post, and for those that don’t nothing will happen.
Add another action to schedule an API workflow on a list - the workflow in question being the one you created to create new tags, and the ‘list’ in question to be the list of text from the dropdown, filtered to only include items that don’t match existing tag names from the database.
Schedule the workflow for the current date/time, with a 5 second interval between items, and send ‘This text’ for the tag name parameter, and the Post you just created (the result of step 1) as the ‘Post’
In that case you can’t run backend workflows until you upgrade, but you can still set it up (it just won’t work in run mode on a free plan).
You need to turn on Backend Workflows in your API tab of your app settings before you can set up backend workflows.
Alternatively, if you need to do it without backend workflows on a free plan, you’ll have to look into the ‘List Shifter’ plugin, which will let you do something very similar in your browser workflow.
But to add a tag that already exists there’s no need to use a backend workflow.
The first step of the workflow on the page (in the post above) should take care of that.
When you create the new post (in step 1) you’re setting the tags list to contain the tags from the multidropdown that already exist.
Then sending those that don’t to the backend worfklow to be added to the database.
If you want to add a list field of Posts to the ‘Tags’ datatype as well, and add the new post to the tags, then just follow the same concept…
For tags that exist already you can do it in the browser workflow, in another step, either in between step 1 and 2, or after the current step 2. Use ‘make changes to list’, with the list being the results of step 1’s tags, and add the post to the tag’s Post field.
For tags that don’t exist, do it in the backend workflow - just add the Post to the tags when you create them.
It’s worth noting though, that there’s probably no particular reason to store the posts on a list field of the tags (as the tags are already stored on the post data entries) and, if you end up with a large number of posts in your database it could slow down performance when searching for, or loading tags. But, it depends on your app and how you plan to access data, and how many tags and posts you expect to get.
By the list of threads count i want to measure tags popularity. Also, i would like to allow user to click on some tag and see all posts related to this tag. I thought that adding list of posts to Tag database is the only option to do that.
Another way would be to have a ‘post count’ field on the tag, instead of a list of posts, and just add ‘1’ to the number every time a new post is created.
That would keep the data much lighter, and would be much faster than doing a count every time you want to see the number of posts (especially if you have a high number of posts). But you can also do it your way, it just might become very data heavy in the long term, which could slow things down.
As for seeing all posts that contain a given tag, again one way is using a list of posts on the tag but, if you end up with hundreds (or thousands) of posts for each tag it might not be a great idea doing it that way.
Another way would just be to do a search for posts that contain the given tag.
It really depends on the numbers - if each tag will only have a small number of posts then it’s probably easier and faster to store them on a list field on the tag. If you anticipating having hundreds or thousands, it’s probably not very efficient when it comes to loading tags.
Deciding on the most efficient data structure for your app to keep performance high can be tricky, and getting it wrong can cause performance issues, and headaches in the long term if you need to change things around.
What the best data structure for a given app is will depend on a large number of factors, so it’s worth giving a lot of thought to it.
Check out Bubble’s own manual on the best practices of connecting datatypes for more detailed explanation.