-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch tracks, and show results with some queries
- Loading branch information
Showing
6 changed files
with
147 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { computed, toRef, type UnwrapNestedRefs, type Ref } from 'vue' | ||
import type SqliteClient from '@/db-client' | ||
import api from '@/api' | ||
import { useAuthStore } from '@/stores/auth' | ||
|
||
export default class TrackDao { | ||
client: SqliteClient | ||
authStore: UnwrapNestedRefs<{ secret: string | null; deviceId: string | null }> | ||
|
||
constructor(client: SqliteClient) { | ||
this.client = client | ||
this.authStore = useAuthStore() | ||
} | ||
|
||
async refresh() { | ||
let done = false | ||
const generator = api.tracks.index({ | ||
secret: this.authStore.secret!, | ||
device_id: this.authStore.deviceId! | ||
}) | ||
while (!done) { | ||
const obj = await generator.next() | ||
// TODO(chvp): Delete old obsolete values | ||
// TODO(chvp): Also insert track_genres and track_artists (in the same transaction, if possible) | ||
await this.client.insertAll( | ||
obj.value, | ||
'tracks', | ||
'INSERT INTO tracks VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16) ON CONFLICT(id) DO UPDATE SET title=?2, normalized_title=?3, number=?4, album_id=?5, review_comment=?6, created_at=?7, updated_at=?8, codec_id=?9, length=?10, bitrate=?11, location_id=?12, audio_file_id=?13, filename=?14, sample_rate=?15, bit_depth=?16;', | ||
(row) => [ | ||
row.id, | ||
row.title, | ||
row.normalized_title, | ||
row.number, | ||
row.album_id, | ||
row.review_comment, | ||
row.created_at, | ||
row.updated_at, | ||
row.codec_id, | ||
row.length, | ||
row.bitrate, | ||
row.location_id, | ||
row.audio_file_id, | ||
row.filename, | ||
row.sample_rate, | ||
row.bit_depth | ||
] | ||
) | ||
done = obj.done! | ||
} | ||
} | ||
|
||
getAll() { | ||
return this.client.executeSelect(['tracks'], 'SELECT * FROM tracks;') | ||
} | ||
|
||
getPage(num: number | Ref<number>, perPage: number | Ref<number>) { | ||
const numRef = toRef(num) | ||
const perPageRef = toRef(perPage) | ||
return this.client.executeSelect( | ||
['tracks'], | ||
'SELECT * FROM tracks LIMIT ? OFFSET ?;', | ||
perPageRef, | ||
computed(() => numRef.value * perPageRef.value) | ||
) | ||
} | ||
|
||
getCount() { | ||
return this.client.executeTransformedSelect( | ||
['tracks'], | ||
'SELECT COUNT(*) AS count FROM tracks;', | ||
(rows) => (rows.length > 0 ? rows[0].count : 0) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
<script setup lang="ts"> | ||
import { inject } from 'vue' | ||
import type UserDao from '@/db/user_dao' | ||
const { users }: { users: UserDao } = inject('db')! | ||
import { inject, ref, computed, type Ref } from 'vue' | ||
import type { DaoCollection } from '@/sqlite' | ||
const { users, tracks }: DaoCollection = inject('db')! | ||
const values = users.getAll() | ||
const tracksCount = tracks.getCount() | ||
const page = ref(0) | ||
function nextPage() { | ||
page.value++ | ||
} | ||
function prevPage() { | ||
page.value-- | ||
} | ||
const someTracks: Ref<any[]> = tracks.getPage(page, 25) | ||
const titles = computed(() => someTracks.value.map((t) => t.title)) | ||
</script> | ||
|
||
<template> | ||
<p>Home</p> | ||
<div>Table contents: {{ values }}</div> | ||
<div>Track count: {{ tracksCount }}</div> | ||
<v-btn @click="prevPage">Previous page</v-btn> | ||
<v-btn @click="nextPage">Next page</v-btn> | ||
<div>Some tracks: {{ titles }}</div> | ||
</template> |