Closed ThomasAribart closed 1 year ago
Hey π
You can brew a dialect with existing built-in components and replace the driver with built-in DummyDriver
.
This is documented in the browser example.
Now just use .compile()
with any of the query builders to get CompiledQuery
instances that contain sql
and parameters
fields.
Hi @igalklebanov !
Thanks for the response, I missed the DummyDriver
class :)
I had seen the compile
method but it still requires to hydrate the parameters in the sql
query. Is there a way to not have to do that ? Nitpick but it's always nice to reduce verbosity and custom code.
Still, I'll give it a try ! Thanks again π
Normally you should never "hydrate" parameters to the SQL string. That's a sure way to get your system pwned. For that reason, there's no built in way to do it.
I see, high risk of SQL injection there.
Sadly there is no Parameters
attribute in the S3Select
interface, so I'll have to create a dangerouslyHydrateParameters
helper to do that π€·ββοΈ Many thanks !
I see, high risk of SQL injection there.
Sadly there is no
Parameters
attribute in theS3Select
interface, so I'll have to create adangerouslyHydrateParameters
helper to do that man_shrugging Many thanks !
Can you share your helper?
Sadly there is no Parameters attribute in the S3Select interface
Yeah, I understand that in this case it's necessary. I was just explaining why a helper like that won't be added to Kysely.
Can you share your helper?
const parameters = ['foo', 'bar']
const sql = 'select $1 from $2'
const hydrated = sql.replace(/(\$\d+)/g, (arg) => parameters[parseInt(arg.slice(1)) - 1])
In case of mysql placeholders:
const parameters = ['foo', 'bar']
const sql = 'select ? from ?'
let i = 0;
const hydrated = sql.replace(/(\?)/g, () => parameters[i++])
Depending on your query, you also need to add some '
characters around strings
@wirekang I went for this:
const dangerouslyHydrateSQLParameters = (
sql: string,
parameters: readonly unknown[],
): string => {
for (const parameter of parameters) {
sql = sql.replace('?', `'${String(parameter)}'`);
}
return sql;
};
@ThomasAribart Got inspired and implemented this based on your blog post. π€
@igalklebanov Awesome ! π€©
Hello and thanks for this wonderful lib !
On my project, I use AWS S3 Select feature on CSVs stored in S3. It allows executing a SQL-like query directly on those CSVs:
I would really like using
kysely
to build such queries but for the moment I cannot:Kysely
class constructor requires adialect
whereas I don't really need one (other solution is to build an adapter forS3Select
but that's a whole lotta work)Would it be possible to make
dialect
optional in the constructor (and throw an error if none is found when needed) and expose acompileQuery
method on thedb
to retrieve the compiled query ?