Skip to content

Commit

Permalink
fix (DAL): parametrized squel queries (#438)
Browse files Browse the repository at this point in the history
Changes to the query builder of the DAL to use squel's support for
parametrized queries.
  • Loading branch information
kevin-dp authored Sep 18, 2023
1 parent ed4dd4b commit 3603703
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-garlics-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electric-sql": patch
---

Use parametrized SQL queries.
11 changes: 0 additions & 11 deletions clients/typescript/src/client/execution/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,11 @@ import { Row, Statement } from '../../util'
* some underlying drivers do not support promises.
*/
export interface DB {
run(
statement: string,
successCallback?: (tx: DB, res: RunResult) => void,
errorCallback?: (error: any) => void
): void
run(
statement: QueryBuilder,
successCallback?: (tx: DB, res: RunResult) => void,
errorCallback?: (error: any) => void
): void
query<Z>(
statement: string,
schema: z.ZodType<Z>,
successCallback: (tx: DB, res: Z[]) => void,
errorCallback?: (error: any) => void
): void
query<Z>(
statement: QueryBuilder,
schema: z.ZodType<Z>,
Expand Down
10 changes: 6 additions & 4 deletions clients/typescript/src/client/execution/nonTransactionalDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export class NonTransactionalDB implements DB {
constructor(private _adapter: DatabaseAdapter) {}

run(
statement: string | QueryBuilder,
statement: QueryBuilder,
successCallback?: (db: DB, res: RunResult) => void,
errorCallback?: (error: any) => void
) {
const { text, values } = statement.toParam({ numberedParameters: false })
this._adapter
.run({ sql: statement.toString() })
.run({ sql: text, args: values })
.then((res) => {
if (typeof successCallback !== 'undefined') {
try {
Expand All @@ -33,13 +34,14 @@ export class NonTransactionalDB implements DB {
}

query<Z>(
statement: string | QueryBuilder,
statement: QueryBuilder,
schema: z.ZodType<Z>,
successCallback: (db: DB, res: Z[]) => void,
errorCallback?: (error: any) => void
) {
const { text, values } = statement.toParam({ numberedParameters: false })
this._adapter
.query({ sql: statement.toString() })
.query({ sql: text, args: values })
.then((rows) => {
try {
const objects = rows.map((row) => schema.parse(row))
Expand Down
10 changes: 6 additions & 4 deletions clients/typescript/src/client/execution/transactionalDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { Row, Statement } from '../../util'
export class TransactionalDB implements DB {
constructor(private _tx: Transaction) {}
run(
statement: QueryBuilder | string,
statement: QueryBuilder,
successCallback?: (db: DB, res: RunResult) => void,
errorCallback?: (error: any) => void
): void {
const { text, values } = statement.toParam({ numberedParameters: false })
this._tx.run(
{ sql: statement.toString() },
{ sql: text, args: values },
(tx, res) => {
if (typeof successCallback !== 'undefined')
successCallback(new TransactionalDB(tx), res)
Expand All @@ -22,13 +23,14 @@ export class TransactionalDB implements DB {
}

query<Z>(
statement: QueryBuilder | string,
statement: QueryBuilder,
schema: z.ZodType<Z>,
successCallback: (db: DB, res: Z[]) => void,
errorCallback?: (error: any) => void
): void {
const { text, values } = statement.toParam({ numberedParameters: false })
this._tx.query(
{ sql: statement.toString() },
{ sql: text, args: values },
(tx, rows) => {
if (typeof successCallback !== 'undefined') {
const objects = rows.map((row) => schema.parse(row)) //.partial().parse(row))
Expand Down
8 changes: 8 additions & 0 deletions clients/typescript/src/squel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {} from 'squel'

declare module 'squel' {
export interface ToParamOptions {
numberedParametersStartAt?: number
numberedParameters?: boolean
}
}

0 comments on commit 3603703

Please sign in to comment.