IndexedDB 之對象倉庫(Object Stores)
Object Stores
Once you have established a connection to the database, the next step is to interact with object stores. If the database version doesn’t match the one you expect then you likely will need to create an object store. Before creating an object store, however, it’s important to think about the type of data you want to store.
翻譯:
一旦你建立了和數據庫的鏈接,下一步就是和對象倉庫(stores)的交互了。假如數據庫版本和你期望的不匹配,那么你有可能需要創建一個數據倉庫。然而,在創建一個數據倉庫之前,想好自己想要存儲什么樣的數據類型是很重要的。
Suppose that you’d like to store user records containing username, password, and so on. The object to hold a single record may look like this:
翻譯:
假設你想要存儲包含用戶名,密碼等等的用戶記錄。擁有一個單一對象的記錄可能像下面這樣:
var user = { username: “007”, firstName: “James”, lastName: “Bond”, password: “foo” };
Looking at this object, you can easily see that an appropriate key for this object store is the username property. A username must be globally unique, and it’s probably the way you’ll be accessing data most of the time. This is important because you must specify a key when creating an object store. Here’s how you would create an object store for these users:
翻譯:看看這個對象,你能一眼看出這個對象最合適的主鍵是 username 屬性。username屬性一定是全局唯一的。他可能是你訪問數據最多的方式。這個很重要,因為創建對象存儲的時候,你必須指定一個主鍵。下面是如何為這些users創建一個對象存儲:
var store = db.createObjectStore(“users”, { keyPath: “username” });
The keyPath property of the second argument indicates the property name of the stored objects that should be used as a key.
翻譯:
第二個參數的屬性keyPath指明了被存儲對象中應當被當做主鍵的屬性名。
Since you now have a reference to the object store, it’s possible to populate it with data using either add() or put(). Both of these methods accept a single argument, the object to store, and save the object into the object store. The difference between these two occurs only when an object with the same key already exists in the object store. In that case, add() will cause an error while put() will simply overwrite the object. More simply, think of add() as being used for inserting new values while put() is used for updating values. So to initialize an object store for the first time, you may want to do something like this:
翻譯:
既然現在有了一個對對象倉庫的引用,那么就可以使用方法add()或者put()來操作數據了。這兩個方法都接受一個參數,要存儲的對象,然后保存對象到對象倉庫。他們的不同點在于當對象倉庫已經存在相同主鍵對象的時候。在這種情況下,add()將拋出一個錯誤而put()將簡單的覆蓋這個對象。更簡單的,把add()想象成被用來插入一個新值,而put()則被用來更新一個值。因此,為了首次初始化一個對象倉庫,你可能會如下這么做:
//where users is an array of new users
var i=0, len = users.length;
while(i < len){
store.add(users[i++]);
}
Each call to add() or put() creates a new update request for the object store. If you want verification that the request completed successfully, you can store the request object in a variable and assign onerror and onsuccess event handlers:
翻譯:
每次對add()或者put()的調用都會創建一個對對象倉庫的更新請求。假如你想驗證該請求是否成功完成,你能存儲這個請求對象到一個變量,然后給他綁定上成功和錯誤事件。
//where users is an array of new users
var i=0, request, requests = [], len = users.length;
while(i < len){
request = store.add(users[i++]);
request.onerror = function(){ //handle error };
request.onsuccess = function(){ //handle success };
requests.push(request);
}
Once the object store is created and filled with data, it’s time to start querying.
翻譯:
一旦對象倉庫建好,并填充了數據,那么就可以對他進行查詢了。