1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import Database from 'better-sqlite3'
import { app, BrowserWindow } from 'electron'
import fs from 'node:fs'
import path from 'node:path'
const isDev = !app.isPackaged // reliable dev/prod switch. [oai_citation:2‡Electron](https://electronjs.org/docs/latest/api/app?utm_source=chatgpt.com)
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js'),
},
})
if (isDev) {
const url = process.env.VITE_DEV_SERVER_URL ?? 'http://localhost:5173'
win.loadURL(url).finally(() => {
win.show()
win.webContents.openDevTools({ mode: 'detach' })
})
} else {
const indexHtml = path.join(
app.getAppPath(),
'renderer',
'dist',
'index.html'
)
win.loadFile(indexHtml).finally(() => win.show())
}
}
function initDb() {
const dbDir = app.getPath('userData')
if (!fs.existsSync(dbDir)) fs.mkdirSync(dbDir, { recursive: true })
const dbPath = path.join(dbDir, 'app.db')
const db = new Database(dbPath)
db.pragma('journal_mode = WAL')
db.prepare('CREATE TABLE IF NOT EXISTS messages (text TEXT)').run()
db.prepare('INSERT INTO messages (text) VALUES (?)').run(
'hello from better-sqlite3'
)
const row = db.prepare('SELECT text FROM messages LIMIT 1').get()
console.log('Selected row:', row)
db.close()
}
app.whenReady().then(() => {
initDb()
createWindow()
app.on(
'activate',
() => BrowserWindow.getAllWindows().length === 0 && createWindow()
)
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
|