Generating dynamic raw query help

Hello, we are trying to create a function utility to call RPCs, we want to dynamically generate the following SQL query:
SELECT my_function(arg1 => 'some_value', arg2 => 123);
SELECT my_function(arg1 => 'some_value', arg2 => 123);
We are trying the following
input = Object.entries(payload).map(([key, value]) => {
return sql`${sql.raw(key)} => ${sql.val(value)}`;
});

const x = sql`SELECT ${sql.raw(name)}(${sql.raw(input.join(','))})`;
input = Object.entries(payload).map(([key, value]) => {
return sql`${sql.raw(key)} => ${sql.val(value)}`;
});

const x = sql`SELECT ${sql.raw(name)}(${sql.raw(input.join(','))})`;
But the outputs have been things like: SELECT my_function($1, $2) SELECT my_function([object Object],[object Object]) Is there a Kysely helper function to generate the arg1 => 'some_value', arg2 => 123 section of my query, or how should I go about it? Thanks for any help!
2 Replies
bombillazo
bombillazoOP4d ago
I think the issue is in using multiple sql instances, but om not sure how to then to insert the dynamic section that depends on the payload values
koskimas
koskimas21h ago
You're joining a bunch of objects into a string here.
input.join(',')
input.join(',')
Try this instead
const x = sql`SELECT ${sql.raw(name)}(${sql.join(input)})`;
const x = sql`SELECT ${sql.raw(name)}(${sql.join(input)})`;

Did you find this page helpful?