diff --git a/ui/autoMate/src/main/db/connect.ts b/ui/autoMate/src/main/db/connect.ts new file mode 100644 index 0000000..fd3b74b --- /dev/null +++ b/ui/autoMate/src/main/db/connect.ts @@ -0,0 +1,7 @@ +import Database, * as BetterSqlite3 from 'better-sqlite3'; +import { app } from 'electron'; +import {resolve} from 'node:path' +const file = resolve(app.getPath('home'), "Desktop", 'hd.db') +const db: BetterSqlite3.Database = new Database(file, {}); +db.pragma('journal_mode = WAL'); +export {db}; \ No newline at end of file diff --git a/ui/autoMate/src/main/db/index.ts b/ui/autoMate/src/main/db/index.ts index fd3b74b..e42d1e7 100644 --- a/ui/autoMate/src/main/db/index.ts +++ b/ui/autoMate/src/main/db/index.ts @@ -1,7 +1 @@ -import Database, * as BetterSqlite3 from 'better-sqlite3'; -import { app } from 'electron'; -import {resolve} from 'node:path' -const file = resolve(app.getPath('home'), "Desktop", 'hd.db') -const db: BetterSqlite3.Database = new Database(file, {}); -db.pragma('journal_mode = WAL'); -export {db}; \ No newline at end of file +import './tables' \ No newline at end of file diff --git a/ui/autoMate/src/main/db/ipc.ts b/ui/autoMate/src/main/db/ipc.ts new file mode 100644 index 0000000..c6e3209 --- /dev/null +++ b/ui/autoMate/src/main/db/ipc.ts @@ -0,0 +1,5 @@ +import { IpcMainInvokeEvent, ipcMain } from "electron"; +import * as query from './query' +ipcMain.handle('sql', (_event: IpcMainInvokeEvent, sql: string, type: SqlActionType) => { + return query[type](sql) +}) \ No newline at end of file diff --git a/ui/autoMate/src/main/db/query.ts b/ui/autoMate/src/main/db/query.ts new file mode 100644 index 0000000..a120bdb --- /dev/null +++ b/ui/autoMate/src/main/db/query.ts @@ -0,0 +1,21 @@ +import {db} from './connect' + +export const findAll = (sql: string) => { + return db.prepare(sql).all(); +} + +export const findOne = (sql: string) => { + return db.prepare(sql).get(); +} + +export const create = (sql: string) => { + return db.prepare(sql).run().lastInsertRowid; +} + +export const update = (sql: string) => { + return db.prepare(sql).run().changes; +} + +export const del = (sql: string) => { + return db.prepare(sql).run().changes; +} \ No newline at end of file diff --git a/ui/autoMate/src/main/db/tables.ts b/ui/autoMate/src/main/db/tables.ts new file mode 100644 index 0000000..0b35bcb --- /dev/null +++ b/ui/autoMate/src/main/db/tables.ts @@ -0,0 +1,20 @@ +import { db } from "./connect"; + +db.exec(` +CREATE TABLE IF NOT EXISTS categories ( + id INTEGER PRIMARY KEY AUTOINCREMENT not null, + name TEXT not null, + created_at text not null +); +`) + + +db.exec(` +CREATE TABLE IF NOT EXISTS contents ( + id INTEGER PRIMARY KEY AUTOINCREMENT not null, + title TEXT not null, + content TEXT not null, + category_id INTEGER, + created_at TEXT not null +); +`) \ No newline at end of file diff --git a/ui/autoMate/src/preload/index.d.ts b/ui/autoMate/src/preload/index.d.ts index 2fa484a..0e5e6aa 100644 --- a/ui/autoMate/src/preload/index.d.ts +++ b/ui/autoMate/src/preload/index.d.ts @@ -7,7 +7,8 @@ declare global { hideWindow: () => void, shortCut: (type: 'search', shortCut: string) => Promise, setIgnoreMouseEvents: (ignore: boolean, options?: { forward: boolean }) => void, - openConfigWindow: () => void + openConfigWindow: () => void, + sql: (sql: string, type: SqlActionType) => Promise } } } diff --git a/ui/autoMate/src/preload/index.ts b/ui/autoMate/src/preload/index.ts index 9979b77..c8f544c 100644 --- a/ui/autoMate/src/preload/index.ts +++ b/ui/autoMate/src/preload/index.ts @@ -14,7 +14,11 @@ const api = { }, openConfigWindow: () => { ipcRenderer.send("openConfigWindow") + }, + sql: (sql: string, type: SqlActionType) => { + return ipcRenderer.invoke("sql", sql, type) } + } // Use `contextBridge` APIs to expose Electron APIs to diff --git a/ui/autoMate/tsconfig.node.json b/ui/autoMate/tsconfig.node.json index db23a68..163a5eb 100644 --- a/ui/autoMate/tsconfig.node.json +++ b/ui/autoMate/tsconfig.node.json @@ -1,6 +1,6 @@ { "extends": "@electron-toolkit/tsconfig/tsconfig.node.json", - "include": ["electron.vite.config.*", "src/main/**/*", "src/preload/**/*"], + "include": ["electron.vite.config.*", "src/main/**/*", "src/preload/**/*", "types.d.ts"], "compilerOptions": { "composite": true, "types": ["electron-vite/node"] diff --git a/ui/autoMate/tsconfig.web.json b/ui/autoMate/tsconfig.web.json index 9c16b66..26eaa06 100644 --- a/ui/autoMate/tsconfig.web.json +++ b/ui/autoMate/tsconfig.web.json @@ -4,7 +4,8 @@ "src/renderer/src/env.d.ts", "src/renderer/src/**/*", "src/renderer/src/**/*.tsx", - "src/preload/*.d.ts" + "src/preload/*.d.ts", + "types.d.ts" ], "compilerOptions": { "composite": true, diff --git a/ui/autoMate/types.d.ts b/ui/autoMate/types.d.ts new file mode 100644 index 0000000..773b590 --- /dev/null +++ b/ui/autoMate/types.d.ts @@ -0,0 +1 @@ +type SqlActionType = 'findAll' | 'findOne' | 'create' | 'update' | 'del'