import { appendWhereClause } from './appendWhereCluase'; import type { Schema } from '../schema'; import type { PreparedQueries, TableColumnNames, TableColumnValue, TableRow } from '../types'; /** * Creates a simple update query for sqlite. * * @param {string} table Table name * @param {Object} set Set conditions for update query. * @param {Object} whereCondition Where condition for select query. * e.g., { id: 'vishal', cid: ['messaging:id1', 'messaging:id2'] }. * All the conditions will be joined with AND in final query. * @returns {string} Final update query for sqlite */ export const createUpdateQuery = ( table: T, set: Partial>, whereCondition: Partial<{ [k in TableColumnNames]: TableColumnValue | TableColumnValue[] }>, ): PreparedQueries => { const fields = Object.keys(set).map((key) => `${key} = ?`); const updateQuery = `UPDATE OR IGNORE ${table} SET ${fields.join(',')}`; const [updateQueryWithWhere, whereParams] = appendWhereClause(updateQuery, whereCondition); return [`${updateQueryWithWhere}`, [...Object.values(set), ...(whereParams || [])]]; };