Something like toKeyedObject in Server Actions

Hi, i’m a bit stuck in this, please a bit of help:

This works perfectly in the front, but how to replicate it in Server Actions?

<dmx-value id=“var_config” dmx-bind:value="sc_config.data.toKeyedObject(‘config_Name’, ‘config_Value’)>

------And to obtain the value:---------
{{var_config.value.section_cars)}}

I want to use this to restrict certains parts of a page depending of config_Value, the idea is to output a “Set Value” depending of “Database Query” using in the front the option of “Server Side Binding” in a condicion format in node page.

The condition in node page with server side binding will be:

<% if (_('var_params_config_sys==0',locals)) { %>
---content here----
<% } %>

My table is:

id | business_id(int) | config_nNme(varchar) | config_Value(varchar) | onload(enum, yes, no)
1 | 1 | section_cars| 1 | yes
2 | 1 | section_houses| 0 | yes

Thanks

Hi.
Not sure if this formatter is available in SA.
But, you can make use of RunJS custom extension to manupilate JSON objects easily.
If you want to reuse this functionality, I would suggest to create a custom formatter/module extension instead of RunJS.

Thank sid
So, that means that there is not wappler in house functions that make something like that? Current option will be a custom extension.
Working with json fields in database using keys and value to store data sounds great, and maybe wapple functions without coding allow that, but something that I can’t found how to do with json fields is how to update values base on certains keys, example, if the structure is:

config->value1
home->value1, value2
other->value1

How to update the “value2” from key “home”, actually, something that I already asked with no luck

Chears

You post is about getting JSON data, from what I understood.
As for updating JSON data in DB, I am not sure if Wappler’s DB steps support that.
Here’s the doc that I could find about this: PREVIEW: Using JSON Database Fields to Store Multiple Choices
We have done JSON field updates in PostgreSQL using custom query step.

Maybe @Teodor can help if there is any UI option.

I think you might be able to achieve this using GroupBy formatter.

After your query use a set value step (for the code below, I am using a name of sv) and set it to query.groupBy('config_nNme')

On your page, you should then be able to use:

<% if (_('sv.var_params_config_sys[0].config_Value==0',locals)) { %>
---content here----
<% } %>

GroupBy always returns an array (even if only one record is inside) hence the [0] to pull the first record

Thanks to all for your help,

In base of your suggestions and after too much test and error, this is what I did.

Basic example in a table mysql (NOT JSON, easy to update fields from some Backend just like with any other Database Update Action):

id(int) | config_name(varchar) | config_value (varchar)
    1  |  smtp_env       |   dev
    2  |  smtp_host      |   smtp.gmail.com
    3  |  smtp_host_dev  |   maildev
    4  |  smtp_port      |   465
    4  |  smtp_port_dev  |   1025
    5  |  smtp_user      |   myuser_smtp
    6  |  smtp_pass      |   mypass_smtp

With this table the idea is to use the column “config_name” with value “smtp_env”, this can be “dev” or “real”, base on that I configure the Mailer Action in Globals.
So, If “smtp_env” is “dev” I need the values from “smtp_host_dev” and “smtp_port_dev”,
else if “smtp_env” is “real” I need the values from “smtp_host” and “smtp_port”.

Steps (Option 1):

  • A simple query (smtp_db): SELECT config_name, config_value FROM system_config
  • A Set Value: smtp_env = smtp_db[0].config_value (The above should output “dev” or “real” base on the database values, and is [0] because is the first row in table.)
  • Another Set Value (using ternary): smtp_host_db = smtp_env=='dev' ? smtp_db.where('config_name','==','smtp_host_dev') : smtp_db.where('config_name','==','smtp_host)
  • Repeat - output fields both cols (smtp_host_db)–>EXEC–>Set Value = smtp_host = ''
  • Set Value: smtp_host = repeat[0].config_value

Results:

smtp_env = 'env'
smtp_host = 'maildev'

Steps (Option 2)

  • A simple query (smtp_db): SELECT config_name, config_value FROM system_config
  • A Set Value: smtp_env = smtp_db[0].config_value (The above should output “dev” or “real” base on the database values, and is [0] because is the first row in table.)
  • Set Value: db_host_dev = smtp_db.where('config_name', '==', 'smtp_host_dev')
  • Set Value: db_host_real = smtp_db.where('config_name', '==', 'smtp_host')
  • Set Value: host_dev = db_host_dev[0].config_value
  • Set Value: host_real = db_host_real[0].config_value
  • Set Value (ternary): db_host = smtp_env == ‘dev’ ? host_dev : host_real

Results:

smtp_env = 'env'
db_host= 'maildev'

Now, in both Options 1 or 2 if in the database the column of “config_value” of “smtp_env” change from “dev” to “real”, both results will be:
smtp_env = 'real'
db_host= 'smtp.gmail.com'

The Option 3 is with custom query using SELECT JSON_VALUE . The only inconvennient for me with this option is the parcial update from some Backend, update values bases on keys, everything will be with custom code in querys, updates, etc. (Would be great if Wappler Team implement in house options for this scenarios)

All this needs to be in Global, otherwise in Mailer not be able to choose from Data Binding.

This same structure works for conditions using server side bindings, e.g., hide content base on admin conditions in any scenario.

QUESTION: Is All this sceneario OK? I really do not know which “Option” 1 or 2 or 3 is more efficcient and secure in production, keeping in mind that this is loading in Globals in a NodeJS project. Someone can guide me with that please? Any help is totally welcome

To be honest, I do not quite understand what you are are doing or trying to achieve.
If you want to manage different values on client and server sides based on DEV and PROD, you can simply use ENV variables.
This is assuming that you have separate servers for prod and dev, so you can set different ENV values.

Oh, you right, for this scenario like mail credentials is not the best example, using .env is better:

But I still considering as an option if credentials will be in database for an user configuration from Backend in a SaaS model web.

But again, If the case where not email credentiales, would be this an option for this: is good or bad idea, secure or insecure.

In case of any credentials in the DB, I would rather look into the option of using libraries outside of Wappler’s.
For eg: for emails in a SaaS app, with credentials in DB, I would rather used SendGrid like email service’s API, over built in Wappler option.