简体   繁体   English

用于桥接IndexedDB和WebSQL的JavaScript库

[英]JavaScript Library to Bridge IndexedDB and WebSQL

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. 我很好奇是否有一个库或项目为IndexedDB或WebSQL提供通用接口,具体取决于用户的浏览器支持。 If they're using Chrome/Safari, use WebSQL, if they're using Firefox or Internet Explorer, use IndexedDB. 如果他们使用Chrome / Safari,请使用WebSQL,如果他们使用的是Firefox或Internet Explorer,请使用IndexedDB。

The poster of this question appears to have homegrown a solution, but did not provide any source code. 这个问题的海报似乎有自己开发的解决方案,但没有提供任何源代码。

JasonCasden has shared a huge list of libraries/wrappers at his presentation In-browser storage and me . JasonCasden在他的演示文稿浏览器内存和我共享了大量的库/包装 Here is the list: 这是清单:

lawnchair
persistence.js
persistJS
amplify.store
localStorageDB
https://github.com/axemclion/IndexedDB
realStorage
YUI3 CacheOffline
dojox.storage
DomSQL
Impel
ActiveJS ActiveRecord
JazzRecord
picnet.data.DataManager
ShinyCar
lscache
Kizzy
Artemia
microcache.js
Store.js

You might want to go with Lawnchair , which is quite well known, as mentioned by Guido Tapia in the question that you link to. 您可能想要使用Lawnchair ,这是众所周知的,正如Guido Tapia在您链接的问题中所提到的那样。

Either that, or use his picnet.data.DataManager solution. 要么是这样,要么使用他的picnet.data.DataManager解决方案。

Also take a look at persistence.js . 另请参阅persistence.js

I have written YDN-DB for the exact purpose. 我已经为确切的目的编写了YDN-DB It is database wrapper for IndexedDB, WebSql and localStorage, build on the top of closure library. 它是IndexedDB,WebSql和localStorage的数据库包装器,构建在闭包库的顶部。

Goal 目标

Beautiful API for secure robust high-performance large-scale web app. 美丽的API,用于安全可靠的高性能大型Web应用程序。

Features 特征

  • Support IndexedDB, Web SQL and localStorage storage mechanisms. 支持IndexedDB,Web SQL和localStorage存储机制。
  • 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. 遵循通常的javascript礼仪,如:单个命名空间,没有全局,没有错误通配(除非我们在doc中告诉你),没有eval,参数化查询,这就是这个,编码错误抛出错误。
  • JQuery plugin available (see download section). JQuery插件可用(参见下载部分)。

Basic usage 基本用法

Import lastest minified JS script (see download section) to your HTML files. 将最新的缩小JS脚本(请参阅下载部分)导入HTML文件。 This will create single object in the global scope, call ydn.db.Storage. 这将在全局范围内创建单个对象,调用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. 可以使用SHA-1密码选择性地加密字符串值数据。

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

Take a look at this: https://github.com/axemclion/IndexedDBShim 看看这个: https//github.com/axemclion/IndexedDBShim

It's a polyfill to enable IndexedDB using WebSql. 它是使用WebSql启用IndexedDB的polyfill。 I use it and I think it's quite good, but as every solution, it has some limitations, although you can develop it almost whatever you want without big problems. 我使用它并且我认为它非常好,但是作为每个解决方案,它都有一些限制,尽管你几乎可以随意开发它而不会出现大问题。

The question is answered, I just want to share the updates. 问题得到解答,我只是想分享更新。

In May 2012 JayData has been released, which is the unified data access library for JavaScript and helps to manage data in IndexedDB, WebSQL, SQLite, MongoDB, HTML5 localStorage databases and Facebook, OData, WebAPI, YQL data services with the same JavaScript Language Query syntax . 2012年5月JayData已经发布,它是JavaScript的统一数据访问库,有助于管理IndexedDB,WebSQL,SQLite,MongoDB,HTML5 localStorage数据库和Facebook,OData,WebAPI,YQL数据服务中的数据,具有相同的JavaScript语言查询语法

