@hot-updater/server 1.0.0-rc.0 → 1.0.0-rc.14

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 (213) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +1 -1
  2. package/dist/adapters/databaseConstraintErrors.cjs +0 -1
  3. package/dist/adapters/databasePluginUtils.cjs +14 -1
  4. package/dist/adapters/databasePluginUtils.mjs +14 -2
  5. package/dist/adapters/drizzle.cjs +9 -4
  6. package/dist/adapters/drizzle.d.cts +0 -1
  7. package/dist/adapters/drizzle.d.mts +0 -1
  8. package/dist/adapters/drizzle.mjs +10 -4
  9. package/dist/adapters/drizzleCrud.cjs +93 -3
  10. package/dist/adapters/drizzleCrud.mjs +93 -5
  11. package/dist/adapters/drizzleLazyDB.cjs +2 -0
  12. package/dist/adapters/drizzleLazyDB.mjs +2 -0
  13. package/dist/adapters/drizzleQuery.cjs +3 -2
  14. package/dist/adapters/drizzleQuery.mjs +3 -1
  15. package/dist/adapters/kysely.cjs +1 -1
  16. package/dist/adapters/kysely.d.cts +0 -1
  17. package/dist/adapters/kysely.d.mts +0 -1
  18. package/dist/adapters/kysely.mjs +2 -1
  19. package/dist/adapters/kyselyCrud.cjs +44 -5
  20. package/dist/adapters/kyselyCrud.mjs +45 -6
  21. package/dist/adapters/kyselyQuery.cjs +2 -2
  22. package/dist/adapters/kyselyQuery.mjs +2 -1
  23. package/dist/adapters/mongodb.cjs +36 -1
  24. package/dist/adapters/mongodb.d.cts +1 -1
  25. package/dist/adapters/mongodb.d.mts +1 -1
  26. package/dist/adapters/mongodb.mjs +36 -0
  27. package/dist/adapters/mongodbCollections.cjs +2 -0
  28. package/dist/adapters/mongodbCollections.mjs +2 -0
  29. package/dist/adapters/mongodbQuery.cjs +17 -4
  30. package/dist/adapters/mongodbQuery.mjs +17 -4
  31. package/dist/adapters/mongodbReads.cjs +88 -30
  32. package/dist/adapters/mongodbReads.mjs +88 -29
  33. package/dist/adapters/mongodbWrites.cjs +1 -2
  34. package/dist/adapters/mongodbWrites.mjs +1 -1
  35. package/dist/adapters/prisma.cjs +44 -7
  36. package/dist/adapters/prisma.d.cts +0 -1
  37. package/dist/adapters/prisma.d.mts +0 -1
  38. package/dist/adapters/prisma.mjs +44 -6
  39. package/dist/adapters/prismaInsights.cjs +99 -0
  40. package/dist/adapters/prismaInsights.mjs +97 -0
  41. package/dist/adapters/prismaQuery.cjs +0 -1
  42. package/dist/adapters/prismaRows.cjs +7 -8
  43. package/dist/adapters/prismaRows.mjs +8 -8
  44. package/dist/apiKeys.cjs +6 -2
  45. package/dist/apiKeys.d.cts +0 -1
  46. package/dist/apiKeys.d.mts +0 -1
  47. package/dist/apiKeys.mjs +3 -3
  48. package/dist/createHotUpdaterCore.cjs +27 -12
  49. package/dist/createHotUpdaterCore.d.cts +8 -8
  50. package/dist/createHotUpdaterCore.d.mts +8 -8
  51. package/dist/createHotUpdaterCore.mjs +26 -12
  52. package/dist/db/createBundleDiff.cjs +2 -2
  53. package/dist/db/createBundleDiff.d.cts +1 -5
  54. package/dist/db/createBundleDiff.d.mts +1 -5
  55. package/dist/db/databasePluginCore.cjs +0 -1
  56. package/dist/db/fixedMigrator.cjs +2 -2
  57. package/dist/db/fixedMigrator.mjs +2 -2
  58. package/dist/db/fixedMigratorMongo.cjs +6 -4
  59. package/dist/db/fixedMigratorMongo.mjs +6 -4
  60. package/dist/db/index.d.cts +0 -1
  61. package/dist/db/index.d.mts +0 -1
  62. package/dist/db/releaseCatalog.cjs +0 -1
  63. package/dist/db/releaseCatalog.d.cts +0 -1
  64. package/dist/db/releaseCatalog.d.mts +0 -1
  65. package/dist/db/schema/insightsCollation.cjs +14 -0
  66. package/dist/db/schema/insightsCollation.mjs +14 -0
  67. package/dist/db/schema/sql.cjs +11 -7
  68. package/dist/db/schema/sql.mjs +8 -8
  69. package/dist/db/schemaGenerators.cjs +29 -9
  70. package/dist/db/schemaGenerators.mjs +29 -9
  71. package/dist/db/schemaReadiness.cjs +2 -0
  72. package/dist/db/schemaReadiness.d.cts +1 -0
  73. package/dist/db/schemaReadiness.d.mts +1 -0
  74. package/dist/db/schemaReadiness.mjs +2 -0
  75. package/dist/db/sqlMigrationExecution.cjs +1 -1
  76. package/dist/db/sqlMigrationExecution.mjs +1 -0
  77. package/dist/db/types.cjs +3 -1
  78. package/dist/db/types.d.cts +3 -6
  79. package/dist/db/types.d.mts +3 -6
  80. package/dist/db/types.mjs +2 -2
  81. package/dist/db/updateArtifacts.cjs +0 -1
  82. package/dist/handler.cjs +5 -5
  83. package/dist/handler.d.cts +1 -2
  84. package/dist/handler.d.mts +1 -2
  85. package/dist/handler.mjs +5 -5
  86. package/dist/handlerReleaseCatalogRoutes.cjs +0 -1
  87. package/dist/handlerReleaseManagementRoutes.cjs +0 -1
  88. package/dist/handlerTypes.d.cts +0 -1
  89. package/dist/handlerTypes.d.mts +0 -1
  90. package/dist/handlerVersionRoutes.d.cts +1 -0
  91. package/dist/handlerVersionRoutes.d.mts +1 -0
  92. package/dist/index.cjs +2 -2
  93. package/dist/index.d.cts +4 -4
  94. package/dist/index.d.mts +4 -4
  95. package/dist/index.mjs +2 -2
  96. package/dist/insights/domain.d.cts +92 -0
  97. package/dist/insights/domain.d.mts +92 -0
  98. package/dist/insights/errors.cjs +15 -0
  99. package/dist/insights/errors.mjs +14 -0
  100. package/dist/{analytics → insights}/eventInput.cjs +61 -16
  101. package/dist/{analytics → insights}/eventInput.mjs +61 -18
  102. package/dist/insights/provider.cjs +332 -0
  103. package/dist/insights/provider.d.cts +6 -0
  104. package/dist/insights/provider.d.mts +6 -0
  105. package/dist/insights/provider.mjs +332 -0
  106. package/dist/insights/queryInput.cjs +93 -0
  107. package/dist/insights/queryInput.mjs +91 -0
  108. package/dist/insights/routes.cjs +58 -0
  109. package/dist/insights/routes.mjs +56 -0
  110. package/dist/insights/types.d.cts +32 -0
  111. package/dist/insights/types.d.mts +32 -0
  112. package/dist/node.d.cts +0 -1
  113. package/dist/node.d.mts +0 -1
  114. package/dist/package.cjs +1 -1
  115. package/dist/package.mjs +1 -1
  116. package/dist/schema/index.cjs +17 -3
  117. package/dist/schema/index.mjs +4 -4
  118. package/dist/schema/v1_0_0.cjs +64 -35
  119. package/dist/schema/v1_0_0.mjs +64 -36
  120. package/dist/storageAccess.cjs +0 -1
  121. package/dist/types/index.d.cts +0 -1
  122. package/dist/types/index.d.mts +0 -1
  123. package/package.json +7 -7
  124. package/src/adapters/databasePluginTestDatabase.ts +15 -5
  125. package/src/adapters/databasePluginUtils.ts +24 -0
  126. package/src/adapters/drizzle.spec.ts +203 -5
  127. package/src/adapters/drizzle.ts +16 -4
  128. package/src/adapters/drizzleCrud.ts +200 -9
  129. package/src/adapters/drizzleLazyDB.ts +33 -6
  130. package/src/adapters/kysely.spec.ts +62 -0
  131. package/src/adapters/kysely.ts +7 -1
  132. package/src/adapters/kyselyCrud.ts +115 -7
  133. package/src/adapters/kyselyInsights.spec.ts +168 -0
  134. package/src/adapters/mongodb.integration.spec.ts +483 -0
  135. package/src/adapters/mongodb.spec.ts +89 -0
  136. package/src/adapters/mongodb.ts +59 -1
  137. package/src/adapters/mongodbCollections.ts +16 -0
  138. package/src/adapters/mongodbQuery.ts +22 -3
  139. package/src/adapters/mongodbReads.ts +78 -2
  140. package/src/adapters/mongodbTestClient.ts +107 -6
  141. package/src/adapters/mongodbTestFilter.ts +5 -1
  142. package/src/adapters/mongodbWrites.ts +1 -0
  143. package/src/adapters/prisma.spec.ts +270 -0
  144. package/src/adapters/prisma.ts +75 -2
  145. package/src/adapters/prismaInsights.ts +190 -0
  146. package/src/adapters/prismaRows.ts +17 -10
  147. package/src/adapters/prismaTestClient.ts +89 -7
  148. package/src/apiKeys.integration.spec.ts +3 -3
  149. package/src/createHotUpdaterCore.ts +30 -16
  150. package/src/db/databasePluginUpdateCheck.bench.ts +10 -0
  151. package/src/db/fixedMigrator.spec.ts +63 -0
  152. package/src/db/fixedMigratorMongo.spec.ts +70 -9
  153. package/src/db/fixedMigratorMongo.ts +10 -10
  154. package/src/db/index.spec.ts +6 -1
  155. package/src/db/schema/insightsCollation.ts +21 -0
  156. package/src/db/schema/sql.ts +8 -5
  157. package/src/db/schemaGenerators.ts +42 -13
  158. package/src/db/types.spec.ts +11 -3
  159. package/src/db/types.ts +13 -7
  160. package/src/handler-standalone.integration.spec.ts +6 -8
  161. package/src/handler.ts +12 -12
  162. package/src/index.ts +3 -3
  163. package/src/insights/domain.ts +106 -0
  164. package/src/insights/errors.ts +11 -0
  165. package/src/{analytics → insights}/eventInput.ts +66 -16
  166. package/src/insights/insights.integration.spec.ts +475 -0
  167. package/src/insights/provider.spec.ts +396 -0
  168. package/src/insights/provider.ts +611 -0
  169. package/src/insights/queryInput.ts +142 -0
  170. package/src/insights/routes.ts +104 -0
  171. package/src/insights/types.ts +55 -0
  172. package/src/schema/dsl-all-versions.spec.ts +1 -1
  173. package/src/schema/v1_0_0.spec.ts +77 -3
  174. package/src/schema/v1_0_0.ts +83 -37
  175. package/dist/analytics/bounded/activeOverview.cjs +0 -87
  176. package/dist/analytics/bounded/activeOverview.mjs +0 -85
  177. package/dist/analytics/bounded/installationSearch.cjs +0 -42
  178. package/dist/analytics/bounded/installationSearch.mjs +0 -42
  179. package/dist/analytics/bounded/persistence.cjs +0 -39
  180. package/dist/analytics/bounded/persistence.mjs +0 -38
  181. package/dist/analytics/bounded/provider.cjs +0 -170
  182. package/dist/analytics/bounded/provider.d.cts +0 -7
  183. package/dist/analytics/bounded/provider.d.mts +0 -7
  184. package/dist/analytics/bounded/provider.mjs +0 -170
  185. package/dist/analytics/bounded/scan.cjs +0 -144
  186. package/dist/analytics/bounded/scan.mjs +0 -138
  187. package/dist/analytics/domain.d.cts +0 -121
  188. package/dist/analytics/domain.d.mts +0 -121
  189. package/dist/analytics/errors.cjs +0 -22
  190. package/dist/analytics/errors.mjs +0 -20
  191. package/dist/analytics/persistence.d.cts +0 -6
  192. package/dist/analytics/persistence.d.mts +0 -6
  193. package/dist/analytics/queryInput.cjs +0 -58
  194. package/dist/analytics/queryInput.mjs +0 -55
  195. package/dist/analytics/routes.cjs +0 -63
  196. package/dist/analytics/routes.mjs +0 -61
  197. package/dist/analytics/types.d.cts +0 -17
  198. package/dist/analytics/types.d.mts +0 -17
  199. package/src/analytics/analytics.integration.spec.ts +0 -144
  200. package/src/analytics/bounded/activeOverview.spec.ts +0 -189
  201. package/src/analytics/bounded/activeOverview.ts +0 -148
  202. package/src/analytics/bounded/installationSearch.spec.ts +0 -130
  203. package/src/analytics/bounded/installationSearch.ts +0 -69
  204. package/src/analytics/bounded/persistence.ts +0 -43
  205. package/src/analytics/bounded/provider.spec.ts +0 -219
  206. package/src/analytics/bounded/provider.ts +0 -229
  207. package/src/analytics/bounded/scan.ts +0 -235
  208. package/src/analytics/domain.ts +0 -139
  209. package/src/analytics/errors.ts +0 -18
  210. package/src/analytics/persistence.ts +0 -10
  211. package/src/analytics/queryInput.ts +0 -102
  212. package/src/analytics/routes.ts +0 -123
  213. package/src/analytics/types.ts +0 -44
