Skip to main content

Usage

slonik-trpc allows you to create a loader for each SQL query. With this loader, you can filter, sort, and paginate your data with ease, all while leveraging the power and efficiency of SQL.

You can look at the minimal-example playground for a simple query loader, or other examples.

Create a query loader

Create a file at src/postsLoader.ts:

postsLoader.ts
import { makeQueryLoader, buildView, sql } from 'slonik-trpc';

const postsQuery = {
select: sql.type(z.object({
id: z.number(),
author: z.string(),
title: z.string(),
date: z.string(),
}))`SELECT
posts.id,
users.first_name || ' ' || users.last_name AS author,
posts.title,
posts.date`,
view: buildView`FROM posts
LEFT JOIN users
ON users.id = posts.author_id`
};

export const postsLoader = makeQueryLoader({
db,
query: postsQuery,
});