SqliteHelper.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { usqlite } from '@/utils/usqlite.js'
  2. var dbVersionModel, mapInfoModel
  3. export default {
  4. /**
  5. * 打开数据库
  6. * @param {string} dbName 数据库的名字
  7. * @param {int} version 数据库的版本
  8. * @return {object} 该函数会返回一个数据库实例
  9. */
  10. async openDB(dbName = 'Asistent', version = 1.00) {
  11. try {
  12. console.log(`[openDB] name: ${dbName} version: ${version}`);
  13. await usqlite.connect({
  14. name: dbName, // 数据库名称
  15. path:`_doc/${dbName}_record.db`, // 路径
  16. });
  17. console.log(`数据库打开成功 name: ${dbName} version: ${version}`);
  18. if (dbName == 'Asistent') {
  19. dbVersionModel = await usqlite.model('dbVersion',{
  20. version:{
  21. type: Number,
  22. default: 0
  23. }
  24. });
  25. mapInfoModel = await usqlite.model('mapInfo',{
  26. shopId:{
  27. type: Number,
  28. primaryKey: true
  29. },
  30. mapName:{
  31. type: String,
  32. },
  33. zipImageMd5:{
  34. type: String,
  35. },
  36. zipImageUrl:{
  37. type: String,
  38. },
  39. zipFileName:{
  40. type: String,
  41. },
  42. zipFileKey:{
  43. type: String,
  44. },
  45. // zipFilePath:{
  46. // type: String,
  47. // },
  48. // zipFileData:{
  49. // type: 'BLOB',
  50. // }
  51. });
  52. let resDbVersion = await dbVersionModel.find()
  53. // console.log("dbVersion:", resDbVersion);
  54. let localVersion = (resDbVersion.length > 0) ? resDbVersion[0].version : ''
  55. console.log("localVersion:", localVersion);
  56. if (localVersion == '') {
  57. await dbVersionModel.insert({ version: version })
  58. }
  59. else if (localVersion < version) {
  60. console.warn(`本地数据库版本低,需要升级! current:${localVersion} New:${version}`);
  61. if (localVersion == 1) {
  62. await mapInfoModel.alter([
  63. { name: 'zipFileName', option: String },
  64. { name: 'zipFileKey', option: String }
  65. // { name: 'zipFilePath', option: String }
  66. // { name: 'zipFileData', option: 'BLOB' }
  67. ])
  68. }
  69. await dbVersionModel.update({ version: version })
  70. }
  71. }
  72. return usqlite;
  73. } catch (e) {
  74. console.log('[openDB] err', e)
  75. throw e
  76. }
  77. },
  78. async insertMapData(tableName, arrayBuffer) {
  79. try {
  80. if (tableName == 'mapInfo') {
  81. let res = await mapInfoModel.insertMapData(arrayBuffer)
  82. console.log('[insertMapData] 成功');
  83. return res
  84. }
  85. } catch (e) {
  86. console.error("[insertMapData] 失败", e);
  87. throw e
  88. }
  89. },
  90. /**
  91. * 新增数据
  92. * @param {string} tableName 表名
  93. * @param {string} data 数据
  94. */
  95. async insertData(tableName, data) {
  96. try {
  97. if (tableName == 'mapInfo') {
  98. let res = await mapInfoModel.insert(data)
  99. console.log('[insertData] 数据写入成功');
  100. return res
  101. }
  102. } catch (e) {
  103. console.error("[insertData] 数据写入失败", e);
  104. throw e
  105. }
  106. },
  107. /**
  108. * 更新数据
  109. * @param {object} db 数据库实例
  110. * @param {string} tableName 表名
  111. * @param {object} data 数据
  112. */
  113. async updateData(tableName, data, options='') {
  114. try {
  115. if (tableName == 'mapInfo') {
  116. let res = await mapInfoModel.update(data, options)
  117. console.log('[updateData] 数据更新成功');
  118. return res
  119. }
  120. } catch (e) {
  121. console.error("[updateData] 数据更新失败", e);
  122. throw e
  123. }
  124. },
  125. /**
  126. * 通过主键删除数据
  127. * @param {object} db 数据库实例
  128. * @param {string} tableName 表名
  129. * @param {object} key 主键值
  130. */
  131. async deleteDataByKey(tableName, key) {
  132. try {
  133. if (tableName == 'mapInfo') {
  134. let options = 'shopId = ' + key
  135. let res = await mapInfoModel.delete(options)
  136. console.log('[deleteDataByKey] 数据删除成功');
  137. return res
  138. }
  139. } catch (e) {
  140. console.error("[deleteDataByKey] 数据删除失败", e);
  141. throw e
  142. }
  143. },
  144. /**
  145. * 通过主键读取数据
  146. * @param {object} db 数据库实例
  147. * @param {string} tableName 表名
  148. * @param {string} key 主键值
  149. */
  150. async getDataByKey(tableName, key) {
  151. try {
  152. if (tableName == 'mapInfo') {
  153. let options = 'shopId = ' + key
  154. let res = await mapInfoModel.find(options)
  155. console.log('[getDataByKey] 数据读取成功', res);
  156. return res[0]
  157. }
  158. } catch (e) {
  159. console.error("[getDataByKey] 数据读取失败", e);
  160. throw e
  161. }
  162. },
  163. /**
  164. * 关闭数据库
  165. * @param {object} db 数据库实例
  166. */
  167. async closeDB() {
  168. try {
  169. await usqlite.close();
  170. console.log("关闭数据库成功");
  171. } catch (e) {
  172. console.error("[closeDB] 关闭数据库失败", e);
  173. throw e
  174. }
  175. }
  176. }