MCPcopy Index your code
hub / github.com/TanStack/db / compileExpression

Function compileExpression

packages/powersync-db-collection/src/sqlite-compiler.ts:82–116  ·  view source on GitHub ↗

* Compiles a BasicExpression to a SQL string, mutating the params array.

(
  exp: IR.BasicExpression<unknown>,
  params: Array<unknown>,
  compileOptions?: CompileSQLiteOptions,
)

Source from the content-addressed store, hash-verified

80 * Compiles a BasicExpression to a SQL string, mutating the params array.
81 */
82function compileExpression(
83 exp: IR.BasicExpression<unknown>,
84 params: Array<unknown>,
85 compileOptions?: CompileSQLiteOptions,
86): string {
87 switch (exp.type) {
88 case `val`:
89 params.push(exp.value)
90 return `?`
91 case `ref`: {
92 if (exp.path.length !== 1) {
93 throw new Error(
94 `SQLite compiler doesn't support nested properties: ${exp.path.join(`.`)}`,
95 )
96 }
97 const columnName = exp.path[0]!
98
99 // PowerSync stores `id` as a top-level row column rather than inside the
100 // JSON `data` object, so we skip json_extract. However, when compiling for
101 // trigger WHEN clauses we still need the OLD./NEW. prefix. Extract it from
102 // the jsonColumn option.
103 if (compileOptions?.jsonColumn && columnName === `id`) {
104 const prefix = compileOptions.jsonColumn.split('.')[0]!
105 return `${prefix}.${quoteIdentifier(columnName)}`
106 } else if (compileOptions?.jsonColumn) {
107 return `json_extract(${compileOptions.jsonColumn}, '$.${columnName}')`
108 }
109 return quoteIdentifier(columnName)
110 }
111 case `func`:
112 return compileFunction(exp, params, compileOptions)
113 default:
114 throw new Error(`Unknown expression type: ${(exp as any).type}`)
115 }
116}
117
118/**
119 * Compiles an OrderBy array to a SQL ORDER BY clause.

Callers 3

compileSQLiteFunction · 0.70
compileOrderByClauseFunction · 0.70
compileFunctionFunction · 0.70

Calls 3

quoteIdentifierFunction · 0.70
compileFunctionFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected