DbUtils.ts // 封装文件
const db = require('sqlite3').verbose()
const path = require("path")
const fs = require("fs")
const {Sequelize} = require('sequelize');
import {localFilePath} from "@/config";
import {creatFolder, fileExistence} from "@/utils/file";
import writers from "@/db/writers/tables/writers";
export default class DbUtils {
// 所有要存放或者要读取的本地文件根路径
public localFilePath: string = localFilePath()
protected dbPathTem: string = '' // 临时的数据库文件路径
public readonly defaultFolder: string = 'dbFile'
constructor() {
this.setLocalFilePath()
}
public setLocalFilePath = (path: string = localFilePath()): void => {
this.localFilePath = path
}
/**
* @description 保存数据库文件
* @param {string} dbNameIsFullPath dbName是否完整路径,如果不是会自动拼接
* @param {string} dbName 数据库文件名称
* @param {string} folderName 存放的文件夹名称(默认dbs),如果没有则自动创建
* @return {string} 如果数据库已经存在,则返回路径,否则返回空字符串
*/
public createDbFile(dbNameIsFullPath: boolean = true, dbName: string = this.dbPathTem, folderName?: string): Promise<string> {
return new Promise((resolve, reject) => {
if (!this.judgeDbExists(dbNameIsFullPath, dbName, folderName)) {
const dbFile = new db.Database(this.dbPathTem, (err: any) => {
if (err) {
console.error('创建数据库失败=> ', err)
resolve('')
} else {
console.log(`创建数据库${dbName}成功`)