Html 我需要一个客户端浏览器数据库。我有哪些选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4777968/
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
I need a client side browser database. What are my options
提问by JnBrymn
I'm creating a web site that I think must have a client side database. The other option would be to stick everythingon the server at the expense of increased complexity and decreased scalability. What options do I have? Must I build a plugin? Must I wait until everybody's HTML5 compliant?
我正在创建一个我认为必须有客户端数据库的网站。另一种选择是以增加复杂性和降低可扩展性为代价将所有内容都放在服务器上。我有哪些选择?我必须建立一个插件吗?我必须等到每个人都符合 HTML5 标准吗?
UpdateThere's been a lot of comments about why I would actually need this. Here are my thoughts. Tell me if I'm being silly:
更新有很多关于为什么我真的需要这个的评论。这是我的想法。告诉我是不是傻:
- The clients will have a large and complex state that will require something like a database to provide the data interaction that I need. Therefore (I think) cookies are out of the picture.
- This data is transient, so the client won't care if it gets erased as soon as they close a session. However they will need to keep the data if they go to a different web page and then come back. Therefore (I think) somehow storing the data in some sort of a javascript SQL implementation will not work.
- I can certainly do everything that I want to do on the server, and servers canscale to manage the load (Facebook). But(I think) I'd rather build a plugin than pay for the infrastructure to support this load. This is for a bare bones startup. (The richer the startup is, the barer my bones will be.)
- 客户端将有一个庞大而复杂的状态,需要像数据库这样的东西来提供我需要的数据交互。因此(我认为)饼干不在图片中。
- 此数据是暂时的,因此客户端不会在乎它们是否在关闭会话后立即被删除。但是,如果他们转到不同的网页然后又回来,他们将需要保留数据。因此(我认为)以某种方式将数据存储在某种 javascript SQL 实现中是行不通的。
- 我当然可以在服务器上做我想做的一切,服务器可以扩展以管理负载(Facebook)。 但是(我认为)我宁愿构建一个插件而不是为支持这种负载的基础设施付费。这是一个裸机启动。(创业公司越富有,我的骨头就越少。)
采纳答案by ?ime Vidas
回答by Kevin
I'm about 5 years late in answering this, but given that there are errors and outdated data in some of the existing answers, and unaddressed points in the original question, I figured i'd throw in my two cents.
我回答这个问题晚了大约 5 年,但鉴于某些现有答案中存在错误和过时的数据,以及原始问题中未解决的问题,我想我会投入两分钱。
First, contrary to what others have implied on here, localStorageis not a database. It is (or should be perceived as) a persistent, string-based key-value store...
首先,与其他人在这里暗示的相反,localStorage不是数据库。它是(或应该被视为)一个持久的、基于字符串的键值存储......
...which may be perfectly fine for your needs (and brings me to my second point).
...这可能非常适合您的需求(并让我谈到我的第二点)。
- Do you need explicit or implicitly relationships between your data items?
- How about the ability to query over said items?
- Or more than 5 MBin space?
- 您需要数据项之间的显式或隐式关系吗?
- 查询上述项目的能力如何?
- 或者超过5 MB的空间?
If you answered "no" to all all of the above, go with localStorage and save yourself from the headaches that are the WebSQL and IndexedDB APIs. Well, maybe just the latter headache, since the former has been deprecated.
如果您对以上所有问题的回答都是“否”,请使用 localStorage,从而避免 WebSQL 和 IndexedDB API 的麻烦。好吧,也许只是后者令人头疼,因为前者已被弃用。
There are also several other client-side storage facilities (native and non-native) you may want to look in to, some of which are deprecated* but still see support from some browsers:
您可能还需要查看其他几种客户端存储设施(本机和非本机),其中一些已弃用*,但仍会看到某些浏览器的支持:
- userData*
- The rest of webStorage (sessionStorageand globalStorage*)
- HTML5 File System*
- Flash Locally Shared Objects
- Silverlight Isolated Storage
- 用户数据*
- webStorage 的其余部分(sessionStorage和globalStorage*)
- HTML5文件系统*
- Flash本地共享对象
- Silverlight独立存储
Check out BakedGoodsif you want to utilize any of these facilities, and more, without having to write low-level storage operation code. With it, placing data in one (or more) of them, for example, is as simple as:
如果您想使用这些工具中的任何一个,而无需编写低级存储操作代码,请查看BakedGoods。例如,有了它,将数据放入其中一个(或多个)中就像这样简单:
bakedGoods.set({
data: [{key: "key1", value: "val1"}, {key: "key2", value: "val2"}],
storageTypes: ["silverlight", "fileSystem", "localStorage"],
options: optionsObj,
complete: function(byStorageTypeStoredKeysObj, byStorageTypeErrorObj){}
});
Oh, and for the sake of complete transparency, BakedGoods is maintained by this guy right here :) .
哦,为了完全透明,BakedGoods 由这里的这个人维护:)。
回答by DogBot
I came across a JavaScript Database http://www.taffydb.com/still trying it out myself, hope this helps.
我遇到了一个 JavaScript 数据库http://www.taffydb.com/仍在自己尝试,希望这会有所帮助。
回答by Dhruvdutt Jadhav
Use PouchDB.
使用PouchDB。
PouchDB is an open-source JavaScript database inspired by Apache CouchDB that is designed to run well within the browser.
PouchDB 是一个受 Apache CouchDB 启发的开源 JavaScript 数据库,旨在在浏览器中良好运行。
It helps building applications that works online as well as offline.
它有助于构建在线和离线工作的应用程序。
Basically, it stores the last fetched data in the in-browser database (uses IndexedDB, WebSQL under the hood) and then syncs again when the network gets active.
基本上,它将最后获取的数据存储在浏览器内的数据库中(在后台使用 IndexedDB、WebSQL),然后在网络激活时再次同步。
回答by Rob Evans
If you are looking for a NoSQL-style db on the client you can check out http://www.forerunnerdb.com. It supports the same query language as MongoDB and has a data-binding module if you want your DOM to reflect changes to your data automatically.
如果您正在客户端上寻找 NoSQL 风格的数据库,您可以查看http://www.forerunnerdb.com。如果您希望 DOM 自动反映对数据的更改,它支持与 MongoDB 相同的查询语言并具有数据绑定模块。
It is also open source, is constantly being updated with new features and the community around it is growing rapidly.
它也是开源的,不断更新新功能,并且围绕它的社区正在迅速发展。
Disclaimer, I'm the lead developer of the project.
免责声明,我是该项目的首席开发人员。
回答by Samuel Neff
If you feel like you need it then use it for the clients that support it and implement a server-side fallback for clients that don't.
如果你觉得你需要它,那么将它用于支持它的客户端,并为不支持它的客户端实现服务器端回退。
An alternative is you can use Flash and Local Shared Objects which can store a lot more information than a cookie, will work in all browsers with Flash (which is pretty much all browsers), and store typed data. You don't have to do the whole app in Flash, you can just write a tiny utility to read/write LSO data. This can be done using straight ActionScript projects without any framework and will give you a tiny 5-15kb swf.
另一种选择是您可以使用 Flash 和本地共享对象,它们可以存储比 cookie 多得多的信息,适用于所有具有 Flash 的浏览器(几乎所有浏览器),并存储键入的数据。您不必在 Flash 中完成整个应用程序,您只需编写一个小实用程序来读取/写入 LSO 数据。这可以使用没有任何框架的直接 ActionScript 项目来完成,并且会给你一个很小的 5-15kb swf。
There are two API's you'll primarily need. SharedObject.getLocal()
to get access to a LSO and read/write it's data, and ExternalInterface.addCallback
which you can use to register an AS3 method as a callback to call your read/write LSO method.
您主要需要两个 API。 SharedObject.getLocal()
访问 LSO 并读/写它的数据,ExternalInterface.addCallback
您可以使用它来注册 AS3 方法作为回调来调用您的读/写 LSO 方法。
SharedObject
共享对象
ExternalInterface
外部接口
These links are to Flex references but for this you can just create an ActionScript project with no need for the Flex framework and therefore greatly reduced swf size. There are a number of good IDEs including free open-source ones like FlashDevelop.
这些链接指向 Flex 引用,但为此,您只需创建一个 ActionScript 项目即可,无需 Flex 框架,因此大大减少了 swf 大小。有许多不错的 IDE,包括免费的开源 IDE,例如 FlashDevelop。
FlashDevelop
FlashDevelop
回答by jmort253
Check out HTML5 Local Storage:
查看 HTML5 本地存储:
http://people.w3.org/mike/localstorage.html
http://people.w3.org/mike/localstorage.html
You may also find this helpful: HTML5 database storage (SQL lite) - few questions
您可能还会发现这很有帮助: HTML5 数据库存储 (SQL lite) - 几个问题
When Windows 98 first came out, there were a lot of us still stuck on MS-DOS 6.22. Naturally, there were really cool features on the new operating system that wouldn't run in MS-DOS.
当 Windows 98 首次出现时,我们中的很多人仍然停留在 MS-DOS 6.22 上。自然,新操作系统上有一些非常酷的功能,这些功能无法在 MS-DOS 中运行。
There comes a time when some things must be left behind to make room for innovation. If your application is really innovative and will offer cool new functionality that uses the latest and greatest technologies, then some older browsers will naturally need to be left behind.
总有一天,有些东西必须被抛在后面,以便为创新腾出空间。如果您的应用程序确实具有创新性,并且将提供使用最新和最出色技术的酷炫新功能,那么自然需要将一些旧浏览器抛在后面。
The advantage that you have is that, unlike upgrading an operating system, upgrading from IE7 to Chrome 8 or Firefox 3.6 is a more reachable goal for the average user of your app, especially if you provide a link and upgrade instructions.
您拥有的优势在于,与升级操作系统不同,从 IE7 升级到 Chrome 8 或 Firefox 3.6 对于您的应用程序的普通用户来说是一个更容易实现的目标,尤其是如果您提供链接和升级说明。
回答by Dmitry
I would try Mozilla's localForage. https://localforage.github.io/localForage/
我会尝试 Mozilla 的 localForage。https://localforage.github.io/localForage/