yathit / ydn-db

Javascript database module for Indexeddb, Web SQL and localStorage storage mechanisms supporting version migration, advanced query, SQL and transaction.
Apache License 2.0
503 stars 41 forks source link

generator function for index with keyPath array #96

Closed kalimantos closed 8 years ago

kalimantos commented 8 years ago

Hi, i know that it's possible to use generator for create an index with one field, for example:

indexes: [{
  name: 'my-index',
  generator: function(obj) { return obj.myField.toUpperCase() }
}]

but can i create a index with more than one field? (as using keyPath: ['myField1', 'myField2']) maybe as this?

indexes: [{
  name: 'myField1, myField2',
  generator: function(obj) { 
    return [
      obj.myField1.toUpperCase(),
      obj.myField2.toUpperCase()
    ];
  }
}]

thanks in advance!

yathit commented 8 years ago

Yes.

kalimantos commented 8 years ago

Hi, i'm using that that store schema:

{
  name: 'contact',
  keyPath: 'contactid',
  indexes: [
    {
      name: 'accountid, name',
      //keyPath: ['accountid', 'name'],
      generator: function (obj) {
        return [
          obj.accountid, 
          obj.name.toUpperCase()
        ];
      }
    }
  ]
}

if i use the keyPath all is ok, but if i use the generator, an error was given when i istantiate the db with

var db = ydn.db.Storage(config.dbName, dbSchema, {
    mechanisms: ['indexeddb', 'websql']
});

the error is: screen shot 2016-02-12 at 10 12 01

How can i make it work? Thank you

yathit commented 8 years ago

Then it is a bug.

yathit commented 8 years ago

Fixed in release 1.4.1. Thanks.

kalimantos commented 8 years ago

Thank you so much!!

kalimantos commented 8 years ago

Hi! i'm having some problems while query using the index under

{
  name: 'contact',
  keyPath: 'contactid',
  indexes: [
    {
      name: 'accountid, name',
      //keyPath: ['accountid', 'name'],
      generator: function (obj) {
        return [
          obj.accountid, 
          obj.name.toUpperCase()
        ];
      }
    }
  ]
}

if i query using that code:

//accountId is a string assuming the value of the account i need to filter
db.from('contact')
  .where('accountid', '=', accountId)
  .order('name')
  .list()
  .done(function(l){console.log(l)});

that error is raised: Uncaught ConstraintError: Require index "accountid, name" not found in store "contact"

How can i solve that? Thank you so much

kalimantos commented 8 years ago

replacing

//accountId is a string assuming the value of the account i need to filter
db.from('contact')
  .where('accountid', '=', accountId)
  .order('name')
  .list()
  .done(function(l){console.log(l)});

with

var keyRange = ydn.db.KeyRange.starts([accountid]);
db.values('contact', 'accountid, name', keyRange).done(function(l){console.log(l)});

it works!

yathit commented 8 years ago

@kalimantos looks like bug in Query.