mirror of
https://github.com/Myxelium/Bridge-Multi.git
synced 2026-04-11 22:29:38 +00:00
Add database for library
This commit is contained in:
18
src-electron/database/dataSource.ts
Normal file
18
src-electron/database/dataSource.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { DataSource } from 'typeorm'
|
||||
import { Chart } from './entities/Chart.js'
|
||||
import { Init1743124434920 } from './migrations/1743124434920-init.js'
|
||||
|
||||
const migrations = [Init1743124434920]
|
||||
const entities = [Chart]
|
||||
|
||||
export const dataSource = new DataSource({
|
||||
type: "sqlite",
|
||||
database: "library.sqlite",
|
||||
entities: entities,
|
||||
// Configure migrations to use a folder that contains your migration files:
|
||||
migrations: migrations,
|
||||
// Keep synchronize off when using migrations in production
|
||||
synchronize: false,
|
||||
logging: true,
|
||||
migrationsRun: true,
|
||||
})
|
||||
125
src-electron/database/databaseService.ts
Normal file
125
src-electron/database/databaseService.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import { ChartData } from 'src-shared/interfaces/search.interface.js'
|
||||
import { dataSource } from './dataSource.js'
|
||||
import { Chart } from './entities/Chart.js'
|
||||
import { Like } from 'typeorm'
|
||||
|
||||
export class DatabaseService {
|
||||
async insertChart(chartData: ChartData): Promise<ChartData> {
|
||||
try {
|
||||
if (!dataSource.isInitialized) {
|
||||
await dataSource.initialize()
|
||||
}
|
||||
|
||||
const chartRepository = dataSource.getRepository(Chart)
|
||||
|
||||
// if one already exist dont create
|
||||
const existingChart = await chartRepository.findOneBy({ md5: chartData.md5 })
|
||||
|
||||
if (existingChart) {
|
||||
return existingChart as unknown as ChartData
|
||||
}
|
||||
|
||||
const newChart = chartRepository.create({
|
||||
name: chartData.name!,
|
||||
album: chartData.album!,
|
||||
artist: chartData.artist!,
|
||||
genre: chartData.genre!,
|
||||
year: chartData.year!,
|
||||
charter: chartData.charter!,
|
||||
md5: chartData.md5,
|
||||
hasVideoBackground: chartData.hasVideoBackground,
|
||||
})
|
||||
|
||||
return await chartRepository.save(newChart) as unknown as ChartData
|
||||
} catch (error) {
|
||||
console.error('Error inserting chart:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async removeChart(md5: string): Promise<void> {
|
||||
try {
|
||||
if (!dataSource.isInitialized) {
|
||||
await dataSource.initialize()
|
||||
}
|
||||
|
||||
const chartRepository = dataSource.getRepository(Chart)
|
||||
|
||||
await chartRepository.delete({ md5 })
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error removing chart:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async removeCharts(charts: ChartData[]): Promise<void> {
|
||||
try {
|
||||
if (!dataSource.isInitialized) {
|
||||
await dataSource.initialize()
|
||||
}
|
||||
|
||||
const chartRepository = dataSource.getRepository(Chart)
|
||||
|
||||
// delete the array of charts provided using querybulilder
|
||||
charts.forEach(async chart => {
|
||||
console.log('removing chart:', chart.name)
|
||||
await chartRepository.delete({ md5: chart.md5 })
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error removing charts:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async getChartsBySearchTerm(searchTerm?: string): Promise<ChartData[]> {
|
||||
try {
|
||||
if (!dataSource.isInitialized) {
|
||||
await dataSource.initialize()
|
||||
}
|
||||
|
||||
const chartRepository = dataSource.getRepository(Chart)
|
||||
|
||||
let charts: Chart[]
|
||||
|
||||
if (searchTerm) {
|
||||
const likeSearchTerm = `%${searchTerm}%`
|
||||
charts = await chartRepository.find({
|
||||
where: [
|
||||
{ name: Like(likeSearchTerm) },
|
||||
{ album: Like(likeSearchTerm) },
|
||||
{ artist: Like(likeSearchTerm) },
|
||||
{ genre: Like(likeSearchTerm) },
|
||||
{ year: Like(likeSearchTerm) },
|
||||
{ charter: Like(likeSearchTerm) },
|
||||
],
|
||||
})
|
||||
} else {
|
||||
charts = await chartRepository.find()
|
||||
}
|
||||
|
||||
return charts as unknown as ChartData[]
|
||||
} catch (error) {
|
||||
console.error('Error fetching charts by search term:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async removeAllCharts(): Promise<void> {
|
||||
try {
|
||||
if (!dataSource.isInitialized) {
|
||||
await dataSource.initialize()
|
||||
}
|
||||
|
||||
const chartRepository = dataSource.getRepository(Chart)
|
||||
|
||||
await chartRepository.clear()
|
||||
} catch (error) {
|
||||
console.error('Error removing all charts:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const databaseService = new DatabaseService()
|
||||
31
src-electron/database/entities/Chart.ts
Normal file
31
src-electron/database/entities/Chart.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
|
||||
|
||||
@Entity()
|
||||
export class Chart {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string
|
||||
|
||||
@Column()
|
||||
md5: string
|
||||
|
||||
@Column()
|
||||
hasVideoBackground: boolean
|
||||
|
||||
@Column()
|
||||
charter: string
|
||||
|
||||
@Column()
|
||||
name: string
|
||||
|
||||
@Column()
|
||||
artist: string
|
||||
|
||||
@Column()
|
||||
album: string
|
||||
|
||||
@Column()
|
||||
genre: string
|
||||
|
||||
@Column()
|
||||
year: string
|
||||
}
|
||||
14
src-electron/database/migrations/1743124434920-init.ts
Normal file
14
src-electron/database/migrations/1743124434920-init.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class Init1743124434920 implements MigrationInterface {
|
||||
name = 'Init1743124434920'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`CREATE TABLE "chart" ("id" varchar PRIMARY KEY NOT NULL, "md5" varchar NOT NULL, "hasVideoBackground" boolean NOT NULL, "charter" varchar NOT NULL, "name" varchar NOT NULL, "artist" varchar NOT NULL, "album" varchar NOT NULL, "genre" varchar NOT NULL, "year" varchar NOT NULL)`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE "chart"`);
|
||||
}
|
||||
|
||||
}
|
||||
10
src-electron/database/readme.md
Normal file
10
src-electron/database/readme.md
Normal file
@@ -0,0 +1,10 @@
|
||||
## Migrations
|
||||
In order to create a new migration, there is a some steps to go through.
|
||||
|
||||
1. Run ``npm run migration:add --name <migration name>`` This currently work for Windows machines. If using Linux or Mac run this instead ``npx typeorm-ts-node-esm migration:generate ./src-electron/database/migrations/<migration name> -d ./src-electron/database/dataSource.ts``
|
||||
|
||||
2. Go to ``./src-electron/database/dataSource.ts`` and add the newly
|
||||
generated migration to the migrations array and entity variables. In that way it will automatically apply the latest changes to the database on startup.
|
||||
|
||||
## The database
|
||||
A Sqlite database file is automatically created on startup named Library, it will be placed in the same directory as the executable.
|
||||
Reference in New Issue
Block a user