IndexedDB 之对象仓库(Object Stores)

欢欢欢欢 发表于 2018-2-8 17:25

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.

翻译:

一旦对象仓库建好,并填充了数据,那么就可以对他进行查询了。