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

[WebSQL] How to avoid duplicate index definition? #98

Open iseki-masaya opened 8 years ago

iseki-masaya commented 8 years ago

Hi,

I run the below code at Google Chrome(v48) and Safari(v9) on MacOSX(v10.11).

var db = new ydn.db.Storage('MyDB', {
 stores: [
  {
    name: 'test',
    keyPath: 'id',
    indexes: [
       {
      keyPath: ['first', 'last']
       },
       {
         keyPath: 'first'
       },
       {
         keyPath: 'last'
       }
     ]
}, {mechanisms: ['websql']});

db.put('test', {id: 1, first: 'First', last: 'Last'});
db.put('test', {id: 2, first: 'First', last: 'Last'});

db.from('test').where('first', '=', 'First').order('last').list(20)
.done( (data) => {
  console.log(data);
})
.fail( (err) => {
  console.log(err);
})

This script works correctly for the first time. However, I got the below error(Chrome) from the second time.

ydn.error.ArgumentException: index "first, last" already defined in store: test

There are no error using IndexedDB however we need to support iOS and Android. Is there way to avoid duplicate index definition?

Thanks

mehdirande commented 7 years ago

Hi, I'm also bothered by this issue, @iseki-masaya : a simple way to avoid this problem is to switch the order of youre indexes definition. By declaring the composite index in last, the bug is not trigered. @yathit : I might submit a PR about this bug, the fix seems to bo simple but i'm not sure about this. In ydn.db.schema.Store you use name.indexOf(otherName) >= 0 to match index name. By doing this first and last match first, last and that's what is triggering the bug. Since I don't understand why you use indexOf instead of = I'm not sure about what is going to break if I fix it this way..

yathit commented 7 years ago

@mehdirande As far I remember, WebSQL has problem with reflection on composite index.

Where is name.indexOf(otherName) >= 0? If you submit PR, I will see.

mehdirande commented 7 years ago

Okay done, thanks!