indexedDBUtil.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // const indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
  2. const indexedDB = window.indexedDB;
  3. class IndexedDB {
  4. private _db: IDBDatabase | null; // 数据库
  5. private _transaction: IDBTransaction | null; // 事务
  6. private _request: IDBOpenDBRequest | null;
  7. private _dbName: string; // 数据库名
  8. private _cacheTableName: string; // 表名
  9. private _dbversion: number; // 数据库版本
  10. constructor() {
  11. this._db = null;
  12. this._transaction = null;
  13. this._request = null;
  14. this._dbName = "userWritingDB";
  15. this._cacheTableName = "userWritingTable";
  16. this._dbversion = 1;
  17. }
  18. async initDB(val: string): Promise<Event> {
  19. return new Promise<Event>((resolve, reject) => {
  20. this._request = indexedDB.open(this._dbName, this._dbversion);
  21. // 数据库初始化成功
  22. this._request!.onsuccess = (event) => {
  23. this._db = this._request!.result;
  24. resolve(event);
  25. };
  26. // 数据库初始化失败
  27. this._request!.onerror = (event) => {
  28. reject(event);
  29. };
  30. // 数据库初次创建或更新时会触发
  31. this._request!.onupgradeneeded = (event) => {
  32. const db = this._request!.result;
  33. if (!db.objectStoreNames.contains(this._cacheTableName)) {
  34. db.createObjectStore(this._cacheTableName, {
  35. keyPath: val,
  36. });
  37. }
  38. resolve(event);
  39. };
  40. });
  41. }
  42. closeDB() {
  43. if (this._db) {
  44. this._db.close();
  45. console.log(`关闭数据库`);
  46. }
  47. }
  48. async addData(params: any): Promise<Event> {
  49. return new Promise<Event>((resolve, reject) => {
  50. const transaction = this._db!.transaction(this._cacheTableName, "readwrite");
  51. const store = transaction.objectStore(this._cacheTableName);
  52. const response = store.add(params);
  53. // 操作成功
  54. response.onsuccess = (event) => {
  55. console.log("操作成功");
  56. resolve(event);
  57. };
  58. // 操作失败
  59. response.onerror = (event) => {
  60. console.log("操作失败");
  61. reject(event);
  62. };
  63. });
  64. }
  65. async getDataByKey(key: any): Promise<any> {
  66. return new Promise<any>((resolve, reject) => {
  67. const transaction = this._db!.transaction(this._cacheTableName);
  68. const objectStore = transaction.objectStore(this._cacheTableName);
  69. // 通过主键读取数据
  70. const request = objectStore.get(key);
  71. // 操作成功
  72. request.onsuccess = () => {
  73. resolve(request.result);
  74. };
  75. // 操作失败
  76. request.onerror = (event) => {
  77. reject(event);
  78. };
  79. });
  80. }
  81. async clearDB(): Promise<Event> {
  82. return new Promise<Event>((resolve, reject) => {
  83. const transaction = this._db && this._db.transaction(this._cacheTableName, "readwrite");
  84. const store = transaction && transaction.objectStore(this._cacheTableName);
  85. const response = store && store.clear();
  86. // 操作成功
  87. response.onsuccess = (event) => {
  88. console.log("清空数据库数据");
  89. resolve(event);
  90. };
  91. // 操作失败
  92. response.onerror = (event) => {
  93. reject(event);
  94. };
  95. });
  96. }
  97. async deleteDBAll(): Promise<void> {
  98. const deleteRequest = indexedDB.deleteDatabase(this._dbName);
  99. return new Promise<void>((resolve, reject) => {
  100. deleteRequest.onerror = function (event) {
  101. console.log("删除失败");
  102. };
  103. deleteRequest.onsuccess = function (event) {
  104. console.log("删除数据库成功");
  105. };
  106. });
  107. }
  108. }
  109. export default IndexedDB;