Created my first test custom query on Strapi! OK seems simple enough... next: a custom query for production, for real

Jason Leow Author

@scriptifyjs I'm using Postgres for db instead, so I should look at this instead ? https://www.postgresql.org/docs/9.5/tutorial-agg.html

0 Likes

I've used to build Node.js REST and GQL for quite some time now, and Strapi offers a very good tradeoff between flexibility and out-of-the box functionalities, perfect for smaller projects. Only heard good things about Rails, never tried it myself, I grew up in the Node.js land 🍭

0 Likes

If you use the package I think you actually don't need the custom query, as you can use Strapi's abstraction (.search) directly. But I think opting for SQL would be a good idea performance-wise, although SQL can be really difficult to grasp 🤷‍♂️ https://www.db-fiddle.com/ has always helped me a lot with that, didn't touch SQL for quite some time now though

0 Likes

You could extend the "find" method of the controller and pass custom data in the result, like so: https://ibb.co/fxPb3K3

0 Likes
Jason Leow Author

Btw @scriptifyjs I'm trying to create a custom query to count articles I posted on consecutive days…thought about using the node module date-streaks, but unsure how to integrate and use in my controller/model… any pointers?

0 Likes

SQL is even better suited for tasks like this! Maybe this article can help you: http://www.johnshiver.org/posts/finding-longest-streak-of-days-with-postgresql/ If you can make raw SQL Queries with Strapi, I'm sure there's a way, that'd be the best solution I think.

0 Likes
Jason Leow Author

Ah ok! thanks for pointing that out! Will try…. and also try SQL and see which one works better perf wise…

0 Likes
Jason Leow Author

Hey @scriptifyjs now i managed to install and get the date-streaks package working using some fake data, but it needs the data in an array, which my raw db query isn’t providing.

This is my strapi query:

strapi.connections.default.raw("SELECT published_at FROM posts WHERE author='1' ORDER BY published_at ASC");

What I get from the strapi query:

"command": "SELECT", "rowCount": 3, "oid": null, "rows": [ { "published_at": "2020-10-30T04:00:00.193Z" }, { "published_at": "2020-10-31T13:30:09.589Z" }, { "published_at": "2020-11-01T13:29:20.990Z" } ], "fields": [ { "name": "published_at", "tableID": 16641, "columnID": 7, "dataTypeID": 1184, "dataTypeSize": 8, "dataTypeModifier": -1, "format": "text" } ]......

What I need is to strip out all the other data and just get an array of published_at dates, like this:

[ "2020-10-30T11:00:00.193Z", "2020-10-31T13:30:09.589Z", "2020-11-01T15:29:20.990Z" ]

Do u have any advice on what I can do? TIA!! 😊

0 Likes

I deeply fell in love when first using Strapi, it gives you so much out of the box. Perfect for Indie Makers.

0 Likes

If you were using MongoDB, that's where your Aggregation would go

0 Likes
Jason Leow Author

Yes me too! It's marketed as a headless cms but to me it's like a low-code API builder! Love it..

0 Likes
Jason Leow Author

@scriptifyjs oh COOL thanks for the code snippet! Will try and let you know how it goes! So I don't need date-streaks npm right?

0 Likes

Glad I could help, anytime! ✨

0 Likes

You could use that package but than you need to fetch all blogposts within the timeframe you want to analyze, doing that on every query can become a performance problem. If you only use Strapi for an SSG (and fetch the articles at build time) and you are sure it will stay like that, than you could also go that route. But MongoDB Aggregations are pretty cool IMO, just a bit more complicated to get into

0 Likes
Jason Leow Author

@scriptifyjs progress update: managed to make raw sqlite db query in Strapi! Added a new streak route and a controller method using strapi.connections.default.raw("SELECT author, strftime('%d-%m-%Y %H:%M:%f', published_at/1000.0, 'unixepoch') FROM posts ORDER BY author DESC"); Not completely there yet but at least now I can query the author and published_at dates and see the output on my api client. Next steps - install date-streaks package and integrate it to calculate streaks, figure out how to query in production db since its Postgres

0 Likes

Using the JavaScript map array method:

const data = [
  { published_at: "2020-10-30T04:00:00.193Z" },
  { published_at: "2020-10-31T13:30:09.589Z" },
  { published_at: "2020-11-01T13:29:20.990Z" }
];
const datesOnly = data.map((row) => row.published_at);
console.log({ datesOnly });
0 Likes

Anytime, glad I could help!

0 Likes
Jason Leow Author

IT WORKS YAAASSS!!! Had to tweak to const datesOnly = data.rows.map(...) but thanks for the right direction!!! So grateful for your help man! 🙏🙏🙏

0 Likes
Jason Leow Author

@scriptifyjs oh haha great minds - I just landed on that john shiver article after searching through google. Never tried making SQL queries before ever, but will read up and figure out…

0 Likes
Jason Leow Author

Thaaanks so much! Will try and report back!

0 Likes
Jason Leow Author

Ah ok i seeee…thanks thanks!

0 Likes

Totally! Never build that CRUD API yourself haha

0 Likes
Jason Leow Author

Not sure yet actually. Will try out the date-streaks package. Also want to try out using SQL to calculate directly as per https://learnsql.com/blog/how-to-calculate-length-of-series-in-sql/

0 Likes
Jason Leow Author

@scriptifyjs yeah looking at the mongodb docs now…wow it's so out of my league haha..

0 Likes
Jason Leow Author

Hahaha yes now that I know Strapi. Was using Rails to build APIs and tbh it was a bit of a leap of faith. I don't know any Node.js, Express.js or Koa.js which Strapi uses, so had a bit of separation anxiety initially haha

0 Likes

You could extend the "find" method of the controller and pass custom data in the result, like so: https://ibb.co/fxPb3K3

0 Likes
Jason Leow Author

Quick qn: in the code image you shared, what's supposed to go into that part where you wrote // Aggregate ?

0 Likes
Jason Leow Author

Node.js land seems to be where everyone is headed these days! Worth learning nonetheless (for me)

0 Likes

Regarding the calculation of the streak itself, I'd suggest you to have a look at MongoDB's Aggregation Pipelines: https://docs.mongodb.com/manual/aggregation/ You can calculate pretty much every derived value you can immagine with such a pipeline + it's way more performant than fetching all documents yourself and do the calculation on every query.

0 Likes

Awesome sounds good! So you plan to do the rest of the calculation in Node.js?

0 Likes

Please sign in to leave a comment.