Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using local database after electron build #275

Closed
j-nguyen opened this issue May 15, 2019 · 7 comments
Closed

Using local database after electron build #275

j-nguyen opened this issue May 15, 2019 · 7 comments

Comments

@j-nguyen
Copy link

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?

@nklayman
Copy link
Owner

I don't see any error when I run it myself. Please send me a demo repo that has the error.

@j-nguyen
Copy link
Author

Hello, @nklayman.

Here's the repo:
https://github.com/j-nguyen/demo_vue_electron_sqlite

Here's some context. When I use npm run electron:build to generate a MacOS binrary, or Windows app, this is the error I receive:

Screen Shot 2019-05-21 at 4 24 59 PM

@nklayman
Copy link
Owner

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.

@j-nguyen
Copy link
Author

@nklayman

Thank you! This works, but I have another issue with sequelize and SQLite. I don't have a table named fs, but it keeps yelling at me that there is no such table for it. This hasn't happened to me when doing electron:serve, so I'm a bit confused now.

Screen Shot 2019-05-23 at 4 44 14 PM

@j-nguyen
Copy link
Author

Oh, nevermind. Got it cleared up. Just had to declare the modelName on sequelize. Thank you for your help!

@techuila
Copy link

techuila commented Aug 8, 2019

@j-nguyen Have you encountered this error?

Please install sqlite3 package manually

@ihewro
Copy link

ihewro commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants