Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make raw sql query on strapi v4 typescript

in strapi v3 you could make a raw sql query the following way:

const data = await strapi.db.connection.raw(`SELECT * from table`);

However, in strapi v4 with typescript enabled, the same line gives the following error:

Property 'connection' does not exist on type 'Database'.

I've searched the documentation but I could not find how to make a custom sql query in strapi 4 with typescript enabled

like image 709
Mauro Zadunaisky Avatar asked Oct 23 '25 07:10

Mauro Zadunaisky


1 Answers

you can do that exactly the same:

//@ts-ignore
let { rows } = await strapi.db.connection.raw(
    `select id from posts where published_at IS NOT null order by random() limit ${count};`
);

sadly the strapi.db.connection does yet not support types, for some reason cannot find this issue, but this prop is Knex prop i'm not entirely sure but you an somehow cast it to Knex

like image 114
antokhio Avatar answered Oct 24 '25 21:10

antokhio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!