Avoid inefficient database query patterns by using batch operations and appropriate query types. Instead of making multiple individual queries, use batch queries with in
clauses. When you only need counts, use count queries rather than fetching full records.
Examples of improvements:
// Use a single batch query const roleIds = userRoles.map(ur => ur.roleId); const roles = await ctx.context.adapter.findMany({ model: “role”, where: [{ field: “id”, operator: “in”, value: roleIds }] });
- Use count queries when you don't need the actual data:
```typescript
// Instead of fetching all records
const allUsers = await db.findMany({ model: "user" });
const userCount = allUsers.length;
// Use count directly
const userCount = await db.count({ model: "user" });
This approach reduces database load, improves response times, and minimizes memory usage by avoiding unnecessary data transfer.
Enter the URL of a public GitHub repository