2

For few days I'm trying to integrate my electron app with sqlite. I have already made the build of the sqlite with following script:

"rebuild": "electron-rebuild -f -w sqlite3"

and I'm able to connect to database, create db file but from javascript like following:

 var sqlite3 = require('sqlite3').verbose();
            var db = new sqlite3.Database('./database2.sqllite3');

            db.serialize(function() {
            db.run("CREATE TABLE lorem (info TEXT)");

            var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
            for (var i = 0; i < 10; i++) {
                stmt.run("Ipsum " + i);
            }
            stmt.finalize();

            db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
                console.log(row.id + ": " + row.info);
            });
            });

     db.close();

But since I'm using the component approach I would like to crate separated typescript service to provide all DB functionality and make it accessible form TS components. So I just tried to move this code to TS but it doesn't work. All examples from the web are about using this lines in js files or in component but I think that in older version of Angular as I'm getting following error:

ERROR in src/app/database.service.ts(19,18): error TS2304: Cannot find name 'require'.

I was able to call the dedicated js file from the ts script but with this same effect. Looks lite it was to late to use require ? Did anybody managed to make it working ?

This is my config

"dependencies": {
    "@angular/animations": "^5.0.1",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "@ngui/datetime-picker": "^0.16.2",
    "@types/lodash": "^4.14.85",
    "angular-svg-round-progressbar": "^1.2.0",
    "bootstrap": "^3.3.7",
    "commonjs": "latest",
    "core-js": "^2.4.1",
    "electron": "^1.8.1",
    "rxjs": "^5.5.2",
    "sqlite3": "^3.1.13",
    "xlsx": "^0.11.8",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.5.0",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "~2.6.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~8.0.50",
    "codelyzer": "~4.0.1",
    "electron-rebuild": "^1.6.0",
    "jasmine-core": "^2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.2.0",
    "ts-node": "~3.3.0",
    "tslint": "~5.8.0",
    "typescript": "~2.6.1"
  }

Regards Jan

6
  • 3
    You cannot use node modules (require) at client (angular) side. Calls to db must been made from server (node) side and than transferred to angular by using electron IPC. electron.atom.io/docs/api/ipc-main or github.com/leota/electron-angular4-sqlite3/tree/master/src/… Commented Nov 15, 2017 at 23:58
  • Possible duplicate Commented Nov 16, 2017 at 0:26
  • Thank you. I have found nice example how to integrate it with sqlite3 by @leota under following link stackoverflow.com/a/46707990/3316121 but when I'm trying to package this example with electron-packager it seems that it's more complicated then making the package from 'simple' Angular4 and electron app and I have problems with it. Did anybody tried to package it with success ? Commented Nov 22, 2017 at 22:04
  • @Jan, not sure if you've already figured out how to bundle sqlite in your Electron app. You might have a look at my sample app using electron, sqlite3, bootstrap 4 and webpack. Access to database straight from renderer process. IPC between main and renderer process is not needed. Commented Dec 27, 2017 at 20:49
  • I have found this project that can help you: electron-angular4-sqlite3 , Can you please explain on how to package after using an external package ? Commented Jan 15, 2018 at 13:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.