0

I want to build a small app using:

  • Electron
  • Angular
  • SQLite

I am trying to connect to an SQLite DB and I found that there are multiple approaches. There are 2 that interested me:

  1. Using a library such as sqlite3.
  2. Using IPC that Electron library makes available: IpcRender and IpcMain

For now I've tried the first approach. Using this repository as starting point I get errors after npm install sqlite3 such as:

./node_modules/tar/node_modules/mkdirp/lib/use-native.js:1:11-24 - 
Error: Module not found: Error: Can't resolve 'fs' in '.../angular-electron/node_modules/tar/node_modules/mkdirp/lib'

For the second approach I found this article published on Feb 15, 2019 which uses electron-forge to generate the project with Angular 2 template and for the database connection it uses IpcRender.

I don't understand if I am doing something wrong, or if this library isn't compatible with the project or if I should switch to the second approach.

How is the second approach with IpcMain and IpcRender using typeorm library compared with using sqlite3 directly in an Angular Service, performance wise and best practice?

Robert
  • 186
  • 3
  • 6
  • 16
  • The sqlite3 library should work just fine. The error you included leads me to believe that the `moduleResolution` property from the `compilerOptions` inside your `tsconfig.json` is not set to `"node"`. Can you double check that? – Octavian Mărculescu Apr 13 '23 at 08:37
  • I've just checked and it is set `"moduleResolution": "node"`. Also it is a matter of what approach to use, it's not very clear to me what are the implications. A very important point it that I am **not** using any server such as **NodeJS** or **.NET**, I want to **avoid** internet connections – Robert Apr 13 '23 at 08:43
  • You should be able to use sqlite to manage a database locally. Can you have a look at [this answer](https://stackoverflow.com/a/62011612/1440005)? The problem looks similar. – Octavian Mărculescu Apr 13 '23 at 08:56
  • I've checked that article before posting this question and it wasn't clear for me if this is the right approach as it is mentioned in [the response](https://github.com/angular/angular-cli/issues/8272#issuecomment-341428996). That's why I decided to post this question to have a better understanding of what approach should I chose. I am not trying only to make it work temporarily, I want to understand future implications. – Robert Apr 13 '23 at 09:08
  • From what I noticed in [other SO posts](https://stackoverflow.com/a/41230765/1440005) it looks like sqlite needs to be rebuilt in order to work with electron. Feel free to give it a shot, maybe you'll get it to work. There seem to be also pure javascript alternatives, but are not as performant as native sqlite. – Octavian Mărculescu Apr 13 '23 at 09:31
  • The [github readme of the sqlite3 library](https://github.com/TryGhost/node-sqlite3#custom-builds-and-electron) also lists some instructions on how to go about adding it to an electron project, maybe that can be of help. – Octavian Mărculescu Apr 13 '23 at 09:31
  • I updated this post with a more specific question. Can you take a look please? Thank you. – Robert Apr 13 '23 at 09:58

1 Answers1

-1

I have been using sqlite3 in my angular electron app. These are the steps that I followed for implementation:

  1. Download sqlite3 in your project using npm.

  2. In your main.js file import sqlite3 using require.

    const sqlite = require('sqlite3').verbose()

  3. Create a db file in your project and use it to connect to the database.

    let db = new sqlite.Database('./your_file_name.db', (err) => { if (err) {

    }

    console.log('Connected to sqlite database') })

  4. Now you can simply use the db object to run sql queries using db.run callback function.

  • 1
    From my POV, this implementation that you are preseinting is very ambiguous. From what I've documented until now, the logical implementation is the second approach with **IpcMain** and **IpcRender**. It can also be used an ORM like **TypeORM** which I've tried using but with no success. In this conditions I've decided to use a separate service for **DB management** and call it with **REST** and for **OS API** I've decided to use this **IpcMain, IpcRender** approach via the **ElectronService**. – Robert May 01 '23 at 08:02