Skip to content

Commit

Permalink
v2.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tmotagam committed Nov 10, 2022
1 parent 0e733be commit 12725ff
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 259 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

sqlite-electron module for electron and nodejs
Copyright (C) 2022 Motagamwala Taha Arif Ali
Copyright (C) 2022-2023 Motagamwala Taha Arif Ali

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

sqlite-electron Copyright (C) 2022 Motagamwala Taha Arif Ali
sqlite-electron Copyright (C) 2022-2023 Motagamwala Taha Arif Ali
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
Expand Down
94 changes: 65 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
# Sqlite Electron

Sqlite Electron is a module for electron to use sqlite3 database without rebuilding as of now supports Windows(win32) (x64, x32) and Linux (x64).
Sqlite Electron is a module for electron to use sqlite3 database without rebuilding it supports Windows(win32) (x64, x32) and Linux (x64). It now supports ESM and CJS 🎉.

## Installation

Use the package manager [npm](https://npmjs.com/) to install Sqlite Electron.
Use the package manager [npm](https://www.npmjs.com/package/sqlite-electron) to install Sqlite Electron.

```bash
npm install sqlite-electron
```
OR

## Note
The package installs the prebuilt binaries of the sqlite on your system (if your system is supported) if you want any other platform binaries go to
https://github.com/tmotagam/sqlite-electron/tree/master/binaries
Use the package manager [yarn](https://yarnpkg.com/package/sqlite-electron) to install Sqlite Electron.

```bash
yarn add sqlite-electron
```

## Notes
1. The package installs the prebuilt binaries of the sqlite on your system (if your system is supported) if you want any other platform binaries for a specific version go to https://github.com/tmotagam/sqlite-electron/releases.

## Functions
2. The example written for this library is not a Boilerplate because of its disregards to the security required for electron so do not use it in your application.

| Functions | Description |
3. Never give values in the query string use values array for giving the values for the query not taking this precaution will result in sql injection attacks !.

Good parctice example
```javascript
import { executeQuery } from 'sqlite-electron'
executeQuery("INSERT INTO sqlite_main (NAME,AGE,ADDRESS,SALARY) VALUES (?, ?, ?, ?);", [var_name, var_age, var_address, var_salary]) // Do this
```

Bad parctice example:
```javascript
import { executeQuery } from 'sqlite-electron'
executeQuery(`INSERT INTO sqlite_main (NAME,AGE,ADDRESS,SALARY) VALUES (${var_name}, ${var_age}, ${var_address}, ${var_salary});`) // Never do this
```


## API`s

| Api | Description |
| ---------------- |:---------------------:|
| dbPath | Variable to set your path for the database and also to connect to the database if it already exists |
| setdbPath(path='') | It opens or creates the database for operation |
| executeQuery(Query = '', fetch = '', values = []) | It Executes single query with fetch and values the fetch must be in string eg:- 'all', '1','2'... '' values must be array |
| executeMany(Query = '', values = []) | It executes single query with multiple values |
| executeScript(scriptName = '') | It execute the sql script scriptName must be name of the script |
| executeScript(scriptName = '') | It execute the sql script scriptName must be name of the script or the script itself |

## Usage

Expand All @@ -46,11 +68,11 @@ app.on('window-all-closed', () => {
})
```

### dbPath
### setdbPath

This is a exposed variable for setting the path of the new database and also for connecting to the existing database.
This is a function for opening a existing database or creating a new database for operation.

Set this variable before using any of the api.
Call this function before calling the other 3 functions.

```javascript
const { app, BrowserWindow, ipcMain } = require('electron')
Expand All @@ -67,19 +89,36 @@ app.on('window-all-closed', () => {
// Your Code
})

ipcMain.handle('databasePath', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
ipcMain.handle('databasePath', async (event, dbPath) => {
return await sqlite.setdbPath(dbPath)
})
```

### executeQuery
Now you can also create In-memory database like this.

This is the function for executing any single query eg: 'SELECT * FROM sqlite_main' you can give values through value array and tell the function to fetch data by specifying the fetch parameter eg: "all", 1, 2, 3, .., infinity.
```javascript
const { app, BrowserWindow, ipcMain } = require('electron')
const sqlite = require('sqlite-electron')

Note: Never give values in the query string use value array for giving the values for the query not taking this precaution will result in sql injection attacks !.
function createWindow () {
// Your Code
}
app.whenReady().then(() => {
// Your Code
})

app.on('window-all-closed', () => {
// Your Code
})

ipcMain.handle('createInMemoryDatabase', async() => {
return await sqlite.setdbPath(':memory:')
})
```

### executeQuery

eg: ("INSERT INTO sqlite_main (NAME,AGE,ADDRESS,SALARY) VALUES (?, ?, ?, ?);", ["name", 900, "example street", 123456789000]) // This is best practice
This is the function for executing any single query eg: 'SELECT * FROM sqlite_main' you can give values through value array and tell the function to fetch data by specifying the fetch parameter eg: "all", 1, 2, 3, .., infinity.

```javascript
const { app, BrowserWindow, ipcMain } = require('electron')
Expand All @@ -96,9 +135,8 @@ app.on('window-all-closed', () => {
// Your Code
})

ipcMain.handle('databasePath', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
ipcMain.handle('databasePath', async (event, dbPath) => {
return await sqlite.setdbPath(dbPath)
})

ipcMain.handle('executeQuery', async (event, query, fetch, value) => {
Expand Down Expand Up @@ -129,9 +167,8 @@ app.on('window-all-closed', () => {
// Your Code
})

ipcMain.handle('databasePath', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
ipcMain.handle('databasePath', async (event, dbPath) => {
return await sqlite.setdbPath(dbPath)
})

ipcMain.handle('executeMany', async (event, query, values) => {
Expand Down Expand Up @@ -167,9 +204,8 @@ app.on('window-all-closed', () => {
// Your Code
})

ipcMain.handle('databasePath', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
ipcMain.handle('databasePath', async (event, dbPath) => {
return await sqlite.setdbPath(dbPath)
})

ipcMain.handle('executeScript', async (event, scriptpath) => {
Expand All @@ -181,7 +217,7 @@ ipcMain.handle('executeScript', async (event, scriptpath) => {


## Example
[See sqlite-electron in action using electron 19.0.6](https://github.com/tmotagam/sqlite-electron/tree/master/example)
[See sqlite-electron in action using electron 21.0.1](https://github.com/tmotagam/sqlite-electron/tree/master/example)

## Contributing
Pull requests and issues are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
72 changes: 37 additions & 35 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
const sqlite = require('sqlite-electron')
const { app, BrowserWindow, ipcMain } = require("electron");
const { join } = require("path");
const { setdbPath, executeQuery, executeMany, executeScript } = require("sqlite-electron");

function createWindow () {
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
preload: join(__dirname, "preload.js"),
},
});

win.loadFile('index.html')
win.loadFile("index.html");
}
app.enableSandbox()
app.enableSandbox();
app.whenReady().then(() => {
sqlite.dbPath = 'test.db';
createWindow()
createWindow();

app.on('activate', () => {
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
createWindow();
}
})
})
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
})

ipcMain.handle('potd', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
})
});

ipcMain.handle('executeQuery', async (event, query, fetch, value) => {
ipcMain.handle("potd", async (event, dbPath) => {
try {
return await sqlite.executeQuery(query, fetch, value);
return await setdbPath(dbPath)
} catch (error) {
return error
}
})
});

ipcMain.handle('executeMany', async (event, query, values) => {
ipcMain.handle("executeQuery", async (event, query, fetch, value) => {
try {
return await sqlite.executeMany(query, values)
return await executeQuery(query, fetch, value);
} catch (error) {
return error
return error;
}
})
});

ipcMain.handle('executeScript', async (event, scriptpath) => {
ipcMain.handle("executeMany", async (event, query, values) => {
try {
return await sqlite.executeScript(scriptpath);
return await executeMany(query, values);
} catch (error) {
return error
return error;
}
});

ipcMain.handle("executeScript", async (event, scriptpath) => {
try {
return await executeScript(scriptpath);
} catch (error) {
return error;
}
})
});
14 changes: 7 additions & 7 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"name": "test",
"version": "1.0.0",
"version": "2.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"author": "Motagamwala Taha Arif Ali",
"license": "ISC",
"devDependencies": {
"electron": "^19.0.6",
"electron-packager": "^15.5.1"
},
"dependencies": {
"sqlite-electron": "^2.0.5"
"electron": "^21.2.2",
"electron-packager": "^17.1.1"
},
"browser": {
"fs": false,
"path": false,
"os": false
},
"dependencies": {
"sqlite-electron": "^2.2.3"
}
}
10 changes: 7 additions & 3 deletions example/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ window.addEventListener('DOMContentLoaded', async () => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}

for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
Expand All @@ -14,8 +14,12 @@ window.addEventListener('DOMContentLoaded', async () => {
contextBridge.exposeInMainWorld('api', {
path: async () => {
const path = document.getElementById('dbpath').value
const res = await ipcRenderer.invoke('potd', path)
document.getElementById('pout').innerText = 'Output: ' + res;
try {
const res = await ipcRenderer.invoke('potd', path)
document.getElementById('pout').innerText = 'Output: ' + res;
} catch (error) {
document.getElementById('pout').innerText = 'Output: ' + error;
}
},
equery: async () => {
const query = document.getElementById('singlequery').value
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
{
"name": "sqlite-electron",
"version": "2.0.5",
"version": "2.2.3",
"description": "A module for electron to use sqlite3 without rebuilding",
"main": "sqlite-electron.js",
"main": "./cjs/sqlite-electron.js",
"module": "./esm.sqlite-electron.mjs",
"types": "./sqlite-electron.d.ts",
"scripts": {
"postinstall": "node ./scripts/postinstall.js"
},
"exports": {
"import": "./esm/sqlite-electron.mjs",
"require": "./cjs/sqlite-electron.js"
},
"author": "Motagamwala Taha Arif Ali",
"files": [
"sqlite-electron.js",
"cjs",
"esm",
"sqlite-electron.d.ts",
"scripts"
],
Expand Down

0 comments on commit 12725ff

Please sign in to comment.