convex-mux-component 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/videos.ts +50 -0
package/README.md CHANGED
@@ -36,7 +36,7 @@ This creates:
36
36
 
37
37
  - `convex/convex.config.ts`
38
38
  - `convex/migrations.ts`
39
- - `convex/muxWebhook.node.ts`
39
+ - `convex/muxWebhook.ts`
40
40
  - `convex/http.ts`
41
41
 
42
42
  If files already exist, the CLI skips them unless you pass `--force`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convex-mux-component",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "Convex Component for syncing Mux video data and app metadata.",
5
5
  "type": "module",
6
6
  "keywords": [
package/videos.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { mutation, query } from "./_generated/server";
2
2
  import { v } from "convex/values";
3
3
 
4
+ const DEFAULT_METADATA_USER_ID = "default";
5
+
4
6
  function omitUndefined<T extends Record<string, unknown>>(doc: T): Partial<T> {
5
7
  return Object.fromEntries(
6
8
  Object.entries(doc).filter(([, value]) => value !== undefined)
@@ -21,6 +23,7 @@ export const upsertVideoMetadata = mutation({
21
23
  },
22
24
  handler: async (ctx, args) => {
23
25
  const now = Date.now();
26
+ const isFallbackUser = args.userId === DEFAULT_METADATA_USER_ID;
24
27
  const existing = await ctx.db
25
28
  .query("videoMetadata")
26
29
  .withIndex("by_asset_and_user", (q) =>
@@ -38,10 +41,57 @@ export const upsertVideoMetadata = mutation({
38
41
  });
39
42
 
40
43
  if (existing) {
44
+ if (!isFallbackUser) {
45
+ const fallback = await ctx.db
46
+ .query("videoMetadata")
47
+ .withIndex("by_asset_and_user", (q) =>
48
+ q.eq("muxAssetId", args.muxAssetId).eq("userId", DEFAULT_METADATA_USER_ID)
49
+ )
50
+ .unique();
51
+
52
+ if (fallback && fallback._id !== existing._id) {
53
+ const mergedPatchDoc = omitUndefined({
54
+ title: args.title ?? existing.title ?? fallback.title,
55
+ description:
56
+ args.description ?? existing.description ?? fallback.description,
57
+ tags: args.tags ?? existing.tags ?? fallback.tags,
58
+ visibility: args.visibility ?? existing.visibility ?? fallback.visibility,
59
+ custom: args.custom ?? existing.custom ?? fallback.custom,
60
+ updatedAtMs: now,
61
+ });
62
+ await ctx.db.patch(existing._id, mergedPatchDoc);
63
+ await ctx.db.delete(fallback._id);
64
+ return existing._id;
65
+ }
66
+ }
67
+
41
68
  await ctx.db.patch(existing._id, patchDoc);
42
69
  return existing._id;
43
70
  }
44
71
 
72
+ if (!isFallbackUser) {
73
+ const fallback = await ctx.db
74
+ .query("videoMetadata")
75
+ .withIndex("by_asset_and_user", (q) =>
76
+ q.eq("muxAssetId", args.muxAssetId).eq("userId", DEFAULT_METADATA_USER_ID)
77
+ )
78
+ .unique();
79
+
80
+ if (fallback) {
81
+ const migratedPatchDoc = omitUndefined({
82
+ userId: args.userId,
83
+ title: args.title ?? fallback.title,
84
+ description: args.description ?? fallback.description,
85
+ tags: args.tags ?? fallback.tags,
86
+ visibility: args.visibility ?? fallback.visibility,
87
+ custom: args.custom ?? fallback.custom,
88
+ updatedAtMs: now,
89
+ });
90
+ await ctx.db.patch(fallback._id, migratedPatchDoc);
91
+ return fallback._id;
92
+ }
93
+ }
94
+
45
95
  return await ctx.db.insert("videoMetadata", {
46
96
  muxAssetId: args.muxAssetId,
47
97
  userId: args.userId,