@@ -2,71 +2,83 @@ import { IPC_CHANNELS } from '@datary/ipc'
22import { ipcMain } from 'electron'
33import Store from 'electron-store'
44
5+ import { buildCredentialId } from '../../services/credential/credential.utils'
6+ import { CredentialVault } from '../../services/credential/credential.vault'
57import { logger } from '../../utils/logger'
68
79export interface ConnectionHistoryItem {
8- id ? : string
10+ id : string
911 name ?: string
1012 host : string
1113 port : number
1214 user : string
13- password ?: string
1415 database : string
1516 type : 'postgresql' | 'mysql' | 'mariadb' | 'mssql'
1617 ssl ?: boolean
1718 lastUsed ?: number
1819}
1920
20- const ENCRYPTION_KEY = 'datary-secret-key-123'
2121const store = new Store < { history : ConnectionHistoryItem [ ] } > ( {
2222 name : 'connections' ,
23- defaults : { history : [ ] } ,
24- encryptionKey : ENCRYPTION_KEY
23+ defaults : { history : [ ] }
2524} )
2625
26+ const vault = new CredentialVault ( )
27+
2728export function registerConnectionHandlers ( ) {
2829 ipcMain . handle ( IPC_CHANNELS . CONNECTIONS . GET , ( ) => {
2930 return store . get ( 'history' ) ?? [ ]
3031 } )
3132
32- ipcMain . handle ( IPC_CHANNELS . CONNECTIONS . ADD , ( _ , connection : ConnectionHistoryItem ) => {
33- const history = store . get ( 'history' ) ?? [ ]
34- const index = history . findIndex (
35- c =>
36- c . host === connection . host &&
37- c . port === connection . port &&
38- c . user === connection . user &&
39- c . database === connection . database &&
40- c . type === connection . type
41- )
42-
43- const newConnection = { ...connection , lastUsed : Date . now ( ) }
44-
45- if ( index > - 1 ) {
46- history [ index ] = newConnection
47- } else {
48- history . unshift ( newConnection )
49- }
33+ ipcMain . handle (
34+ IPC_CHANNELS . CONNECTIONS . ADD ,
35+ async ( _ , connection : ConnectionHistoryItem & { password ?: string } ) => {
36+ const history = store . get ( 'history' ) ?? [ ]
5037
51- store . set ( 'history' , history )
52- return history
53- } )
38+ const credentialId = buildCredentialId ( connection )
39+
40+ if ( connection . password ) await vault . save ( credentialId , connection . password )
5441
55- ipcMain . handle ( IPC_CHANNELS . CONNECTIONS . DELETE , ( _ , connection : ConnectionHistoryItem ) => {
56- const history = store . get ( 'history' ) ?? [ ]
57- const filtered = history . filter (
58- c =>
59- ! (
42+ const index = history . findIndex (
43+ c =>
6044 c . host === connection . host &&
6145 c . port === connection . port &&
6246 c . user === connection . user &&
6347 c . database === connection . database &&
6448 c . type === connection . type
65- )
66- )
67- store . set ( 'history' , filtered )
68- return filtered
69- } )
49+ )
50+
51+ const newConnection = { ...connection , lastUsed : Date . now ( ) }
52+
53+ if ( index > - 1 ) history [ index ] = newConnection
54+ else history . unshift ( newConnection )
55+
56+ store . set ( 'history' , history )
57+ return history
58+ }
59+ )
60+
61+ ipcMain . handle (
62+ IPC_CHANNELS . CONNECTIONS . DELETE ,
63+ async ( _ , connection : ConnectionHistoryItem ) => {
64+ if ( connection . id ) await vault . delete ( connection . id )
65+
66+ const history = store . get ( 'history' ) ?? [ ]
67+ const filtered = history . filter (
68+ c =>
69+ ! (
70+ c . host === connection . host &&
71+ c . port === connection . port &&
72+ c . user === connection . user &&
73+ c . database === connection . database &&
74+ c . type === connection . type
75+ )
76+ )
77+
78+ store . set ( 'history' , filtered )
79+ return filtered
80+ }
81+ )
7082
7183 ipcMain . handle ( IPC_CHANNELS . CONNECTIONS . CLEAR , ( ) => {
7284 store . set ( 'history' , [ ] )
0 commit comments