@@ -0,0 +1,483 @@
1
+ import net from "node:net";
2
+ import path from "node:path";
3
+
4
+ import { type BundleEventRow } from "@hot-updater/plugin-core";
5
+ import { execa } from "execa";
6
+ import { MongoClient, type CommandStartedEvent, type Document } from "mongodb";
7
+ import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
8
+
9
+ import { createBundleEventRowFixture } from "../../../test-utils/src/databaseTestFixtures";
10
+ import { mongoAdapter } from "./mongodb";
11
+
12
+ const availablePort = (): Promise<number> =>
13
+ new Promise((resolve, reject) => {
14
+ const server = net.createServer();
15
+ server.once("error", reject);
16
+ server.listen(0, "127.0.0.1", () => {
17
+ const address = server.address();
18
+ if (address === null || typeof address === "string") {
19
+ server.close();
20
+ reject(new Error("Failed to allocate MongoDB test port"));
21
+ return;
22
+ }
23
+ server.close((error) => (error ? reject(error) : resolve(address.port)));
24
+ });
25
+ });
26
+
27
+ describe("MongoDB native Insights storage", () => {
28
+ let client: MongoClient;
29
+ let composeEnvironment: Record<string, string>;
30
+ const composeFile = path.resolve(
31
+ import.meta.dirname,
32
+ "../../../../examples-server/hono-mongodb/docker-compose.yml",
33
+ );
34
+ const compose = (args: string[]) =>
35
+ execa("docker", ["compose", "-f", composeFile, ...args], {
36
+ env: composeEnvironment,
37
+ });
38
+ const insights = () => mongoAdapter({ client }).models.insights;
39
+ const record = (event: BundleEventRow) =>
40
+ insights().recordEvent({
41
+ event,
42
+ });
43
+
44
+ const measure = async (read: () => Promise<unknown>) => {
45
+ let pipeline: Document[] | undefined;
46
+ const capture = (event: CommandStartedEvent) => {
47
+ if (event.command.aggregate === "bundle_event_heads")
48
+ pipeline = event.command.pipeline;
49
+ };
50
+ client.on("commandStarted", capture);
51
+ let result: unknown;
52
+ try {
53
+ result = await read();
54
+ } finally {
55
+ client.off("commandStarted", capture);
56
+ }
57
+ expect(pipeline).toBeDefined();
58
+ const plan = await client
59
+ .db()
60
+ .collection("bundle_event_heads")
61
+ .aggregate(pipeline!, { collation: { locale: "simple" } })
62
+ .explain("executionStats");
63
+ expect(JSON.stringify(plan)).not.toContain('"stage":"COLLSCAN"');
64
+ const cursor = plan.stages?.find(
65
+ (stage: Record<string, unknown>) => "$cursor" in stage,
66
+ )?.$cursor;
67
+ const stats = plan.executionStats ?? cursor?.executionStats;
68
+ const lookup = plan.stages?.find(
69
+ (stage: Record<string, unknown>) => "$lookup" in stage,
70
+ );
71
+ return {
72
+ result,
73
+ keysExamined: stats.totalKeysExamined + (lookup?.totalKeysExamined ?? 0),
74
+ docsExamined: stats.totalDocsExamined + (lookup?.totalDocsExamined ?? 0),
75
+ };
76
+ };
77
+
78
+ beforeAll(async () => {
79
+ const port = await availablePort();
80
+ composeEnvironment = {
81
+ COMPOSE_PROJECT_NAME: `hot-updater-insights-${process.pid}`,
82
+ HOT_UPDATER_E2E_MONGODB_PORT: String(port),
83
+ };
84
+ await compose(["up", "-d", "--wait"]);
85
+ client = new MongoClient(
86
+ `mongodb://127.0.0.1:${port}/insights_native?replicaSet=rs0&directConnection=true`,
87
+ { monitorCommands: true },
88
+ );
89
+ await client.connect();
90
+ const adapter = mongoAdapter({ client });
91
+ await (await adapter.createMigrator!().migrateToLatest()).execute();
92
+ }, 120_000);
93
+
94
+ beforeEach(async () => {
95
+ await client.db().collection("bundle_events").deleteMany({});
96
+ await client.db().collection("bundle_event_heads").deleteMany({});
97
+ });
98
+
99
+ afterAll(async () => {
100
+ await client?.close();
101
+ if (composeEnvironment) await compose(["down", "-v", "--remove-orphans"]);
102
+ }, 60_000);
103
+
104
+ it("preserves latest state after a native event validation error", async () => {
105
+ const previous = {
106
+ ...createBundleEventRowFixture("801", 100),
107
+ user_id: "original",
108
+ };
109
+ await record(previous);
110
+ await client.db().command({
111
+ collMod: "bundle_events",
112
+ validator: { user_id: { $ne: "reject" } },
113
+ });
114
+ const failed = {
115
+ ...createBundleEventRowFixture("802", 200),
116
+ install_id: previous.install_id,
117
+ user_id: "reject",
118
+ };
119
+ try {
120
+ await expect(record(failed)).rejects.toThrow();
121
+ expect(
122
+ await client.db().collection("bundle_events").countDocuments({}),
123
+ ).toBe(1);
124
+ await expect(
125
+ insights().findLatestEvents({ installId: previous.install_id }),
126
+ ).resolves.toEqual([previous]);
127
+ } finally {
128
+ await client.db().command({ collMod: "bundle_events", validator: {} });
129
+ }
130
+ await record(failed);
131
+ await record(failed);
132
+ expect(
133
+ await client.db().collection("bundle_events").countDocuments({}),
134
+ ).toBe(2);
135
+ await expect(
136
+ insights().findLatestEvents({ installId: previous.install_id }),
137
+ ).resolves.toEqual([failed]);
138
+ });
139
+
140
+ it("retries native conflicts without dropping events or letting duplicate IDs rewrite state", async () => {
141
+ const events = Array.from({ length: 12 }, (_, index) => ({
142
+ ...createBundleEventRowFixture(String(810 + index), 100),
143
+ install_id: "concurrent-installation",
144
+ user_id: index === 11 ? null : "old-user",
145
+ }));
146
+ await Promise.all(events.map(record));
147
+ const winner = events.at(-1)!;
148
+ await Promise.all(Array.from({ length: 4 }, () => record(winner)));
149
+ const conflictingDuplicate = {
150
+ ...events[0]!,
151
+ install_id: "different-installation",
152
+ received_at_ms: 1000,
153
+ };
154
+ await record(conflictingDuplicate);
155
+ expect(
156
+ await client.db().collection("bundle_events").countDocuments({}),
157
+ ).toBe(12);
158
+ await expect(
159
+ insights().findLatestEvents({ installId: winner.install_id }),
160
+ ).resolves.toEqual([winner]);
161
+ await expect(
162
+ insights().findLatestEvents({
163
+ installId: conflictingDuplicate.install_id,
164
+ }),
165
+ ).resolves.toEqual([]);
166
+ await expect(
167
+ insights().findLatestEvents({ userId: "old-user", limit: 10 }),
168
+ ).resolves.toEqual([]);
169
+ });
170
+
171
+ it("accepts concurrent retries of a previously unseen event ID exactly once", async () => {
172
+ const event = createBundleEventRowFixture("825", 100);
173
+ await Promise.all(Array.from({ length: 12 }, () => record(event)));
174
+ expect(
175
+ await client.db().collection("bundle_events").countDocuments({}),
176
+ ).toBe(1);
177
+ await expect(
178
+ insights().findLatestEvents({ installId: event.install_id }),
179
+ ).resolves.toEqual([event]);
180
+ });
181
+
182
+ it("atomically moves scope membership while rejecting old or duplicate state", async () => {
183
+ const previous = {
184
+ ...createBundleEventRowFixture("840", 100),
185
+ user_id: "old",
186
+ };
187
+ const current = {
188
+ ...previous,
189
+ id: createBundleEventRowFixture("842", 200).id,
190
+ received_at_ms: 200,
191
+ user_id: null,
192
+ channel: "preview",
193
+ };
194
+ await record(previous);
195
+ const heads = client.db().collection("bundle_event_heads");
196
+ await client.db().command({
197
+ collMod: "bundle_event_heads",
198
+ validator: { channel: { $ne: "preview" } },
199
+ });
200
+ try {
201
+ await expect(record(current)).rejects.toThrow();
202
+ expect(
203
+ await client.db().collection("bundle_events").countDocuments({}),
204
+ ).toBe(1);
205
+ await expect(
206
+ insights().findLatestEvents({ installId: current.install_id }),
207
+ ).resolves.toEqual([previous]);
208
+ } finally {
209
+ await client
210
+ .db()
211
+ .command({ collMod: "bundle_event_heads", validator: {} });
212
+ }
213
+ await record(current);
214
+ await record({ ...current, channel: "duplicate", received_at_ms: 300 });
215
+ await record({
216
+ ...previous,
217
+ id: createBundleEventRowFixture("841", 200).id,
218
+ received_at_ms: 200,
219
+ });
220
+ await expect(
221
+ insights().findLatestEvents({ userId: "old", limit: 10 }),
222
+ ).resolves.toEqual([]);
223
+ for (const [channel, expected] of [
224
+ ["production", 0],
225
+ ["preview", 1],
226
+ ["duplicate", 0],
227
+ ] as const)
228
+ await expect(
229
+ insights().countLatestEvents({ platform: "ios", channel, sinceMs: 0 }),
230
+ ).resolves.toBe(expected);
231
+ expect(
232
+ Object.keys((await heads.findOne({ install_id: current.install_id }))!)
233
+ .filter((key) => key !== "_id")
234
+ .sort(),
235
+ ).toEqual([
236
+ "channel",
237
+ "from_bundle_id",
238
+ "id",
239
+ "install_id",
240
+ "platform",
241
+ "received_at_ms",
242
+ "to_bundle_id",
243
+ "type",
244
+ "user_id",
245
+ ]);
246
+ });
247
+
248
+ it("keeps native latest reads bounded as history and unrelated scopes grow", async () => {
249
+ const events = Array.from({ length: 24 }, (_, index) => ({
250
+ ...createBundleEventRowFixture(String(10000 + index), 1000),
251
+ install_id: `installation-${String(index).padStart(3, "0")}`,
252
+ user_id: "current",
253
+ }));
254
+ await Promise.all(events.map(record));
255
+ const reads = async () => ({
256
+ user: await measure(() =>
257
+ insights().findLatestEvents({ userId: "current", limit: 10 }),
258
+ ),
259
+ scope: await measure(() =>
260
+ insights().countLatestEvents({
261
+ platform: "ios",
262
+ channel: "production",
263
+ sinceMs: 500,
264
+ }),
265
+ ),
266
+ bundle: await measure(() =>
267
+ insights().countLatestEvents({
268
+ platform: "ios",
269
+ channel: "production",
270
+ sinceMs: 500,
271
+ bundle: [
272
+ {
273
+ field: "to_bundle_id",
274
+ value: events[0]!.to_bundle_id,
275
+ types: ["UPDATE_APPLIED"],
276
+ },
277
+ ],
278
+ }),
279
+ ),
280
+ });
281
+ const before = await reads();
282
+ // Older accepted events do not advance heads. Bulk seed the immutable
283
+ // history so this measurement isolates read cost from transaction timing.
284
+ await client
285
+ .db()
286
+ .collection("bundle_events")
287
+ .insertMany(
288
+ Array.from({ length: 2400 }, (_, index) => ({
289
+ ...events[index % events.length]!,
290
+ id: createBundleEventRowFixture(String(20000 + index), 1).id,
291
+ received_at_ms: index % 100,
292
+ user_id: "previous",
293
+ })),
294
+ );
295
+ const afterHistory = await reads();
296
+ await Promise.all(
297
+ Array.from({ length: 240 }, (_, index) =>
298
+ record({
299
+ ...createBundleEventRowFixture(String(30000 + index), 1000),
300
+ install_id: `other-${index}`,
301
+ user_id: "other",
302
+ channel: "other",
303
+ }),
304
+ ),
305
+ );
306
+ const afterScopes = await reads();
307
+ for (const sample of [before, afterHistory, afterScopes]) {
308
+ expect(sample.user.result).toEqual(events.slice(0, 10));
309
+ expect(sample.user.keysExamined).toBeLessThanOrEqual(22);
310
+ expect(sample.user.docsExamined).toBeLessThanOrEqual(20);
311
+ expect(sample.scope.result).toBe(events.length);
312
+ expect(sample.scope.keysExamined).toBeLessThanOrEqual(events.length + 1);
313
+ expect(sample.scope.docsExamined).toBeLessThanOrEqual(events.length);
314
+ expect(sample.bundle.result).toBe(1);
315
+ expect(sample.bundle.keysExamined).toBeLessThanOrEqual(2);
316
+ expect(sample.bundle.docsExamined).toBeLessThanOrEqual(1);
317
+ }
318
+ expect(afterHistory).toEqual(before);
319
+ await expect(
320
+ insights().findLatestEvents({ userId: "previous", limit: 10 }),
321
+ ).resolves.toEqual([]);
322
+ const metrics = (sample: Awaited<ReturnType<typeof reads>>) =>
323
+ Object.fromEntries(
324
+ Object.entries(sample).map(
325
+ ([query, { keysExamined, docsExamined }]) => [
326
+ query,
327
+ { keysExamined, docsExamined },
328
+ ],
329
+ ),
330
+ );
331
+ console.info(
332
+ "MongoDB latest read growth",
333
+ JSON.stringify({
334
+ before: metrics(before),
335
+ afterHistory: metrics(afterHistory),
336
+ afterScopes: metrics(afterScopes),
337
+ historyEventsAdded: 2400,
338
+ unrelatedInstallationsAdded: 240,
339
+ }),
340
+ );
341
+ });
342
+
343
+ it("uses raw recovery predicates consistently in native counts and indexed lists", async () => {
344
+ const applied = createBundleEventRowFixture("831", 100);
345
+ const recovered: BundleEventRow = {
346
+ ...createBundleEventRowFixture("832", 200),
347
+ type: "RECOVERED",
348
+ metadata: {
349
+ ...createBundleEventRowFixture("832", 200).metadata,
350
+ update_strategy: "appVersion",
351
+ },
352
+ install_id: applied.install_id,
353
+ from_bundle_id: applied.to_bundle_id,
354
+ to_bundle_id: applied.from_bundle_id!,
355
+ };
356
+ await record(applied);
357
+ await record(recovered);
358
+ const filter = {
359
+ type: "RECOVERED" as const,
360
+ platform: "ios" as const,
361
+ channel: "production",
362
+ fromBundleId: applied.to_bundle_id,
363
+ };
364
+ await expect(
365
+ insights().countEvents({ filter, sinceMs: 200, beforeReceivedAtMs: 201 }),
366
+ ).resolves.toBe(1);
367
+ await expect(
368
+ insights().listEvents({
369
+ filter: { kind: "bundle", ...filter },
370
+ sinceMs: 200,
371
+ beforeReceivedAtMs: 201,
372
+ limit: 10,
373
+ }),
374
+ ).resolves.toEqual([recovered]);
375
+ await expect(
376
+ insights().countEvents({ filter, sinceMs: 100, beforeReceivedAtMs: 200 }),
377
+ ).resolves.toBe(0);
378
+ await expect(
379
+ insights().countLatestEvents({
380
+ platform: "ios",
381
+ channel: "production",
382
+ bundle: [
383
+ {
384
+ field: "to_bundle_id",
385
+ value: applied.to_bundle_id,
386
+ types: ["UPDATE_APPLIED", "RECOVERED", "UNCHANGED"],
387
+ },
388
+ ],
389
+ sinceMs: 0,
390
+ }),
391
+ ).resolves.toBe(0);
392
+ const explanation = await client
393
+ .db()
394
+ .collection("bundle_events")
395
+ .find({
396
+ type: "RECOVERED",
397
+ platform: "ios",
398
+ channel: "production",
399
+ from_bundle_id: applied.to_bundle_id,
400
+ received_at_ms: { $gte: 200, $lt: 201 },
401
+ })
402
+ .collation({ locale: "simple" })
403
+ .sort({ received_at_ms: -1, id: -1 })
404
+ .hint("bundle_events_from_bundle_idx")
405
+ .explain("executionStats");
406
+ expect(explanation.executionStats.nReturned).toBe(1);
407
+ expect(explanation.executionStats.totalKeysExamined).toBeLessThanOrEqual(2);
408
+ });
409
+
410
+ it("uses native bundle indexes for rare, common, absent, and overlapping predicates", async () => {
411
+ const rare = createBundleEventRowFixture("40000", 1000).to_bundle_id;
412
+ const common = createBundleEventRowFixture("40001", 1000).to_bundle_id;
413
+ const absent = createBundleEventRowFixture("40002", 1000).to_bundle_id;
414
+ const events = Array.from({ length: 1000 }, (_, index) => {
415
+ const bundleId = index === 0 ? rare : common;
416
+ return {
417
+ ...createBundleEventRowFixture(String(50000 + index), 1000),
418
+ from_bundle_id: bundleId,
419
+ to_bundle_id: bundleId,
420
+ };
421
+ });
422
+ // Seed a consistent large read fixture; transaction correctness is covered
423
+ // above. The index contains only the access fields used by production.
424
+ await client.db().collection("bundle_events").insertMany(events);
425
+ await client
426
+ .db()
427
+ .collection("bundle_event_heads")
428
+ .insertMany(
429
+ events.map((event) => ({
430
+ install_id: event.install_id,
431
+ id: event.id,
432
+ received_at_ms: event.received_at_ms,
433
+ user_id: event.user_id,
434
+ platform: event.platform,
435
+ channel: event.channel,
436
+ type: event.type,
437
+ from_bundle_id: event.from_bundle_id,
438
+ to_bundle_id: event.to_bundle_id,
439
+ })),
440
+ );
441
+ const count = (bundleId: string, overlap = false) =>
442
+ measure(() =>
443
+ insights().countLatestEvents({
444
+ platform: "ios",
445
+ channel: "production",
446
+ sinceMs: 500,
447
+ bundle: [
448
+ {
449
+ field: "from_bundle_id",
450
+ value: bundleId,
451
+ types: overlap ? ["UPDATE_APPLIED"] : ["UPDATE_DOWNLOADED"],
452
+ },
453
+ {
454
+ field: "to_bundle_id",
455
+ value: bundleId,
456
+ types: ["UNCHANGED", "UPDATE_APPLIED", "RECOVERED"],
457
+ },
458
+ ],
459
+ }),
460
+ );
461
+ const samples = {
462
+ rare: await count(rare),
463
+ common: await count(common),
464
+ absent: await count(absent),
465
+ rareOverlap: await count(rare, true),
466
+ commonOverlap: await count(common, true),
467
+ };
468
+ for (const query of [samples.rare, samples.rareOverlap]) {
469
+ expect(query.result).toBe(1);
470
+ expect(query.keysExamined).toBeLessThanOrEqual(8);
471
+ expect(query.docsExamined).toBeLessThanOrEqual(1);
472
+ }
473
+ expect(samples.absent.result).toBe(0);
474
+ expect(samples.absent.keysExamined).toBeLessThanOrEqual(4);
475
+ expect(samples.absent.docsExamined).toBe(0);
476
+ for (const query of [samples.common, samples.commonOverlap]) {
477
+ expect(query.result).toBe(999);
478
+ expect(query.keysExamined).toBeLessThanOrEqual(2 * 999 + 8);
479
+ expect(query.docsExamined).toBeLessThanOrEqual(1000);
480
+ }
481
+ console.info("MongoDB native bundle selectivity", JSON.stringify(samples));
482
+ });
483
+ });
@@ -4,6 +4,7 @@ import { describe, expect, it } from "vitest";
4
4
  import {
5
5
  createBundlePatchRowFixture,
6
6
  createBundleRowFixture,
7
+ createBundleEventRowFixture,
7
8
  } from "../../../test-utils/src/databaseTestFixtures";
