Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate API for SQLite #24

Open
el3um4s opened this issue May 27, 2022 · 1 comment
Open

Investigate API for SQLite #24

el3um4s opened this issue May 27, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@el3um4s
Copy link
Owner

el3um4s commented May 27, 2022

No description provided.

@el3um4s el3um4s added the enhancement New feature or request label May 27, 2022
@el3um4s el3um4s changed the title Investigare API for SQLite Investigate API for SQLite Jun 2, 2022
@el3um4s
Copy link
Owner Author

el3um4s commented Aug 17, 2022

ELECTRON: sqlite.ts

import { IPC, SendChannels } from "@el3um4s/ipc-for-electron";
import { BrowserWindow, dialog, OpenDialogSyncOptions } from "electron";

import path from "path";
import sqlite3 from "sqlite3";

import { toTry } from "@el3um4s/to-try";

import { statSync, unlinkSync } from "node:fs";

const nameAPI = "sqlite";

// to Main
const validSendChannel: SendChannels = {
  testSQLite: testSQLite,
  loadHistoryBrowser: loadHistoryBrowser,
};

// from Main
const validReceiveChannel: string[] = ["loadHistoryBrowser"];

const sqlite = new IPC({
  nameAPI,
  validSendChannel,
  validReceiveChannel,
});

export default sqlite;

async function testSQLite(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const options = {
    filters: [
      {
        name: "Bookmark (Database SQLite)",
        extensions: ["bookmark", "sqlite3", "db3", "sqlite", "db"],
      },
    ],
  };
  const savePath = await dialog.showSaveDialog(mainWindow, options);
  if (!savePath.canceled && savePath.filePath) {
    const filePath = savePath.filePath;
    console.log(savePath);

    const { historyBrowser } = message;
    const [fileExists] = toTry(() => statSync(filePath));

    if (fileExists) {
      unlinkSync(filePath);
    }

    console.log("testSQLite");
    const db = new sqlite3.Database(filePath);
    db.serialize(() => {
      db.run(
        "CREATE TABLE historyBrowser (url, title, note, starred, folderHandle)"
      );
      const stmtHB = db.prepare(
        "INSERT INTO historyBrowser (url, title, note, starred, folderHandle) VALUES($url, $title, $note, $starred, $folderHandle)"
      );
      historyBrowser.forEach((el: any) => {
        const { url, title, note, starred } = el;
        const folderHandle = el?.folderHandle ? el.folderHandle : null;
        stmtHB.run({
          $url: url,
          $title: title,
          $note: note,
          $starred: starred,
          $folderHandle: folderHandle,
        });
      });
      stmtHB.finalize();

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

      db.each(
        "SELECT rowid AS id, url, title, note, starred, folderHandle FROM historyBrowser",
        (err, row) => {
          console.log(row.id, " : ", row.url);
        }
      );
    });

    db.close();
  }
}

async function loadHistoryBrowser(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const options: OpenDialogSyncOptions = {
    filters: [
      {
        name: "Bookmark (Database SQLite)",
        extensions: ["bookmark", "db", "sqlite3", "sqlite", "db3"],
      },
    ],
    properties: ["openFile"],
  };

  const loadFile = dialog.showOpenDialogSync(mainWindow, options);

  if (loadFile && loadFile.length > 0) {
    console.log(loadFile[0]);

    const db = new sqlite3.Database(loadFile[0]);

    db.each(
      "SELECT rowid AS id, url, title, note, starred, folderHandle FROM historyBrowser",
      async (err, row) => {
        console.log(row.id, " : ", row.url);
        if (row?.folderHandle) {
          console.log(row.folderHandle);
        }
      }
    );

    db.all("SELECT * FROM historyBrowser", (err, rows) => {
      console.log(rows);
      // mainWindow.webContents.send("loadHistoryBrowser", rows);
    });
    db.close();
  }
}

RENDERER: sqlite.ts

export const testSQLite = (historyBrowser) => {
  globalThis.api.sqlite.send("testSQLite", historyBrowser);
};

export const loadHistoryBrowser = () => {
  globalThis.api.sqlite.send("loadHistoryBrowser");
};

// https://willschenk.com/articles/2021/sq_lite_in_the_browser/
// https://sql.js.org/#/
// https://github.com/WiseLibs/better-sqlite3

Bookmark.svelte

  import { testSQLite, loadHistoryBrowser } from "../../Functions/sqlite";

  globalThis.api.sqlite.receive("loadHistoryBrowser", (data) => {
    console.log(data);
    let customBookmark = data;
    status.historyBrowserReplaceList(customBookmark);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant