
indexeddb 是一种强大的客户端存储解决方案,它以对象存储(object store)的形式组织数据,每个对象存储类似于关系型数据库中的一张表。在 indexeddb 中,数据库的结构(即包含哪些对象存储以及它们的索引)被称为“模式”(schema)。模式的创建和修改只能在特定的生命周期事件中进行,即 idbopendbrequest 对象的 onupgradeneeded 回调函数中。
当满足以下任一条件时,onupgradeneeded 事件会被触发:
这意味着,像 db.createObjectStore() 这样的模式修改操作,必须且只能在 onupgradeneeded 事件处理函数内部执行。一旦数据库成功打开(触发 onsuccess),其模式就已固定,不允许再进行结构性更改,否则会抛出错误。
在某些应用场景中,开发者可能希望根据运行时需求动态地创建不同的“数据分区”,例如,为不同的用户或模块创建独立的存储空间,类似于文件系统中的文件夹。一个直观的想法是为每个分区创建一个新的 IndexedDB 对象存储。然而,正如前文所述,这要求每次添加新分区时都必须提升数据库版本号,从而触发 onupgradeneeded 事件。
考虑以下尝试动态创建对象存储的伪代码:
class LocalStorageAsync {
#database: Promise<IDBDatabase>;
#dbName = 'LocalStorageAsyncDB';
constructor(storeName = 'default') {
const openRequest = indexedDB.open(this.#dbName); // 首次打开或版本未变
openRequest.onsuccess = (event) => {
const db = event.target.result as IDBDatabase;
if (!db.objectStoreNames.contains(storeName)) {
// 错误:db.createObjectStore() 不能在 onsuccess 中调用
// db.createObjectStore(storeName);
console.error("无法在 onsuccess 中创建对象存储。");
}
// ... resolve promise
};
openRequest.onupgradeneeded = (event) => {
const db = event.target.result as IDBDatabase;
// 这里可以创建对象存储,但需要版本号提升
// db.createObjectStore(storeName);
};
this.#database = new Promise(resolve => {
// ...
});
}
// ... getItem, setItem methods
}这种方法存在几个问题:
更符合 IndexedDB 设计哲学且更为健壮的方法是:使用单个对象存储来存储所有数据,并通过在数据对象内部添加一个“分区键”(或类型、标签)属性来实现数据分区。
这种方法将数据的逻辑分区从数据库模式层面转移到数据本身。例如,如果需要区分“默认”和“foo”分区的数据,可以在每个存储的数据对象中包含一个 partition 字段。
以下是如何重构 LocalStorageAsync 类以实现基于内部属性的数据分区:
interface StoredItem {
id: string; // 唯一ID,可以是 partitionKey-key 的组合
partition: string; // 分区键,例如 'default', 'foo'
key: string; // 原始的键
value: string; // 原始的值
}
class LocalStorageAsync {
#database: Promise<IDBDatabase>;
#dbName = 'LocalStorageAsyncDB';
#fixedStoreName = 'universalDataStore'; // 固定使用的对象存储名称
#currentPartitionKey: string; // 当前实例操作的分区键
constructor(partitionKey = 'default') {
this.#currentPartitionKey = partitionKey;
const openRequest = indexedDB.open(this.#dbName, 1); // 版本号固定为1,通常只在首次创建或重大模式变更时提升
openRequest.onupgradeneeded = (event) => {
const db = event.target.result as IDBDatabase;
// 检查并创建唯一的一个对象存储
if (!db.objectStoreNames.contains(this.#fixedStoreName)) {
const store = db.createObjectStore(this.#fixedStoreName, { keyPath: 'id' });
// 为 'partition' 字段创建索引,以便高效地按分区查询
store.createIndex('partitionIndex', 'partition', { unique: false });
}
};
this.#database = new Promise((resolve, reject) => {
openRequest.onsuccess = (event) => {
resolve(event.target.result as IDBDatabase);
};
openRequest.onerror = (event) => {
console.error("IndexedDB open error:", event.target.error);
reject(event.target.error);
};
});
}
/**
* 将键值对存储到当前分区。
* @param key 数据的键。
* @param value 数据的值。
*/
async setItem(key: string, value: string): Promise<void> {
const db = await this.#database;
const transaction = db.transaction([this.#fixedStoreName], 'readwrite');
const store = transaction.objectStore(this.#fixedStoreName);
// 构建存储对象,包含分区键和唯一ID
const dataToStore: StoredItem = {
id: `${this.#currentPartitionKey}-${key}`, // 使用分区键和原始键组合作为唯一ID
partition: this.#currentPartitionKey,
key: key,
value: value
};
return new Promise((resolve, reject) => {
const request = store.put(dataToStore); // put 方法用于添加或更新数据
request.onsuccess = () => resolve();
request.onerror = (event) => reject(event.target.error);
});
}
/**
* 从当前分区获取指定键的值。
* @param key 数据的键。
* @returns 对应的值,如果不存在则为 undefined。
*/
async getItem(key: string): Promise<string | undefined> {
const db = await this.#database;
const transaction = db.transaction([this.#fixedStoreName], 'readonly');
const store = transaction.objectStore(this.#fixedStoreName);
// 使用组合ID进行检索
const request = store.get(`${this.#currentPartitionKey}-${key}`);
return new Promise((resolve, reject) => {
request.onsuccess = (event) => {
const result = event.target.result as StoredItem | undefined;
resolve(result ? result.value : undefined);
};
request.onerror = (event) => reject(event.target.error);
});
}
/**
* 获取当前分区的所有键值对。
* 注意:此方法依赖于 'partitionIndex' 索引。
* @returns 包含键值对的数组。
*/
async getAllItemsInPartition(): Promise<Array<{ key: string, value: string }>> {
const db = await this.#database;
const transaction = db.transaction([this.#fixedStoreName], 'readonly');
const store = transaction.objectStore(this.#fixedStoreName);
const partitionIndex = store.index('partitionIndex'); // 使用分区索引
const request = partitionIndex.getAll(this.#currentPartitionKey);
return new Promise((resolve, reject) => {
request.onsuccess = (event) => {
const results = (event.target.result as StoredItem[]).map(item => ({ key: item.key, value: item.value }));
resolve(results);
};
request.onerror = (event) => reject(event.target.error);
});
}
}
// 使用示例:
async function demonstrateUsage() {
const defaultStore = new LocalStorageAsync();
await defaultStore.setItem('user_name', 'Alice');
await defaultStore.setItem('theme', 'dark');
const fooStore = new LocalStorageAsync('foo');
await fooStore.setItem('app_version', '1.2.3');
await fooStore.setItem('last_login', new Date().toISOString());
console.log('Default partition user_name:', await defaultStore.getItem('user_name')); // Alice
console.log('Foo partition app_version:', await fooStore.getItem('app_version')); // 1.2.3
console.log('All items in default partition:', await defaultStore.getAllItemsInPartition());
// [{ key: 'user_name', value: 'Alice' }, { key: 'theme', value: 'dark' }]
console.log('All items in foo partition:', await fooStore.getAllItemsInPartition());
// [{ key: 'app_version', value: '1.2.3' }, { key: 'last_login', value: '...' }]
}
demonstrateUsage();尽管上述内部属性管理策略适用于大多数数据分区场景,但在以下情况下,考虑使用多个独立的 IndexedDB 对象存储可能更为合适:
在 IndexedDB 中,动态创建对象存储并非理想的解决方案,因为它强制进行数据库版本升级并频繁修改模式。更推荐的做法是采用数据内部属性管理策略,即在单个对象存储中通过添加“分区键”字段来区分和组织数据。这种方法不仅简化了数据库模式管理,提高了应用的稳定性,还通过合理的索引设计保证了数据检索的效率。理解并遵循 IndexedDB 的设计原则,能够帮助开发者构建出更健壮、可维护的客户端数据存储方案。
以上就是IndexedDB:动态对象存储管理与数据分区策略的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号