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

推荐订阅源

WordPress大学
WordPress大学
N
News | PayPal Newsroom
雷峰网
雷峰网
Y
Y Combinator Blog
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
The Cloudflare Blog
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
Vercel News
Vercel News
N
Netflix TechBlog - Medium
GbyAI
GbyAI
博客园 - 司徒正美
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
J
Java Code Geeks
P
Proofpoint News Feed
I
InfoQ
IT之家
IT之家
F
Full Disclosure
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
The Last Watchdog
The Last Watchdog
月光博客
月光博客
W
WeLiveSecurity
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - 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();