Electron + Sqlite3 + Vue3 + Ts +Sequelize数据库创建连接封装

框架搭建文档参考Electron + Vue3 + Vite + Ts + Sqlite3 + Sequelize搭建笔记

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}成功`)
                        
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值