- From: Joshua Bell <notifications@github.com>
- Date: Fri, 15 Apr 2016 16:47:06 -0700
- To: w3c/IndexedDB <IndexedDB@noreply.github.com>
- Message-ID: <w3c/IndexedDB/issues/33/210686933@github.com>
[Worklets](https://drafts.css-houdini.org/worklets/) might be the way to go for this. They do require developers to be consistent, but that seems like an acceptable constraint.
Very rough sketch: I haven't thought this through in detail, but something like:
* IDBDatabase gets a `worklet` property; code can be loaded in via `db.worklet.import(...)`
* IDBWorklet exposes a `registerIndexExpression(name, func)` global function
* When creating an index, you can specify `name()` as the keyPath
Developers need to remember to call `db.worklet.import(...)` on every connection to the DB that might alter an index (create or add/update values). This import is also _async_, so maybe a new step in the open() phase. So the boilerplate would look like:
```js
var open = indexedDB.open('stuff');
open.onregisterworklet = function() {
var db = open.result;
db.worklet.import('my_worklet_code.js'); // returns Promise
};
open.onupgradeneeded = function() {
var db = open.result;
var store = db.createObjectStore('records');
store.createIndex('freeText', 'words()');
store.createIndex('area', 'area()');
};
open.onsuccess = function() {
var db = open.result;
...
};
```
And `my_worklet_code.js` would look like:
```js
registerIndexExpression('area', r => r.width * r.height);
registerIndexExpression('words', r => r.body.split(/\s+/));
```
---
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/IndexedDB/issues/33#issuecomment-210686933
Received on Friday, 15 April 2016 23:47:36 UTC