Custom Queries
As Arkos generates Prisma Model Routes, it also allows you to customize the underlying Prisma queries for each operation without handrolling your own controller logic. This is done through a [model].query.ts file that Arkos picks up automatically at boot time.
Setup
Create a query options file for your model:
import { PrismaQueryOptions } from "arkos/prisma";
import { prisma } from "@/src/utils/prisma";
const postQueryOptions: PrismaQueryOptions<typeof prisma.post> = {
global: {
where: { published: true },
orderBy: { createdAt: "desc" },
},
};
export default postQueryOptions;Or generate it with the CLI:
npx arkos generate query-options --module post
# shorthand
npx arkos g q -m postConfiguration Keys
Options are applied in this priority order — higher overrides lower:
- Request query parameters (always highest)
- Individual operation keys (
findMany,createOne, etc.) - Grouped operation keys (
find,save,create, etc.) global
Grouped Keys
| Key | Applies To |
|---|---|
find | findOne, findMany |
save | createOne, updateOne, createMany, updateMany |
create | createOne, createMany |
update | updateOne, updateMany |
delete | deleteOne, deleteMany |
saveOne | createOne, updateOne |
saveMany | createMany, updateMany |
Individual Keys
findOne, findMany, createOne, updateOne, deleteOne, createMany, updateMany, deleteMany
Each key accepts the full Prisma options for that operation — select, include, omit, where, orderBy, take, skip, distinct.
Options are deep-merged at each level, not replaced. Request query parameters always win but non-conflicting options from your config are preserved.
Examples
Excluding sensitive fields
import { PrismaQueryOptions } from "arkos/prisma";
import { prisma } from "@/src/utils/prisma";
const userQueryOptions: PrismaQueryOptions<typeof prisma.user> = {
global: {
omit: {
password: true,
resetToken: true,
},
},
};
export default userQueryOptions;Different behavior per operation
import { PrismaQueryOptions } from "arkos/prisma";
import { prisma } from "@/src/utils/prisma";
const postQueryOptions: PrismaQueryOptions<typeof prisma.post> = {
find: {
where: { published: true },
orderBy: { createdAt: "desc" },
},
findMany: {
take: 10,
select: {
id: true,
title: true,
excerpt: true,
createdAt: true,
},
},
findOne: {
include: {
author: true,
comments: {
where: { approved: true },
orderBy: { createdAt: "asc" },
},
tags: true,
},
},
save: {
include: { author: true, tags: true },
},
};
export default postQueryOptions;Related
- Prisma Model Routes — The generated endpoints these options apply to
- Handling Relations — How Arkos handles relational data
- Interceptors — Run logic before or after any operation