JavaScript 库连接 IndexedDB 和 WebSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6116053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:32:22  来源:igfitidea点击:

JavaScript Library to Bridge IndexedDB and WebSQL

javascriptweb-sqlindexeddb

提问by Peder Rice

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 using Chrome/Safari, use WebSQL, if they're using Firefox or Internet Explorer, use IndexedDB.

我很好奇是否有一个库或一个项目来为 IndexedDB 或 WebSQL 提供通用接口,具体取决于用户浏览器的支持。如果他们使用 Chrome/Safari,请使用 WebSQL,如果他们使用 Firefox 或 Internet Explorer,请使用 IndexedDB。

The poster of this questionappears to have homegrown a solution, but did not provide any source code.

的海报这个问题似乎有自主开发的解决方案,但没有提供任何的源代码。

采纳答案by Sam Dutton

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.DataManagersolution.

要么,要么使用他的picnet.data.DataManager解决方案。

Also take a look at persistence.js.

也看看persistence.js

回答by Lee Chee Kiam

JasonCasden has shared a huge list of libraries/wrappers at his presentation In-browser storage and me. Here is the list:

JasonCasden 在他的演示文稿In-browser storage 和我分享了大量的库/包装。这是清单:

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

回答by Kyaw Tun

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

我已经为确切的目的编写了YDN-DB。它是 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.
  • Well tested closure library module.
  • Support version migration, encryption, queryand 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).
  • 支持 IndexedDB、Web SQL 和 localStorage 存储机制。
  • 经过良好测试的闭包库模块。
  • 支持版本迁移、加密、查询交易
  • 每个方法调用都是一个原子事务。所有方法都是异步的。
  • 遵循通常的 javascript 礼节,例如:单一命名空间、无全局性、无错误通配符(除非我们在文档中告诉您)、无 eval、参数化查询,这就是编码错误抛出错误。
  • 可用的 JQuery 插件(请参阅下载部分)。

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.

将最新缩小的 JS 脚本(参见下载部分)导入您的 HTML 文件。这将在全局范围内创建单个对象,调用 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

回答by Gecko

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

看看这个:https: //github.com/axemclion/IndexedDBShim

It's a polyfill to enable IndexedDB using WebSql. 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.

这是一个使用 WebSql 启用 IndexedDB 的 polyfill。我使用它并且我认为它非常好,但是作为每个解决方案,它都有一些局限性,尽管您几乎可以随心所欲地开发它而不会出现大问题。

回答by Robesz

The question is answered, I just want to share the updates.

问题得到了回答,我只想分享更新。

In May 2012 JayDatahas 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 Language Query语法

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 项目开发团队的成员

回答by Kevin

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 由您真正维护:)。