Hi all. I’ve been working with Strapi for over a year now, and it’s just a great solution. =)
During that time I had to write a lot of queries, sometimes very complex, both in terms of size and in terms of construction. And I needed a tool for type-safe and flexible construction of queries, something like (Entity Framework). Since I couldn’t find it, I started to write a small utility. I’ve been using it for more than half year and it repeatedly saved me when field names were changed or new fields were added. And in general I was able to organize the code closer to DDD.
So now I can write queries with autocompletion, something like this.
import SQLBuilder from "strapi-query-builder";
const query = new SQBuilder<PostType>()
.filters("title", (b) => b.eq("Hello World"))
.filters("createdAt", (b) => b.gte("2021-11-17T14:28:25.843Z"))
.sort("title").asc()
.fields(["title", "description"])
.populate<CategoryType>("category", (categoryBuilder) =>
categoryBuilder
.or()
.filters("slug")
.contains("phone")
.filters("name")
.contains("phone")
)
.build();
Finally, got everything ready and put it out as a package. I hope someone might find it useful.
strapi-query-builder - npm (npmjs.com)
I’m not good at publishing packages yet, since this is my first time. But I tried to make a good description.