

This returns the date in this format: YYYY-MM-DD Once you are inside the transaction, you can access the object stores that hold your data and make your requests.SQLite supports five date and time functions as follows − Sr.No. Transactions come from the database object, and you have to specify which object stores you want the transaction to span. Using a key generatorīefore you can do anything with your new database, you need to start a transaction. To learn how this is done, see the section on using an index. We can now retrieve the stored customer objects using their ssn from the object store directly, or using their name by using the index. Adding objects that don't have a name property still succeeds, but the objects won't appear in the "name" index. As with createObjectStore(), createIndex() takes an optional options object that refines the type of index that you want to create. We've also asked for an index named "name" that looks at the name property of the stored objects. "ssn" must be present on every object that is stored in the objectStore. That property in this example is "ssn" since a social security number is guaranteed to be unique. In our case, we've asked for an object store named "customers" and defined a keyPath, which is the property that makes an individual object in the store unique. Even though the parameter object is optional, it is very important, because it lets you define important optional properties and refine the type of object store you want to create. The method takes a name of the store, and a parameter object. Object stores are created with a single call to createObjectStore(). In it, you can create and delete object stores and build and remove indices. onerror = ( event ) => Īs indicated previously, onupgradeneeded is the only place where you can alter the structure of the database.

More on this later in Creating or updating the version of the database below, and the IDBFactory.open reference page. If the database does exist but you are specifying an upgraded version number, an onupgradeneeded event is triggered straight away, allowing you to provide an updated schema in its handler. If the database doesn't already exist, it is created by the open operation, then an onupgradeneeded event is triggered and you create the database schema in the handler for this event. The version of the database determines the database schema - the object stores in the database and their structure. The second parameter to the open method is the version of the database. The result for the open function is an instance of an IDBDatabase. Most other asynchronous functions in IndexedDB do the same thing - return an IDBRequest object with the result or error. The call to the open() function returns an IDBOpenDBRequest object with a result (success) or error value that you handle as an event. The open request doesn't open the database or start the transaction right away.

See that? Opening a database is just like any other operation - you have to "request" it. Let us open our database const request = window.
