-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from jdeniau/save-sql-query-into-file
- Loading branch information
Showing
8 changed files
with
105 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { existsSync, readFileSync, writeFileSync } from 'node:fs'; | ||
import { join } from 'path'; | ||
import { | ||
createConfigurationFolderIfNotExists, | ||
getConfigurationFolder, | ||
} from '../configuration/filePaths'; | ||
import { SQL_FILE_STORAGE_CHANNEL } from '../preload/sqlFileStorageChannel'; | ||
|
||
const LATEST_SQL_FILENAME = 'latest.sql'; | ||
|
||
function getSqlFilePath(): string { | ||
const configurationFolder = getConfigurationFolder(); | ||
|
||
return join(configurationFolder, LATEST_SQL_FILENAME); | ||
} | ||
|
||
function loadLatest(): string | null { | ||
const latestSqlFilePath = getSqlFilePath(); | ||
|
||
if (!existsSync(latestSqlFilePath)) { | ||
return null; | ||
} | ||
|
||
return readFileSync(latestSqlFilePath, 'utf-8'); | ||
} | ||
|
||
function saveLatest(sql: string): void { | ||
const latestSqlFilePath = getSqlFilePath(); | ||
|
||
createConfigurationFolderIfNotExists(); | ||
|
||
return writeFileSync(latestSqlFilePath, sql); | ||
} | ||
|
||
const IPC_EVENT_BINDING = { | ||
[SQL_FILE_STORAGE_CHANNEL.LOAD_LATEST]: loadLatest, | ||
[SQL_FILE_STORAGE_CHANNEL.SAVE_LATEST]: saveLatest, | ||
} as const; | ||
|
||
export function bindIpcMainSqlFileStorage(ipcMain: Electron.IpcMain): void { | ||
for (const [channel, handler] of Object.entries(IPC_EVENT_BINDING)) { | ||
ipcMain.handle(channel, (event, ...args: unknown[]) => | ||
// convert the first argument to senderId and bind the rest | ||
// @ts-expect-error issue with strict type in tsconfig, but seems to work at runtime | ||
handler(...args) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { bindChannel } from './bindChannel'; | ||
import { SQL_FILE_STORAGE_CHANNEL } from './sqlFileStorageChannel'; | ||
|
||
interface SqlFileStorage { | ||
loadLatest(): Promise<string | null>; | ||
saveLatest(sql: string): Promise<void>; | ||
} | ||
|
||
export const sqlFileStorage: SqlFileStorage = { | ||
loadLatest: bindChannel(SQL_FILE_STORAGE_CHANNEL.LOAD_LATEST), | ||
saveLatest: bindChannel(SQL_FILE_STORAGE_CHANNEL.SAVE_LATEST), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum SQL_FILE_STORAGE_CHANNEL { | ||
LOAD_LATEST = 'sql-file-storage:load-latest', | ||
SAVE_LATEST = 'sql-file-storage:save-latest', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters