Electron with Knex Sqlite3 failed


#1

I use create-react-app to init my project. Then I added electron to that project. I’m using knex with sqlite3 to manage the database. I know the react+knex app work. Only when i run the electron then sqlite3 starts throwing hissing fits.

This is my code to create the sqlite database and initiate knex with the database file name.

const electron = window.require("electron");
const sqlite3 = window.require('sqlite3');
const app = (electron.app || electron.remote.app);
const userDir = path.join(app.getPath('userData'), 'databases/database.sqlite');
const db = new sqlite3.Database(userDir);

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

Next i create the table if not exists

knex.schema.createTableIfNotExists(...).return();

The electron app still runs, but console log the errors with Sqlite3.

nhandled rejection Error: Error calling all on connection.
    at http://localhost:3000/static/js/bundle.js:38994:25
    at Client_SQLite3._query (http://localhost:3000/static/js/bundle.js:38992:12)
    at Client_SQLite3.query (http://localhost:3000/static/js/bundle.js:33064:17)
    at Runner.<anonymous> (http://localhost:3000/static/js/bundle.js:43386:36)
From previous event:
    at Runner.<anonymous> (http://localhost:3000/static/js/bundle.js:43437:19)
From previous event:
    at Runner.queryArray (http://localhost:3000/static/js/bundle.js:43436:106)
    at http://localhost:3000/static/js/bundle.js:43296:23
    at <anonymous>
From previous event:
    at Runner.run (http://localhost:3000/static/js/bundle.js:43284:31)
    at SchemaBuilder.Target.then (http://localhost:3000/static/js/bundle.js:40626:43)
    at SchemaBuilder.Target.(anonymous function) [as return] (http://localhost:3000/static/js/bundle.js:40676:26)
    at Object../src/filereader-app/actions.js (http://localhost:3000/static/js/bundle.js:131761:10)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at fn (http://localhost:3000/static/js/bundle.js:89:20)
    at Object../src/filereader-app/app.js (http://localhost:3000/static/js/bundle.js:131880:16)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at fn (http://localhost:3000/static/js/bundle.js:89:20)
    at Object../src/index.js (http://localhost:3000/static/js/bundle.js:132745:12)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at fn (http://localhost:3000/static/js/bundle.js:89:20)
    at Object.1 (http://localhost:3000/static/js/bundle.js:132936:18)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at http://localhost:3000/static/js/bundle.js:725:37
    at http://localhost:3000/static/js/bundle.js:728:10

I also ran the distribution build. The build runs fine, but it just wouldn’t connect to the sqlite database.

Has any one encountered this issue before with electron and knex?

I’d really appreciate if you can show me how you configure your Knex to use with electron.


#2

have you tried to log ‘userDir’ variable to see if the .sqlite file is actually exist?


#3

Yeah, the userDir .sqlite is created.


#4

I realized that i required sqlite3 from window object; but I’m requiring knex from local node.

I changed my knex to window.require and the problem has been resolved.

export const knex = window.require('knex')({
  client:"sqlite3",
  connection:{
    filename: userDir
  },
  useNullAsDefault: true
});