storage.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. 'use strict';
  2. const Service = require('ee-core').Service;
  3. const Storage = require('ee-core').Storage;
  4. const _ = require('lodash');
  5. /**
  6. * 数据存储
  7. * @class
  8. */
  9. class StorageService extends Service {
  10. constructor (ctx) {
  11. super(ctx);
  12. // lowdb数据库
  13. this.systemDB = Storage.JsonDB.connection('system');
  14. let lowdbOptions = {
  15. driver: 'lowdb'
  16. }
  17. this.demoDB = Storage.JsonDB.connection('demo', lowdbOptions);
  18. this.systemDBKey = {
  19. cache: 'cache'
  20. };
  21. this.demoDBKey = {
  22. preferences: 'preferences',
  23. test_data: 'test_data'
  24. };
  25. // sqlite数据库
  26. let sqliteOptions = {
  27. driver: 'sqlite',
  28. default: {
  29. timeout: 6000,
  30. verbose: console.log // 打印sql语法
  31. }
  32. }
  33. this.demoSqliteDB = Storage.JsonDB.connection('sqlite-demo.db', sqliteOptions);
  34. }
  35. /*
  36. * 增 Test data
  37. */
  38. async addTestData(user) {
  39. const key = this.demoDBKey.test_data;
  40. if (!this.demoDB.db.has(key).value()) {
  41. this.demoDB.db.set(key, []).write();
  42. }
  43. const data = this.demoDB.db
  44. .get(key)
  45. .push(user)
  46. .write();
  47. return data;
  48. }
  49. /*
  50. * 删 Test data
  51. */
  52. async delTestData(name = '') {
  53. const key = this.demoDBKey.test_data;
  54. const data = this.demoDB.db
  55. .get(key)
  56. .remove({name: name})
  57. .write();
  58. return data;
  59. }
  60. /*
  61. * 改 Test data
  62. */
  63. async updateTestData(name= '', age = 0) {
  64. const key = this.demoDBKey.test_data;
  65. const data = this.demoDB.db
  66. .get(key)
  67. .find({name: name}) // 修改找到的第一个数据,貌似无法批量修改 todo
  68. .assign({age: age})
  69. .write();
  70. return data;
  71. }
  72. /*
  73. * 查 Test data
  74. */
  75. async getTestData(age = 0) {
  76. const key = this.demoDBKey.test_data;
  77. let data = this.demoDB.db
  78. .get(key)
  79. //.find({age: age}) 查找单个
  80. .filter(function(o) {
  81. let isHas = true;
  82. isHas = age === o.age ? true : false;
  83. return isHas;
  84. })
  85. //.orderBy(['age'], ['name']) 排序
  86. //.slice(0, 10) 分页
  87. .value();
  88. if (_.isEmpty(data)) {
  89. data = []
  90. }
  91. return data;
  92. }
  93. /*
  94. * all Test data
  95. */
  96. async getAllTestData() {
  97. const key = this.demoDBKey.test_data;
  98. if (!this.demoDB.db.has(key).value()) {
  99. this.demoDB.db.set(key, []).write();
  100. }
  101. let data = this.demoDB.db
  102. .get(key)
  103. .value();
  104. if (_.isEmpty(data)) {
  105. data = []
  106. }
  107. return data;
  108. }
  109. /*
  110. * 检查并创建表 (sqlite)
  111. */
  112. async checkAndCreateTableSqlite(tableName = '') {
  113. if (_.isEmpty(tableName)) {
  114. throw new Error(`table name is required`);
  115. }
  116. // 检查表是否存在
  117. const userTable = this.demoSqliteDB.db.prepare('SELECT * FROM sqlite_master WHERE type=? AND name = ?');
  118. const result = userTable.get('table', tableName);
  119. //console.log('result:', result);
  120. if (result) {
  121. return;
  122. }
  123. // 创建表
  124. const create_table_user =
  125. `CREATE TABLE ${tableName}
  126. (
  127. id INTEGER PRIMARY KEY AUTOINCREMENT,
  128. name CHAR(50) NOT NULL,
  129. age INT
  130. );`
  131. this.demoSqliteDB.db.exec(create_table_user);
  132. }
  133. /*
  134. * 增 Test data (sqlite)
  135. */
  136. async addTestDataSqlite(data) {
  137. //console.log("add data:", data);
  138. let table = 'user';
  139. await this.checkAndCreateTableSqlite(table);
  140. const insert = this.demoSqliteDB.db.prepare(`INSERT INTO ${table} (name, age) VALUES (@name, @age)`);
  141. insert.run(data);
  142. return true;
  143. }
  144. /*
  145. * 删 Test data (sqlite)
  146. */
  147. async delTestDataSqlite(name = '') {
  148. //console.log("delete name:", name);
  149. let table = 'user';
  150. await this.checkAndCreateTableSqlite(table);
  151. const delUser = this.demoSqliteDB.db.prepare(`DELETE FROM ${table} WHERE name = ?`);
  152. delUser.run(name);
  153. return true;
  154. }
  155. /*
  156. * 改 Test data (sqlite)
  157. */
  158. async updateTestDataSqlite(name= '', age = 0) {
  159. //console.log("update :", {name, age});
  160. let table = 'user';
  161. await this.checkAndCreateTableSqlite(table);
  162. const updateUser = this.demoSqliteDB.db.prepare(`UPDATE ${table} SET age = ? WHERE name = ?`);
  163. updateUser.run(age, name);
  164. return true;
  165. }
  166. /*
  167. * 查 Test data (sqlite)
  168. */
  169. async getTestDataSqlite(age = 0) {
  170. //console.log("select :", {age});
  171. let table = 'user';
  172. await this.checkAndCreateTableSqlite(table);
  173. const selectUser = this.demoSqliteDB.db.prepare(`SELECT * FROM ${table} WHERE age = @age`);
  174. const users = selectUser.all({age: age});
  175. //console.log("select users:", users);
  176. return users;
  177. }
  178. /*
  179. * all Test data (sqlite)
  180. */
  181. async getAllTestDataSqlite() {
  182. //console.log("select all user");
  183. let table = 'user';
  184. await this.checkAndCreateTableSqlite(table);
  185. const selectAllUser = this.demoSqliteDB.db.prepare(`SELECT * FROM ${table} `);
  186. const allUser = selectAllUser.all();
  187. //console.log("select allUser:", allUser);
  188. return allUser;
  189. }
  190. }
  191. StorageService.toString = () => '[class StorageService]';
  192. module.exports = StorageService;