mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-15 04:51:06 +02:00
Add preliminary prisma support
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { app, ipcMain } from 'electron';
|
||||
import isDev from 'electron-is-dev';
|
||||
import './server';
|
||||
|
||||
const dbPath = isDev
|
||||
? path.join(__dirname, '../../../../prisma/dev.db')
|
||||
: path.join(app.getPath('userData'), 'database.db');
|
||||
|
||||
if (!isDev) {
|
||||
try {
|
||||
// database file does not exist, need to create
|
||||
fs.copyFileSync(path.join(process.resourcesPath, 'prisma/dev.db'), dbPath, fs.constants.COPYFILE_EXCL);
|
||||
console.log(`DB does not exist. Create new DB from ${path.join(process.resourcesPath, 'prisma/dev.db')}`);
|
||||
} catch (err) {
|
||||
if (err && 'code' in (err as { code: string }) && (err as { code: string }).code !== 'EEXIST') {
|
||||
console.error(`DB creation faild. Reason:`, err);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getPlatformName(): string {
|
||||
const isDarwin = process.platform === 'darwin';
|
||||
if (isDarwin && process.arch === 'arm64') {
|
||||
return `${process.platform}Arm64`;
|
||||
}
|
||||
|
||||
return process.platform;
|
||||
}
|
||||
|
||||
const platformToExecutables: Record<string, any> = {
|
||||
darwin: {
|
||||
migrationEngine: 'node_modules/@prisma/engines/migration-engine-darwin',
|
||||
queryEngine: 'node_modules/@prisma/engines/libquery_engine-darwin.dylib.node',
|
||||
},
|
||||
darwinArm64: {
|
||||
migrationEngine: 'node_modules/@prisma/engines/migration-engine-darwin-arm64',
|
||||
queryEngine: 'node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node',
|
||||
},
|
||||
linux: {
|
||||
migrationEngine: 'node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x',
|
||||
queryEngine: 'node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node',
|
||||
},
|
||||
win32: {
|
||||
migrationEngine: 'node_modules/@prisma/engines/migration-engine-windows.exe',
|
||||
queryEngine: 'node_modules/@prisma/engines/query_engine-windows.dll.node',
|
||||
},
|
||||
};
|
||||
|
||||
const extraResourcesPath = app.getAppPath().replace('app.asar', ''); // impacted by extraResources setting in electron-builder.yml
|
||||
const platformName = getPlatformName();
|
||||
|
||||
const mePath = path.join(extraResourcesPath, platformToExecutables[platformName].migrationEngine);
|
||||
const qePath = path.join(extraResourcesPath, platformToExecutables[platformName].queryEngine);
|
||||
|
||||
ipcMain.on('config:get-app-path', (event) => {
|
||||
event.returnValue = app.getAppPath();
|
||||
});
|
||||
|
||||
ipcMain.on('config:get-platform-name', (event) => {
|
||||
const isDarwin = process.platform === 'darwin';
|
||||
event.returnValue =
|
||||
isDarwin && process.arch === 'arm64' ? `${process.platform}Arm64` : (event.returnValue = process.platform);
|
||||
});
|
||||
|
||||
ipcMain.on('config:get-prisma-db-path', (event) => {
|
||||
event.returnValue = dbPath;
|
||||
});
|
||||
|
||||
ipcMain.on('config:get-prisma-me-path', (event) => {
|
||||
event.returnValue = mePath;
|
||||
});
|
||||
|
||||
ipcMain.on('config:get-prisma-qe-path', (event) => {
|
||||
event.returnValue = qePath;
|
||||
});
|
||||
|
||||
export const prisma = new PrismaClient({
|
||||
datasources: {
|
||||
db: {
|
||||
url: `file:${dbPath}`,
|
||||
},
|
||||
},
|
||||
errorFormat: 'minimal',
|
||||
// see https://github.com/prisma/prisma/discussions/5200
|
||||
// __internal: {
|
||||
// engine: {
|
||||
// binaryPath: qePath,
|
||||
// },
|
||||
// },
|
||||
});
|
||||
|
||||
prisma.server.findMany({
|
||||
where: {},
|
||||
});
|
||||
|
||||
export const exclude = <T, Key extends keyof T>(resultSet: T, ...keys: Key[]): Omit<T, Key> => {
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const key of keys) {
|
||||
delete resultSet[key];
|
||||
}
|
||||
return resultSet;
|
||||
};
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
prisma.$use(async (params, next) => {
|
||||
const maxRetries = 5;
|
||||
let retries = 0;
|
||||
|
||||
do {
|
||||
try {
|
||||
const result = await next(params);
|
||||
return result;
|
||||
} catch (err) {
|
||||
retries += 1;
|
||||
return sleep(500);
|
||||
}
|
||||
} while (retries < maxRetries);
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { prisma } from '..';
|
||||
|
||||
export enum ServerApi {
|
||||
GET_SERVER = 'api:server:get-server',
|
||||
GET_SERVERS = 'api:server:get-servers',
|
||||
}
|
||||
|
||||
ipcMain.handle(ServerApi.GET_SERVERS, async () => {
|
||||
const result = await prisma.server.findMany();
|
||||
return result;
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
import './mpv-player';
|
||||
import './api';
|
||||
@@ -0,0 +1,134 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import MpvAPI from 'node-mpv';
|
||||
import { getWindow } from '../../..';
|
||||
|
||||
const mpv = new MpvAPI(
|
||||
{
|
||||
audio_only: true,
|
||||
auto_restart: true,
|
||||
binary: 'C:/ProgramData/chocolatey/lib/mpv.install/tools/mpv.exe',
|
||||
time_update: 1,
|
||||
},
|
||||
['--gapless-audio=yes', '--prefetch-playlist']
|
||||
);
|
||||
|
||||
mpv.start().catch((error: any) => {
|
||||
console.log('error', error);
|
||||
});
|
||||
|
||||
mpv.on('status', (status: any) => {
|
||||
if (status.property === 'playlist-pos') {
|
||||
if (status.value !== 0) {
|
||||
getWindow()?.webContents.send('renderer-player-auto-next');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is playing
|
||||
mpv.on('started', () => {
|
||||
getWindow()?.webContents.send('renderer-player-play');
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is stopped
|
||||
mpv.on('stopped', () => {
|
||||
getWindow()?.webContents.send('renderer-player-stop');
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is paused
|
||||
mpv.on('paused', () => {
|
||||
getWindow()?.webContents.send('renderer-player-pause');
|
||||
});
|
||||
|
||||
mpv.on('quit', () => {
|
||||
console.log('mpv quit');
|
||||
});
|
||||
|
||||
// Event output every interval set by time_update, used to update the current time
|
||||
mpv.on('timeposition', (time: number) => {
|
||||
getWindow()?.webContents.send('renderer-player-current-time', time);
|
||||
});
|
||||
|
||||
mpv.on('seek', () => {
|
||||
console.log('mpv seek');
|
||||
});
|
||||
|
||||
// Starts the player
|
||||
ipcMain.on('player-play', async () => {
|
||||
await mpv.play();
|
||||
});
|
||||
|
||||
// Pauses the player
|
||||
ipcMain.on('player-pause', async () => {
|
||||
await mpv.pause();
|
||||
});
|
||||
|
||||
// Stops the player
|
||||
ipcMain.on('player-stop', async () => {
|
||||
await mpv.stop();
|
||||
});
|
||||
|
||||
// Stops the player
|
||||
ipcMain.on('player-next', async () => {
|
||||
await mpv.next();
|
||||
});
|
||||
|
||||
// Stops the player
|
||||
ipcMain.on('player-previous', async () => {
|
||||
await mpv.prev();
|
||||
});
|
||||
|
||||
// Seeks forward or backward by the given amount of seconds
|
||||
ipcMain.on('player-seek', async (_event, time: number) => {
|
||||
await mpv.seek(time);
|
||||
});
|
||||
|
||||
// Seeks to the given time in seconds
|
||||
ipcMain.on('player-seek-to', async (_event, time: number) => {
|
||||
await mpv.goToPosition(time);
|
||||
});
|
||||
|
||||
// Sets the queue in position 0 and 1 to the given data. Used when manually starting a song or using the next/prev buttons
|
||||
ipcMain.on('player-set-queue', async (_event, data: any) => {
|
||||
if (data.queue.current) {
|
||||
await mpv.load(data.queue.current.streamUrl, 'replace');
|
||||
}
|
||||
|
||||
if (data.queue.next) {
|
||||
await mpv.load(data.queue.next.streamUrl, 'append');
|
||||
}
|
||||
});
|
||||
|
||||
// Replaces the queue in position 1 to the given data
|
||||
ipcMain.on('player-set-queue-next', async (_event, data: any) => {
|
||||
const size = await mpv.getPlaylistSize();
|
||||
|
||||
if (size > 1) {
|
||||
await mpv.playlistRemove(1);
|
||||
}
|
||||
|
||||
if (data.queue.next) {
|
||||
await mpv.load(data.queue.next.streamUrl, 'append');
|
||||
}
|
||||
});
|
||||
|
||||
// Sets the next song in the queue when reaching the end of the queue
|
||||
ipcMain.on('player-auto-next', async (_event, data: any) => {
|
||||
// Always keep the current song as position 0 in the mpv queue
|
||||
// This allows us to easily set update the next song in the queue without
|
||||
// disturbing the currently playing song
|
||||
await mpv.playlistRemove(0);
|
||||
|
||||
if (data.queue.next) {
|
||||
await mpv.load(data.queue.next.streamUrl, 'append');
|
||||
}
|
||||
});
|
||||
|
||||
// Sets the volume to the given value (0-100)
|
||||
ipcMain.on('player-volume', async (_event, value: number) => {
|
||||
mpv.volume(value);
|
||||
});
|
||||
|
||||
// Toggles the mute status
|
||||
ipcMain.on('player-mute', async () => {
|
||||
mpv.mute();
|
||||
});
|
||||
Reference in New Issue
Block a user