Getting Started
Configuration
Troubleshooting
Querying and mutating data are the core operations you perform to interact with your database. Whether you need to retrieve specific information, organize results, or combine data from multiple tables, these features provide a clear, readable way to express your intent without needing to write raw database language.
This system uses a "builder" approach, where you chain methods together to construct your request step-by-step.
To retrieve data, you start with the select method. You can either select all columns from a table or define specific fields to retrieve.
db.select() on your database object..from() to specify which table or data source you are querying..where() to restrict results based on conditions..orderBy() for sorting, .limit() for capping result counts, or .offset() for pagination.You can filter your data using logical conditions. For example, combine conditions with and() or or() to create complex filters. If you are grouping data to perform calculations (like counting items), use .groupBy() alongside aggregate functions, and further filter those groups using .having().
Tip
You can use a function inside methods like .where() or .orderBy() to access a helper that lets you reference columns dynamically, making your queries cleaner and more maintainable.
When your data is split across different tables, you can use built-in join methods to combine them into a single result set.
Note
For advanced scenarios, many of these joins have "lateral" variants, which allow the joined query to reference columns defined in the main table.
You can combine the results of multiple independent queries using set operators. These are useful for merging lists of data that share the same structure.
Warning
When using set operations, ensure that both queries select the same fields in the exact same order. The system will throw an error if the structures do not match.
.prepare() method. This allows the database to compile the query once and reuse it, which can significantly improve performance..for() method to specify a lock strength if you need to ensure data consistency during concurrent operations.$withCache() to enable automatic caching for your query results, reducing the load on your database for frequently accessed data.Caution
Always be mindful of query performance when using crossJoin or large offset values, as these can put significant strain on your database resources.