惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
H
Help Net Security
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
T
Troy Hunt's Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Project Zero
Project Zero
PCI Perspectives
PCI Perspectives
B
Blog RSS Feed
G
GRAHAM CLULEY
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
Recorded Future
Recorded Future
Know Your Adversary
Know Your Adversary
Attack and Defense Labs
Attack and Defense Labs
C
Cisco Blogs
月光博客
月光博客
Spread Privacy
Spread Privacy
J
Java Code Geeks
The Register - Security
The Register - Security
V2EX - 技术
V2EX - 技术
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Latest news
Latest news
爱范儿
爱范儿
Hacker News - Newest:
Hacker News - Newest: "LLM"
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
P
Privacy & Cybersecurity Law Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 叶小钗
B
Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Security Latest
Security Latest
博客园 - 司徒正美
C
CERT Recently Published Vulnerability Notes
V
Visual Studio Blog
The Cloudflare Blog
IT之家
IT之家
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理

博客园 - SKILL·NULL

如何为GIT设置全局勾子,为每次提交追加信息 一文了解大模型、小模型与各类神经网络的关系 如何在Mac上调整外星人鼠标AW720M的灯光颜色 Karabiner-Elements最常用配置 echarts获取坐标上的点距离顶部底部高度 Let`s Encrypt 生成免费自动续签 HTTPS 证书 H5滚动截取长图 ReactNative常见问题及处理 根据.nvmrc自动切换项目所需node版本 Command PhaseScriptExecution failed with a nonzero exit code echarts双Y轴,实现均分为包含刻度0的指定段数,同时对齐刻度 env(safe-area-inset-bottom) 兼容写法 缩放实现0.5px 禁止 IOS 橡皮筋效果 JS 拦截浏览器返回 海康威视DS-IPC-E42H-IWPT监控画面竖线处理 Echarts 5 动态按需引入图表 React 18 自定义 Hook 获取 useState 最新值 处理报错 ResizeObserver loop completed with undelivered notifications.
IndexedDB封装
SKILL·NULL · 2026-01-23 · via 博客园 - SKILL·NULL
export type INDEXEDDB_PARAMS = {
	db_name: string; // 数据库名称
	store_name: string; // 仓库名
	version?: number; // 版本
	data?: any; // 存储数据
};

class IDB {
	protected db: IDBDatabase | null;

	constructor() {
		this.db = null;
		this.open = this.open.bind(this);
		this.put = this.put.bind(this);
		this.get = this.get.bind(this);
		this.getAll = this.getAll.bind(this);
		this.delete = this.delete.bind(this);
	}

	// 开
	open({ db_name, store_name, version }: INDEXEDDB_PARAMS) {
		return new Promise((resolve, reject) => {
			const indexedDB = (window.indexedDB =
				window.indexedDB ||
				window.mozIndexedDB ||
				window.webkitIndexedDB ||
				window.msIndexedDB);

			const DB = indexedDB?.open(db_name, version);

			DB.onerror = (event) => {
				const target = event?.target as IDBRequest;
				console.log(`IndexedDB ${db_name} 打开失败:${target?.error?.name}`);
				reject(target?.error);
			};

			DB.onsuccess = (event) => {
				console.log(`IndexedDB ${db_name} 打开成功`);
				const target = event?.target as IDBRequest;
				this.db = target?.result;
				resolve(this.db);
			};

			DB.onupgradeneeded = (event) => {
				console.log(`IndexedDB ${db_name} onupgradeneeded`);
				const target = event?.target as IDBRequest;
				const _db = target?.result;
				if (!_db?.objectStoreNames?.contains(store_name)) {
					_db.createObjectStore(store_name, {
						autoIncrement: true,
					});
				}
			};
		});
	}

	// 有则覆盖,无则新增
	put({ store_name, data }: INDEXEDDB_PARAMS) {
		return new Promise((resolve, reject) => {
			const transaction = this?.db?.transaction([store_name], "readwrite");
			const objectStore = transaction?.objectStore(store_name);
			const request = objectStore?.put(data);

			if (request) {
				request.onsuccess = () => {
					console.log("数据写入成功");
					resolve("数据写入成功");
				};

				request.onerror = (event) => {
					const target = event?.target as IDBRequest;
					console.log("数据写入失败:", target?.error?.name);
					reject(target?.error);
				};
			} else {
				reject("put error");
			}
		});
	}

	// 取
	get({
		store_name,
		key,
	}: INDEXEDDB_PARAMS & { key: IDBValidKey | IDBKeyRange }) {
		return new Promise((resolve, reject) => {
			const transaction = this?.db?.transaction([store_name], "readonly");
			const objectStore = transaction?.objectStore(store_name);
			const request = objectStore?.get(key);

			if (request) {
				request.onsuccess = (event) => {
					const target = event?.target as IDBRequest;
					resolve(target?.result);
				};

				request.onerror = (event) => {
					const target = event?.target as IDBRequest;
					console.log(
						`${store_name} 获取数据 ${key} 失败:`,
						target?.error?.name
					);
					reject(target?.error);
				};
			} else {
				reject("get error");
			}
		});
	}

	// 全取
	getAll({ store_name }: INDEXEDDB_PARAMS) {
		return new Promise((resolve, reject) => {
			const transaction = this?.db?.transaction([store_name], "readonly");
			const objectStore = transaction?.objectStore(store_name);
			const request = objectStore?.getAll();

			if (request) {
				request.onsuccess = (event) => {
					const target = event?.target as IDBRequest;
					resolve(target?.result);
				};

				request.onerror = (event) => {
					const target = event?.target as IDBRequest;
					console.log(`${store_name} 获取所有数据失败:`, target?.error?.name);
					reject(target?.error);
				};
			} else {
				reject("get all error");
			}
		});
	}

	// 删
	delete({
		store_name,
		key,
	}: INDEXEDDB_PARAMS & { key: IDBValidKey | IDBKeyRange }) {
		return new Promise((resolve, reject) => {
			const transaction = this?.db?.transaction([store_name], "readwrite");
			const objectStore = transaction?.objectStore(store_name);
			const request = objectStore?.delete(key);

			if (request) {
				request.onsuccess = () => {
					console.log("数据删除成功");
					resolve("数据删除成功");
				};

				request.onerror = (event) => {
					const target = event?.target as IDBRequest;
					console.log("数据删除失败:", target?.error?.name);
					reject(target?.error);
				};
			} else {
				reject("delete error");
			}
		});
	}
}

export default new IDB();