JavaScript Library to Bridge IndexedDB and WebSQL

后端 未结 6 1304
野的像风
野的像风 2020-12-23 02:50

I\'m curious if there is a library or a project out there to provide a generic interface to IndexedDB or to WebSQL, depending on user\'s browser\'s support. If they\'re usin

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 03:01

    I have written YDN-DB for the exact purpose. It is database wrapper for IndexedDB, WebSql and localStorage, build on the top of closure library.

    Goal

    Beautiful API for secure robust high-performance large-scale web app.

    Features

    • Support IndexedDB, Web SQL and localStorage storage mechanisms.
    • Well tested closure library module.
    • Support version migration, encryption, query and transaction.
    • Each method call is an atomic transaction. All methods are asynchronous.
    • Follow usual javascript etiquette like: single namespace, no global, no error globbing (unless we told you so in doc), no eval, parameterized query, this is this, coding error throw error.
    • JQuery plugin available (see download section).

    Basic usage

    Import lastest minified JS script (see download section) to your HTML files. This will create single object in the global scope, call ydn.db.Storage.

    var db = new ydn.db.Storage('db name');
    
    db.setItem('x', 'some value')
    
    db.getItem('x').success(function(value) {
      console.log('x = ' + value);
    }
    

    Query

    Calculate average by using query

    q = db.query('customer').average('age');
    avg = q.fetch()
    

    Key query

    q = db.query('customer', 'age').bound(18, 25, true).where('sex', '=', 'FEMALE').select('full_name')
    young_girl_names = q.fetch()
    

    Transaction

    p1 = db.key('player', 1);
    db.transaction(function() {
       p1.get().success(function(p1_obj) {
            p1_obj.health += 10;
            p1.put(p123_obj);
       });
    }, [p1]);
    

    Encryption

    String value data can be optionally encrypted using SHA-1 cipher.

    db = new ydn.db.Store('store name')
    db.setSecret(passphase); // generally send from server side upon login
    db.setItem(key, value, 3600*1000); // data expire on one hour
    db.getItem(key); // data will be decrypted using the provided passphase
    

提交回复
热议问题