Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion files/en-us/web/api/idbdatabase/close/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ None ({{jsxref("undefined")}}).

## Examples

For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4); // opening a database.
Expand Down Expand Up @@ -70,4 +72,3 @@ DBOpenRequest.onsuccess = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
2 changes: 1 addition & 1 deletion files/en-us/web/api/idbdatabase/close_event/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A generic {{domxref("Event")}}.

## Examples

This example opens a database and listens for the `close` event:
This example opens a database and listens for the `close` event. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Open the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ one of the following types:

## Examples

For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
const request = window.indexedDB.open("toDoList", 4);
Expand Down Expand Up @@ -124,4 +126,3 @@ request.onupgradeneeded = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
54 changes: 32 additions & 22 deletions files/en-us/web/api/idbdatabase/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ The following events are available to `IDBDatabase` via event bubbling from {{do

## Example

In the following code snippet, we open a database asynchronously ({{domxref("IDBFactory")}}), handle success and error cases, and create a new object store in the case that an upgrade is needed (`IDBDatabase`). For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
In the following code snippet, we open a database asynchronously ({{domxref("IDBFactory")}}), handle success and error cases, and create or update the object store in the case that an upgrade is needed (`IDBDatabase`). For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
const DBOpenRequest = window.indexedDB.open("toDoList", 2);

// these two event handlers act on the IDBDatabase object,
// when the database is opened successfully, or not
Expand All @@ -69,7 +69,7 @@ DBOpenRequest.onerror = (event) => {
};

DBOpenRequest.onsuccess = (event) => {
node.appendChild(document.createElement("li")).textContent =
note.appendChild(document.createElement("li")).textContent =
"Database initialized.";

// store the result of opening the database in the db
Expand All @@ -82,7 +82,7 @@ DBOpenRequest.onsuccess = (event) => {
};

// This event handles the event whereby a new version of
// the database needs to be created Either one has not
// the database needs to be created. Either one has not
// been created before, or a new version number has been
// submitted via the window.indexedDB.open line above

Expand All @@ -94,25 +94,36 @@ DBOpenRequest.onupgradeneeded = (event) => {
"Error loading database.";
};

// Create an objectStore for this database using
// IDBDatabase.createObjectStore

const objectStore = db.createObjectStore("toDoList", {
keyPath: "taskTitle",
});

// define what data items the objectStore will contain

objectStore.createIndex("hours", "hours", { unique: false });
objectStore.createIndex("minutes", "minutes", { unique: false });
objectStore.createIndex("day", "day", { unique: false });
objectStore.createIndex("month", "month", { unique: false });
objectStore.createIndex("year", "year", { unique: false });

objectStore.createIndex("notified", "notified", { unique: false });
// event.oldVersion is the version the user already has: 0 if the
// database is being created for the first time. Apply each upgrade
// step the user hasn't had yet, so that both new users and users
// with an older version end up with the same database.

if (event.oldVersion < 1) {
// Version 1: create an objectStore for this database using
// IDBDatabase.createObjectStore, and define what data items
// it will contain
const objectStore = db.createObjectStore("toDoList", {
keyPath: "taskTitle",
});

objectStore.createIndex("hours", "hours", { unique: false });
objectStore.createIndex("minutes", "minutes", { unique: false });
objectStore.createIndex("day", "day", { unique: false });
objectStore.createIndex("month", "month", { unique: false });
objectStore.createIndex("year", "year", { unique: false });
}

if (event.oldVersion < 2) {
// Version 2: add an index to the object store created in version 1.
// Calling createObjectStore() again here would throw, because the
// store already exists, so get it from the upgrade transaction
const objectStore = DBOpenRequest.transaction.objectStore("toDoList");
objectStore.createIndex("notified", "notified", { unique: false });
}

note.appendChild(document.createElement("li")).textContent =
"Object store created.";
"Database upgraded.";
};
```

Expand All @@ -139,4 +150,3 @@ const objectStore = db
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
7 changes: 1 addition & 6 deletions files/en-us/web/api/idbdatabase/name/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ A string containing the name of the connected database.

## Examples

This example shows a database connection being opened, the resulting
{{domxref("IDBDatabase")}} object being stored in a db variable, and the name property
then being logged. For a full example, see our
[To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications)
app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
This example shows a database connection being opened, the resulting {{domxref("IDBDatabase")}} object being stored in a db variable, and the name property then being logged. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
Expand Down Expand Up @@ -63,4 +59,3 @@ DBOpenRequest.onsuccess = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
3 changes: 2 additions & 1 deletion files/en-us/web/api/idbdatabase/objectstorenames/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ in the connected database.

## Examples

For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
Expand Down Expand Up @@ -59,4 +61,3 @@ DBOpenRequest.onsuccess = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
3 changes: 1 addition & 2 deletions files/en-us/web/api/idbdatabase/transaction/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ An {{domxref("IDBTransaction")}} object.
## Examples

In this example we open a database connection, then use transaction() to open a transaction on the database.
For a complete example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
let db;
Expand Down Expand Up @@ -139,4 +139,3 @@ const objectStore = transaction.objectStore("toDoList");
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
3 changes: 2 additions & 1 deletion files/en-us/web/api/idbdatabase/version/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ An integer containing the version of the connected database.

## Examples

For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
Expand Down Expand Up @@ -55,4 +57,3 @@ DBOpenRequest.onsuccess = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A generic {{domxref("Event")}}.

## Examples

This example opens a database and, on success, adds a listener to `versionchange`:
This example opens a database and, on success, adds a listener to `versionchange`. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Open the database
Expand Down
3 changes: 1 addition & 2 deletions files/en-us/web/api/idbfactory/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The **`IDBFactory`** interface of the [IndexedDB API](/en-US/docs/Web/API/Indexe

## Example

In the following code snippet, we make a request to open a database, and include handlers for the success and error cases. For a full working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
In the following code snippet, we make a request to open a database, and include handlers for the success and error cases. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open version 4 of our database
Expand Down Expand Up @@ -57,4 +57,3 @@ DBOpenRequest.onsuccess = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
3 changes: 1 addition & 2 deletions files/en-us/web/api/idbfactory/open/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const request = window.indexedDB.open("toDoList", 4);
```

In the following code snippet, we make a request to open a database, and include handlers for the success and error cases.
For a full working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
const note = document.querySelector("ul");
Expand Down Expand Up @@ -93,4 +93,3 @@ DBOpenRequest.onsuccess = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
6 changes: 1 addition & 5 deletions files/en-us/web/api/idbobjectstore/add/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ one of the following types:

## Examples

In the following code snippet, we open a read/write transaction on our database and add
some data to an object store using `add()`. Note also the functions attached
to transaction event handlers to report on the outcome of the transaction opening in the
event of success or failure. For a full working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
In the following code snippet, we open a read/write transaction on our database and add some data to an object store using `add()`. Note also the functions attached to transaction event handlers to report on the outcome of the transaction opening in the event of success or failure. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
Expand Down Expand Up @@ -147,4 +144,3 @@ function addData() {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
8 changes: 1 addition & 7 deletions files/en-us/web/api/idbobjectstore/autoincrement/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ A boolean value:

## Examples

In the following code snippet, we open a read/write transaction on our database and add
some data to an object store using `add()`. After the object store has been
created, we log `objectStore.autoIncrement` to
the console. For a full working example, see
our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app
([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
In the following code snippet, we open a read/write transaction on our database and add some data to an object store using `add()`. After the object store has been created, we log `objectStore.autoIncrement` to the console. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
Expand Down Expand Up @@ -107,4 +102,3 @@ function addData() {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
6 changes: 1 addition & 5 deletions files/en-us/web/api/idbobjectstore/clear/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ If the operation is successful, the value of the request's {{domxref("IDBRequest

## Examples

In the following code snippet, we open a read/write transaction on our database and
clear all the current data out of the object store using `clear()`. For a
full working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app
([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
In the following code snippet, we open a read/write transaction on our database and clear all the current data out of the object store using `clear()`. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
// Let us open our database
Expand Down Expand Up @@ -111,4 +108,3 @@ function clearData() {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
7 changes: 1 addition & 6 deletions files/en-us/web/api/idbobjectstore/createindex/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ This method may raise a {{domxref("DOMException")}} of one of the following type

## Examples

In the following example you can see
the {{domxref("IDBOpenDBRequest.upgradeneeded_event", "onupgradeneeded")}} handler being used to update the
database structure if a database with a higher version number is loaded.
`createIndex()` is used to create new indexes on the object store. For a
full working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
In the following example you can see the {{domxref("IDBOpenDBRequest.upgradeneeded_event", "onupgradeneeded")}} handler being used to update the database structure if a database with a higher version number is loaded. `createIndex()` is used to create new indexes on the object store. For a complete working example, see our [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) app ([view example live](https://mdn.github.io/dom-examples/to-do-notifications/)).

```js
let db;
Expand Down Expand Up @@ -151,4 +147,3 @@ DBOpenRequest.onupgradeneeded = (event) => {
- Setting a range of keys: {{domxref("IDBKeyRange")}}
- Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
- Using cursors: {{domxref("IDBCursor")}}
- Reference example: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([View the example live](https://mdn.github.io/dom-examples/to-do-notifications/)).
Loading
Loading