serialization of query/WhereClause?

44 views
Skip to first unread message

Simon Weber

unread,
Nov 1, 2015, 11:11:20 AM11/1/15
to Dexie.js
Hi! I'm using Dexie to back a database of songs. Supporting autoplaylists requires users to be able to specify arbitrary queries. This all works well.

Now, I need to serialize these queries somehow, since the definition of an autoplaylist needs to be remembered. There doesn't seem to be a built in way to do this -- I was hoping WhereClause could to serialize to json.

If that's correct, does anyone have suggestions? Serialization of most query languages is baked in, so I wanted to ask before recreating a wheel.

ps, I'm willing to use eval if it makes life significantly easier, but I'd prefer to avoid it.

David Fahlander

unread,
Nov 2, 2015, 5:03:38 AM11/2/15
to Dexie.js
Hi Simon,

There's no built-in support for serializing querys so I'd recommend you to develop that feature. What you could look at is the _ctx property of the Collection class. It should contain all info needed to serialize the query. Some notes what to think about:

  • or() queries will reference two other Collections, so recursive serialization has to be supported
  • and() will store the javascript filter as a Function pointer. Function.toString() can be used to serialize them and reinstanciate through new Function() constructor. Be aware though that closures wont work so you'd need to be sure that expressions doesnt involve variables from the closure. For example
    db.friends.filter(function (friend) { return friend.name === "name"; })

    is ok, but: 
    db.friends.filter(function (friend) { return friend.name === localScopedNameVar; })

    is not.


Reply all
Reply to author
Forward
0 new messages