0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

sql-jsの導入部分でハマった

Posted at

Node.jsではなく、Chrome Extensionなどのブラウザ上でのjavascriptでの利用を想定。

GitHub: https://github.com/sql-js/sql.js
CDN: https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.4.0/dist/sql-wasm.js

導入方法(initSqlJs)

ここだけが難関でした。
外部依存のwasm(Web-Assembly...)を利用するための手続きらしいです。
このfileの先の戻り値が適切な値でないと動きません。

SQL = await initSqlJs({locateFile: file =>
     `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.4.0/dist/${file}`});

sql読み込み・操作

dataはbinary dataの形で引き渡します。

//dl with github rest api
data = await fetch("https://api.github.com/repos/XXXXXX/XXXXXXXX/git/blobs/XXXXXXXXXXXX")
    .then(d=>d.json()).then(d=>atob(d.content));
//読み込み
db = new SQL.Database(data)

//SQLの操作
texts = db.exec("select * from texts")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?