{"id":20513782,"url":"https://github.com/webreflection/sql.js","last_synced_at":"2025-04-14T00:01:27.371Z","repository":{"id":243297658,"uuid":"812020754","full_name":"WebReflection/sql.js","owner":"WebReflection","description":"An easier to bootstrap sql.js","archived":false,"fork":false,"pushed_at":"2025-01-09T12:28:06.000Z","size":2968,"stargazers_count":24,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-09T00:56:03.800Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-07T19:33:11.000Z","updated_at":"2025-02-05T21:12:03.000Z","dependencies_parsed_at":"2024-12-09T11:28:21.978Z","dependency_job_id":"d8a99f3d-73b7-417d-97a8-c0ced2925535","html_url":"https://github.com/WebReflection/sql.js","commit_stats":null,"previous_names":["webreflection/sql.js"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsql.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsql.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsql.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsql.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/sql.js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248799931,"owners_count":21163403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T21:13:08.222Z","updated_at":"2025-04-14T00:01:27.285Z","avatar_url":"https://github.com/WebReflection.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @webreflection/sql.js\n\nAn ESM re-packaged [sql.js](https://sql.js.org/) with embedded `sqlite.wasm` as buffer:\n\n  * same `initSqlJs` API and functionalities\n  * zero need to host the `sqlite-wasm.wasm` file a part\n  * works already in both main or workers\n\n```js\n// works in both main and workers\nimport initSqlJs from 'https://esm.run/@webreflection/sql.js';\n\nconst SQL = await initSqlJs();\n\nconst db = new SQL.Database();\n\ndb.run(`\nCREATE TABLE hello (a int, b char);\nINSERT INTO hello VALUES (0, 'hello');\nINSERT INTO hello VALUES (1, 'world');\n`); // Run the query without returning anything\n\n// Prepare an sql statement\nconst stmt = db.prepare(\"SELECT * FROM hello WHERE a=:aval AND b=:bval\");\n\n// Bind values to the parameters and fetch the results of the query\nconst result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});\nconsole.log(result); // Will print {a:1, b:'world'}\n\ndb.close();\n```\n\n## @webreflection/sql.js/persistent\n\nBased on [IndexedDB](https://github.com/WebReflection/idb-map?tab=readme-ov-file#idbmapsync-api),\nthis export provides an extend of the [SQL.Database](https://sql.js.org/documentation/Database.html)\nclass with an additional `delete()` and `save()` methods plus a *smart constructor* that does the following:\n\n```js\nimport Database from 'https://esm.run/@webreflection/sql.js/persistent';\n\nconst db = new Database(\n  // the persistent name of such db\n  'data.db',\n  // an optional buffer to use when\n  // no such name exists in the storage\n  someOptionalBufferToInit\n);\n\n// create a table only if not present\ndb.run('CREATE TABLE IF NOT EXISTS hello (a int, b char)');\n\n// do anything you would do with an SQLite DB\n\n// save it, optionally awaiting for it\nawait db.save();\n\n// close it or keep going\ndb.close();\n```\n\nThe constructor signature allows to create or reuse a previously stored DB, providing eventually a DB that existed already and that's exported as buffer or *Uint8Array*.\n\nWhen previously stored, the next time that DB would be used instead of such provided initial DB, simplifying the orchestration between new user or freed cache VS some data to crawl anyway for such user.\n\nIt is also possible to `db.delete()` (optionally awaitable) some file ti be sure the storage is clean and to migrate from a version of a DB to another one.\n\nLike it is for the default export of this module, `/persistent` works in both main thread and workers.\n\n### Warnings ⚠️\n\nThe last `db.save()` operation will define the state of the database.\n\nThis export works if you are running it on a single Page (PWA, Electron, etc.) but if you are running the same thing in multiple tabs last `.save()` will last, not the others.\n\nTo solve this issue I've provided also the following `shared` variant.\n\n\n## @webreflection/sql.js/shared\n\nThis export is an *async* version of the *persistent Database* that bootstraps a *SharedWorker* so that multiple tabs can read and/or write to the same *db* where `db.save()` invokes will be queue to avoid concurrent saves within the worker.\n\nThe **prepare statement** is currently missing due to the nature of the cursor and the needed orchestration behind but I am not excluding its availability in the future.\n\nAlmost all other methods are supported and provide an async version of the [official API](https://sql.js.org/documentation/Database.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fsql.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebreflection%2Fsql.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fsql.js/lists"}