refactor: align export, settings and database with the VidIQ record model

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
IgorVolochay
2026-10-25 12:07:24 +03:00
co-authored by Claude Sonnet 5.5
parent 6de9e9656c
commit 1cf5270704
5 changed files with 40 additions and 57 deletions
+9 -18
View File
@@ -7,10 +7,7 @@ export function openDb() {
dbPromise ??= new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onupgradeneeded = () => {
const db = request.result;
db.createObjectStore('outliers', { keyPath: 'videoId' }).createIndex('updatedAt', 'updatedAt');
db.createObjectStore('channels', { keyPath: 'channelId' });
db.createObjectStore('seen', { keyPath: 'videoId' });
request.result.createObjectStore('outliers', { keyPath: 'videoId' }).createIndex('updatedAt', 'updatedAt');
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
@@ -28,19 +25,19 @@ function promisify(request) {
});
}
async function getMany(storeName, keys) {
export async function getOutliers(videoIds) {
const db = await openDb();
const store = db.transaction(storeName, 'readonly').objectStore(storeName);
const records = await Promise.all(keys.map((key) => promisify(store.get(key))));
return new Map(records.filter(Boolean).map((record) => [record[store.keyPath], record]));
const store = db.transaction('outliers', 'readonly').objectStore('outliers');
const records = await Promise.all(videoIds.map((id) => promisify(store.get(id))));
return new Map(records.filter(Boolean).map((record) => [record.videoId, record]));
}
async function putMany(storeName, records) {
export async function putOutliers(records) {
if (records.length === 0) return;
const db = await openDb();
await new Promise((resolve, reject) => {
const tx = db.transaction(storeName, 'readwrite');
const store = tx.objectStore(storeName);
const tx = db.transaction('outliers', 'readwrite');
const store = tx.objectStore('outliers');
for (const record of records) store.put(record);
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
@@ -48,13 +45,7 @@ async function putMany(storeName, records) {
});
}
export const getSeen = (videoIds) => getMany('seen', videoIds);
export const putSeen = (records) => putMany('seen', records);
export const getChannels = (channelIds) => getMany('channels', channelIds);
export const putChannels = (records) => putMany('channels', records);
export const getOutliers = (videoIds) => getMany('outliers', videoIds);
export const putOutliers = (records) => putMany('outliers', records);
// Newest `updatedAt` first.
export async function listOutliers(limit = Infinity) {
const db = await openDb();
return new Promise((resolve, reject) => {
+1 -5
View File
@@ -3,16 +3,12 @@ export const COLUMNS = [
'title',
'url',
'thumbnailUrl',
'channelId',
'channelTitle',
'channelUrl',
'subscribers',
'views',
'publishedAt',
'durationSec',
'outlierScore',
'isShort',
'score',
'vidiqScore',
'foundOn',
'firstSeenAt',
'updatedAt',
+1 -12
View File
@@ -1,4 +1,4 @@
export const DEFAULT_SETTINGS = { apiKey: '', threshold: 5, minViews: 10000 };
export const DEFAULT_SETTINGS = { threshold: 5, minViews: 10000 };
const toNumber = (value) => (value === null || value === undefined || value === '' ? NaN : Number(value));
@@ -6,7 +6,6 @@ export function normalizeSettings(raw = {}) {
const threshold = toNumber(raw.threshold);
const minViews = toNumber(raw.minViews);
return {
apiKey: typeof raw.apiKey === 'string' ? raw.apiKey.trim() : DEFAULT_SETTINGS.apiKey,
threshold: Number.isFinite(threshold) && threshold > 0 ? threshold : DEFAULT_SETTINGS.threshold,
minViews: Number.isFinite(minViews) && minViews >= 0 ? Math.floor(minViews) : DEFAULT_SETTINGS.minViews,
};
@@ -22,13 +21,3 @@ export async function saveSettings(raw) {
await chrome.storage.local.set({ settings });
return settings;
}
// Status is { state: 'ok' | 'no-key' | 'invalid-key' | 'quota', message?, until? }.
export async function getStatus() {
const { status } = await chrome.storage.local.get('status');
return status ?? { state: 'ok' };
}
export async function setStatus(status) {
await chrome.storage.local.set({ status });
}