db_music.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import Logger from "@/util/logger.ts";
  2. import { loadDb} from "@/common/db/db.ts";
  3. import {handle, PromiseResult} from "@/util/promiseHandle.ts";
  4. import {AppDbName} from "@/types/appConfig.ts";
  5. import {Knex} from "knex";
  6. import {MusicScanSetting, MusicTableName} from "@/types/musicType.ts";
  7. let logger = Logger.logger('music_db', 'info');
  8. async function _initScanConfigTable(db : Knex): PromiseResult<boolean> {
  9. let [err, hasTable] = await handle(
  10. db.schema.hasTable(MusicTableName.music_scan_setting)
  11. )
  12. if (err) {
  13. err = err as Error;
  14. logger.error(`[音频扫描库] ${err.message}`)
  15. return [new Error('音频扫描库初始化失败'), false]
  16. }
  17. if (hasTable) {
  18. return [null, true];
  19. }
  20. let [createErr, _res] = await handle(db?.schema.createTable('scanConfig', (table) => {
  21. // 初始化扫描配置
  22. logger.error(`[初始化音频扫描库]`)
  23. table.string('name')
  24. table.string('path')
  25. table.boolean('scanSubDir')
  26. table.boolean('isFileRepeat')
  27. }))
  28. if (createErr) {
  29. createErr = createErr as Error;
  30. logger.error(`[初始化磁贴表失败] ${createErr.message}`)
  31. return [createErr, false];
  32. }
  33. logger.info('[初始化音频扫描配置成功]')
  34. return [null, true];
  35. }
  36. async function _initPlayListTable(db : Knex): PromiseResult<boolean> {
  37. let [err, hasTable] = await handle(
  38. db.schema.hasTable(MusicTableName.music_scan_setting)
  39. )
  40. if (err) {
  41. err = err as Error;
  42. logger.error(`[歌单初始化] ${err.message}`)
  43. return [new Error('歌单初始化'), false]
  44. }
  45. if (hasTable) {
  46. return [null, true];
  47. }
  48. let [createErr, _res] = await handle(
  49. db.schema.createTable(MusicTableName.music_play_list, (table) => {
  50. logger.error(`[初始化音频播放列表库]`)
  51. table.increments('id').primary()
  52. table.string('name')
  53. table.string('icon')
  54. table.string('cover')
  55. table.string('description')
  56. table.integer('playCount')
  57. table.integer('trackCount')
  58. table.integer('createTime')
  59. table.boolean('isTagSearch')
  60. table.integer('lastPlayTime')
  61. table.boolean('isSync')
  62. table.boolean('isPublic')
  63. table.boolean('isLike')
  64. }))
  65. if (createErr) {
  66. createErr = createErr as Error;
  67. logger.error(`[初始化歌单失败] ${createErr.message}`)
  68. return [createErr, false];
  69. }
  70. // 添加默认歌单
  71. await db.insert({
  72. name: '我的喜爱',
  73. icon: 'favorite',
  74. cover: 'favorite',
  75. description: '我的喜爱',
  76. playCount: 0,
  77. trackCount: 0,
  78. createTime: Date.now(),
  79. isTagSearch: false,
  80. lastPlayTime: Date.now(),
  81. isSync: false,
  82. isPublic: false,
  83. isLike: true,
  84. }).into(MusicTableName.music_play_list)
  85. logger.info('[初始化我的喜爱歌单成功]')
  86. return [null, true];
  87. }
  88. async function _initSongsTable(db : Knex): PromiseResult<boolean>
  89. {
  90. let [err, hasTable] = await handle(
  91. db.schema.hasTable(MusicTableName.music_songs)
  92. )
  93. if (err) {
  94. err = err as Error;
  95. logger.error(`[音频表初始化] ${err.message}`)
  96. return [new Error('音频表初始化'), false]
  97. }
  98. if (hasTable) {
  99. return [null, true];
  100. }
  101. let [createErr, _res] = await handle(
  102. db.schema.createTable(MusicTableName.music_songs, (table) => {
  103. logger.error(`[初始化音频库]`)
  104. table.increments('id').primary()
  105. table.string('name')
  106. table.string('artists')
  107. table.string('album')
  108. table.string('cover')
  109. table.integer('duration')
  110. table.boolean('isLike')
  111. table.string('origin')
  112. table.integer('type')
  113. table.boolean('isLocal')
  114. table.string('filePath')
  115. table.string('lyricPath')
  116. table.string('tags')
  117. table.integer('playCount')
  118. }))
  119. if (createErr) {
  120. createErr = createErr as Error;
  121. logger.error(`[初始化音频库失败] ${createErr.message}`)
  122. return [createErr, false];
  123. }
  124. logger.info('[初始化音频库成功]')
  125. return [null, true];
  126. }
  127. async function _initPlaylistSongs(db : Knex): PromiseResult<boolean>
  128. {
  129. let [err, hasTable] = await handle(
  130. db.schema.hasTable(MusicTableName.music_play_list_songs)
  131. )
  132. if (err) {
  133. err = err as Error;
  134. logger.error(`[歌单歌曲表初始化] ${err.message}`)
  135. return [new Error('歌单歌曲表初始化'), false]
  136. }
  137. if (hasTable) {
  138. return [null, true];
  139. }
  140. let [createErr, _res] = await handle(
  141. db.schema.createTable(MusicTableName.music_play_list_songs, (table) => {
  142. logger.error(`[初始化歌单歌曲库]`)
  143. table.increments('id').primary()
  144. table.integer('musicId')
  145. table.integer('playListId')
  146. table.integer('order')
  147. }))
  148. if (createErr) {
  149. createErr = createErr as Error;
  150. logger.error(`[初始化歌单歌曲库失败] ${createErr.message}`)
  151. return [createErr, false];
  152. }
  153. logger.info('[初始化歌单歌曲库成功]')
  154. return [null, true];
  155. }
  156. export async function initMusicData() : PromiseResult<boolean>
  157. {
  158. let db = loadDb(AppDbName.music_db)
  159. if(!db){
  160. logger.error('数据库初始化失败')
  161. return [new Error('音乐数据库初始化失败'), false]
  162. }
  163. let [err, res] = await _initScanConfigTable(db);
  164. if (err) {
  165. err = err as Error;
  166. logger.error(`[初始化扫描库失败] ${err.message}`)
  167. return [err, false];
  168. }
  169. [err, res] = await _initPlayListTable(db);
  170. if (err) {
  171. err = err as Error;
  172. logger.error(`[初始化播放列表库失败] ${err.message}`)
  173. return [err, false];
  174. }
  175. [err, res] = await _initSongsTable(db);
  176. if (err) {
  177. err = err as Error;
  178. logger.error(`[初始化音频库失败] ${err.message}`)
  179. return [err, false];
  180. }
  181. [err, res] = await _initPlaylistSongs(db);
  182. if (err) {
  183. err = err as Error;
  184. logger.error(`[初始化歌单歌曲库失败] ${err.message}`)
  185. return [err, false];
  186. }
  187. logger.info('[初始化音频库成功]')
  188. return [null, res]
  189. }
  190. // 根据扫描地址获取扫描配置
  191. export async function getScanConfigByPath(path: string) : PromiseResult<MusicScanSetting[]>
  192. {
  193. let db = loadDb(AppDbName.music_db)
  194. if(!db){
  195. logger.error('数据库初始化失败')
  196. return [new Error('音乐数据库初始化失败'), null]
  197. }
  198. let [err, res] = await handle(
  199. db.select('name', 'path', 'scanSubDir', 'isFileRepeat')
  200. .from(MusicTableName.music_scan_setting)
  201. .where('path', path)
  202. )
  203. if (err) {
  204. err = err as Error;
  205. logger.error(`[获取扫描配置失败] ${err.message}`)
  206. return [err, null];
  207. }
  208. return [null, res as MusicScanSetting[]];
  209. }
  210. export async function getScanConfig() : PromiseResult<MusicScanSetting[]>
  211. {
  212. let db = loadDb(AppDbName.music_db)
  213. if(!db){
  214. logger.error('数据库初始化失败')
  215. return [new Error('音乐数据库初始化失败'), null]
  216. }
  217. let [err, res] = await handle(
  218. db.select('name', 'path', 'scanSubDir', 'isFileRepeat')
  219. .from(MusicTableName.music_scan_setting)
  220. )
  221. if (err) {
  222. err = err as Error;
  223. logger.error(`[获取扫描配置失败] ${err.message}`)
  224. return [err, null];
  225. }
  226. return [null, res as MusicScanSetting[]];
  227. }
  228. export async function addScanConfig(scanConfig: MusicScanSetting) : PromiseResult<boolean>
  229. {
  230. let db = loadDb(AppDbName.music_db)
  231. if(!db){
  232. logger.error('数据库初始化失败')
  233. return [new Error('音乐数据库初始化失败'), false]
  234. }
  235. let [err, _res] = await handle(
  236. db.insert(scanConfig).into(MusicTableName.music_scan_setting)
  237. )
  238. if (err) {
  239. err = err as Error;
  240. logger.error(`[添加扫描配置失败] ${err.message}`)
  241. return [err, false];
  242. }
  243. return [null, true];
  244. }
  245. export async function updateScanConfig(scanConfig: MusicScanSetting) : PromiseResult<boolean>
  246. {
  247. let db = loadDb(AppDbName.music_db)
  248. if(!db){
  249. logger.error('数据库初始化失败')
  250. return [new Error('音乐数据库初始化失败'), false]
  251. }
  252. let [err, _res] = await handle(
  253. db.update(scanConfig).into(MusicTableName.music_scan_setting)
  254. )
  255. if (err) {
  256. err = err as Error;
  257. }
  258. return [err, true];
  259. }
  260. export async function deleteScanConfig(path: string) : PromiseResult<boolean>
  261. {
  262. let db = loadDb(AppDbName.music_db)
  263. if(!db){
  264. logger.error('数据库初始化失败')
  265. return [new Error('音乐数据库初始化失败'), false]
  266. }
  267. let [err, _res] = await handle(
  268. db.delete().from(MusicTableName.music_scan_setting).where('path', path)
  269. )
  270. if (err) {
  271. err= err as Error;
  272. }
  273. return [err, true];
  274. }