Skip to main content
Puri is a type-safe query builder that lets you write SQL queries safely in TypeScript. This document explains the basic usage of SELECT, INSERT, UPDATE, and DELETE.

Getting Started with Queries

SELECT

Query data select, selectAll, first

INSERT

Add data insert, upsert, returning

UPDATE

Modify data update, increment, decrement

DELETE

Delete data delete

SELECT - Querying Data

Basic SELECT

The select method uses an object form to select columns. Keys are result field names, values are actual table column names.

Column Aliases

appendSelect - Add Columns

You can add more columns to an already selected set.

WHERE - Filtering Conditions

Basic WHERE

Multiple Conditions (AND)

OR Conditions

orWhere is a simple OR condition. For complex condition groups, use whereGroup. See Advanced Patterns for details.

IN / NOT IN

INSERT - Adding Data

Insert Single Record

Get Inserted Data with RETURNING

Insert Multiple Records

UPDATE - Modifying Data

Basic UPDATE

WHERE clause required: UPDATE must always be used with a WHERE condition. Modifying all data without conditions may cause errors.

increment / decrement

You can increase/decrease numeric columns.

Update Multiple Columns

DELETE - Deleting Data

Basic DELETE

WHERE clause required: DELETE must also be used with a WHERE condition.

Delete Multiple Records

LIMIT & OFFSET - Pagination

LIMIT - Restrict Result Count

OFFSET - Skip Records

Pagination Example

ORDER BY - Sorting

Basic Sorting

Multiple Column Sorting

first() - Query Single Result

first() returns only the first result.
first() returns undefined if no result. Always check for existence.

pluck() - Extract Single Column

Get only values from a specific column as an array.

count - Count Records

To query record count, use the Puri.count() static method inside select.
Puri.count() is a static method used inside the SELECT clause. For more complex aggregations, see Aggregations.

Practical Examples

User List Query API

Create Post API

Increment View Count

Query Debugging

debug() - Output SQL

rawQuery() - Get Knex QueryBuilder

Access the internal Knex query builder.

Next Steps

Joins

Joining tables

Aggregations

Using aggregate functions

Type Safety

Understanding type safety

Advanced Patterns

Learning advanced patterns