Skip to content

Commit 0bd5839

Browse files
committed
feat: iterator util
1 parent 7433b15 commit 0bd5839

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,18 @@ import {
217217
exportDatabase,
218218
importDatabase,
219219
initSQLiteCore,
220+
iterator,
220221
run,
221222
} from '@subframe7536/sqlite-wasm'
222223

223224
const core = await initSQLiteCore(/* options */)
224225

225226
await importDatabase(core.vfs, core.path, stream)
226227

228+
for await (const row of iterator(core, 'select * from test')) {
229+
console.log(row)
230+
}
231+
227232
customFunctionCore(core, 'test', num => num)
228233
await run(core, 'select test(?)', [1])
229234

playground/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ document.querySelector('.worker')?.addEventListener('click', async () => {
119119
}
120120
worker.postMessage('')
121121
for await (const data of test()) {
122-
console.log('iterator', data)
122+
console.log('[stream]', data)
123123
}
124124
})
125125
document.querySelector('.importW')?.addEventListener('click', async () => {

playground/src/runSQL.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { iterator, type SQLiteDBCore } from '../../src'
2+
13
export async function runSQL(run: (sql: string, parameters?: any[]) => Promise<any>): Promise<void> {
24
await run('CREATE TABLE if not exists t1(a INTEGER, b INTEGER, c VARCHAR);')
35
await run('INSERT INTO t1 VALUES(1, 19147, \'nineteen thousand one hundred forty-seven\');')
@@ -16,3 +18,13 @@ export async function runSQLStream(
1618
await run('INSERT INTO t1 VALUES(3, 46582, \'forty-six thousand five hundred eighty-two\');')
1719
await stream(onData, 'select * from t1')
1820
}
21+
22+
export async function runIterator(
23+
core: SQLiteDBCore,
24+
): Promise<void> {
25+
const sql = 'INSERT INTO t1 VALUES(100, 19147, \'nineteen thousand one hundred forty-seven\'), (200, 26008, \'twenty-six thousand eight\'), (300, 46582, \'forty-six thousand five hundred eighty-two\');select * from t1;'
26+
const it = iterator(core, sql)
27+
for await (const row of it) {
28+
console.log('[iterator]', row)
29+
}
30+
}

playground/src/worker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { uuidv7 } from 'uuidv7'
22
import url from '../../dist/wa-sqlite.wasm?url'
3-
import { customFunction, initSQLite, isOpfsSupported, withExistDB } from '../../src'
3+
import { customFunction, initSQLite, isOpfsSupported, iterator, withExistDB } from '../../src'
44
import { useOpfsStorage } from '../../src/vfs/opfs'
5-
import { runSQLStream } from './runSQL'
5+
import { runIterator, runSQLStream } from './runSQL'
66

77
onmessage = async ({ data }) => {
88
if (!await isOpfsSupported()) {
@@ -17,10 +17,12 @@ onmessage = async ({ data }) => {
1717
postMessage(await db.dump())
1818
return
1919
}
20+
2021
// if (data) {
2122
// await db.sync(data)
2223
// }
2324
await runSQLStream(db.run, db.stream, data => postMessage(data))
25+
await runIterator(db)
2426
console.log(db.lastInsertRowId(), db.changes())
2527
customFunction(db.sqlite, db.pointer, 'uuidv7', () => uuidv7())
2628
console.log(

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,21 @@ export async function run(
220220
await stream(core, data => results.push(data), sql, parameters)
221221
return results
222222
}
223+
224+
export async function* iterator(
225+
core: SQLiteDBCore,
226+
sql: string,
227+
parameters?: SQLiteCompatibleType[],
228+
): AsyncIterableIterator<Record<string, SQLiteCompatibleType>> {
229+
const { sqlite, pointer } = core
230+
for await (const stmt of sqlite.statements(pointer, sql)) {
231+
if (parameters?.length) {
232+
sqlite.bind_collection(stmt, parameters)
233+
}
234+
const cols = sqlite.column_names(stmt)
235+
while (await sqlite.step(stmt) === SQLITE_ROW) {
236+
const row = sqlite.row(stmt)
237+
yield Object.fromEntries(cols.map((key, i) => [key, row[i]]))
238+
}
239+
}
240+
}

0 commit comments

Comments
 (0)