From fc1c25f404cbee062236986f0dbf31d92a1204d7 Mon Sep 17 00:00:00 2001 From: qier222 Date: Sat, 16 Apr 2022 16:50:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=20db.ts=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/cache.ts | 2 +- src/main/db.ts | 93 +++++++++++++++++++------ src/main/migrations/init.sql | 18 ++--- tsconfig.json => src/main/tsconfig.json | 4 +- 4 files changed, 83 insertions(+), 34 deletions(-) rename tsconfig.json => src/main/tsconfig.json (84%) diff --git a/src/main/cache.ts b/src/main/cache.ts index d787d58..75d11cc 100644 --- a/src/main/cache.ts +++ b/src/main/cache.ts @@ -23,7 +23,7 @@ class Cache { db.upsert(Tables.AccountData, { id: api, json: JSON.stringify(data), - updateAt: Date.now(), + updatedAt: Date.now(), }) break } diff --git a/src/main/db.ts b/src/main/db.ts index 30ebf0d..49afd80 100644 --- a/src/main/db.ts +++ b/src/main/db.ts @@ -7,19 +7,48 @@ import { createFileIfNotExist } from './utils' const isDev = process.env.NODE_ENV === 'development' -export enum Tables { - Track = 'track', - Album = 'album', - Artist = 'artist', - Playlist = 'playlist', - ArtistAlbum = 'artist_album', - Lyric = 'lyric', - - // Special tables - AccountData = 'account_data', - Audio = 'audio', - CoverColor = 'cover_color', +export const enum Tables { + Track = 'Track', + Album = 'Album', + Artist = 'Artist', + Playlist = 'Playlist', + ArtistAlbum = 'ArtistAlbum', + Lyric = 'Lyric', + Audio = 'Audio', + AccountData = 'AccountData', + CoverColor = 'CoverColor', } +interface CommonTableStructure { + id: number + json: string + updatedAt: number +} +export interface TablesStructures { + [Tables.Track]: CommonTableStructure + [Tables.Album]: CommonTableStructure + [Tables.Artist]: CommonTableStructure + [Tables.Playlist]: CommonTableStructure + [Tables.ArtistAlbum]: CommonTableStructure + [Tables.Lyric]: CommonTableStructure + [Tables.AccountData]: { + id: string + json: string + updatedAt: number + } + [Tables.Audio]: { + id: number + br: number + source: 'netease' | 'migu' | 'kuwo' | 'kugou' | 'youtube' + url: string + updatedAt: number + } + [Tables.CoverColor]: { + id: number + color: string + } +} + +type TableNames = keyof TablesStructures class DB { sqlite: SQLite3.Database @@ -55,27 +84,41 @@ class DB { this.sqlite.exec(migration) } - find(table: Tables, key: number | string) { + find( + table: T, + key: TablesStructures[T]['id'] + ): TablesStructures[T] { return this.sqlite .prepare(`SELECT * FROM ${table} WHERE id = ? LIMIT 1`) .get(key) } - findMany(table: Tables, keys: number[] | string[]) { + findMany( + table: T, + keys: TablesStructures[T]['id'][] + ): TablesStructures[T][] { const idsQuery = keys.map(key => `id = ${key}`).join(' OR ') return this.sqlite.prepare(`SELECT * FROM ${table} WHERE ${idsQuery}`).all() } - findAll(table: Tables) { + findAll(table: T): TablesStructures[T][] { return this.sqlite.prepare(`SELECT * FROM ${table}`).all() } - create(table: Tables, data: any, skipWhenExist: boolean = true) { + create( + table: T, + data: TablesStructures[T], + skipWhenExist: boolean = true + ) { if (skipWhenExist && db.find(table, data.id)) return return this.sqlite.prepare(`INSERT INTO ${table} VALUES (?)`).run(data) } - createMany(table: Tables, data: any[], skipWhenExist: boolean = true) { + createMany( + table: T, + data: TablesStructures[T][], + skipWhenExist: boolean = true + ) { const valuesQuery = Object.keys(data[0]) .map(key => `:${key}`) .join(', ') @@ -90,7 +133,7 @@ class DB { insertMany(data) } - upsert(table: Tables, data: any) { + upsert(table: T, data: TablesStructures[T]) { const valuesQuery = Object.keys(data) .map(key => `:${key}`) .join(', ') @@ -99,7 +142,7 @@ class DB { .run(data) } - upsertMany(table: Tables, data: any[]) { + upsertMany(table: T, data: TablesStructures[T][]) { const valuesQuery = Object.keys(data[0]) .map(key => `:${key}`) .join(', ') @@ -112,16 +155,22 @@ class DB { upsertMany(data) } - delete(table: Tables, key: number | string) { + delete( + table: T, + key: TablesStructures[T]['id'] + ) { return this.sqlite.prepare(`DELETE FROM ${table} WHERE id = ?`).run(key) } - deleteMany(table: Tables, keys: number[] | string[]) { + deleteMany( + table: T, + keys: TablesStructures[T]['id'][] + ) { const idsQuery = keys.map(key => `id = ${key}`).join(' OR ') return this.sqlite.prepare(`DELETE FROM ${table} WHERE ${idsQuery}`).run() } - truncate(table: Tables) { + truncate(table: T) { return this.sqlite.prepare(`DELETE FROM ${table}`).run() } diff --git a/src/main/migrations/init.sql b/src/main/migrations/init.sql index 516fea9..c9905f0 100644 --- a/src/main/migrations/init.sql +++ b/src/main/migrations/init.sql @@ -1,9 +1,9 @@ -CREATE TABLE IF NOT EXISTS "account_data" ("id" text NOT NULL,"json" text NOT NULL,"updateAt" int NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "album" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "artist_album" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "artist" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "audio" ("id" integer NOT NULL,"br" int NOT NULL,"type" text NOT NULL,"srouce" text NOT NULL,"updateAt" int NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "lyric" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" integer NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "playlist" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "track" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); -CREATE TABLE IF NOT EXISTS "cover_color" ("id" integer NOT NULL,"color" text NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "AccountData" ("id" text NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "Album" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "ArtistAlbum" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "Artist" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "Audio" ("id" integer NOT NULL,"br" int NOT NULL,"type" text NOT NULL,"srouce" text NOT NULL,"updateAt" int NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "Lyric" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" integer NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "Playlist" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "Track" ("id" integer NOT NULL,"json" text NOT NULL,"updatedAt" int NOT NULL, PRIMARY KEY (id)); +CREATE TABLE IF NOT EXISTS "CoverColor" ("id" integer NOT NULL,"color" text NOT NULL, PRIMARY KEY (id)); diff --git a/tsconfig.json b/src/main/tsconfig.json similarity index 84% rename from tsconfig.json rename to src/main/tsconfig.json index 1704250..40c14c6 100644 --- a/tsconfig.json +++ b/src/main/tsconfig.json @@ -12,8 +12,8 @@ "jsx": "react-jsx", "baseUrl": "./", "paths": { - "@/*": ["src/*"] + "@/*": ["../*"] } }, - "exclude": ["node_modules", "./dist"] + "include": ["./**/*.ts"] }