Getting Started
Database Drivers
Schema Integrations
Seed Generators
Utilities and Testing
The following files were used as context for generating this wiki page:
The "As -> StringChunk" flow describes how Drizzle transforms a configured query builder into a named subquery expression within a SQL statement. This process is triggered when a user calls the .as() method on a SingleStoreSelect instance. The end result is a Subquery object wrapped in a SelectionProxyHandler that can be seamlessly embedded into larger queries, ensuring SQL syntax safety and structural integrity.
as method invocationThe execution starts in the SingleStoreSelectQueryBuilderBase class. The .as() method is called with an alias string. It initializes the subquery creation by extracting all tables involved in the original query (the main table and any joined tables) to maintain correct table references in the scope of the new alias.
Sources: drizzle-orm/src/singlestore-core/query-builders/select.ts:912-917
getSQL call for query constructionWithin .as(), the builder calls its own getSQL() method. This method triggers the dialect to build the actual SQL string representation of the current select configuration, including filters, joins, and limits.
Sources: drizzle-orm/src/singlestore-core/query-builders/select.ts:903-905
The SingleStoreDialect.buildSelectQuery method is invoked. It assembles the complete SQL SELECT structure. If the query includes common table expressions (CTEs), it calls buildWithCTE to properly format the WITH clause before the primary SELECT query.
Sources: drizzle-orm/src/singlestore-core/dialect.ts:299-314
buildWithCTE processes the withList configuration. It iterates through registered subqueries, creating SQL objects for each CTE, and joins them using the sql template literal function, ensuring the resulting SQL conforms to SingleStore syntax expectations.
Sources: drizzle-orm/src/singlestore-core/dialect.ts:111-123
sql tag and chunk creationThe sql function is responsible for parsing template strings and parameters. It segments the query into an array of SQLChunk objects. When it encounters raw strings in the template, it instantiates StringChunk to represent fixed SQL parts.
Sources: drizzle-orm/src/sql/sql.ts:485-494
StringChunk instantiationThe StringChunk class acts as the final wrapper for literal SQL fragments. It stores the SQL string as a list of components, which the engine later joins during the toQuery phase to generate the final executable string and parameter binding set.
Sources: drizzle-orm/src/sql/sql.ts:89-101
Tip
The .as() method is crucial for subquery reuse. By wrapping the result in a SelectionProxyHandler, Drizzle allows you to continue writing type-safe code as if the alias were a standard table.
Sources: drizzle-orm/src/singlestore-core/query-builders/select.ts:912-922, drizzle-orm/src/singlestore-core/dialect.ts:299-314, drizzle-orm/src/sql/sql.ts:485-494
Sources: drizzle-orm/src/singlestore-core/query-builders/select.ts:912-920, drizzle-orm/src/singlestore-core/dialect.ts:299-350, drizzle-orm/src/sql/sql.ts:485-500
select.ts) through Dialect-specific transformation (dialect.ts) into core SQL AST manipulation (sql.ts).StringChunk and the sql template system, Drizzle avoids costly string concatenations for complex queries, deferring query generation until toQuery() is invoked.