bramski / angular-indexedDB

An angularjs serviceprovider to utilize indexedDB with angular
165 stars 49 forks source link

How to filter against multiple indexes? #25

Closed mhsimkin closed 9 years ago

mhsimkin commented 9 years ago

I'm trying to figure out how I can filter my data against multiple columns.

The schema for the object being stored in indexeddb is:

"Environment": { type: "string", editable: false, nullable: false },
"SequenceId": { type: "string", editable: false, nullable: false },
"LogLevel": { type: "string", editable: false, nullable: false },
"ServerTime": { type: "date", editable: false, nullable: false },
"Program": { type: "string", editable: false, nullable: false },
"Subject": { type: "string", editable: false, nullable: true },
"Message": { type: "string", editable: false, nullable: true },
"Exception": { type: "string", editable: false, nullable: true },
"User": { type: "string", editable: false, nullable: false },
"MachineName": { type: "string", editable: false, nullable: false },
"InstanceId": { type: "string", editable: false, nullable: false },
"ComponentId": { type: "string", editable: false, nullable: false },
"ScribeId": { type: "string", editable: false, nullable: false },

I have set up and configured my object store as:

 $indexedDBProvider.connection("ScribeMessagesDB").
        upgradeDatabase(1, function (event, db, tx) {
                var objStore = db.createObjectStore("ScribeMessages", {
                      keyPath: ["Environment", "SequenceId"], unique: true });
                objStore.createIndex("program_idx", "Program", { unique: false });
                objStore.createIndex("logLevel_idx", "LogLevel", { unique: false });
                objStore.createIndex("machineName_idx", "MachineName", { unique: false });
                objStore.createIndex("user_idx", "User", { unique: false });
                objStore.createIndex("environment", "Environment", { unique: false });
                objStore.createIndex("sequenceId", "SequenceId", { unique: false });
    });

I am trying to filter this way:

$indexedDB.openStore("ScribeMessages", function(store) {
                            var query = store.query();
                            store.findWhere(query.$index("environment").$eq("REN-Prod").$index("sequenceId").$gt(0).$desc(false)).then(function(limitedObjects) {
                                e.success(limitedObjects);
                            });
                        });

I should not be getting any values back, as the "Environment" column in the doesn't contain any values for "REN-Prod".

Also, how do I just query against the primary key?

thank you for your help.

marc

bramski commented 9 years ago

This is really difficult to read, can you please use github markup properly?

bramski commented 9 years ago

So the query interface really isn't very complex. You can't query on multiple indexes. Only the last call to $index is going to be effective. Read the source code, it's pretty stupid simple. Same thing with all of the $lt, $gt, and $lte functions, you're basically overwriting the other query parameters. IndexedDB only supports a basic range check in the iterator.

It's just a thin wrapper around the IDBKeyRange interface: https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange

So your querying ability is limited by that; and angular-indexed-db essentially implements that. Any further filtering on "secondary indices" you'll have to implement on your own after the fact or build a compound index on which you can execute your query.

bramski commented 9 years ago

In chrome it seems that the main index is just called "key", and by default a query executes against this. May vary per browser? I can't find any standards on it's name. If you specify no index for your query you should execute against the primary key.

I haven't used compound indices at all though, so if you get that working do post an example about how it works! I'm guessing you just specify a tuple to run the query? Best of luck.

mhsimkin commented 9 years ago

Thank you for all the help. If I figure out how to use compound keys, I will let you know.

bramski commented 9 years ago

Figure this out? I'm closing this issue.