question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using local database after electron build

See original GitHub issue

Hey all. I’m currently using Electron 5.0.1, Vue 2.6, and The Electron Builder v1.3.1. I’m having trouble getting SQLite to work when I’m trying to build a MacOS or Windows app.

Here’s what my vue.config.js looks like:

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        data: `
          @import "@/styles/main.scss";
        `,
      },
    },
  },
  configureWebpack: {
    externals: {
      sequelize: "require('sequelize')",
    },
  },
  pluginOptions: {
    electronBuilder: {
      externals: ['sequelize'],
      builderOptions: {
        extraResources: ['src/data.db'],
      },
    }
  },
};

and Here’s how I connect via my db:

import Sequelize from 'sequelize';
import Component from '@/models/component';
import Element from '@/models/element';
import Kiosk from '@/models/kiosk';
import Page from '@/models/page';
import path from 'path';

const isBuild = process.env.NODE_ENV === 'production';
const pathToDbFile = path.join( // eslint-disable-next-line
  (isBuild ? __dirname : __static),
  (isBuild ? '../../' : ''),
  '../src/data.db',
);

// setup the connection to make sure it works
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: pathToDbFile,
  logging: (process.env.NODE_ENV !== 'production') ? console.log : false,
  define: {
    timestamps: false,
    underscored: true,
  },
});

const db = {};

// test for the connection
if (process.env.NODE_ENV !== 'production') {
  sequelize.authenticate()
    .then(() => {
      console.log('Connection has been established.');
    })
    .catch(console.error);
}

db.Component = Component.init(sequelize);
db.Element = Element.init(sequelize);
db.Kiosk = Kiosk.init(sequelize);
db.Page = Page.init(sequelize);

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) db[modelName].associate(db);
});

db.Sequelize = Sequelize;
db.sequelize = sequelize;

export default db;

Any idea what’s going on?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nklaymancommented, May 23, 2019

Use this:

const pathToDbFile = path.join(
  // eslint-disable-next-line
  isBuild ? __dirname : __static,
  '../src/data.db',
);

not sure why the path changed, but this works now.

0reactions
ihewrocommented, Apr 12, 2021

@j-nguyen Have you encountered this error?

Please install sqlite3 package manually

externals: ['sequelize'] in the vue.config.js works for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use a local database (SQLite or NeDB) after build?
I'm trying to use a local database in my electron app, it works in development but when i bundled the app (I only...
Read more >
How to embed a database in your electron app | by
In this post, I'll walk through my NEDB setup. It's fairly simple and is being used in production at HTTPSLocalhost app. nedb-promises is...
Read more >
Is it possible to have local database file used by Electron ...
Yes, you can easily create and use a local database in an Electron app using an npm package like node-sqlite3.
Read more >
Electron local Database : r/electronjs
I am creating an electron application which will be 100% offline all data will be stored on computer system, but data can go...
Read more >
Local Data storage for Electron
ajv is a JSON schema validator library. Schema validator is optional to build the NoSQL database, but I'd recommend using it since well-defined ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found