---
title: "What is a keyPath and how is it used in IndexedDB?"  
description: "What is a keyPath and how is it used in IndexedDB?"  
author: "ICSM Computer"  
published: 2025-07-01  
updated: 2025-07-01  
canonical: https://www.mindstick.com/interview/34303/what-is-a-keypath-and-how-is-it-used-in-indexeddb  
category: "database"  
tags: ["indexeddb"]  
reading_time: 4 minutes  

---

# What is a keyPath and how is it used in IndexedDB?

In **IndexedDB**, a `keyPath` is a property (or path) within your stored object that serves as its **primary key** — a unique identifier for each record.

## What is `keyPath`?

- It's a **string or array** that tells IndexedDB **which property** of the object should be used as the key.
- This key must be **unique** in the object store.
- You define it **when creating the object store**.

## How `keyPath` is Used

### Example: Simple KeyPath

```javascript
const request = indexedDB.open('MyDB', 1);

request.onupgradeneeded = (e) => {
    const db = e.target.result;

    // Use 'id' as the keyPath
    db.createObjectStore('users', { keyPath: 'id' });
};
```

Now, when you store an object like this:

```javascript
store.add({ id: 101, name: "Alice" });
```

IndexedDB uses `101` (from `id`) as the primary key.

### Example: Nested KeyPath

```javascript
db.createObjectStore('orders', { keyPath: 'order.id' });
```

Here, the object:

```plaintext
{ order: { id: 999 }, item: "Pen" }
```

Will be stored with `999` as the key.

### Example: Compound KeyPath

```javascript
db.createObjectStore('contacts', { keyPath: ['firstName', 'lastName'] });
```

Now the combination of `firstName` and `lastName` is the unique key.

```javascript
store.add({ firstName: "John", lastName: "Doe", email: "john@example.com" });
// Key: ["John", "Doe"]
```

### Without `keyPath` (Manual Keys)

If you don’t use a `keyPath`, you must pass the key manually:

```javascript
const store = db.createObjectStore('notes'); // No keyPath

store.add({ title: "Reminder" }, 1); // '1' is the key
```

## Summary

| Term | Meaning |
| --- | --- |
| `keyPath` | A field name (or array of names) in your object used as key |
| Purpose | Tells IndexedDB where to find the key inside the object |
| Benefit | No need to pass the key manually during `add()` or `put()` |

## Answers

### Answer by ICSM Computer

In **IndexedDB**, a `keyPath` is a property (or path) within your stored object that serves as its **primary key** — a unique identifier for each record.

## What is `keyPath`?

- It's a **string or array** that tells IndexedDB **which property** of the object should be used as the key.
- This key must be **unique** in the object store.
- You define it **when creating the object store**.

## How `keyPath` is Used

### Example: Simple KeyPath

```javascript
const request = indexedDB.open('MyDB', 1);

request.onupgradeneeded = (e) => {
    const db = e.target.result;

    // Use 'id' as the keyPath
    db.createObjectStore('users', { keyPath: 'id' });
};
```

Now, when you store an object like this:

```javascript
store.add({ id: 101, name: "Alice" });
```

IndexedDB uses `101` (from `id`) as the primary key.

### Example: Nested KeyPath

```javascript
db.createObjectStore('orders', { keyPath: 'order.id' });
```

Here, the object:

```plaintext
{ order: { id: 999 }, item: "Pen" }
```

Will be stored with `999` as the key.

### Example: Compound KeyPath

```javascript
db.createObjectStore('contacts', { keyPath: ['firstName', 'lastName'] });
```

Now the combination of `firstName` and `lastName` is the unique key.

```javascript
store.add({ firstName: "John", lastName: "Doe", email: "john@example.com" });
// Key: ["John", "Doe"]
```

### Without `keyPath` (Manual Keys)

If you don’t use a `keyPath`, you must pass the key manually:

```javascript
const store = db.createObjectStore('notes'); // No keyPath

store.add({ title: "Reminder" }, 1); // '1' is the key
```

## Summary

| Term | Meaning |
| --- | --- |
| `keyPath` | A field name (or array of names) in your object used as key |
| Purpose | Tells IndexedDB where to find the key inside the object |
| Benefit | No need to pass the key manually during `add()` or `put()` |


---

Original Source: https://www.mindstick.com/interview/34303/what-is-a-keypath-and-how-is-it-used-in-indexeddb

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
