Skip to Content

Kysely (query builder)

Documentation

Here are some resources that may help:

  • Kysely’s high-level docs are very useful for writing simple queries, including joining/querying related tables and returning the results as nested json (like an ORM)
  • When it comes to more complicated queries, or for extracting helper functions, those docs can be a little bit lacking
    • Kysely’s API reference docs have a fair amount of documentation contained within them (also available via intellisense). See especially the SelectQueryBuilder , ExpressionBuilder , and QueryCreator  interfaces.
    • Beyond that, their Discord  is active and has a searchable Q/A history with many practical examples. Searching the Kysely github  will also often turn up examples for how to use particular methods.
  • It will be at least as important to understand PostgreSQL to use Kysely effectively. The postgres docs  are useful for reviewing SQL basics and as reference documentation for the many functions we’ll use.

For database type generation, see Database.

Maintainability

It’s easy to end up with a mess of SQL functions in a variety of ways (too many helper functions, helpers are too complex, too much duplication) and there aren’t any silver bullets to prevent that. As we develop patterns that serve us well we will document them here. For now, be attentive to the number of queries being run in a single request and try to strike a balance between readability, composability, and efficiency as we rewrite our queries and factor out common functionality. On a practical note, consider using the clearSelect, clearWhere and similar methods when attempting to customize an existing helper function, instead of modifying the function.

Last updated on