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

Jan
  • 29
  • 1
  • 4
  • 9
  • 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. https://electron.atom.io/docs/api/ipc-main or https://github.com/leota/electron-angular4-sqlite3/tree/master/src/electron – MRsa Nov 15 '17 at 23:58
  • Possible [duplicate](https://stackoverflow.com/questions/38889801/angular-2-and-sqlite-connection) – Andresson Nov 16 '17 at 00:26
  • Thank you. I have found nice example how to integrate it with sqlite3 by @leota under following link https://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 ? – Jan Nov 22 '17 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](https://github.com/pamtbaau/angular5-electron-sqlite3-bootstrap4-webpack) using electron, sqlite3, bootstrap 4 and webpack. Access to database straight from renderer process. IPC between main and renderer process is not needed. – passerby Dec 27 '17 at 20:49
  • I have found this project that can help you: [electron-angular4-sqlite3](https://github.com/leota/electron-angular4-sqlite3) , Can you please explain on how to package after using an external package ? – Pini Cheyni Jan 15 '18 at 13:16
  • The following starter works https://github.com/CubikNeRubik/angular-electron-typeorm-starter – Andrew Allen Jul 23 '19 at 08:12

0 Answers0