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

How to use a local database (SQLite or NeDB) after build? #76

Closed
renatomattos2912 opened this issue Sep 10, 2018 · 11 comments
Closed

How to use a local database (SQLite or NeDB) after build? #76

renatomattos2912 opened this issue Sep 10, 2018 · 11 comments

Comments

@renatomattos2912
Copy link

I'm trying to use a local database in my electron app, it works in development but when i bundled the app (I only tested in mac os bundle), the database is not connecting anymore, so maybe the bundle is not moving my database to a folder with the right permissions, but i dont know in what folder i can put my database file, i don't find nothing in the docs too, can someone help me with this?

@nklayman
Copy link
Owner

nklayman commented Sep 10, 2018

The reason it doesn't work is because all of your files get packed into an asar archive. This archive is read only, preventing a database from working. To fix this, you need to have the database file/folder not packed into the archive:

Note: change src/database.txt to the path of your database file/folder

// vue.config.js

module.exports = {
  pluginOptions: {
    electronBuilder: {
      builderOptions: {
        extraResources: ['src/database.txt']
      }
    }
  }
}
// Wherever you use the database
import path from 'path'

const isBuild = process.env.NODE_ENV === 'production'

const pathToDbFile = path.join(
  (isBuild ? __dirname : __static),
  (isBuild ? '../../' : ''),
  '../src/database.txt'
)

pathToDbFile is equal to PROJECT_ROOT/src/database.txt.

UPDATE:
Issue #275 reports that the path to access the file has changed. If the original answer does not work for you, try this instead:

const pathToDbFile = path.join(
  isBuild ? __dirname : __static,
  '../src/database.txt',
);

@renatomattos2912
Copy link
Author

Man, you are a life saver, thank you so much.

@piascikj
Copy link

@nklayman Thanks for this great plugin! I'm having a similar issue where I'm trying to use child_process.fork, but it doesn't seem to work in the bundled app. It only works from electron:serve.

// vue.config.js
module.exports = {
  configureWebpack: {
    resolve: {
      symlinks: false
    }
  },
  pluginOptions: {
    electronBuilder: {
      extraResources: [
        "src/imdone-repo-worker.js"
      ]
    }
  }
}
// forking the worker
    const pathToWorker = path.join(
      (isDevelopment ? __static : __dirname),
      (isDevelopment ? '' : '../../'),
      '../src/imdone-repo-worker.js'
    )

    workers[this.path] = this.worker = fork(pathToWorker, {silent: true})

Any idea why this won't work?

@nklayman
Copy link
Owner

@piascikj have you tried placing the script in the public folder and running it inside the archive? Also, try just reading the contents of the file, or the parent directory. In addition, can you tell me where the worker file ends up in your built app? Thanks!

@piascikj
Copy link

Thanks a bunch @nklayman! Moving imdone-repo-worker.js to public/ and using path.join(__static,'imdone-repo-worker.js') did the trick.

@nathansalwen
Copy link

Thank you for this note! Is there any way this information could be made more visible or linked to in, for example the following page?

https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/guide.html#native-modules

Thanks!

@nklayman
Copy link
Owner

@nathansalwen Added a link to the above comment in that section, the updated docs should be live now.

@nathansalwen
Copy link

nathansalwen commented Jun 23, 2019

I see the comment. I think that is an improvement. In my case, the "extraResources" configuration was necessary for ffi-napi to find external libraries. Perhaps, the following text would be useful for others in my situation.

"If files need to be present in an unpacked form, such as library files for ffi-napi or database files for MySQL or MongoDB, additional configuration is needed. See Issue #76 (comment) for usage of builderOptions->ExtraResources in vue.config.js."

Again, thank you for an awesome tool!

@douglast2t
Copy link

hi @nklayman, i'm having problems on building, can you be more specific about the configuration to use mysql?
thank for this awesome project by the way!

@nklayman
Copy link
Owner

nklayman commented Aug 3, 2019

@douglast2t use mysql as if you are creating a regular nodejs script, but follow the instructions in #76 (comment) for determining the location of your database file/folder. The mysql code should go in src/background.js.

@mahmoud-alragabi
Copy link

this worked for me

in vue.config.js

module.exports = { chainWebpack: config => { config.externals({ "nedb":"commonjs nedb" }) } }

these could also be helpful for you

https://stackoverflow.com/questions/40650701/nedb-not-loading-or-storing-to-file

louischatriot/nedb#329

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

6 participants