From 63a4fe84bafc5ff19a4005b663a0bfe5ab2afb1f Mon Sep 17 00:00:00 2001 From: yuruo Date: Wed, 12 Jun 2024 16:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0(ui/autoMate)?= =?UTF-8?q?=EF=BC=9A=E5=BC=95=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=A8=A1=E5=9D=97=E5=92=8C=E6=9F=A5=E8=AF=A2=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=20=20=20=F0=9F=93=9D=20=E6=B7=BB=E5=8A=A0(ui/autoMate?= =?UTF-8?q?)=EF=BC=9A=E6=9B=B4=E6=96=B0=E8=A1=A8=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E5=92=8C=E6=93=8D=E4=BD=9C=E7=B1=BB=E5=9E=8B=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=20=20=20=F0=9F=93=9D=20=E6=B7=BB=E5=8A=A0(ui/autoMate)?= =?UTF-8?q?=EF=BC=9A=E6=9B=B4=E6=96=B0TypeScript=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=20=20=20=F0=9F=93=9D=20=E6=B7=BB=E5=8A=A0(ui?= =?UTF-8?q?/autoMate)=EF=BC=9A=E6=B7=BB=E5=8A=A0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=96=87=E4=BB=B6=20types.d.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/autoMate/src/main/db/connect.ts | 7 +++++++ ui/autoMate/src/main/db/index.ts | 8 +------- ui/autoMate/src/main/db/ipc.ts | 5 +++++ ui/autoMate/src/main/db/query.ts | 21 +++++++++++++++++++++ ui/autoMate/src/main/db/tables.ts | 20 ++++++++++++++++++++ ui/autoMate/src/preload/index.d.ts | 3 ++- ui/autoMate/src/preload/index.ts | 4 ++++ ui/autoMate/tsconfig.node.json | 2 +- ui/autoMate/tsconfig.web.json | 3 ++- ui/autoMate/types.d.ts | 1 + 10 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 ui/autoMate/src/main/db/connect.ts create mode 100644 ui/autoMate/src/main/db/ipc.ts create mode 100644 ui/autoMate/src/main/db/query.ts create mode 100644 ui/autoMate/src/main/db/tables.ts create mode 100644 ui/autoMate/types.d.ts 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'