DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

Node.js SQLite each and Async Iterator

I mean this Node.js SQLite each.

Wonder if it is possible to use async iterator and call,

for await (const r of SQL_STATEMENT) {

}
Enter fullscreen mode Exit fullscreen mode

Or perhaps some other conveniences, like subscribe or observable?

Top comments (3)

Collapse
 
louy2 profile image
Yufan Lou • Edited on

The meat of Database#each is implemented in C++ unfortunately:
github.com/mapbox/node-sqlite3/blo...

Essentially the callback is sent over to the C++ side and scheduled over there.

Not sure how the C++ side can pass an async generator back to JavaScript.

Collapse
 
leoplaw profile image
leoplaw

This might be of use to you.

How to create an SQLite database in node.js

dev.to/forbeslindesay/how-to-creat...

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

Maybe atdatabases support async iterator via SQLQuery.querystream; but I am not so sure. Maybe tutorial is missing in this aspect.

The best (and simplest) driver for SQLite in Node.js would still be better-sqlite3.

Still, you have done well wrapping around pg (and also with TypeScript typings). I will have to borrow it for PostgreSQL.

Classic DEV Post from 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!