mirror of
https://github.com/yuruotong1/autoMate.git
synced 2026-03-22 13:07:17 +08:00
✨ 添加(ui/autoMate):引入数据库连接模块和查询模块
📝 添加(ui/autoMate):更新表结构和操作类型定义 📝 添加(ui/autoMate):更新TypeScript配置文件 📝 添加(ui/autoMate):添加类型定义文件 types.d.ts
This commit is contained in:
7
ui/autoMate/src/main/db/connect.ts
Normal file
7
ui/autoMate/src/main/db/connect.ts
Normal file
@@ -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};
|
||||
@@ -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};
|
||||
import './tables'
|
||||
5
ui/autoMate/src/main/db/ipc.ts
Normal file
5
ui/autoMate/src/main/db/ipc.ts
Normal file
@@ -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)
|
||||
})
|
||||
21
ui/autoMate/src/main/db/query.ts
Normal file
21
ui/autoMate/src/main/db/query.ts
Normal file
@@ -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;
|
||||
}
|
||||
20
ui/autoMate/src/main/db/tables.ts
Normal file
20
ui/autoMate/src/main/db/tables.ts
Normal file
@@ -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
|
||||
);
|
||||
`)
|
||||
3
ui/autoMate/src/preload/index.d.ts
vendored
3
ui/autoMate/src/preload/index.d.ts
vendored
@@ -7,7 +7,8 @@ declare global {
|
||||
hideWindow: () => void,
|
||||
shortCut: (type: 'search', shortCut: string) => Promise<boolean>,
|
||||
setIgnoreMouseEvents: (ignore: boolean, options?: { forward: boolean }) => void,
|
||||
openConfigWindow: () => void
|
||||
openConfigWindow: () => void,
|
||||
sql: <T>(sql: string, type: SqlActionType) => Promise<T>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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,
|
||||
|
||||
1
ui/autoMate/types.d.ts
vendored
Normal file
1
ui/autoMate/types.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
type SqlActionType = 'findAll' | 'findOne' | 'create' | 'update' | 'del'
|
||||
Reference in New Issue
Block a user