Hello to the wonderful Text Blaze community and thank you for the amazing tool!!
This is my first show&tell but I do intent to make it a series as I think there is a rabbit hole to explore here (hope you'll see it too
)
If my post lacks the proper forms/code or manners, pls let me know as I'm also discovering the Blaze community text editor.
TL;DR, the last script is the good one (you'll need your table's ID)
Thanking the TB team some more
First of all, I wanna take this chance to thanks the TB team! For their amazing product that keeps evolving and honnest pricing strategy over the years. This chrome extension (and the desktop apps) are truly life changers !!
Backstory and use case at hand
Here goes,
I've been using and abusing TB for a few years now, binging documentation and pondering the endless possibilities but then took a bit of back step. So I became rusty.
But when an friend entrepreneur exposed me his issue, the use case was SCREAMING text blaze/data blaze so here I am.
My entrepreneur friend sells products. A line of, let's say, 6 variations within same category. So they all have same 'properties' but different features/values for each property. Diff prices, lenght, volume, color, etc. Quite close, easy to mix up, even for him.
So he finds himself talking about his product via mail, forms (when filing for stuff, listings, etc), texts everyday all-day to a range of interlocutors...constantly repeating the features of each, the distinctions, bulk prices, etc.
It can get confusing, at times, and then mistakes happen. Which takes even more time to correct, apologize, etc.
Dude clearly needed a proper table (for his products line up and features) and a bunch of snippets to talk about them.
Making the snippet was quite straightforward. It's dynamic so if the data change or he add a new product, the updated data set will immediately pass thru to the snippet. But then I realized: what if he wanted to add or substract a column to his specs sheet ? Modify their names, etc ?
Also the {dbselect} command ("Read from Table") force you to pick which field you want to load upon calling it. And then, to chose which variables go where in the snippet.
Yes I could place them in a form menu and let him pick but something more powerful and versatile emerged.
Because he has multiple scenario and might need this or that data point, or several of them. Maybe he wants the color of this one product...or the price of two different options. Or the name, size, description of his three products that are blue.
Don't know and can't have the man calling me every time a new use comes up.
I remembered giving a name to your {dbselect} turn its results into a list you can pick from. using the list["key"] syntax
And using the transitiveness of variables, as explained here Text Blaze | Formula Reference, I realize you can use a namesake like list[variable] (notice the lack of quotation marks). And pick it's content later.
I try this capability on a {dbselect} and it worked:
{test="origin"}
{dbselect: SELECT product_name, Color, Price, Origin, Lenght, motto FROM Mock_Data; space=id; multiple=no; menu=yes; name=db}
{=db[test]}
{note}The final goal being to create a single script, which we want to be as simple, concise and elegant as possible, that can invoke any data point or list from any Data Blaze array...it's essential to validate the flexibility of the commands that invoke the array variables. This is done here.
NB: the variables loaded by the snippet are associated with lower-case “keys” that are case-sensitive, whatever the initial and actual spelling of the header. A lower case function is required{endnote}
So I use the API call {urlload} command to get these fields name, put them in a list he can pick from:
(you'll probably need to copy and save this one on your dashboard for the {urlload} command to work properly and so you can use your own table ID for it to work properly. Obviously, you can use the data set of the previous snippet)
{formtext: name=Table_id; default=22Char ID of your DB table; cols=22}
The different toggle commands are here in order to pace the snippets script, so it triggers when YOU'RE ready, avoiding errors and weird output in the process.
{formtoggle: name=Explanations; default=no}{note}First you need to fetch the field's name of your table using the Data Blaze api capabilities. You access them from your DB dashboard, inside the desired space
And then get your table ID through your personal API documentation that DB generate for you (very useful feature)
Soon enough, you'll be able to make a dropdown menu, or even a DB table, of your table's ID and names so you can fetch and use their content in a breeze{endnote}{endformtoggle}
{formtoggle: name=Ready?; default=no}
{urlload: https://data-api.blaze.today/api/database/fields/table/{=table_id}/; done=(res) -> ["Data":(res)]}
Here is the whole JSON object we get:
{=Data}
Now we make it into a list (easier to manipulate with Text Blaze snippets).
Let's look at the first item of this list, the properties of our first field, especially its name:
{Resulta=lower(fromjson({=Data}))}
{=resulta[1]}
{=resulta[1].name}
Then we parse all the item of this JSON-list, to extract the name of the field and string them into a list (thru list manipulation), put that list in a variable and finally shove that variable in a form menu so you can pick one.
{list=[n.name for n in resulta]}
{=list}
{formtoggle: name=Show final form menu; default=no}
{formmenu: values={=list}; name=ch}{endformtoggle}{endformtoggle}
So now, we're able to take off the training wheels and mush everything together in a dense package (so that our output doesn't line-break for no reason), our final script to fetch any data from any table:
(again, {urlload} command toward DB api so you'll probably need to copy this into your dashboard to properly try it)
{note}{formtext: name=Table_id; default=22Char ID of your DB table; cols=22}{endnote}{formtoggle: name=Ready?; default=no}{urlload: https://data-api.blaze.today/api/database/fields/table/{=table_id}/; done=(res) -> ["Data":(res)]}{Resulta=lower(fromjson({=Data}))}{list=[n.name for n in resulta]}{dbselect: SELECT product_name, Color, Price, Origin, Lenght, motto FROM Mock_Data; space=id; multiple=no; menu=yes; name=db}{formtoggle: name=Show final form menu; default=no}{note}{formmenu: values={=list}; name=ch; multiple=no}{endnote}{=db[ch]; trim=yes}{endformtoggle}{endformtoggle}
This post is already quite long so I'll stop there.
Next time I'll take advantage of the fact that both {formmenu} and {dbselect} can choosingly output one or multiple values so the same script allow to pick:
- One data from one item/row/record
- Many data from one item
- One data from many item
- Or many data from many items (list of lists) that we'll tie together using this Text Blaze | Interacting with lists
Thanks to anyone that read so far !
Let me know of possible development and expansion I might have missed
Bye !