5.1. Opening a database
The steps to open a database are as follows. The algorithm in these steps takes four arguments: the origin which requested the database to be opened, a database name, a database version, and a request.
Let queue be the connection queue for origin and name.
Add request to queue.
Wait until all previous requests in queue have been processed.
Let db be the database named name in origin, or null otherwise.
If version is undefined, let version be 1 if db is null, or db’s version otherwise.
If db is null, let db be a new database with name name, version 0 (zero), and with no object stores. If this fails for any reason, return an appropriate error (e.g. a “
[QuotaExceededError](https://www.w3.org/TR/WebIDL-1/#quotaexceedederror)
“ or “[UnknownError](https://www.w3.org/TR/WebIDL-1/#unknownerror)
“[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
).If db’s version is greater than version, return a newly created “
[VersionError](https://www.w3.org/TR/WebIDL-1/#versionerror)
“[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
and abort these steps.Let connection be a new connection to db.
Set connection’s version to version.
If db’s version is less than version, then:
Let openConnections be the set of all connections, except connection, associated with db.
For each entry in openConnections that does not have its close pending flag set, queue a task to fire a version change event named
versionchange
at entry with db’s version and version.Firing this event might cause one or more of the other objects in openConnections to be closed, in which case the
versionchange
event is not fired at those objects, even if that hasn’t yet been done.Wait for all of the events to be fired.
If any of the connections in openConnections are still not closed, queue a task to fire a version change event named
[blocked](#request-blocked)
at request with db’s version and version.Wait until all connections in openConnections are closed.
Run the steps to run an upgrade transaction using connection, version and request.
If connection was closed, return a newly created “
[AbortError](https://www.w3.org/TR/WebIDL-1/#aborterror)
“[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
and abort these steps.If the upgrade transaction was aborted, run the steps to close a database connection with connection, return a newly created “
[AbortError](https://www.w3.org/TR/WebIDL-1/#aborterror)
“[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
and abort these steps.
Return connection.