8
9
  import { setupDatabasePluginTestSuite } from "../../../test-utils/src/setupDatabasePluginTestSuite";
9
10
  import { mongoAdapter } from "./mongodb";
@@ -22,6 +23,94 @@ setupDatabasePluginTestSuite({
22
23
  });
23
24
 
24
25
  describe("mongoAdapter capabilities", () => {
26
+ it("records Insights atomically even without legacy catalog transactions enabled", async () => {
27
+ harness.reset();
28
+ const insights = mongoAdapter({ client: harness.client }).models.insights;
29
+ const event = createBundleEventRowFixture("981", 100);
30
+ const input = { event };
31
+ harness.failNextEventWrite();
32
+ await expect(insights.recordEvent(input)).rejects.toThrow(
33
+ "injected event write failure",
34
+ );
35
+ await expect(
36
+ insights.listEvents({
37
+ filter: { kind: "all" },
38
+ beforeReceivedAtMs: 200,
39
+ limit: 10,
40
+ }),
41
+ ).resolves.toEqual([]);
42
+ await expect(
43
+ insights.findLatestEvents({ installId: event.install_id }),
44
+ ).resolves.toEqual([]);
45
+ await insights.recordEvent(input);
46
+ await insights.recordEvent(input);
47
+ await expect(
48
+ insights.listEvents({
49
+ filter: { kind: "all" },
50
+ beforeReceivedAtMs: 200,
51
+ limit: 10,
52
+ }),
53
+ ).resolves.toEqual([event]);
54
+ await expect(
55
+ insights.findLatestEvents({ installId: event.install_id }),
56
+ ).resolves.toEqual([input.event]);
57
+ });
58
+
59
+ it("preserves all concurrent events while advancing one installation and clearing its user", async () => {
60
+ harness.reset();
61
+ const insights = mongoAdapter({ client: harness.client }).models.insights;
62
+ const events = ["990", "993", "991", "992"].map((suffix) => ({
63
+ ...createBundleEventRowFixture(suffix, 100),
64
+ install_id: "concurrent-installation",
65
+ user_id: suffix === "993" ? null : "previous-user",
66
+ }));
67
+ await Promise.all(
68
+ events.map((event) =>
69
+ insights.recordEvent({
70
+ event,
71
+ }),
72
+ ),
73
+ );
74
+ await expect(
75
+ insights.findLatestEvents({ installId: "concurrent-installation" }),
76
+ ).resolves.toEqual([events[1]!]);
77
+ await expect(
78
+ insights.findLatestEvents({ userId: "previous-user", limit: 10 }),
79
+ ).resolves.toEqual([]);
80
+ await expect(
81
+ insights.listEvents({
82
+ filter: { kind: "all" },
83
+ beforeReceivedAtMs: 200,
84
+ limit: 10,
85
+ }),
86
+ ).resolves.toHaveLength(4);
87
+ });
88
+
89
+ it("rolls back a new event when its private head write fails", async () => {
90
+ harness.reset();
91
+ const insights = mongoAdapter({ client: harness.client }).models.insights;
92
+ const event = createBundleEventRowFixture("995", 100);
93
+ harness.failNextHeadWrite();
94
+ await expect(insights.recordEvent({ event })).rejects.toThrow(
95
+ "injected head write failure",
96
+ );
97
+ await expect(
98
+ insights.listEvents({
99
+ filter: { kind: "all" },
100
+ beforeReceivedAtMs: 200,
101
+ limit: 10,
102
+ }),
103
+ ).resolves.toEqual([]);
104
+ await expect(
105
+ insights.findLatestEvents({ installId: event.install_id }),
106
+ ).resolves.toEqual([]);
107
+ await insights.recordEvent({ event });
108
+ await insights.recordEvent({ event });
109
+ await expect(
110
+ insights.findLatestEvents({ installId: event.install_id }),
111
+ ).resolves.toEqual([event]);
112
+ });
113
+
25
114
  it("returns an adapter without an unsafe atomic-batch fallback", () => {
26
115
  const plugin = mongoAdapter({ client: harness.client });
27
116
  expect(plugin.name).toBe("mongodb");
@@ -4,7 +4,11 @@ import {
4
4
  type DatabasePluginImplementation,
5
5
  type TransactionDatabasePluginImplementation,
6
6
  } from "@hot-updater/plugin-core/internal";
7
- import type { ClientSession, MongoClient } from "mongodb";
7
+ import {
8
+ MongoServerError,
9
+ type ClientSession,
10
+ type MongoClient,
11
+ } from "mongodb";
8
12
 
9
13
  import { createMongoMigrator } from "../db/fixedMigrator";
10
14
  import type { DatabaseAdapterWithCapabilities } from "../db/types";
@@ -14,6 +18,7 @@ import { createMongoWrites } from "./mongodbWrites";
14
18
 
15
19
  export interface MongoDBConfig {
16
20
  readonly client: MongoClient;
21
+ /** Enables atomic catalog commits. Insights always requires MongoDB 5+ on a replica set or sharded cluster. */
17
22
  readonly transactions?: boolean;
18
23
  }
19
24
 
@@ -23,6 +28,59 @@ const createMongoImplementation = (
23
28
  ): DatabasePluginImplementation => {
24
29
  const collections = createMongoCollections(client);
25
30
  return {
31
+ recordInsights: async ({ event }) => {
32
+ const record = () =>
33
+ client.withSession((insightsSession) =>
34
+ insightsSession.withTransaction(async () => {
35
+ const insights = createMongoCollections(client);
36
+ const options = {
37
+ session: insightsSession,
38
+ collation: { locale: "simple" },
39
+ };
40
+ const accepted = await insights.bundleEvents.updateOne(
41
+ { id: event.id },
42
+ { $setOnInsert: event },
43
+ { ...options, upsert: true },
44
+ );
45
+ if (accepted.upsertedCount === 0) return;
46
+ const current = await insights.bundleEventHeads.findOne(
47
+ { install_id: event.install_id },
48
+ options,
49
+ );
50
+ if (
51
+ current !== null &&
52
+ (event.received_at_ms < current.received_at_ms ||
53
+ (event.received_at_ms === current.received_at_ms &&
54
+ event.id <= current.id))
55
+ )
56
+ return;
57
+ await insights.bundleEventHeads.updateOne(
58
+ { install_id: event.install_id },
59
+ {
60
+ $set: {
61
+ install_id: event.install_id,
62
+ id: event.id,
63
+ received_at_ms: event.received_at_ms,
64
+ user_id: event.user_id,
65
+ platform: event.platform,
66
+ channel: event.channel,
67
+ type: event.type,
68
+ from_bundle_id: event.from_bundle_id,
69
+ to_bundle_id: event.to_bundle_id,
70
+ },
71
+ },
72
+ { ...options, upsert: true },
73
+ );
74
+ }),
75
+ );
76
+ try {
77
+ await record();
78
+ } catch (error) {
79
+ if (!(error instanceof MongoServerError) || error.code !== 11000)
80
+ throw error;
81
+ await record();
82
+ }
83
+ },
26
84
  ...createMongoWrites(collections, session),
27
85
  ...createMongoReads(collections, session),
28
86
  };
@@ -24,6 +24,19 @@ export type MongoBundleDocument = BundleRow & {
24
24
  readonly [DELETION_TOKEN_FIELD]?: string;
25
25
  };
26
26
 
27
+ export type MongoBundleEventHead = Pick<
28
+ BundleEventRow,
29
+ | "install_id"
30
+ | "id"
31
+ | "received_at_ms"
32
+ | "user_id"
33
+ | "platform"
34
+ | "channel"
35
+ | "type"
36
+ | "from_bundle_id"
37
+ | "to_bundle_id"
38
+ >;
39
+
27
40
  export const WITHOUT_INTERNAL_FIELDS = {
28
41
  ...WITHOUT_MONGO_ID,
29
42
  [DELETION_TOKEN_FIELD]: 0,
@@ -37,6 +50,7 @@ export type MongoCollections = {
37
50
  readonly bundles: Collection<MongoBundleDocument>;
38
51
  readonly bundlePatches: Collection<BundlePatchRow>;
39
52
  readonly bundleEvents: Collection<BundleEventRow>;
53
+ readonly bundleEventHeads: Collection<MongoBundleEventHead>;
40
54
  readonly channels: Collection<ChannelRow>;
41
55
  readonly apiKeys: Collection<ApiKeyRow>;
42
56
  readonly releases: Collection<ReleaseRow>;
@@ -51,6 +65,8 @@ export const createMongoCollections = (
51
65
  bundles: database.collection<MongoBundleDocument>("bundles"),
52
66
  bundlePatches: database.collection<BundlePatchRow>("bundle_patches"),
53
67
  bundleEvents: database.collection<BundleEventRow>("bundle_events"),
68
+ bundleEventHeads:
69
+ database.collection<MongoBundleEventHead>("bundle_event_heads"),
54
70
  channels: database.collection<ChannelRow>("channels"),
55
71
  apiKeys: database.collection<ApiKeyRow>("api_keys"),
56
72
  releases: database.collection<ReleaseRow>("releases"),