Conversation
leviding
left a comment
There was a problem hiding this comment.
我大概看了一下,有几个格式上的小问题,是代表性问题,麻烦全文检查修改下哈~
| # IndexedDB | ||
|
|
||
| IndexedDB is a built-in database, much more powerful than `localStorage`. | ||
| indexedDB是一个内置的数据库,它比 `localStorage` 强大的多。 |
There was a problem hiding this comment.
| indexedDB是一个内置的数据库,它比 `localStorage` 强大的多。 | |
| indexedDB是一个内置的数据库,它比 `localStorage` 强大得多。 |
| - 键/值 储存:值(几乎)可以是任何类型,键有多种类型。 | ||
| - 支撑事务的可靠性。 | ||
| - 支持键范围查询、索引。 | ||
| - 和 `localStorage` 相比,它可以存储更多数据 。 |
There was a problem hiding this comment.
| - 和 `localStorage` 相比,它可以存储更多数据 。 | |
| - 和 `localStorage` 相比,它可以存储更多数据。 |
| 根据规范 <https://www.w3.org/TR/IndexedDB> 中的描述,IndexedDB 的本机接口是基于事件的。 | ||
|
|
||
| We can also use `async/await` with the help of a promise-based wrapper, like <https://github.com/jakearchibald/idb>. That's pretty convenient, but the wrapper is not perfect, it can't replace events for all cases. So we'll start with events, and then, after we gain understanding of IndexedDb, we'll use the wrapper. | ||
| 我们还可以在基于 promise 的包装器(wrapper),如 <https://github.com/jakearchibald/idb> 的帮助下使用 `async/await` 。这要方便的多,但是包装器并不完美,它并不能替代所有情况下的事件。因此,我们先练习事件(events),在理解 IndexedDB 之后,我们将使用包装器。 |
There was a problem hiding this comment.
| 我们还可以在基于 promise 的包装器(wrapper),如 <https://github.com/jakearchibald/idb> 的帮助下使用 `async/await` 。这要方便的多,但是包装器并不完美,它并不能替代所有情况下的事件。因此,我们先练习事件(events),在理解 IndexedDB 之后,我们将使用包装器。 | |
| 我们还可以在基于 promise 的包装器(wrapper),如 <https://github.com/jakearchibald/idb> 的帮助下使用 `async/await`。这要方便的多,但是包装器并不完美,它并不能替代所有情况下的事件。因此,我们先练习事件(events),在理解 IndexedDB 之后,我们将使用包装器。 |
| - `error`: opening failed. | ||
| - `upgradeneeded`: database is ready, but its version is outdated (see below). | ||
| 调用之后,需要监听 `openRequest` 对象上的事件: | ||
| - `success`: 数据库准备就绪,`openRequest.result` 中有了一个数据库对象"Database Object",使用它进行进一步的调用。 |
There was a problem hiding this comment.
| - `success`: 数据库准备就绪,`openRequest.result` 中有了一个数据库对象"Database Object",使用它进行进一步的调用。 | |
| - `success`:数据库准备就绪,`openRequest.result` 中有了一个数据库对象 "Database Object",使用它进行进一步的调用。 |
|
|
||
| openRequest.onupgradeneeded = function() { | ||
| // the existing database version is less than 2 (or it doesn't exist) | ||
| // 现有的数据库版本小于2(或不存在) |
There was a problem hiding this comment.
| // 现有的数据库版本小于2(或不存在) | |
| // 现有的数据库版本小于 2(或不存在) |
| ``` | ||
| 基本有四个步骤: | ||
| 1. 创建一个事务,在(1)表明要访问的所有存储。 | ||
| 2. 使用 `transaction.objectStore(name)` ,在(2)中获取存储对象。 |
There was a problem hiding this comment.
| 2. 使用 `transaction.objectStore(name)` ,在(2)中获取存储对象。 | |
| 2. 使用 `transaction.objectStore(name)`,在(2)中获取存储对象。 |
|
Please make the requested changes. After it, add a comment "/done". |
|
/done |
| # IndexedDB | ||
|
|
||
| IndexedDB is a built-in database, much more powerful than `localStorage`. | ||
| indexedDB 是一个内置的数据库,它比 `localStorage` 强大得多。 |
There was a problem hiding this comment.
| indexedDB 是一个内置的数据库,它比 `localStorage` 强大得多。 | |
| IndexedDB 是一个内置的数据库,它比 `localStorage` 强大得多。 |
| **IndexedDB has a built-in mechanism of "schema versioning", absent in server-side databases.** | ||
|
|
||
| Unlike server-side databases, IndexedDB is client-side, the data is stored in the browser, so we, developers, don't have direct access to it. But when we publish a new version of our app, we may need to update the database. | ||
| **IndexedDB 具有内建的“模式版本控制”机制,这在服务器端数据库中是不存在的。** |
There was a problem hiding this comment.
| **IndexedDB 具有内建的“模式版本控制”机制,这在服务器端数据库中是不存在的。** | |
| **IndexedDB 具有内建的“模式(scheme)版本控制”机制,这在服务器端数据库中是不存在的。** |
| 与服务器端数据库不同,IndexedDB 存在于客户端,数据存储在浏览器中。因此开发人员不能直接访问它。但当新版本的应用程序发布之后,我们可能需要更新数据库。 | ||
|
|
||
| The event also triggers when the database did not exist yet, so we can perform initialization. | ||
| 如果本地数据库版本低于 `open` 中指定的版本,会触发一个特殊事件 `upgradeneeded`。我们可以根据需要比较版本和升级数据结构。 |
There was a problem hiding this comment.
| 如果本地数据库版本低于 `open` 中指定的版本,会触发一个特殊事件 `upgradeneeded`。我们可以根据需要比较版本和升级数据结构。 | |
| 如果本地数据库版本低于 `open` 中指定的版本,会触发一个特殊事件 `upgradeneeded`。我们可以根据需要比较版本并升级数据结构。 |
| */!* | ||
|
|
||
| // ...the db is ready, use it... | ||
| // ...数据库已经准备好,使用它... |
There was a problem hiding this comment.
| // ...数据库已经准备好,使用它... | |
| // ……数据库已经准备好,请使用它…… |
| ``` | ||
|
|
||
| **An object store can only be created/modified while updating the DB version, in `upgradeneeded` handler.** | ||
| **在 `upgradeneeded` 程序中,只有在创建数据库版本时,对象库被才能被 创建/修改。** |
There was a problem hiding this comment.
| **在 `upgradeneeded` 程序中,只有在创建数据库版本时,对象库被才能被 创建/修改。** | |
| **在 `upgradeneeded` 处理程序中,只有在创建数据库版本时,对象库被才能被 创建/修改。** |
| - **`direction`** 是一个可选参数,使用顺序是: | ||
| - `"next"` —— 默认值,光标从有最小索引的记录向上移动。 | ||
| - `"prev"` —— 相反的顺序:从有最大的索引的记录开始下降。 | ||
| - `"nextunique"`,`"prevunique"` —— 同上,但是跳过键相同的记录 (仅适用于索引上的光标,例如,对于价格为5的书,仅返回第一本)。 |
There was a problem hiding this comment.
| - `"nextunique"`,`"prevunique"` —— 同上,但是跳过键相同的记录 (仅适用于索引上的光标,例如,对于价格为5的书,仅返回第一本)。 | |
| - `"nextunique"`,`"prevunique"` —— 同上,但是跳过键相同的记录 (仅适用于索引上的光标,例如,对于价格为 5 的书,仅返回第一本)。 |
| ...report about the error... | ||
| let request = event.target; // IndexedDB 本机请求对象 | ||
| let error = event.reason; // 未处理的错误对象,与 request.error 相同 | ||
| // ...报告错误... |
There was a problem hiding this comment.
| // ...报告错误... | |
| // ……报告错误…… |
|
|
||
| ### "Inactive transaction" pitfall | ||
|
|
||
| ### “非活跃交易”陷阱 |
There was a problem hiding this comment.
| ### “非活跃交易”陷阱 | |
| ### “非活跃事务”陷阱 |
| The next `inventory.add` after `fetch` `(*)` fails with an "inactive transaction" error, because the transaction is already committed and closed at that time. | ||
| `fetch` `(*)` 后的下一个 `inventory.add` 失败,出现“非活动事务”错误,因为这时事务已经被提交并且关闭了。 | ||
|
|
||
| 解决方法与使用本机 IndexedDB 时相同:进行新事务,或者将事物分开。 |
There was a problem hiding this comment.
| 解决方法与使用本机 IndexedDB 时相同:进行新事务,或者将事物分开。 | |
| 解决方法与使用本机 IndexedDB 时相同:进行新事务,或者将事情分开。 |
| let transaction = request.transaction; // 本地事务对象 | ||
|
|
||
| // ...do some native IndexedDB voodoo... | ||
| // ...做些本地的 IndexedDB 的处理... |
There was a problem hiding this comment.
| // ...做些本地的 IndexedDB 的处理... | |
| // ……做些本地的 IndexedDB 的处理…… |
|
Please make the requested changes. After it, add a comment "/done". |
|
还有冲突耶,顺便处理一下吧,谢谢~ |
@Maricaya 图片不翻译,目前是因为本 PR 翻译了图片,而上游 master 更新了导致错误。你把你对图片文件的修改撤销即可。 |
|
@MartinsYong @leviding 冲突已处理,谢谢各位的校验~ |
|
/done |
|
咦,我的 change requests 你还没处理 |
|
@MartinsYong 现在都处理完成啦,重新看下吧 |
|
/done |
|
辛苦啦两位! |
目标章节:
6-data-storage/03-indexeddb
当前上游最新 commit:
62299ed
本 PR 所做更改如下: