MCPcopy Create free account
hub / github.com/TanStack/db / executeSqlInWorker

Function executeSqlInWorker

packages/browser-db-sqlite-persistence/src/opfs-worker.ts:191–252  ·  view source on GitHub ↗
(
  sql: string,
  params: ReadonlyArray<unknown>,
)

Source from the content-addressed store, hash-verified

189}
190
191async function executeSqlInWorker(
192 sql: string,
193 params: ReadonlyArray<unknown>,
194): Promise<ReadonlyArray<Record<string, unknown>>> {
195 const { sqlite3, dbHandle } = getInitializedDatabaseState()
196 const rows = new Array<Record<string, unknown>>()
197 let parametersBound = false
198
199 for await (const statement of sqlite3.statements(dbHandle, sql)) {
200 if (params.length > 0) {
201 if (parametersBound) {
202 throw new InvalidPersistedCollectionConfigError(
203 `wa-sqlite worker only supports parameter binding for a single SQL statement`,
204 )
205 }
206
207 sqlite3.bind_collection(
208 statement,
209 params.map((param) => toBindableValue(param)),
210 )
211 parametersBound = true
212 }
213
214 let columns = [...sqlite3.column_names(statement)]
215 for (;;) {
216 const stepResult = await sqlite3.step(statement)
217
218 if (stepResult === SQLITE_ROW) {
219 if (columns.length === 0) {
220 columns = [...sqlite3.column_names(statement)]
221 }
222 const values = sqlite3.row(statement)
223 const row: Record<string, unknown> = {}
224 for (let columnIndex = 0; columnIndex < columns.length; columnIndex++) {
225 const columnName = columns[columnIndex]
226 if (!columnName) {
227 continue
228 }
229 row[columnName] = values[columnIndex]
230 }
231 rows.push(row)
232 continue
233 }
234
235 if (stepResult === SQLITE_DONE) {
236 break
237 }
238
239 throw new InvalidPersistedCollectionConfigError(
240 `wa-sqlite step returned unexpected result code: ${String(stepResult)}`,
241 )
242 }
243 }
244
245 if (params.length > 0 && !parametersBound) {
246 throw new InvalidPersistedCollectionConfigError(
247 `SQL query parameters were provided but no statement accepted bindings`,
248 )

Callers 1

handleWorkerRequestFunction · 0.85

Calls 4

toBindableValueFunction · 0.85
stepMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected