1. /**
    2. * 1. 存储复杂的关系数据
    3. * 2. 储存类型多样: 字符串,数字,视频,图像等
    4. */
    5. class IDB {
    6. constructor(dbName, version = 1) {
    7. this._db = null;
    8. this._indexedDB = window.indexedDB;
    9. this._dbName = dbName;
    10. this._version = version;
    11. this.stores = [];
    12. }
    13. cereteStore(store) {
    14. if (!this.stores.some((v) => v.name === store.name)) {
    15. this.stores.push(store);
    16. }
    17. }
    18. openDatabase() {
    19. return new Promise((resolve, reject) => {
    20. let request = this._indexedDB.open(this._dbName, this._version);
    21. request.onerror = () => {
    22. console.log("Database failed to open");
    23. reject({ code: 400, msg: "Database failed to open" });
    24. };
    25. request.onsuccess = () => {
    26. console.log("Database opened successfully");
    27. this._db = request.result;
    28. resolve(this, { code: 200, msg: "Database opened successfully" });
    29. };
    30. request.onupgradeneeded = (e) => {
    31. this._db = e.target.result;
    32. this.stores.forEach((store) => {
    33. if (!this._db.objectStoreNames.contains(store.name)) {
    34. store.__os__ = this._db.createObjectStore(store.name, {
    35. keyPath: store.keyPath || "id",
    36. autoIncrement: true,
    37. });
    38. if (store.indexs != undefined) {
    39. store.indexs.forEach((i) => {
    40. store.__os__.createIndex(i.indexName, i.keyPath, i.option);
    41. });
    42. }
    43. }
    44. });
    45. console.log("Database setup complete");
    46. };
    47. });
    48. }
    49. add(storeName, data, fn) {
    50. let transaction = this._db.transaction(storeName, "readwrite");
    51. let request = transaction.objectStore(storeName).add(data);
    52. request.onsuccess = (e) => {
    53. fn && fn({ id: e.target.result });
    54. };
    55. return new Promise((resolve, reject) => {
    56. transaction.oncomplete = () => {
    57. resolve(this, {
    58. code: 200,
    59. msg: "ransaction completed: database modification finished.",
    60. });
    61. };
    62. transaction.onerror = () => {
    63. reject({ code: 400, msg: "Transaction not opened due to error" });
    64. };
    65. });
    66. }
    67. del(storeName, id) {
    68. let transaction = this._db.transaction(storeName, "readwrite");
    69. transaction.objectStore(storeName).delete(id);
    70. return new Promise((resolve, reject) => {
    71. transaction.oncomplete = () => {
    72. resolve(this, {
    73. code: 200,
    74. msg: "ransaction completed: database modification finished.",
    75. });
    76. };
    77. transaction.onerror = () => {
    78. reject({ code: 400, msg: "Transaction not opened due to error" });
    79. };
    80. });
    81. }
    82. put(storeName, data, fn) {
    83. let transaction = this._db.transaction(storeName, "readwrite");
    84. let request = transaction.objectStore(storeName).put(data);
    85. request.onsuccess = (e) => {
    86. fn && fn({ id: e.target.result });
    87. };
    88. return new Promise((resolve, reject) => {
    89. transaction.oncomplete = () => {
    90. resolve(this, {
    91. code: 200,
    92. msg: "ransaction completed: database modification finished.",
    93. });
    94. };
    95. transaction.onerror = () => {
    96. reject({ code: 400, msg: "Transaction not opened due to error" });
    97. };
    98. });
    99. }
    100. get(storeName, id, fn) {
    101. let transaction = this._db.transaction(storeName, "readwrite");
    102. let request = transaction.objectStore(storeName).get(id);
    103. request.onsuccess = (e) => {
    104. fn && fn(e.target.result);
    105. };
    106. return new Promise((resolve, reject) => {
    107. transaction.oncomplete = () => {
    108. resolve(this, {
    109. code: 200,
    110. msg: "ransaction completed: database modification finished.",
    111. });
    112. };
    113. transaction.onerror = () => {
    114. reject({ code: 400, msg: "Transaction not opened due to error" });
    115. };
    116. });
    117. }
    118. getAll(storeName) {
    119. return new Promise((resolve, reject) => {
    120. let list = [];
    121. let cursor = this._db
    122. .transaction(storeName)
    123. .objectStore(storeName)
    124. .openCursor();
    125. cursor.onsuccess = (e) => {
    126. let cursor = e.target.result;
    127. if (cursor) {
    128. list.push(cursor.value);
    129. cursor.continue();
    130. } else {
    131. resolve({ code: 200, data: list });
    132. }
    133. };
    134. cursor.onerror = () => {
    135. reject({ code: 400, msg: "Failed to open the cursor" });
    136. };
    137. });
    138. }
    139. }
    1. window.onload = function () {
    2. const idb = new IDB("test_db");
    3. idb.cereteStore({
    4. name: "notes",
    5. keyPath: "id",
    6. indexs: [
    7. {
    8. indexName: "title",
    9. keyPath: "title",
    10. option: { unique: false },
    11. },
    12. ],
    13. });
    14. idb.openDatabase().then(() => {
    15. const note1 = { title: "20210716笔记", body: "20210716笔记1111111" };
    16. const note2 = { title: "20210717笔记", body: "20210717笔记1111111" };
    17. idb.add("notes", note1, ({ id }) => {
    18. console.log("插入一条新数据,id是: ", id);
    19. idb.del("notes", id);
    20. });
    21. idb.put("notes", note1, ({ id }) => {
    22. console.log("更新一条新数据,id是: ", id);
    23. });
    24. idb.getAll("notes").then((res) => console.log(res));
    25. idb.get("notes", 23, (res) => console.log(res));
    26. });
    27. };
    • Git History (git log) - View git log, file or line history.
    • Project Manager - Easily switch between projects.
    • GitLens - GitLens supercharges the built-in Visual Studio Code Git capabilities. It helps you to visualize code authorship at a glance via inline Git blame annotations and code lens, seamlessly navigate and explore the history of a file or branch, gain valuable insights via powerful comparision commands, and so much more.
    • gitignore - Language support for .gitignore files. Lets you pull .gitignore files from the https://github.com/github/gitignore repository.
    • Open in GitHub / Bitbucket / VisualStudio.com - Jump to a source code line in Github / Bitbucket / VisualStudio.com.