DbStorage.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import path from "path";
  2. import {getDataDir} from "ee-core/ps";
  3. import {Database, SqliteStorage} from "ee-core/storage";
  4. class DbStorage {
  5. private static sqliteOptions = {
  6. timeout: 6000,
  7. verbose: console.log
  8. }
  9. private static storage : SqliteStorage
  10. private static db : Database
  11. /**
  12. * 初始化Sqlite数据库
  13. */
  14. public static _init() {
  15. const dbFile = path.join(getDataDir(),'db', 'print-utils.db')
  16. if (!DbStorage.storage || !DbStorage.db) {
  17. DbStorage.storage = new SqliteStorage(dbFile, DbStorage.sqliteOptions)
  18. DbStorage.db = DbStorage.storage.db
  19. }
  20. }
  21. public static getDb() {
  22. DbStorage._init()
  23. return DbStorage.db
  24. }
  25. public static checkTableExists(tableName: String) {
  26. // language=SQL format=false
  27. const masterStmt = DbStorage.getDb().prepare(`SELECT * FROM sqlite_master WHERE type=? AND name = ?`);
  28. return masterStmt.get('table', tableName) as Boolean
  29. }
  30. }
  31. DbStorage.toString = () => '[class DbStorage]'
  32. export {
  33. DbStorage
  34. }