在生产模式 Electron + Angular 下找不到 sqlite 文件

Posted

技术标签:

【中文标题】在生产模式 Electron + Angular 下找不到 sqlite 文件【英文标题】:Cannot find sqlite file in production mode Electron + Angular 【发布时间】:2021-01-26 04:04:11 【问题描述】:

我正在使用带有 Angular 的电子和 SQLite 数据库来在本地保存数据。它在开发环境中完美运行,但是当我使用 electron-builder 构建它时它在生产中失败了,因为它“找不到”数据库文件。 我也在为这个过程使用这个很棒的样板代码库 => https://github.com/maximegris/angular-electron/tree/master/src

以下是我的目录结构

- src/
- db/
-- database.sqlite
- main.ts

以下是我如何尝试使用 Knex 访问 main.ts 中的数据库文件

let knex = Knex(
  client: "sqlite3",
  connection: 
    filename:path.join(__dirname, '/db/', 'database.sqlite')
  
);

以下是我的脚本和配置

electron-builder.json


  "productName": "POS Billing",
  "directories": 
    "output": "release/"
  ,
    "files": [
        "**/*",
        "!**/*.ts",
        "!*.code-workspace",
        "!LICENSE.md",
        "!package.json",
        "!package-lock.json",
        "!src/",
        "!e2e/",
        "!hooks/",
        "!angular.json",
        "!_config.yml",
        "!karma.conf.js",
        "!tsconfig.json",
        "!tslint.json"
    ],
  "win": 
    "icon": "dist/assets/icons",
    "target": [
      "portable"
    ]
  ,
  "mac": 
    "icon": "dist/assets/icons",
    "target": [
      "dmg"
    ]
  ,
  "linux": 
    "icon": "dist/assets/icons",
    "target": [
      "AppImage"
    ]
  

package.json


  "name": "pos-billing",
  "version": "1.0.0",
  "main": "main.js",
  "private": true,
 
  "scripts": 
    "postinstall": "electron-builder install-app-deps",
    "ng": "ng",
    "start": "npm-run-all -p electron:serve ng:serve",
    "build": "npm run electron:serve-tsc && ng build --base-href ./",
    "build:dev": "npm run build -- -c dev",
    "build:prod": "npm run build -- -c production",
    "ng:serve": "ng serve -c web -o",
   "electron:serve-tsc": "tsc -p tsconfig.serve.json",
    "electron:serve": "wait-on tcp:4200 && npm run electron:serve-tsc && npx electron . --serve",
    "electron:local": "npm run build:prod && npx electron .",
    "electron:build": "npm run build:prod && electron-builder build",
    "test": "ng test --watch=false",
    "test:watch": "ng test",
    "e2e": "npm run build:prod && cross-env TS_NODE_PROJECT='e2e/tsconfig.e2e.json' mocha --timeout 300000 --require ts-node/register e2e/**/*.e2e.ts",
    "version": "conventional-changelog -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
    "lint": "ng lint"
  ,
  "devDependencies": 
    "@angular-builders/custom-webpack": "10.0.1",
    "@angular-devkit/build-angular": "0.1001.6",
    "@angular-eslint/builder": "0.5.0-beta.3",
    "@angular-eslint/eslint-plugin": "0.5.0-beta.3",
    "@angular-eslint/eslint-plugin-template": "0.5.0-beta.3",
    "@angular-eslint/template-parser": "0.5.0-beta.3",
    "@angular/cli": "10.1.6",
    "@angular/compiler": "10.1.5",
    "@angular/compiler-cli": "10.1.5",
    "@angular/language-service": "10.1.5",
    "@angular/platform-browser": "10.1.5",
    "@angular/platform-browser-dynamic": "10.1.5",
    "@ngx-translate/core": "13.0.0",
    "@ngx-translate/http-loader": "6.0.0",
    "@types/jasmine": "3.5.14",
    "@types/jasminewd2": "2.0.8",
    "@types/mocha": "8.0.3",
    "@types/node": "14.11.8",
    "@typescript-eslint/eslint-plugin": "4.4.0",
    "@typescript-eslint/eslint-plugin-tslint": "4.4.0",
    "@typescript-eslint/parser": "4.4.0",
    "chai": "4.2.0",
    "electron-packager": "^15.1.0",
    "conventional-changelog-cli": "2.1.0",
    "core-js": "3.6.5",
    "cross-env": "7.0.2",
    "electron": "10.1.3",
    "electron-builder": "22.8.1",
    "electron-reload": "1.5.0",
    "eslint": "7.11.0",
    "eslint-plugin-import": "2.22.1",
    "jasmine-core": "3.6.0",
    "jasmine-spec-reporter": "6.0.0",
    "karma": "5.2.3",
    "karma-coverage-istanbul-reporter": "3.0.3",
    "karma-electron": "6.3.1",
    "karma-jasmine": "4.0.1",
    "karma-jasmine-html-reporter": "1.5.4",
    "mocha": "8.1.3",
    "npm-run-all": "4.1.5",
    "rxjs": "6.6.3",
    "spectron": "12.0.0",
    "ts-node": "9.0.0",
    "tslib": "2.0.3",
    "typescript": "4.0.3",
    "wait-on": "5.2.0",
    "webdriver-manager": "12.1.7",
    "zone.js": "0.11.1"
  ,
  "dependencies": 
    "@angular/common": "10.1.5",
    "@angular/core": "10.1.5",
    "@angular/forms": "10.1.5",
    "@angular/animations": "~10.1.5",
    "@angular/router": "10.1.5",
    "@ng-select/ng-select": "^5.0.7",
    "@swimlane/ngx-datatable": "^18.0.0",
    "file-saver": "^2.0.2",
    "guid-typescript": "^1.0.9",
    "knex": "^0.21.6",
    "license-key-gen": "^0.1.4",
    "ng-select": "^1.0.2",
    "ngx-export-as": "^1.5.0",
    "ngx-print": "^1.2.0-beta.5",
    "sqlite3": "^5.0.0"
  ,
  "engines": 
    "node": ">=10.13.0"
  

这会在 release/ 目录下创建一个 exe 文件,但是当我打开它时,它无法加载数据库。 我尝试使用app.getPath("userData") 解决方案,但这也不起作用。

任何帮助将不胜感激。感谢您的帮助。

【问题讨论】:

【参考方案1】:

filename:path.join(__dirname, '/db/', 'database.sqlite')

您将文件存储在应用程序资源中,这是不可能的。在应用程序包中,我们不允许编写或更改内容。

这在您开发应用程序时是可能的,但在打包后是不可能的。

您必须将数据库文件存储在某处。通常和所有 应用程序,您可以将其存储在application data 目录。可以通过app.getPath('userData')获取应用数据路径

【讨论】:

以上是关于在生产模式 Electron + Angular 下找不到 sqlite 文件的主要内容,如果未能解决你的问题,请参考以下文章

在 Electron 模式构建中为 Quasar 设置 API URL

Electron builder - 如何为生产 Windows 可执行文件构建 loadURL

如何在 Electron-Angular 项目中使用永远的监视器?

Angular2中的生产模式和开发模式有啥区别?

多个 Angular 库在生产模式下不起作用

Angular-cli aot 构建在生产模式下失败