1.基于idb-js进行二次封装

1.1.安装

  1. npm install idb-js --save

1.2.引入

  1. import Idb from 'idb-js' // 引入Idb

1.3.db配置

idb_lab_testing_config.js代码如下

  1. export default {
  2. dbName: "lab_testing",
  3. version: 1,
  4. tables: [
  5. {
  6. tableName: "qc_trf",
  7. option: { keyPath: "id" },
  8. indexs: [
  9. {
  10. key: "id",
  11. options:{
  12. unique: true
  13. }
  14. },
  15. {
  16. key: "trfId",
  17. options:{
  18. unique: true
  19. }
  20. },
  21. {
  22. key: "bno"
  23. },
  24. {
  25. key: "workingNo"
  26. },
  27. {
  28. key: "season"
  29. },
  30. {
  31. key: "article"
  32. },
  33. {
  34. key: "children"
  35. }
  36. ]
  37. },
  38. {
  39. tableName: "qc_test_code",
  40. option: { keyPath: "id" },
  41. indexs: [
  42. {
  43. key: "id",
  44. options:{
  45. unique: true
  46. }
  47. },
  48. {
  49. key: "testCode"
  50. }
  51. ],
  52. }
  53. ]
  54. };

1.4.封装

  1. /**
  2. * IndexedDB数据库操作
  3. * @author max teng
  4. * @version 2021-11-19 11:11:11
  5. */
  6. import Idb from 'idb-js'
  7. import idb_lab_testing_config from './idb_lab_testing_config.js'
  8. var IndexedDBUtil = {}
  9. IndexedDBUtil.init = Idb(idb_lab_testing_config)
  10. /**
  11. * 关闭数据库
  12. */
  13. IndexedDBUtil.IdbClose = function (){
  14. IndexedDBUtil.init.then(
  15. db => {
  16. db.close_db();
  17. }
  18. )
  19. }
  20. /**
  21. * 新增数据
  22. * @param {Object} tableName 表名
  23. * @param {Object} data 新增数据
  24. */
  25. IndexedDBUtil.IdbInsert = function (tableName,data){
  26. IndexedDBUtil.init.then(
  27. db => {
  28. db.insert({
  29. tableName: tableName,
  30. data: data
  31. });
  32. },
  33. err => {
  34. console.log(err);
  35. }
  36. )
  37. }
  38. /**
  39. * 删除达成条件的数据
  40. * @param {Object} tableName 表名
  41. * @param {Object} conditionFun Lambda表达式,删除数据匹配条件
  42. */
  43. IndexedDBUtil.IdbDelete = function (tableName,conditionFun){
  44. IndexedDBUtil.init.then(
  45. db => {
  46. db.delete({
  47. tableName: tableName,
  48. condition: conditionFun,
  49. success: (res) => {
  50. console.log("删除成功");
  51. }
  52. });
  53. },
  54. err => {
  55. console.log(err);
  56. }
  57. )
  58. }
  59. /**
  60. * 更新数据
  61. * @param {Object} tableName 表名
  62. * @param {Object} conditionFun Lambda表达式,更新数据匹配条件
  63. * @param {Object} handleFun Lambda表达式,更新数据修改方式
  64. */
  65. IndexedDBUtil.IdbUpdate = function (tableName,conditionFun,handleFun){
  66. IndexedDBUtil.init.then(
  67. db => {
  68. db.update({
  69. tableName: tableName,
  70. condition: conditionFun,
  71. handle: handleFun,
  72. success: r => {
  73. console.log("修改成功", r);
  74. },
  75. error: msg => console.log(msg)
  76. });
  77. },
  78. err => {
  79. console.log(err);
  80. }
  81. )
  82. }
  83. /**
  84. * 根据游标查询
  85. * @param {Object} tableName 表名
  86. * @param {Object} conditionFun Lambda表达式,查询数据匹配条件
  87. */
  88. IndexedDBUtil.IdbQuery = function (tableName,conditionFun){
  89. let _that = this;
  90. return new Promise(function(resolve, reject) {
  91. IndexedDBUtil.init.then(
  92. db => {
  93. db.query({
  94. tableName: tableName,
  95. condition: conditionFun,
  96. success: res => {
  97. resolve(res)
  98. }
  99. });
  100. }
  101. )
  102. })
  103. }
  104. /**
  105. * 查询表所有的数据
  106. * @param {Object} tableName 表名
  107. */
  108. IndexedDBUtil.IdbQueryAll = function (tableName){
  109. return new Promise(function(resolve, reject) {
  110. IndexedDBUtil.init.then(
  111. db => {
  112. db.queryAll({
  113. tableName: tableName,
  114. success: res => {
  115. resolve(res)
  116. }
  117. });
  118. },
  119. err => {
  120. console.log(err);
  121. }
  122. )
  123. });
  124. }
  125. /**
  126. * @param {Object} tableName 表名
  127. * @param {Object} indexName 索引字段
  128. * @param {Object} target 查询值
  129. */
  130. IndexedDBUtil.IdbQueryByIndex = function (tableName,indexName,target){
  131. return new Promise(function(resolve, reject) {
  132. IndexedDBUtil.init.then(
  133. db => {
  134. db.query_by_index({
  135. tableName: tableName,
  136. indexName: indexName,
  137. target: target,
  138. success: res => {
  139. resolve(res)
  140. }
  141. });
  142. },
  143. err => {
  144. console.log(err);
  145. }
  146. )
  147. })
  148. }
  149. export default IndexedDBUtil