Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created April 14, 2024 21:43
Show Gist options
  • Save Ciantic/45ff2e2b44fd38c1a81460636d8cd420 to your computer and use it in GitHub Desktop.
Save Ciantic/45ff2e2b44fd38c1a81460636d8cd420 to your computer and use it in GitHub Desktop.
Deno example of mmomtchev/sqlite-wasm-http
import { createSQLiteThread, createHttpBackend } from "npm:sqlite-wasm-http";
// This is required hack:
const OldWorker = globalThis.Worker;
// @ts-ignore: Default to module workers
globalThis.Worker = class {
constructor(url: URL, opts: any = {}) {
return new OldWorker(url, { type: "module", ...opts });
}
};
// This is copied directly from the example
(async () => {
// MBTiles is a common format for storing both vector and raster maps in an SQLite database
const remoteURL = "https://velivole.b-cdn.net/maptiler-osm-2017-07-03-v3.6.1-europe.mbtiles";
const httpBackend = createHttpBackend({
maxPageSize: 4096, // this is the current default SQLite page size
timeout: 10000, // 10s
cacheSize: 4096, // 4 MB
});
// Multiple DB workers can be created, all sharing the same backend cache
const db = await createSQLiteThread({ http: httpBackend });
await db("open", { filename: "file:" + encodeURI(remoteURL), vfs: "http" });
await db("exec", {
sql:
"SELECT zoom_level, tile_column, tile_row, tile_data FROM tiles " +
"WHERE zoom_level = 10 AND tile_column = $col AND tile_row = $row",
bind: { $col: 600, $row: 600 },
callback: (msg) => {
if (msg.row) {
console.log(msg.columnNames);
console.log(msg.row);
} else {
console.log("end");
}
},
});
// This closes the DB connection
await db("close", {});
// This terminates the SQLite worker
db.close();
await httpBackend.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment