Reputation: 23
I`m creating an windows app using React and Electron JS with database file using sqlite3. Im saving the database file in the '/databases' folder in the root folder. The one thing which I noticed is the database.db file is created only after I run 'npm start' in dev mode.
When I build the app using electron-builder, the app gets created without any issues. The problem is, once the app is installed for the first time, all the files are created except the db file. The app is opened automatically after installation. Since db file is not created, it throws error like 'Could not read database'. Once I close the app and reopen it by opening the .exe file, the database folder and the file are created. I need the DB file to be packed with the build in the root folder of the build not inside /build/resources/dist folder of electron. Any help would be appreciated.
Below is my package.json:
"build": {
"appId": "com.electron",
"files": [
"dist/electron.js",
"dist/index.html",
"dist/main.js",
"dist/images",
"dist/preload.js",
"dist/**"
],
"extraResources": [{
"from": "./databases/",
"to": "databases/",
"filter": [
"**/*"
]
}],
"directories": {
"output": "./electron"
},
"electronDownload": {
"cache": "./electron/cache"
},
"win": {
"target": "msi",
"icon": "./src/assets/images/icon.png"
},
"asar": false
}
Below is how I handle the database creation in electron.js:
const dbName = 'mydatabase.db';
const dbPath = path.join(path.dirname(__dirname), 'databases');
const fullDbPath = path.join(dbPath, dbName);
if(!fs.existsSync(dbPath)) {
fs.mkdirSync(dbPath, { recursive: true });
}
Upvotes: 1
Views: 1081
Reputation: 309
I have same problem with .db files when I build my electron app. I tryed to using Knex.js to handle It. Knex.js guid says you have to define Knex for sqlit3 or better-sqlit like this:
const knex = require('knex')({
client: 'sqlite3', // or 'better-sqlite3'
connection: {
filename: "./mydb.sqlite"
}
});
so you need a .sqlite file in your public folder and It`s usable even after building your project
Upvotes: 0