treojs / treo

Consistent API to IndexedDB
http://treojs.com
MIT License
257 stars 18 forks source link

cant use db.store('theName') if not rebuild schema #42

Closed osc2nuke closed 8 years ago

osc2nuke commented 8 years ago

Im want to using the standalone version of treo for my project. Inside it i made a function function myFunction(){} I fire it and all works out of the box as the "library" example.

The database is builded correctly and i not require anymore the creations of the tables as:

            var schema = window.treo.schema()
              .version(1)
                .addStore('books', { key: 'isbn' })
                .addIndex('byTitle', 'title', { unique: true })
                .addIndex('byAuthor', 'author')
              .version(2)
                .getStore('books')
                .addIndex('byYear', 'year')
              .version(3)
                .addStore('magazines')
                .addIndex('byPublisher', 'publisher')
                .addIndex('byFrequency', 'frequency');

So i now just use :

            var schema = window.treo.schema()
              .version(1);

Right? i also take out the batch process: books.batch([............ETC....});

Right? Now i expect to use the :

            books.index('byTitle').get('Bedrock Nights', function(err, book) {

                console.log(book);

                });

Cause the whole db and schema is handled via treo.js ..... right? But i get:

Uncaught TypeError: Cannot read property 'index' of undefined

I have tried everything i know.... but it keeps failing.

Hope to find quickly a solution!!! Greetings Henry!!!

alekseykulikov commented 8 years ago

Hey, I'm not sure what's the question is, but if you just copy/run example in console, it will work and give you a good starting point.

cant use db.store('theName') if not rebuild schema

If answer on your title, yes, you can't, because IndexedDB uses static schemas.

osc2nuke commented 8 years ago

Mmm to bad, then i cannot use the wrapper ( i think not can call it a wrapper anymore, if need to rebuild each time the schema on a page reload), but hey!!!!! Still it is good work.

alekseykulikov commented 8 years ago

Thanks, FYI: IndexedDB does not rebuild schema on page reload, you just pass it, and if current version is 3, upgrade event is skipped. Treo also uses schema object to define internal structure of available stores/indexes.

osc2nuke commented 8 years ago

i think i not understand the whole concept, perhaps you can enlightening me . This is what i trying... Check the console.log WORKS/FAILS (tnx in advanced)

    //Works
    function doCreate(){
        // define db schema
        var schema = treo.schema()
          .version(1)
            .addStore('books', { key: 'isbn' })
            .addIndex('byTitle', 'title', { unique: true })
            .addIndex('byAuthor', 'author')
          .version(2)
            .getStore('books')
            .addIndex('byYear', 'year')
          .version(3)
            .addStore('magazines')
            .addIndex('byPublisher', 'publisher')
            .addIndex('byFrequency', 'frequency');

        // open db
        var db = window.treo('library', schema);
        db.version; // 3

        // put some data in one transaction
        var books = db.store('books');
        books.batch([
          { isbn: 123456, title: 'Quarry Memories', author: 'Fred', year: 2012 },
          { isbn: 234567, title: 'Water Buffaloes', author: 'Fred', year: 2012 },
          { isbn: 345678, title: 'Bedrock Nights', author: 'Barney', year: 2013 },
        ], function(err) {
          // Before this point, all actions were synchronous, and you don't need to wait
          // for db.open, initialize onupgradeneeded event, create readwrite transaction,
          // and handle all possible errors, blocks, aborts.
          // If any error happen on one of this steps, you get it as `err`.
        });

        // get a single book by title using an index
        books.index('byTitle').get('Bedrock Nights', function(err, book) {

            console.log(book);//WORKS!!!!!      

            });

        // get all books filtered by author
        books.index('byAuthor').get('Fred', function(err, all) {}); // all.length == 2

    }
    //Fails 
    function getItem(){
        // define db schema
        var schema = treo.schema();

        // open db
        var db = window.treo('library', schema);
        db.version; // 3

        // put some data in one transaction
        var books = db.store('books');

        // get a single book by title using an index
        books.index('byTitle').get('Bedrock Nights', function(err, book) {

            console.log(book);//FAILS!!!!!

            });

        // get all books filtered by author
        books.index('byAuthor').get('Fred', function(err, all) {}); // all.length == 2

    }
osc2nuke commented 8 years ago

Keep in mind i fire first the WORKING after i directly fire the FAILING , regardless if i reload the page or not.

alekseykulikov commented 8 years ago

Just always pass the whole schema in treo constructor.

var schema = treo.schema(); // bug, it's an empty schema