Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.

Commit 9e35aad

Browse files
committed
changed http backend sharing context to closest [hx-db],[hx-request] to allow disabling shared caching when needed
1 parent ecb12a2 commit 9e35aad

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Any feedback and suggestions are welcome!
1010

1111
This extension is a wrapper for [https://github.com/mmomtchev/sqlite-wasm-http]. Please have a look for more information of the underlying database access implementation.
1212

13-
The http backend is kept open for the lifetime of the element having the corresponding `hx-db` attribute.
13+
The http backend for a query is shared witin the closest element having either `hx-db` or `hx-request` attribute.
14+
15+
Note that the default `shared` backend type caches SQLite pages in memory. I'm not sure if there's any invalidation. If you have problems seeing changes in the database, try forcing the sync backend.
1416

1517
Following events are emitted:
1618
- `htmx:xhr:loadstart` when a query execution begins
@@ -131,5 +133,5 @@ Use one of attributes
131133
#### Override default config
132134

133135
```html
134-
<div hx-trigger="load" hx-get="SELECT * FROM mytable" hx-request="{ maxPageSize: 4096, timeout: 10000, cacheSize: 4096 }"></div>
136+
<div hx-trigger="load" hx-get="SELECT * FROM mytable" hx-request='{ "maxPageSize": 4096, "timeout": 10000, "cacheSize": 4096, "backendType": "sync" }'></div>
135137
```

dist/sqlite.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Extension to use SQLite database backend for Htmx over:
1616
maxPageSize: 4096, // this is the current default SQLite page size
1717
timeout: 10000, // 10s
1818
cacheSize: 4096 // 4 MB
19+
//backendType: 'sync' // 'sync' or 'shared'. Defaults to 'shared' if available, otherwise sync;
1920
};
2021
sqlConfig = {
2122
rowMode: 'object'
@@ -84,19 +85,21 @@ Extension to use SQLite database backend for Htmx over:
8485
...sqlConfig,
8586
...conf
8687
};
88+
89+
var contextElem = htmx.closest(elt, '[hx-db],[hx-request]');
8790

8891
var backend;
89-
if (dbElem._htmx_sqlite_http_backend) {
90-
backend = dbElem._htmx_sqlite_http_backend;
92+
if (contextElem._htmx_sqlite_http_backend) {
93+
backend = contextElem._htmx_sqlite_http_backend;
9194
} else {
9295
backend = dbURI.match(/^https?:/) ? { http: sqliteWasmHttp.createHttpBackend(config) }
9396
: {};
94-
dbElem._htmx_sqlite_http_backend = backend;
97+
contextElem._htmx_sqlite_http_backend = backend;
9598
}
96-
dbElem.addEventListener('htmx:beforeCleanupElement', function(ev) {
97-
if (ev.detail.elt == dbElem && dbElem._htmx_sqlite_http_backend && dbElem._htmx_sqlite_http_backend.http) {
98-
dbElem._htmx_sqlite_http_backend.http.close();
99-
delete dbElem._htmx_sqlite_http_backend;
99+
contextElem.addEventListener('htmx:beforeCleanupElement', function(ev) {
100+
if (ev.detail.elt == contextElem && contextElem._htmx_sqlite_http_backend && contextElem._htmx_sqlite_http_backend.http) {
101+
contextElem._htmx_sqlite_http_backend.http.close();
102+
delete contextElem._htmx_sqlite_http_backend;
100103
}
101104
});
102105

dist/sqlite.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "htmx-sqlite",
3-
"version": "1.9.1",
3+
"version": "1.9.2",
44
"description": "Htmx extension to use SQLite database backend over HTTP or OPFS",
55
"author": "Jyri-Matti Lähteenmäki <[email protected]>",
66
"keywords": [

src/sqlite.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Extension to use SQLite database backend for Htmx over:
1616
maxPageSize: 4096, // this is the current default SQLite page size
1717
timeout: 10000, // 10s
1818
cacheSize: 4096 // 4 MB
19+
//backendType: 'sync' // 'sync' or 'shared'. Defaults to 'shared' if available, otherwise sync;
1920
};
2021
sqlConfig = {
2122
rowMode: 'object'
@@ -84,19 +85,21 @@ Extension to use SQLite database backend for Htmx over:
8485
...sqlConfig,
8586
...conf
8687
};
88+
89+
var contextElem = htmx.closest(elt, '[hx-db],[hx-request]');
8790

8891
var backend;
89-
if (dbElem._htmx_sqlite_http_backend) {
90-
backend = dbElem._htmx_sqlite_http_backend;
92+
if (contextElem._htmx_sqlite_http_backend) {
93+
backend = contextElem._htmx_sqlite_http_backend;
9194
} else {
9295
backend = dbURI.match(/^https?:/) ? { http: sqliteWasmHttp.createHttpBackend(config) }
9396
: {};
94-
dbElem._htmx_sqlite_http_backend = backend;
97+
contextElem._htmx_sqlite_http_backend = backend;
9598
}
96-
dbElem.addEventListener('htmx:beforeCleanupElement', function(ev) {
97-
if (ev.detail.elt == dbElem && dbElem._htmx_sqlite_http_backend && dbElem._htmx_sqlite_http_backend.http) {
98-
dbElem._htmx_sqlite_http_backend.http.close();
99-
delete dbElem._htmx_sqlite_http_backend;
99+
contextElem.addEventListener('htmx:beforeCleanupElement', function(ev) {
100+
if (ev.detail.elt == contextElem && contextElem._htmx_sqlite_http_backend && contextElem._htmx_sqlite_http_backend.http) {
101+
contextElem._htmx_sqlite_http_backend.http.close();
102+
delete contextElem._htmx_sqlite_http_backend;
100103
}
101104
});
102105

test/sqlite.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ describe("sqlite extension", function() {
4545
});
4646
});
4747

48+
it('sync backend', function (done) {
49+
var div = make(`<div hx-trigger="load" hx-request='{"backendType": "sync"}' hx-get="SELECT * FROM mytable"></div>`);
50+
div.addEventListener('htmx:afterSwap', () => {
51+
div.innerText.should.equal('[]');
52+
done();
53+
});
54+
});
55+
4856
it('override http config', function (done) {
4957
window.addEventListener('unhandledrejection', e => {
5058
e.reason.should.equal('Timeout while waiting on backend');

0 commit comments

Comments
 (0)