electron-vue icon indicating copy to clipboard operation
electron-vue copied to clipboard

Using sqlite3 with knex

Open vkiranmaniya opened this issue 5 years ago • 11 comments

Binding the Knex as Global Object of Vue.

The workaround i did.

I just installed sqlite3, knex andelectron-rebuild (to rebuild the native sqlite3 dependancy). Then i attached the knex object in src/renderer/main.js as given below.

const remote = require('electron').remote;
const dbPath = remote.getGlobal('dbPath');

let knex = require("knex")({
    client: 'sqlite3',
    connection: {
        filename: dbPath
    },
    useNullAsDefault: true
});

if (!process.env.IS_WEB) Vue.use(require('vue-electron'))
Vue.http = Vue.prototype.$http = axios
Vue.knex = Vue.prototype.$knex = knex
Vue.config.productionTip = false

Now, I'm able to use the knex object from any Vue component as this.$knex. It works fine when i run the app with npm run dev. But i got an issue when i tried to build the application using the default configurations.

Screenshots

image

Stacktrace
Error: Unresolved node modules: mssql, mysql, mysql2, pg
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/packageDependencies.ts:112:21
From previous event:
    at Collector.resolveUnresolvedHoisted (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/packageDependencies.ts:88:81)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/packageDependencies.ts:83:18
    at Generator.next (<anonymous>)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at Collector.collect (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/packageDependencies.ts:76:28)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/packageDependencies.ts:47:44
    at Generator.next (<anonymous>)
    at _getProductionDependencies (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/packageDependencies.js:92:37)
    at getProductionDependencies (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/packageDependencies.js:83:37)
    at Lazy.<anonymous> (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:138:24)
    at Generator.next (<anonymous>)
    at Lazy.get value [as value] (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/lazy-val/src/main.ts:18:25)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/yarn.js:166:90
From previous event:
    at _getProductionDependencies (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/packageDependencies.js:92:37)
    at getProductionDependencies (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/packageDependencies.js:83:37)
    at Lazy.<anonymous> (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:138:24)
    at Generator.next (<anonymous>)
From previous event:
    at Lazy.get value [as value] (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/lazy-val/src/main.ts:18:25)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/yarn.js:166:90
    at Generator.next (<anonymous>)
From previous event:
    at _rebuild (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/yarn.ts:179:5)
    at rebuild (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/yarn.js:163:19)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/yarn.ts:20:11
    at Generator.next (<anonymous>)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at _installOrRebuild (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/yarn.js:71:28)
    at installOrRebuild (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/out/util/yarn.js:55:28)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:509:13
    at Generator.next (<anonymous>)
From previous event:
    at Packager.installAppDependencies (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:470:70)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:420:20
    at Generator.next (<anonymous>)
From previous event:
    at Packager.doBuild (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:396:24)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:366:57
    at Generator.next (<anonymous>)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/graceful-fs/graceful-fs.js:136:16
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/graceful-fs/graceful-fs.js:57:14
    at FSReqWrap.args [as oncomplete] (fs.js:140:20)
From previous event:
    at Packager._build (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:335:133)
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:331:23
    at Generator.next (<anonymous>)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at Packager.build (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/packager.ts:288:14)
    at build (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/index.ts:59:28)
    at build (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/electron-builder/src/builder.ts:228:10)
    at then (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/electron-builder/src/cli/cli.ts:49:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `node .electron-vue/build.js && electron-builder`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/vkiranmaniya/.npm/_logs/2020-01-27T12_43_56_494Z-debug.log
Kirans-MacBook-Air:accurate vkiranmaniya$ 
Development Environment
  • Node version: 10.16.0
  • NPM version: 6.11.3
  • vue-cli version: 2.9.6
  • Operating System: MacOS High Seirra

vkiranmaniya avatar Jan 27 '20 13:01 vkiranmaniya

Any Updates please!

vkiranmaniya avatar Feb 01 '20 13:02 vkiranmaniya

Have you tried to install all the knexjs modules too

zaherg avatar Feb 08 '20 21:02 zaherg

Yes, I did. When it's working fine. But as per i know, I won't be able to use SQLite with electron-vue as the WebPack does not support native libraries. Is there any workaround to use sqlite3 with electron-vue?

vkiranmaniya avatar Feb 09 '20 04:02 vkiranmaniya

From the docs webpack support native modules, I didn't try it personally, but I see people using it without any problems.

Have you tried to update the packages to the latest version?

zaherg avatar Feb 09 '20 05:02 zaherg

Yes, I did. Can you provide setup steps? I need an electron-vue, sqlite3 with knex. the knex is gonna be used from component methods. So i want to attach the knex object to the vue global as shown in issue.

I did it successfully, but app can't find sqlite3 after production build. However, It runs perfectly in dev environment.

vkiranmaniya avatar Feb 09 '20 05:02 vkiranmaniya

Ok, so now the issue is in production.

can you try this comment https://github.com/SimulatedGREG/electron-vue/issues/49#issuecomment-316266553

zaherg avatar Feb 09 '20 06:02 zaherg

once again it's ending up with module resolution error

Error: Unresolved node modules: mssql, mysql, mysql2, pg
    at /Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/packageDependencies.ts:112:21
From previous event:
    at Collector.resolveUnresolvedHoisted (/Users/vkiranmaniya/Projects/Electron/Vue/accurate/node_modules/app-builder-lib/src/util/packageDependencies.ts:88:81)

After using the mentioned workaround.

vkiranmaniya avatar Feb 09 '20 16:02 vkiranmaniya

Facing the same issue, I tried the initial workaround given by @vkiranmaniya but the app only works in the dev environment. electron-builder fails to build due to module resolution error. I tried to find out the way but according to other resources, webpack doesn't support native APIs. sqlite3 uses native binary. is it the reason of failing build?

vishalvaghasiya avatar Feb 14 '20 04:02 vishalvaghasiya

Same issue here, for now I fix by install the rest of unresolved module by npm i -D mssql mysql mysql2 pg. Then it will be able to build.

nakorndev avatar Mar 07 '20 05:03 nakorndev

cannot find where I found this -- but you ignore the dialects you don't want in your webpack config

'use strict';
const path = require('path');
const { NormalModuleReplacementPlugin } = require('webpack');
module.exports = {
    mode: 'development',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    node: {
        __dirname: false,
        __filename: false
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js', '.json'],
        alias: { 'react/jsx-runtime': require.resolve('jsx-runtime') }
    },
    devtool: 'source-map',
    plugins: [
        // Ignore knex dynamic required dialects that we don't use
        new NormalModuleReplacementPlugin(
            /m[sy]sql2?|oracle(db)?|pg-(native|query)/,
            'pg',
        ),
    ],
};

ajoslin103 avatar Apr 28 '21 11:04 ajoslin103

maybe: https://www.dslemay.com/blog/2020/05/22/how-to-bundle-knex-with-webpack-for-serverless

ajoslin103 avatar Apr 28 '21 11:04 ajoslin103