import { sqlite } from "https://esm.town/v/std/sqlite";
import Mux from "npm:@mux/mux-node";
await sqlite.execute(`create table if not exists videos(
id text unique,
upload_id text unique,
created_at text,
uploaded_at text,
ready_at text,
status text,
playback_ids json,
duration integer,
resolution_tier text,
encoding_tier text,
max_stored_frame_rate integer,
aspect_ratio text,
tracks json,
non_standard_input_reasons json,
environment_id text,
environment_name text
)`);
export function rowObjects(data) {
return data.rows.map(row => {
return data.columns.reduce((obj, column, index) => {
let value;
if (data.columnTypes[index] === "json") {
value = JSON.parse(row[index]);
} else {
value = row[index];
}
obj[column] = value;
return obj;
}, {});
});
}
export async function createVideo(webhook: Mux.Webhooks.VideoUploadCreatedWebhookEvent) {
const {
environment,
data: {
id,
},
} = webhook;
return await sqlite.execute({
sql: `insert into videos(
upload_id,
created_at,
environment_id,
environment_name
)
values (
:id,
datetime('now'),
:environment_id,
:environment_name
)`,
args: {
id,
environment_id: environment.id,
environment_name: environment.name,
},
});
}
export async function updateVideoCreated(webhook: Mux.Webhooks.VideoUploadAssetCreatedWebhookEvent) {
const sql = `
UPDATE videos
SET
id = :id,
uploaded_at = datetime("now")
WHERE upload_id = :upload_id;`;
const {
data: {
id,
asset_id,
},
} = webhook;
return await sqlite.execute({
sql,
args: {
id: asset_id,
upload_id: id,
},
});
}
export function backfillVideo(asset: Mux.Video.Asset) {
const sql = `
INSERT OR IGNORE INTO videos (
id,
upload_id,
status,
created_at,
duration,
resolution_tier,
encoding_tier,