feat: 完善 db.ts 类型

This commit is contained in:
qier222 2022-04-16 16:50:34 +08:00
parent 97233bc750
commit fc1c25f404
No known key found for this signature in database
GPG Key ID: 9C85007ED905F14D
4 changed files with 83 additions and 34 deletions

View File

@ -23,7 +23,7 @@ class Cache {
db.upsert(Tables.AccountData, { db.upsert(Tables.AccountData, {
id: api, id: api,
json: JSON.stringify(data), json: JSON.stringify(data),
updateAt: Date.now(), updatedAt: Date.now(),
}) })
break break
} }

View File

@ -7,19 +7,48 @@ import { createFileIfNotExist } from './utils'
const isDev = process.env.NODE_ENV === 'development' const isDev = process.env.NODE_ENV === 'development'
export enum Tables { export const enum Tables {
Track = 'track', Track = 'Track',
Album = 'album', Album = 'Album',
Artist = 'artist', Artist = 'Artist',
Playlist = 'playlist', Playlist = 'Playlist',
ArtistAlbum = 'artist_album', ArtistAlbum = 'ArtistAlbum',
Lyric = 'lyric', Lyric = 'Lyric',
Audio = 'Audio',
// Special tables AccountData = 'AccountData',
AccountData = 'account_data', CoverColor = 'CoverColor',
Audio = 'audio',
CoverColor = 'cover_color',
} }
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 { class DB {
sqlite: SQLite3.Database sqlite: SQLite3.Database
@ -55,27 +84,41 @@ class DB {
this.sqlite.exec(migration) this.sqlite.exec(migration)
} }
find(table: Tables, key: number | string) { find<T extends TableNames>(
table: T,
key: TablesStructures[T]['id']
): TablesStructures[T] {
return this.sqlite return this.sqlite
.prepare(`SELECT * FROM ${table} WHERE id = ? LIMIT 1`) .prepare(`SELECT * FROM ${table} WHERE id = ? LIMIT 1`)
.get(key) .get(key)
} }
findMany(table: Tables, keys: number[] | string[]) { findMany<T extends TableNames>(
table: T,
keys: TablesStructures[T]['id'][]
): TablesStructures[T][] {
const idsQuery = keys.map(key => `id = ${key}`).join(' OR ') const idsQuery = keys.map(key => `id = ${key}`).join(' OR ')
return this.sqlite.prepare(`SELECT * FROM ${table} WHERE ${idsQuery}`).all() return this.sqlite.prepare(`SELECT * FROM ${table} WHERE ${idsQuery}`).all()
} }
findAll(table: Tables) { findAll<T extends TableNames>(table: T): TablesStructures[T][] {
return this.sqlite.prepare(`SELECT * FROM ${table}`).all() return this.sqlite.prepare(`SELECT * FROM ${table}`).all()
} }
create(table: Tables, data: any, skipWhenExist: boolean = true) { create<T extends TableNames>(
table: T,
data: TablesStructures[T],
skipWhenExist: boolean = true
) {
if (skipWhenExist && db.find(table, data.id)) return if (skipWhenExist && db.find(table, data.id)) return
return this.sqlite.prepare(`INSERT INTO ${table} VALUES (?)`).run(data) return this.sqlite.prepare(`INSERT INTO ${table} VALUES (?)`).run(data)
} }
createMany(table: Tables, data: any[], skipWhenExist: boolean = true) { createMany<T extends TableNames>(
table: T,
data: TablesStructures[T][],
skipWhenExist: boolean = true
) {
const valuesQuery = Object.keys(data[0]) const valuesQuery = Object.keys(data[0])
.map(key => `:${key}`) .map(key => `:${key}`)
.join(', ') .join(', ')
@ -90,7 +133,7 @@ class DB {
insertMany(data) insertMany(data)
} }
upsert(table: Tables, data: any) { upsert<T extends TableNames>(table: T, data: TablesStructures[T]) {
const valuesQuery = Object.keys(data) const valuesQuery = Object.keys(data)
.map(key => `:${key}`) .map(key => `:${key}`)
.join(', ') .join(', ')
@ -99,7 +142,7 @@ class DB {
.run(data) .run(data)
} }
upsertMany(table: Tables, data: any[]) { upsertMany<T extends TableNames>(table: T, data: TablesStructures[T][]) {
const valuesQuery = Object.keys(data[0]) const valuesQuery = Object.keys(data[0])
.map(key => `:${key}`) .map(key => `:${key}`)
.join(', ') .join(', ')
@ -112,16 +155,22 @@ class DB {
upsertMany(data) upsertMany(data)
} }
delete(table: Tables, key: number | string) { delete<T extends TableNames>(
table: T,
key: TablesStructures[T]['id']
) {
return this.sqlite.prepare(`DELETE FROM ${table} WHERE id = ?`).run(key) return this.sqlite.prepare(`DELETE FROM ${table} WHERE id = ?`).run(key)
} }
deleteMany(table: Tables, keys: number[] | string[]) { deleteMany<T extends TableNames>(
table: T,
keys: TablesStructures[T]['id'][]
) {
const idsQuery = keys.map(key => `id = ${key}`).join(' OR ') const idsQuery = keys.map(key => `id = ${key}`).join(' OR ')
return this.sqlite.prepare(`DELETE FROM ${table} WHERE ${idsQuery}`).run() return this.sqlite.prepare(`DELETE FROM ${table} WHERE ${idsQuery}`).run()
} }
truncate(table: Tables) { truncate<T extends TableNames>(table: T) {
return this.sqlite.prepare(`DELETE FROM ${table}`).run() return this.sqlite.prepare(`DELETE FROM ${table}`).run()
} }

View File

@ -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 "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 "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 "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 "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 "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 "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 "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 "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 "CoverColor" ("id" integer NOT NULL,"color" text NOT NULL, PRIMARY KEY (id));

View File

@ -12,8 +12,8 @@
"jsx": "react-jsx", "jsx": "react-jsx",
"baseUrl": "./", "baseUrl": "./",
"paths": { "paths": {
"@/*": ["src/*"] "@/*": ["../*"]
} }
}, },
"exclude": ["node_modules", "./dist"] "include": ["./**/*.ts"]
} }