Skip to content

Commit e9622ae

Browse files
committed
Fix #10 - Added both create_function and close
1 parent d207f58 commit e9622ae

File tree

15 files changed

+104
-36
lines changed

15 files changed

+104
-36
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ The API in a nutshell is:
152152
* **query**: a template literal tag to simply query the database (no result returned)
153153
* **raw**: a template literal tag to represent static parts of the query (not values)
154154
* **transaction**: a function that returns a template literal tag to perform any statement until `tag.commit()` is awaited and executed.
155+
* **close**: a function to force save and close the db at any time.
156+
* **create_function**: a function to register custom SQLite functions. **Please note** when used as *Worker* it requires a **pure function** (no outer scope access) and **Function** evaluation. No limitations when used directly through init.
155157

156158
All tags, except the `raw` helper, are *asynchronous*, so that it's possible to *await* their result.
157159

cjs/init.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,18 @@ const init = (options = {}) => new Promise((resolve, onerror) => {
4040
let queue = Promise.resolve();
4141
const {result} = this;
4242
const db = new Database(result || options.database || new Uint8Array(0));
43-
const save = () => {
44-
queue = queue.then(() => new Promise((resolve, onerror) => {
45-
const uint8array = db.export();
46-
assign(store('readwrite').put(uint8array, keyPath).transaction, {
47-
oncomplete() {
48-
resolve();
49-
if (options.update)
50-
options.update(uint8array);
51-
},
52-
onabort: onerror,
53-
onerror
54-
});
55-
}));
56-
};
43+
const save = () => (queue = queue.then(() => new Promise((resolve, onerror) => {
44+
const uint8array = db.export();
45+
assign(store('readwrite').put(uint8array, keyPath).transaction, {
46+
oncomplete() {
47+
resolve();
48+
if (options.update)
49+
options.update(uint8array);
50+
},
51+
onabort: onerror,
52+
onerror
53+
});
54+
})));
5755
if (!result)
5856
save();
5957
const {all, get, query, raw, transaction} = SQLiteTag({
@@ -91,6 +89,11 @@ const init = (options = {}) => new Promise((resolve, onerror) => {
9189
let t = 0;
9290
resolve({
9391
all, get, raw, transaction,
92+
create_function: (name, func) => db.create_function(name, func),
93+
close: () => {
94+
clearTimeout(t);
95+
return save().then(() => db.close());
96+
},
9497
query(template) {
9598
if (/\b(?:INSERT|DELETE|UPDATE)\b/i.test(template[0])) {
9699
clearTimeout(t);

cjs/sqlite-worker.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ function SQLiteWorker(options = {}) {
5757
}}
5858
);
5959
},
60+
close: () => post('close'),
61+
create_function: (name, func) => post(
62+
'create_function',
63+
[name, func.toString()]
64+
),
6065
all: query('all'),
6166
get: query('get'),
6267
query: q,

cjs/worker.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ addEventListener('message', ({data: {id, action, options}}) => {
1212
({message: error}) => postMessage({id, error})
1313
);
1414
}
15+
else if (action === 'close') {
16+
db.then(module => module.close().then(
17+
() => {
18+
postMessage({id});
19+
},
20+
({message: error}) => {
21+
postMessage({id, error});
22+
}
23+
));
24+
}
25+
else if (action === 'create_function') {
26+
db.then(module => {
27+
try {
28+
const [name, func] = options;
29+
const result = module[action](name, Function('return ' + func)());
30+
postMessage({id, result});
31+
}
32+
catch({message: error}) {
33+
postMessage({id, error});
34+
}
35+
});
36+
}
1537
// action === `all` || `get` || `query`
1638
else {
1739
const {template, values} = options;

dist/index.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.

dist/init.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.

0 commit comments

Comments
 (0)