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
IN / NOT IN
LIKE Search
INSERT - Adding Data
Insert Single Record
Get Inserted Data with RETURNING
Insert Multiple Records
UPDATE - Modifying Data
Basic UPDATE
increment / decrement
You can increase/decrease numeric columns.Update Multiple Columns
DELETE - Deleting Data
Basic DELETE
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 thePuri.count() static method inside select.
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