@tanstack/electric-db-collection 0.2.25 → 0.2.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/electric.cjs +14 -6
- package/dist/cjs/electric.cjs.map +1 -1
- package/dist/cjs/sql-compiler.cjs +26 -16
- package/dist/cjs/sql-compiler.cjs.map +1 -1
- package/dist/cjs/sql-compiler.d.cts +17 -1
- package/dist/esm/electric.js +14 -6
- package/dist/esm/electric.js.map +1 -1
- package/dist/esm/sql-compiler.d.ts +17 -1
- package/dist/esm/sql-compiler.js +26 -16
- package/dist/esm/sql-compiler.js.map +1 -1
- package/package.json +2 -2
- package/src/electric.ts +19 -4
- package/src/sql-compiler.ts +56 -13
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-compiler.js","sources":["../../src/sql-compiler.ts"],"sourcesContent":["import { serialize } from './pg-serializer'\nimport type { SubsetParams } from '@electric-sql/client'\nimport type { IR, LoadSubsetOptions } from '@tanstack/db'\n\nexport type CompiledSqlRecord = Omit<SubsetParams, `params`> & {\n params?: Array<unknown>\n}\n\nexport function compileSQL<T>(options: LoadSubsetOptions): SubsetParams {\n const { where, orderBy, limit } = options\n\n const params: Array<T> = []\n const compiledSQL: CompiledSqlRecord = { params }\n\n if (where) {\n // TODO: this only works when the where expression's PropRefs directly reference a column of the collection\n // doesn't work if it goes through aliases because then we need to know the entire query to be able to follow the reference until the base collection (cf. followRef function)\n compiledSQL.where = compileBasicExpression(where, params)\n }\n\n if (orderBy) {\n compiledSQL.orderBy = compileOrderBy(orderBy, params)\n }\n\n if (limit) {\n compiledSQL.limit = limit\n }\n\n // WORKAROUND for Electric bug: Empty subset requests don't load data\n // Add dummy \"true = true\" predicate when there's no where clause\n // This is always true so doesn't filter data, just tricks Electric into loading\n if (!where) {\n compiledSQL.where = `true = true`\n }\n\n // Serialize the values in the params array into PG formatted strings\n // and transform the array into a Record<string, string>\n const paramsRecord = params.reduce(\n (acc, param, index) => {\n const serialized = serialize(param)\n // Empty strings are valid query values (e.g., WHERE column = '')\n // Only omit null/undefined values from params\n if (param != null) {\n acc[`${index + 1}`] = serialized\n }\n return acc\n },\n {} as Record<string, string>,\n )\n\n return {\n ...compiledSQL,\n params: paramsRecord,\n }\n}\n\n/**\n * Quote PostgreSQL identifiers to handle mixed case column names correctly.\n * Electric/Postgres requires quotes for case-sensitive identifiers.\n * @param name - The identifier to quote\n * @returns The quoted identifier\n */\nfunction quoteIdentifier(name: string): string {\n return `\"${name}\"`\n}\n\n/**\n * Compiles the expression to a SQL string and mutates the params array with the values.\n * @param exp - The expression to compile\n * @param params - The params array\n * @returns The compiled SQL string\n */\nfunction compileBasicExpression(\n exp: IR.BasicExpression<unknown>,\n params: Array<unknown>,\n): string {\n switch (exp.type) {\n case `val`:\n params.push(exp.value)\n return `$${params.length}`\n case `ref`:\n // TODO: doesn't yet support JSON(B) values which could be accessed with nested props\n if (exp.path.length !== 1) {\n throw new Error(\n `Compiler can't handle nested properties: ${exp.path.join(`.`)}`,\n )\n }\n return quoteIdentifier(exp.path[0]!)\n case `func`:\n return compileFunction(exp, params)\n default:\n throw new Error(`Unknown expression type`)\n }\n}\n\nfunction compileOrderBy(orderBy: IR.OrderBy, params: Array<unknown>): string {\n const compiledOrderByClauses = orderBy.map((clause: IR.OrderByClause) =>\n compileOrderByClause(clause, params),\n )\n return compiledOrderByClauses.join(`,`)\n}\n\nfunction compileOrderByClause(\n clause: IR.OrderByClause,\n params: Array<unknown>,\n): string {\n // FIXME: We should handle stringSort and locale.\n // Correctly supporting them is tricky as it depends on Postgres' collation\n const { expression, compareOptions } = clause\n let sql = compileBasicExpression(expression, params)\n\n if (compareOptions.direction === `desc`) {\n sql = `${sql} DESC`\n }\n\n if (compareOptions.nulls === `first`) {\n sql = `${sql} NULLS FIRST`\n }\n\n if (compareOptions.nulls === `last`) {\n sql = `${sql} NULLS LAST`\n }\n\n return sql\n}\n\n/**\n * Check if a BasicExpression represents a null/undefined value\n */\nfunction isNullValue(exp: IR.BasicExpression<unknown>): boolean {\n return exp.type === `val` && (exp.value === null || exp.value === undefined)\n}\n\nfunction compileFunction(\n exp: IR.Func<unknown>,\n params: Array<unknown> = [],\n): string {\n const { name, args } = exp\n\n const opName = getOpName(name)\n\n // Handle comparison operators with null/undefined values\n // These would create invalid queries with missing params (e.g., \"col = $1\" with empty params)\n // In SQL, all comparisons with NULL return UNKNOWN, so these are almost always mistakes\n if (isComparisonOp(name)) {\n const nullArgIndex = args.findIndex((arg: IR.BasicExpression) =>\n isNullValue(arg),\n )\n\n if (nullArgIndex !== -1) {\n // All comparison operators (including eq) throw an error for null values\n // Users should use isNull() or isUndefined() to check for null values\n throw new Error(\n `Cannot use null/undefined value with '${name}' operator. ` +\n `Comparisons with null always evaluate to UNKNOWN in SQL. ` +\n `Use isNull() or isUndefined() to check for null values, ` +\n `or filter out null values before building the query.`,\n )\n }\n }\n\n const compiledArgs = args.map((arg: IR.BasicExpression) =>\n compileBasicExpression(arg, params),\n )\n\n // Special case for IS NULL / IS NOT NULL - these are postfix operators\n if (name === `isNull` || name === `isUndefined`) {\n if (compiledArgs.length !== 1) {\n throw new Error(`${name} expects 1 argument`)\n }\n return `${compiledArgs[0]} ${opName}`\n }\n\n // Special case for NOT - unary prefix operator\n if (name === `not`) {\n if (compiledArgs.length !== 1) {\n throw new Error(`NOT expects 1 argument`)\n }\n // Check if the argument is IS NULL to generate IS NOT NULL\n const arg = args[0]\n if (arg && arg.type === `func`) {\n const funcArg = arg\n if (funcArg.name === `isNull` || funcArg.name === `isUndefined`) {\n const innerArg = compileBasicExpression(funcArg.args[0]!, params)\n return `${innerArg} IS NOT NULL`\n }\n }\n return `${opName} (${compiledArgs[0]})`\n }\n\n if (isBinaryOp(name)) {\n // Special handling for AND/OR which can be variadic\n if ((name === `and` || name === `or`) && compiledArgs.length > 2) {\n // Chain multiple arguments: (a AND b AND c) or (a OR b OR c)\n return compiledArgs.map((arg) => `(${arg})`).join(` ${opName} `)\n }\n\n if (compiledArgs.length !== 2) {\n throw new Error(`Binary operator ${name} expects 2 arguments`)\n }\n const [lhs, rhs] = compiledArgs\n\n // Special case for comparison operators with boolean values\n // PostgreSQL doesn't support < > <= >= on booleans\n // Transform to equivalent equality checks or constant expressions\n if (isBooleanComparisonOp(name)) {\n const lhsArg = args[0]\n const rhsArg = args[1]\n\n // Check if RHS is a boolean literal value\n if (\n rhsArg &&\n rhsArg.type === `val` &&\n typeof rhsArg.value === `boolean`\n ) {\n const boolValue = rhsArg.value\n // Remove the boolean param we just added since we'll transform the expression\n params.pop()\n\n // Transform based on operator and boolean value\n // Boolean ordering: false < true\n if (name === `lt`) {\n if (boolValue === true) {\n // lt(col, true) → col = false (only false is less than true)\n params.push(false)\n return `${lhs} = $${params.length}`\n } else {\n // lt(col, false) → nothing is less than false\n return `false`\n }\n } else if (name === `gt`) {\n if (boolValue === false) {\n // gt(col, false) → col = true (only true is greater than false)\n params.push(true)\n return `${lhs} = $${params.length}`\n } else {\n // gt(col, true) → nothing is greater than true\n return `false`\n }\n } else if (name === `lte`) {\n if (boolValue === true) {\n // lte(col, true) → everything is ≤ true\n return `true`\n } else {\n // lte(col, false) → col = false\n params.push(false)\n return `${lhs} = $${params.length}`\n }\n } else if (name === `gte`) {\n if (boolValue === false) {\n // gte(col, false) → everything is ≥ false\n return `true`\n } else {\n // gte(col, true) → col = true\n params.push(true)\n return `${lhs} = $${params.length}`\n }\n }\n }\n\n // Check if LHS is a boolean literal value (less common but handle it)\n if (\n lhsArg &&\n lhsArg.type === `val` &&\n typeof lhsArg.value === `boolean`\n ) {\n const boolValue = lhsArg.value\n // Remove params for this expression and rebuild\n params.pop() // remove RHS\n params.pop() // remove LHS (boolean)\n\n // Recompile RHS to get fresh param\n const rhsCompiled = compileBasicExpression(rhsArg!, params)\n\n // Transform: flip the comparison (val op col → col flipped_op val)\n if (name === `lt`) {\n // lt(true, col) → gt(col, true) → col > true → nothing is greater than true\n if (boolValue === true) {\n return `false`\n } else {\n // lt(false, col) → gt(col, false) → col = true\n params.push(true)\n return `${rhsCompiled} = $${params.length}`\n }\n } else if (name === `gt`) {\n // gt(true, col) → lt(col, true) → col = false\n if (boolValue === true) {\n params.push(false)\n return `${rhsCompiled} = $${params.length}`\n } else {\n // gt(false, col) → lt(col, false) → nothing is less than false\n return `false`\n }\n } else if (name === `lte`) {\n if (boolValue === false) {\n // lte(false, col) → gte(col, false) → everything\n return `true`\n } else {\n // lte(true, col) → gte(col, true) → col = true\n params.push(true)\n return `${rhsCompiled} = $${params.length}`\n }\n } else if (name === `gte`) {\n if (boolValue === true) {\n // gte(true, col) → lte(col, true) → everything\n return `true`\n } else {\n // gte(false, col) → lte(col, false) → col = false\n params.push(false)\n return `${rhsCompiled} = $${params.length}`\n }\n }\n }\n }\n\n // Special case for = ANY operator which needs parentheses around the array parameter\n if (name === `in`) {\n return `${lhs} ${opName}(${rhs})`\n }\n return `${lhs} ${opName} ${rhs}`\n }\n\n return `${opName}(${compiledArgs.join(`,`)})`\n}\n\nfunction isBinaryOp(name: string): boolean {\n const binaryOps = [\n `eq`,\n `gt`,\n `gte`,\n `lt`,\n `lte`,\n `and`,\n `or`,\n `in`,\n `like`,\n `ilike`,\n ]\n return binaryOps.includes(name)\n}\n\n/**\n * Check if operator is a comparison operator that takes two values\n * These operators cannot accept null/undefined as values\n * (null comparisons in SQL always evaluate to UNKNOWN)\n */\nfunction isComparisonOp(name: string): boolean {\n const comparisonOps = [`eq`, `gt`, `gte`, `lt`, `lte`, `like`, `ilike`]\n return comparisonOps.includes(name)\n}\n\n/**\n * Checks if the operator is a comparison operator (excluding eq)\n * These operators don't work on booleans in PostgreSQL without casting\n */\nfunction isBooleanComparisonOp(name: string): boolean {\n return [`gt`, `gte`, `lt`, `lte`].includes(name)\n}\n\nfunction getOpName(name: string): string {\n const opNames = {\n eq: `=`,\n gt: `>`,\n gte: `>=`,\n lt: `<`,\n lte: `<=`,\n add: `+`,\n and: `AND`,\n or: `OR`,\n not: `NOT`,\n isUndefined: `IS NULL`,\n isNull: `IS NULL`,\n in: `= ANY`, // Use = ANY syntax for array parameters\n like: `LIKE`,\n ilike: `ILIKE`,\n upper: `UPPER`,\n lower: `LOWER`,\n length: `LENGTH`,\n concat: `CONCAT`,\n coalesce: `COALESCE`,\n }\n\n const opName = opNames[name as keyof typeof opNames]\n\n if (!opName) {\n throw new Error(`Unknown operator/function: ${name}`)\n }\n\n return opName\n}\n"],"names":[],"mappings":";AAQO,SAAS,WAAc,SAA0C;AACtE,QAAM,EAAE,OAAO,SAAS,MAAA,IAAU;AAElC,QAAM,SAAmB,CAAA;AACzB,QAAM,cAAiC,EAAE,OAAA;AAEzC,MAAI,OAAO;AAGT,gBAAY,QAAQ,uBAAuB,OAAO,MAAM;AAAA,EAC1D;AAEA,MAAI,SAAS;AACX,gBAAY,UAAU,eAAe,SAAS,MAAM;AAAA,EACtD;AAEA,MAAI,OAAO;AACT,gBAAY,QAAQ;AAAA,EACtB;AAKA,MAAI,CAAC,OAAO;AACV,gBAAY,QAAQ;AAAA,EACtB;AAIA,QAAM,eAAe,OAAO;AAAA,IAC1B,CAAC,KAAK,OAAO,UAAU;AACrB,YAAM,aAAa,UAAU,KAAK;AAGlC,UAAI,SAAS,MAAM;AACjB,YAAI,GAAG,QAAQ,CAAC,EAAE,IAAI;AAAA,MACxB;AACA,aAAO;AAAA,IACT;AAAA,IACA,CAAA;AAAA,EAAC;AAGH,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,EAAA;AAEZ;AAQA,SAAS,gBAAgB,MAAsB;AAC7C,SAAO,IAAI,IAAI;AACjB;AAQA,SAAS,uBACP,KACA,QACQ;AACR,UAAQ,IAAI,MAAA;AAAA,IACV,KAAK;AACH,aAAO,KAAK,IAAI,KAAK;AACrB,aAAO,IAAI,OAAO,MAAM;AAAA,IAC1B,KAAK;AAEH,UAAI,IAAI,KAAK,WAAW,GAAG;AACzB,cAAM,IAAI;AAAA,UACR,4CAA4C,IAAI,KAAK,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A,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"sql-compiler.js","sources":["../../src/sql-compiler.ts"],"sourcesContent":["import { serialize } from './pg-serializer'\nimport type { SubsetParams } from '@electric-sql/client'\nimport type { IR, LoadSubsetOptions } from '@tanstack/db'\n\nexport type CompiledSqlRecord = Omit<SubsetParams, `params`> & {\n params?: Array<unknown>\n}\n\n/**\n * Optional function to encode column names (e.g., camelCase to snake_case)\n * This is typically the `encode` function from a columnMapper\n */\nexport type ColumnEncoder = (columnName: string) => string\n\n/**\n * Options for SQL compilation\n */\nexport interface CompileSQLOptions {\n /**\n * Optional function to encode column names before quoting.\n * Used to transform property names (e.g., camelCase) to database column names (e.g., snake_case).\n * This should be the `encode` function from shapeOptions.columnMapper.\n */\n encodeColumnName?: ColumnEncoder\n}\n\nexport function compileSQL<T>(\n options: LoadSubsetOptions,\n compileOptions?: CompileSQLOptions,\n): SubsetParams {\n const { where, orderBy, limit } = options\n const encodeColumnName = compileOptions?.encodeColumnName\n\n const params: Array<T> = []\n const compiledSQL: CompiledSqlRecord = { params }\n\n if (where) {\n // TODO: this only works when the where expression's PropRefs directly reference a column of the collection\n // doesn't work if it goes through aliases because then we need to know the entire query to be able to follow the reference until the base collection (cf. followRef function)\n compiledSQL.where = compileBasicExpression(where, params, encodeColumnName)\n }\n\n if (orderBy) {\n compiledSQL.orderBy = compileOrderBy(orderBy, params, encodeColumnName)\n }\n\n if (limit) {\n compiledSQL.limit = limit\n }\n\n // WORKAROUND for Electric bug: Empty subset requests don't load data\n // Add dummy \"true = true\" predicate when there's no where clause\n // This is always true so doesn't filter data, just tricks Electric into loading\n if (!where) {\n compiledSQL.where = `true = true`\n }\n\n // Serialize the values in the params array into PG formatted strings\n // and transform the array into a Record<string, string>\n const paramsRecord = params.reduce(\n (acc, param, index) => {\n const serialized = serialize(param)\n // Empty strings are valid query values (e.g., WHERE column = '')\n // Only omit null/undefined values from params\n if (param != null) {\n acc[`${index + 1}`] = serialized\n }\n return acc\n },\n {} as Record<string, string>,\n )\n\n return {\n ...compiledSQL,\n params: paramsRecord,\n }\n}\n\n/**\n * Quote PostgreSQL identifiers to handle mixed case column names correctly.\n * Electric/Postgres requires quotes for case-sensitive identifiers.\n * @param name - The identifier to quote\n * @param encodeColumnName - Optional function to encode the column name before quoting (e.g., camelCase to snake_case)\n * @returns The quoted identifier\n */\nfunction quoteIdentifier(\n name: string,\n encodeColumnName?: ColumnEncoder,\n): string {\n const columnName = encodeColumnName ? encodeColumnName(name) : name\n return `\"${columnName}\"`\n}\n\n/**\n * Compiles the expression to a SQL string and mutates the params array with the values.\n * @param exp - The expression to compile\n * @param params - The params array\n * @param encodeColumnName - Optional function to encode column names (e.g., camelCase to snake_case)\n * @returns The compiled SQL string\n */\nfunction compileBasicExpression(\n exp: IR.BasicExpression<unknown>,\n params: Array<unknown>,\n encodeColumnName?: ColumnEncoder,\n): string {\n switch (exp.type) {\n case `val`:\n params.push(exp.value)\n return `$${params.length}`\n case `ref`:\n // TODO: doesn't yet support JSON(B) values which could be accessed with nested props\n if (exp.path.length !== 1) {\n throw new Error(\n `Compiler can't handle nested properties: ${exp.path.join(`.`)}`,\n )\n }\n return quoteIdentifier(exp.path[0]!, encodeColumnName)\n case `func`:\n return compileFunction(exp, params, encodeColumnName)\n default:\n throw new Error(`Unknown expression type`)\n }\n}\n\nfunction compileOrderBy(\n orderBy: IR.OrderBy,\n params: Array<unknown>,\n encodeColumnName?: ColumnEncoder,\n): string {\n const compiledOrderByClauses = orderBy.map((clause: IR.OrderByClause) =>\n compileOrderByClause(clause, params, encodeColumnName),\n )\n return compiledOrderByClauses.join(`,`)\n}\n\nfunction compileOrderByClause(\n clause: IR.OrderByClause,\n params: Array<unknown>,\n encodeColumnName?: ColumnEncoder,\n): string {\n // FIXME: We should handle stringSort and locale.\n // Correctly supporting them is tricky as it depends on Postgres' collation\n const { expression, compareOptions } = clause\n let sql = compileBasicExpression(expression, params, encodeColumnName)\n\n if (compareOptions.direction === `desc`) {\n sql = `${sql} DESC`\n }\n\n if (compareOptions.nulls === `first`) {\n sql = `${sql} NULLS FIRST`\n }\n\n if (compareOptions.nulls === `last`) {\n sql = `${sql} NULLS LAST`\n }\n\n return sql\n}\n\n/**\n * Check if a BasicExpression represents a null/undefined value\n */\nfunction isNullValue(exp: IR.BasicExpression<unknown>): boolean {\n return exp.type === `val` && (exp.value === null || exp.value === undefined)\n}\n\nfunction compileFunction(\n exp: IR.Func<unknown>,\n params: Array<unknown> = [],\n encodeColumnName?: ColumnEncoder,\n): string {\n const { name, args } = exp\n\n const opName = getOpName(name)\n\n // Handle comparison operators with null/undefined values\n // These would create invalid queries with missing params (e.g., \"col = $1\" with empty params)\n // In SQL, all comparisons with NULL return UNKNOWN, so these are almost always mistakes\n if (isComparisonOp(name)) {\n const nullArgIndex = args.findIndex((arg: IR.BasicExpression) =>\n isNullValue(arg),\n )\n\n if (nullArgIndex !== -1) {\n // All comparison operators (including eq) throw an error for null values\n // Users should use isNull() or isUndefined() to check for null values\n throw new Error(\n `Cannot use null/undefined value with '${name}' operator. ` +\n `Comparisons with null always evaluate to UNKNOWN in SQL. ` +\n `Use isNull() or isUndefined() to check for null values, ` +\n `or filter out null values before building the query.`,\n )\n }\n }\n\n const compiledArgs = args.map((arg: IR.BasicExpression) =>\n compileBasicExpression(arg, params, encodeColumnName),\n )\n\n // Special case for IS NULL / IS NOT NULL - these are postfix operators\n if (name === `isNull` || name === `isUndefined`) {\n if (compiledArgs.length !== 1) {\n throw new Error(`${name} expects 1 argument`)\n }\n return `${compiledArgs[0]} ${opName}`\n }\n\n // Special case for NOT - unary prefix operator\n if (name === `not`) {\n if (compiledArgs.length !== 1) {\n throw new Error(`NOT expects 1 argument`)\n }\n // Check if the argument is IS NULL to generate IS NOT NULL\n const arg = args[0]\n if (arg && arg.type === `func`) {\n const funcArg = arg\n if (funcArg.name === `isNull` || funcArg.name === `isUndefined`) {\n const innerArg = compileBasicExpression(\n funcArg.args[0]!,\n params,\n encodeColumnName,\n )\n return `${innerArg} IS NOT NULL`\n }\n }\n return `${opName} (${compiledArgs[0]})`\n }\n\n if (isBinaryOp(name)) {\n // Special handling for AND/OR which can be variadic\n if ((name === `and` || name === `or`) && compiledArgs.length > 2) {\n // Chain multiple arguments: (a AND b AND c) or (a OR b OR c)\n return compiledArgs.map((arg) => `(${arg})`).join(` ${opName} `)\n }\n\n if (compiledArgs.length !== 2) {\n throw new Error(`Binary operator ${name} expects 2 arguments`)\n }\n const [lhs, rhs] = compiledArgs\n\n // Special case for comparison operators with boolean values\n // PostgreSQL doesn't support < > <= >= on booleans\n // Transform to equivalent equality checks or constant expressions\n if (isBooleanComparisonOp(name)) {\n const lhsArg = args[0]\n const rhsArg = args[1]\n\n // Check if RHS is a boolean literal value\n if (\n rhsArg &&\n rhsArg.type === `val` &&\n typeof rhsArg.value === `boolean`\n ) {\n const boolValue = rhsArg.value\n // Remove the boolean param we just added since we'll transform the expression\n params.pop()\n\n // Transform based on operator and boolean value\n // Boolean ordering: false < true\n if (name === `lt`) {\n if (boolValue === true) {\n // lt(col, true) → col = false (only false is less than true)\n params.push(false)\n return `${lhs} = $${params.length}`\n } else {\n // lt(col, false) → nothing is less than false\n return `false`\n }\n } else if (name === `gt`) {\n if (boolValue === false) {\n // gt(col, false) → col = true (only true is greater than false)\n params.push(true)\n return `${lhs} = $${params.length}`\n } else {\n // gt(col, true) → nothing is greater than true\n return `false`\n }\n } else if (name === `lte`) {\n if (boolValue === true) {\n // lte(col, true) → everything is ≤ true\n return `true`\n } else {\n // lte(col, false) → col = false\n params.push(false)\n return `${lhs} = $${params.length}`\n }\n } else if (name === `gte`) {\n if (boolValue === false) {\n // gte(col, false) → everything is ≥ false\n return `true`\n } else {\n // gte(col, true) → col = true\n params.push(true)\n return `${lhs} = $${params.length}`\n }\n }\n }\n\n // Check if LHS is a boolean literal value (less common but handle it)\n if (\n lhsArg &&\n lhsArg.type === `val` &&\n typeof lhsArg.value === `boolean`\n ) {\n const boolValue = lhsArg.value\n // Remove params for this expression and rebuild\n params.pop() // remove RHS\n params.pop() // remove LHS (boolean)\n\n // Recompile RHS to get fresh param\n const rhsCompiled = compileBasicExpression(\n rhsArg!,\n params,\n encodeColumnName,\n )\n\n // Transform: flip the comparison (val op col → col flipped_op val)\n if (name === `lt`) {\n // lt(true, col) → gt(col, true) → col > true → nothing is greater than true\n if (boolValue === true) {\n return `false`\n } else {\n // lt(false, col) → gt(col, false) → col = true\n params.push(true)\n return `${rhsCompiled} = $${params.length}`\n }\n } else if (name === `gt`) {\n // gt(true, col) → lt(col, true) → col = false\n if (boolValue === true) {\n params.push(false)\n return `${rhsCompiled} = $${params.length}`\n } else {\n // gt(false, col) → lt(col, false) → nothing is less than false\n return `false`\n }\n } else if (name === `lte`) {\n if (boolValue === false) {\n // lte(false, col) → gte(col, false) → everything\n return `true`\n } else {\n // lte(true, col) → gte(col, true) → col = true\n params.push(true)\n return `${rhsCompiled} = $${params.length}`\n }\n } else if (name === `gte`) {\n if (boolValue === true) {\n // gte(true, col) → lte(col, true) → everything\n return `true`\n } else {\n // gte(false, col) → lte(col, false) → col = false\n params.push(false)\n return `${rhsCompiled} = $${params.length}`\n }\n }\n }\n }\n\n // Special case for = ANY operator which needs parentheses around the array parameter\n if (name === `in`) {\n return `${lhs} ${opName}(${rhs})`\n }\n return `${lhs} ${opName} ${rhs}`\n }\n\n return `${opName}(${compiledArgs.join(`,`)})`\n}\n\nfunction isBinaryOp(name: string): boolean {\n const binaryOps = [\n `eq`,\n `gt`,\n `gte`,\n `lt`,\n `lte`,\n `and`,\n `or`,\n `in`,\n `like`,\n `ilike`,\n ]\n return binaryOps.includes(name)\n}\n\n/**\n * Check if operator is a comparison operator that takes two values\n * These operators cannot accept null/undefined as values\n * (null comparisons in SQL always evaluate to UNKNOWN)\n */\nfunction isComparisonOp(name: string): boolean {\n const comparisonOps = [`eq`, `gt`, `gte`, `lt`, `lte`, `like`, `ilike`]\n return comparisonOps.includes(name)\n}\n\n/**\n * Checks if the operator is a comparison operator (excluding eq)\n * These operators don't work on booleans in PostgreSQL without casting\n */\nfunction isBooleanComparisonOp(name: string): boolean {\n return [`gt`, `gte`, `lt`, `lte`].includes(name)\n}\n\nfunction getOpName(name: string): string {\n const opNames = {\n eq: `=`,\n gt: `>`,\n gte: `>=`,\n lt: `<`,\n lte: `<=`,\n add: `+`,\n and: `AND`,\n or: `OR`,\n not: `NOT`,\n isUndefined: `IS NULL`,\n isNull: `IS NULL`,\n in: `= ANY`, // Use = ANY syntax for array parameters\n like: `LIKE`,\n ilike: `ILIKE`,\n upper: `UPPER`,\n lower: `LOWER`,\n length: `LENGTH`,\n concat: `CONCAT`,\n coalesce: `COALESCE`,\n }\n\n const opName = opNames[name as keyof typeof opNames]\n\n if (!opName) {\n throw new Error(`Unknown operator/function: ${name}`)\n }\n\n return opName\n}\n"],"names":[],"mappings":";AA0BO,SAAS,WACd,SACA,gBACc;AACd,QAAM,EAAE,OAAO,SAAS,MAAA,IAAU;AAClC,QAAM,mBAAmB,gBAAgB;AAEzC,QAAM,SAAmB,CAAA;AACzB,QAAM,cAAiC,EAAE,OAAA;AAEzC,MAAI,OAAO;AAGT,gBAAY,QAAQ,uBAAuB,OAAO,QAAQ,gBAAgB;AAAA,EAC5E;AAEA,MAAI,SAAS;AACX,gBAAY,UAAU,eAAe,SAAS,QAAQ,gBAAgB;AAAA,EACxE;AAEA,MAAI,OAAO;AACT,gBAAY,QAAQ;AAAA,EACtB;AAKA,MAAI,CAAC,OAAO;AACV,gBAAY,QAAQ;AAAA,EACtB;AAIA,QAAM,eAAe,OAAO;AAAA,IAC1B,CAAC,KAAK,OAAO,UAAU;AACrB,YAAM,aAAa,UAAU,KAAK;AAGlC,UAAI,SAAS,MAAM;AACjB,YAAI,GAAG,QAAQ,CAAC,EAAE,IAAI;AAAA,MACxB;AACA,aAAO;AAAA,IACT;AAAA,IACA,CAAA;AAAA,EAAC;AAGH,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,EAAA;AAEZ;AASA,SA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AAA,IACJ,IAAI;AAAA,IACJ,KAAK;AAAA,IACL,IAAI;AAAA,IACJ,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,IAAI;AAAA,IACJ,KAAK;AAAA,IACL,aAAa;AAAA,IACb,QAAQ;AAAA,IACR,IAAI;AAAA;AAAA,IACJ,MAAM;AAAA,IACN,OAAO;AAAA,IACP,OAAO;AAAA,IACP,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,UAAU;AAAA,EAAA;AAGZ,QAAM,SAAS,QAAQ,IAA4B;AAEnD,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,8BAA8B,IAAI,EAAE;AAAA,EACtD;AAEA,SAAO;AACT;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/electric-db-collection",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.26",
|
|
4
4
|
"description": "ElectricSQL collection for TanStack DB",
|
|
5
5
|
"author": "Kyle Mathews",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"@standard-schema/spec": "^1.1.0",
|
|
44
44
|
"@tanstack/store": "^0.8.0",
|
|
45
45
|
"debug": "^4.4.3",
|
|
46
|
-
"@tanstack/db": "0.5.
|
|
46
|
+
"@tanstack/db": "0.5.21"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/debug": "^4.1.12",
|
package/src/electric.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
removeTagFromIndex,
|
|
23
23
|
tagMatchesPattern,
|
|
24
24
|
} from './tag-index'
|
|
25
|
+
import type { ColumnEncoder } from './sql-compiler'
|
|
25
26
|
import type {
|
|
26
27
|
MoveOutPattern,
|
|
27
28
|
MoveTag,
|
|
@@ -347,6 +348,7 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
|
|
|
347
348
|
write,
|
|
348
349
|
commit,
|
|
349
350
|
collectionId,
|
|
351
|
+
encodeColumnName,
|
|
350
352
|
}: {
|
|
351
353
|
stream: ShapeStream<T>
|
|
352
354
|
syncMode: ElectricSyncMode
|
|
@@ -359,17 +361,24 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
|
|
|
359
361
|
}) => void
|
|
360
362
|
commit: () => void
|
|
361
363
|
collectionId?: string
|
|
364
|
+
/**
|
|
365
|
+
* Optional function to encode column names (e.g., camelCase to snake_case).
|
|
366
|
+
* This is typically the `encode` function from shapeOptions.columnMapper.
|
|
367
|
+
*/
|
|
368
|
+
encodeColumnName?: ColumnEncoder
|
|
362
369
|
}): DeduplicatedLoadSubset | null {
|
|
363
370
|
// Eager mode doesn't need subset loading
|
|
364
371
|
if (syncMode === `eager`) {
|
|
365
372
|
return null
|
|
366
373
|
}
|
|
367
374
|
|
|
375
|
+
const compileOptions = encodeColumnName ? { encodeColumnName } : undefined
|
|
376
|
+
|
|
368
377
|
const loadSubset = async (opts: LoadSubsetOptions) => {
|
|
369
378
|
// In progressive mode, use fetchSnapshot during snapshot phase
|
|
370
379
|
if (isBufferingInitialSync()) {
|
|
371
380
|
// Progressive mode snapshot phase: fetch and apply immediately
|
|
372
|
-
const snapshotParams = compileSQL<T>(opts)
|
|
381
|
+
const snapshotParams = compileSQL<T>(opts, compileOptions)
|
|
373
382
|
try {
|
|
374
383
|
const { data: rows } = await stream.fetchSnapshot(snapshotParams)
|
|
375
384
|
|
|
@@ -428,7 +437,10 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
|
|
|
428
437
|
orderBy,
|
|
429
438
|
// No limit - get all ties
|
|
430
439
|
}
|
|
431
|
-
const whereCurrentParams = compileSQL<T>(
|
|
440
|
+
const whereCurrentParams = compileSQL<T>(
|
|
441
|
+
whereCurrentOpts,
|
|
442
|
+
compileOptions,
|
|
443
|
+
)
|
|
432
444
|
promises.push(stream.requestSnapshot(whereCurrentParams))
|
|
433
445
|
|
|
434
446
|
debug(
|
|
@@ -442,7 +454,7 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
|
|
|
442
454
|
orderBy,
|
|
443
455
|
limit,
|
|
444
456
|
}
|
|
445
|
-
const whereFromParams = compileSQL<T>(whereFromOpts)
|
|
457
|
+
const whereFromParams = compileSQL<T>(whereFromOpts, compileOptions)
|
|
446
458
|
promises.push(stream.requestSnapshot(whereFromParams))
|
|
447
459
|
|
|
448
460
|
debug(
|
|
@@ -453,7 +465,7 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
|
|
|
453
465
|
await Promise.all(promises)
|
|
454
466
|
} else {
|
|
455
467
|
// No cursor - standard single request
|
|
456
|
-
const snapshotParams = compileSQL<T>(opts)
|
|
468
|
+
const snapshotParams = compileSQL<T>(opts, compileOptions)
|
|
457
469
|
await stream.requestSnapshot(snapshotParams)
|
|
458
470
|
}
|
|
459
471
|
}
|
|
@@ -1296,6 +1308,9 @@ function createElectricSync<T extends Row<unknown>>(
|
|
|
1296
1308
|
write,
|
|
1297
1309
|
commit,
|
|
1298
1310
|
collectionId,
|
|
1311
|
+
// Pass the columnMapper's encode function to transform column names
|
|
1312
|
+
// (e.g., camelCase to snake_case) when compiling SQL for subset queries
|
|
1313
|
+
encodeColumnName: shapeOptions.columnMapper?.encode,
|
|
1299
1314
|
})
|
|
1300
1315
|
|
|
1301
1316
|
unsubscribeStream = stream.subscribe((messages: Array<Message<T>>) => {
|
package/src/sql-compiler.ts
CHANGED
|
@@ -6,8 +6,30 @@ export type CompiledSqlRecord = Omit<SubsetParams, `params`> & {
|
|
|
6
6
|
params?: Array<unknown>
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Optional function to encode column names (e.g., camelCase to snake_case)
|
|
11
|
+
* This is typically the `encode` function from a columnMapper
|
|
12
|
+
*/
|
|
13
|
+
export type ColumnEncoder = (columnName: string) => string
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for SQL compilation
|
|
17
|
+
*/
|
|
18
|
+
export interface CompileSQLOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Optional function to encode column names before quoting.
|
|
21
|
+
* Used to transform property names (e.g., camelCase) to database column names (e.g., snake_case).
|
|
22
|
+
* This should be the `encode` function from shapeOptions.columnMapper.
|
|
23
|
+
*/
|
|
24
|
+
encodeColumnName?: ColumnEncoder
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function compileSQL<T>(
|
|
28
|
+
options: LoadSubsetOptions,
|
|
29
|
+
compileOptions?: CompileSQLOptions,
|
|
30
|
+
): SubsetParams {
|
|
10
31
|
const { where, orderBy, limit } = options
|
|
32
|
+
const encodeColumnName = compileOptions?.encodeColumnName
|
|
11
33
|
|
|
12
34
|
const params: Array<T> = []
|
|
13
35
|
const compiledSQL: CompiledSqlRecord = { params }
|
|
@@ -15,11 +37,11 @@ export function compileSQL<T>(options: LoadSubsetOptions): SubsetParams {
|
|
|
15
37
|
if (where) {
|
|
16
38
|
// TODO: this only works when the where expression's PropRefs directly reference a column of the collection
|
|
17
39
|
// doesn't work if it goes through aliases because then we need to know the entire query to be able to follow the reference until the base collection (cf. followRef function)
|
|
18
|
-
compiledSQL.where = compileBasicExpression(where, params)
|
|
40
|
+
compiledSQL.where = compileBasicExpression(where, params, encodeColumnName)
|
|
19
41
|
}
|
|
20
42
|
|
|
21
43
|
if (orderBy) {
|
|
22
|
-
compiledSQL.orderBy = compileOrderBy(orderBy, params)
|
|
44
|
+
compiledSQL.orderBy = compileOrderBy(orderBy, params, encodeColumnName)
|
|
23
45
|
}
|
|
24
46
|
|
|
25
47
|
if (limit) {
|
|
@@ -58,21 +80,28 @@ export function compileSQL<T>(options: LoadSubsetOptions): SubsetParams {
|
|
|
58
80
|
* Quote PostgreSQL identifiers to handle mixed case column names correctly.
|
|
59
81
|
* Electric/Postgres requires quotes for case-sensitive identifiers.
|
|
60
82
|
* @param name - The identifier to quote
|
|
83
|
+
* @param encodeColumnName - Optional function to encode the column name before quoting (e.g., camelCase to snake_case)
|
|
61
84
|
* @returns The quoted identifier
|
|
62
85
|
*/
|
|
63
|
-
function quoteIdentifier(
|
|
64
|
-
|
|
86
|
+
function quoteIdentifier(
|
|
87
|
+
name: string,
|
|
88
|
+
encodeColumnName?: ColumnEncoder,
|
|
89
|
+
): string {
|
|
90
|
+
const columnName = encodeColumnName ? encodeColumnName(name) : name
|
|
91
|
+
return `"${columnName}"`
|
|
65
92
|
}
|
|
66
93
|
|
|
67
94
|
/**
|
|
68
95
|
* Compiles the expression to a SQL string and mutates the params array with the values.
|
|
69
96
|
* @param exp - The expression to compile
|
|
70
97
|
* @param params - The params array
|
|
98
|
+
* @param encodeColumnName - Optional function to encode column names (e.g., camelCase to snake_case)
|
|
71
99
|
* @returns The compiled SQL string
|
|
72
100
|
*/
|
|
73
101
|
function compileBasicExpression(
|
|
74
102
|
exp: IR.BasicExpression<unknown>,
|
|
75
103
|
params: Array<unknown>,
|
|
104
|
+
encodeColumnName?: ColumnEncoder,
|
|
76
105
|
): string {
|
|
77
106
|
switch (exp.type) {
|
|
78
107
|
case `val`:
|
|
@@ -85,17 +114,21 @@ function compileBasicExpression(
|
|
|
85
114
|
`Compiler can't handle nested properties: ${exp.path.join(`.`)}`,
|
|
86
115
|
)
|
|
87
116
|
}
|
|
88
|
-
return quoteIdentifier(exp.path[0]
|
|
117
|
+
return quoteIdentifier(exp.path[0]!, encodeColumnName)
|
|
89
118
|
case `func`:
|
|
90
|
-
return compileFunction(exp, params)
|
|
119
|
+
return compileFunction(exp, params, encodeColumnName)
|
|
91
120
|
default:
|
|
92
121
|
throw new Error(`Unknown expression type`)
|
|
93
122
|
}
|
|
94
123
|
}
|
|
95
124
|
|
|
96
|
-
function compileOrderBy(
|
|
125
|
+
function compileOrderBy(
|
|
126
|
+
orderBy: IR.OrderBy,
|
|
127
|
+
params: Array<unknown>,
|
|
128
|
+
encodeColumnName?: ColumnEncoder,
|
|
129
|
+
): string {
|
|
97
130
|
const compiledOrderByClauses = orderBy.map((clause: IR.OrderByClause) =>
|
|
98
|
-
compileOrderByClause(clause, params),
|
|
131
|
+
compileOrderByClause(clause, params, encodeColumnName),
|
|
99
132
|
)
|
|
100
133
|
return compiledOrderByClauses.join(`,`)
|
|
101
134
|
}
|
|
@@ -103,11 +136,12 @@ function compileOrderBy(orderBy: IR.OrderBy, params: Array<unknown>): string {
|
|
|
103
136
|
function compileOrderByClause(
|
|
104
137
|
clause: IR.OrderByClause,
|
|
105
138
|
params: Array<unknown>,
|
|
139
|
+
encodeColumnName?: ColumnEncoder,
|
|
106
140
|
): string {
|
|
107
141
|
// FIXME: We should handle stringSort and locale.
|
|
108
142
|
// Correctly supporting them is tricky as it depends on Postgres' collation
|
|
109
143
|
const { expression, compareOptions } = clause
|
|
110
|
-
let sql = compileBasicExpression(expression, params)
|
|
144
|
+
let sql = compileBasicExpression(expression, params, encodeColumnName)
|
|
111
145
|
|
|
112
146
|
if (compareOptions.direction === `desc`) {
|
|
113
147
|
sql = `${sql} DESC`
|
|
@@ -134,6 +168,7 @@ function isNullValue(exp: IR.BasicExpression<unknown>): boolean {
|
|
|
134
168
|
function compileFunction(
|
|
135
169
|
exp: IR.Func<unknown>,
|
|
136
170
|
params: Array<unknown> = [],
|
|
171
|
+
encodeColumnName?: ColumnEncoder,
|
|
137
172
|
): string {
|
|
138
173
|
const { name, args } = exp
|
|
139
174
|
|
|
@@ -160,7 +195,7 @@ function compileFunction(
|
|
|
160
195
|
}
|
|
161
196
|
|
|
162
197
|
const compiledArgs = args.map((arg: IR.BasicExpression) =>
|
|
163
|
-
compileBasicExpression(arg, params),
|
|
198
|
+
compileBasicExpression(arg, params, encodeColumnName),
|
|
164
199
|
)
|
|
165
200
|
|
|
166
201
|
// Special case for IS NULL / IS NOT NULL - these are postfix operators
|
|
@@ -181,7 +216,11 @@ function compileFunction(
|
|
|
181
216
|
if (arg && arg.type === `func`) {
|
|
182
217
|
const funcArg = arg
|
|
183
218
|
if (funcArg.name === `isNull` || funcArg.name === `isUndefined`) {
|
|
184
|
-
const innerArg = compileBasicExpression(
|
|
219
|
+
const innerArg = compileBasicExpression(
|
|
220
|
+
funcArg.args[0]!,
|
|
221
|
+
params,
|
|
222
|
+
encodeColumnName,
|
|
223
|
+
)
|
|
185
224
|
return `${innerArg} IS NOT NULL`
|
|
186
225
|
}
|
|
187
226
|
}
|
|
@@ -270,7 +309,11 @@ function compileFunction(
|
|
|
270
309
|
params.pop() // remove LHS (boolean)
|
|
271
310
|
|
|
272
311
|
// Recompile RHS to get fresh param
|
|
273
|
-
const rhsCompiled = compileBasicExpression(
|
|
312
|
+
const rhsCompiled = compileBasicExpression(
|
|
313
|
+
rhsArg!,
|
|
314
|
+
params,
|
|
315
|
+
encodeColumnName,
|
|
316
|
+
)
|
|
274
317
|
|
|
275
318
|
// Transform: flip the comparison (val op col → col flipped_op val)
|
|
276
319
|
if (name === `lt`) {
|