Changing to IndexedDB from WebSQL means only changing type of the storage provider: 从WebSQL更改为IndexedDB意味着仅更改存储提供程序的类型:

var todoDB = new TodoDatabase({ 
    provider: 'webSql', databaseName: 'MyTodoDatabase' });

var todoDB = new TodoDatabase({ 
    provider: 'indexedDB', databaseName: 'MyTodoDatabase' });

If you don't specify the provider, the library detects the available storage of the browser/device in the following priority order (WebSQL, IndexedDB, HTML5 localStorage). 如果未指定提供程序,则库将按以下优先级顺序(WebSQL,IndexedDB,HTML5 localStorage)检测浏览器/设备的可用存储。

Disclaimer: I'm member of the developer team of the open-source JayData project 免责声明:我是开源JayData项目开发团队的成员

I hope you (OP) are happy with the solutions suggested in the answer you've accepted. 我希望你(OP)对你接受的答案中建议的解决方案感到满意。

For those who are still on the hunt for a capable solution (a group which may or may not include OP), check out BakedGoods . 对于那些仍在寻找有能力的解决方案的人(可能包括也可能不包括OP的团队 ),请查看BakedGoods

It's a library which establishes a uniform interface that can be used to conduct storage operations in all native, and some non-native client storage facilities. 它是一个库,它建立了一个统一的接口,可用于在所有本机和一些非本机客户端存储设施中进行存储操作。 It also maintains the flexibility and options afforded to the user by each. 它还保持了每个人为用户提供的灵活性和选择。

With it, conducting storage operations in whichever of the database types is supported is a matter of... 有了它,在任何数据库类型的支持下进行存储操作都是......

... specifying the appropriate operation options and equivalent configs for both database types: ...为两种数据库类型指定适当的操作选项和等效配置:

//If the operation is a set(), and the referenced structures 
//don't exist, they will be created automatically.

var webSQLOptionsObj = {
    databaseName: "Example_DB",
    databaseDisplayName: "Example DB",
    databaseVersion: "",
    estimatedDatabaseSize: 1024 * 1024,
    tableData: {
        name: "Main",
        keyColumnName: "lastName",
        columnDefinitions: "(lastName TEXT PRIMARY KEY, firstName TEXT)"
    }, 
    tableIndexDataArray: [name: "First_Name_Index", columnNames: "(firstName)"]
};

var indexedDBOptionsObj = {
    databaseName: "Example_DB",
    databaseVersion: 1,
    objectStoreData: {
        name: "Main",
        keyPath: lastName,
        autoIncrement: false
    },
    objectStoreIndexDataArray: [
        {name: "First_Name_Index", keyPath: "firstName", unique: false, multiEntry: false}
    ],
};

var optionsObj = {
    conductDisjointly: false, 
    webSQL: webSQLOptionsObj, 
    indexedDB: indexedDBOptionsObj
};

... and conducting the operation: ......并进行操作:

bakedGoods.set({
    data: [
        {value: {lastName: "Obama", firstName: "Barack"}}, 
        {value: {lastName: "Biden", firstName: "Joe"}}
    ],
    storageTypes: ["indexedDB", "webSQL"],
    options: optionsObj,
    complete: function(byStorageTypeStoredItemRangeDataObj, byStorageTypeErrorObj){}
});

Its simple interface and unmatched storage facility support comes at the cost of lack of support for some storage facility-specific configurations. 其简单的界面和无与伦比的存储设施支持是以缺乏对某些特定存储设施配置的支持为代价的。 For instance, it does not support the conduction of storage operations in WebSQL tables with multi-column primary keys. 例如,它不支持在具有多列主键的WebSQL表中进行存储操作。

So if you make heavy use of those types of features, you may want to look elsewhere. 因此,如果您大量使用这些类型的功能,您可能希望在其他地方寻找。

Oh, and for the sake of complete transparency, BakedGoods is maintained by yours truly :) . 哦,为了完全透明,BakedGoods由你的真正维护:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM