@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
@@ -5,6 +5,7 @@ import { describe, expect, it } from "vitest";
5
5
 
6
6
  import {
7
7
  createBundlePatchRowFixture,
8
+ createBundleEventRowFixture,
8
9
  createBundleRowFixture,
9
10
  createChannelRowFixture,
10
11
  } from "../../../test-utils/src/databaseTestFixtures";
@@ -54,6 +55,67 @@ setupDatabasePluginTestSuite({
54
55
  });
55
56
 
56
57
  describe("kyselyAdapter SQLite JSON storage", () => {
58
+ it("keeps failed event inserts absent and permits retry", async () => {
59
+ const db = new PGlite();
60
+ const kysely = new Kysely<object>({ dialect: new PGliteDialect(db) });
61
+ await db.exec(DATABASE_PLUGIN_TEST_SCHEMA_SQL);
62
+ await db.exec(
63
+ "alter table bundle_events add constraint reject_event check (install_id <> 'install-703')",
64
+ );
65
+ const plugin = kyselyAdapter({ db: kysely, provider: "postgresql" });
66
+ const event = createBundleEventRowFixture("703", 100);
67
+ const input = { event };
68
+ try {
69
+ await expect(plugin.models.insights.recordEvent(input)).rejects.toThrow();
70
+ expect((await db.query("select id from bundle_events")).rows).toEqual([]);
71
+ await db.exec("alter table bundle_events drop constraint reject_event");
72
+ await plugin.models.insights.recordEvent(input);
73
+ await expect(
74
+ plugin.models.insights.findLatestEvents({
75
+ installId: event.install_id,
76
+ }),
77
+ ).resolves.toEqual([input.event]);
78
+ } finally {
79
+ await kysely.destroy();
80
+ await db.close();
81
+ }
82
+ });
83
+ it("decodes text-backed event metadata in history and latest reads", async () => {
84
+ const client = new PGlite();
85
+ const db = new Kysely<object>({ dialect: new PGliteDialect(client) });
86
+ await client.exec(
87
+ DATABASE_PLUGIN_TEST_SCHEMA_SQL.replace(
88
+ "metadata jsonb not null,",
89
+ "metadata text not null,",
90
+ ),
91
+ );
92
+ const insights = kyselyAdapter({ db, provider: "sqlite" }).models.insights;
93
+ const base = createBundleEventRowFixture("704", 100);
94
+ const event = {
95
+ ...base,
96
+ metadata: { ...base.metadata, device: { tags: [null, "ko-KR"] } },
97
+ };
98
+ try {
99
+ await insights.recordEvent({ event });
100
+ expect(
101
+ (await client.query("select metadata from bundle_events")).rows,
102
+ ).toEqual([{ metadata: JSON.stringify(event.metadata) }]);
103
+ await expect(
104
+ insights.findLatestEvents({ installId: event.install_id }),
105
+ ).resolves.toEqual([event]);
106
+ await expect(
107
+ insights.listEvents({
108
+ filter: { kind: "all" },
109
+ beforeReceivedAtMs: 101,
110
+ limit: 10,
111
+ }),
112
+ ).resolves.toEqual([event]);
113
+ } finally {
114
+ await db.destroy();
115
+ await client.close();
116
+ }
117
+ });
118
+
57
119
  it("round-trips JSON values through text columns", async () => {
58
120
  const sqliteClient = new PGlite();
59
121
  const sqliteDatabase = new Kysely<object>({
@@ -11,7 +11,7 @@ import type {
11
11
  ORMSQLProvider,
12
12
  RelationMode,
13
13
  } from "../db/types";
14
- import { createKyselyCrud } from "./kyselyCrud";
14
+ import { createKyselyCrud, recordKyselyInsights } from "./kyselyCrud";
15
15
 
16
16
  type KyselySQLProvider = Exclude<ORMSQLProvider, "mssql">;
17
17
 
@@ -31,6 +31,12 @@ const createImplementation = <TDatabase extends object>(
31
31
  const crud = createKyselyCrud(db, config.provider, relationMode);
32
32
  return {
33
33
  ...crud,
34
+ recordInsights: (input) =>
35
+ db
36
+ .transaction()
37
+ .execute((transaction) =>
38
+ recordKyselyInsights(transaction, config.provider, input),
39
+ ),
34
40
  deleteChannel: (input) =>
35
41
  db
36
42
  .transaction()
@@ -1,11 +1,15 @@
1
1
  import {
2
2
  DatabasePluginInputError,
3
- type BundleEventRow,
4
3
  type BundlePatchRow,
5
4
  type ChannelRow,
6
5
  type ApiKeyRow,
6
+ type InsightsRecordEventInput,
7
7
  type ReleaseCatalogRow,
8
8
  } from "@hot-updater/plugin-core";
9
+ import {
10
+ latestInsightsWhere,
11
+ latestInsightsCountGroups,
12
+ } from "@hot-updater/plugin-core/internal";
9
13
  import type {
10
14
  DatabasePluginImplementation,
11
15
  DatabaseWhere,
@@ -18,6 +22,11 @@ import {
18
22
  isChannelDeleteReferencedError,
19
23
  translateChannelDeleteError,
20
24
  } from "./databaseConstraintErrors";
25
+ import {
26
+ fromStoredBundleEventRow,
27
+ toStoredBundleEventRow,
28
+ type StoredBundleEventRow,
29
+ } from "./databasePluginUtils";
21
30
  import {
22
31
  fromStoredBundleRow,
23
32
  fromStoredReleaseCatalogRow,
@@ -54,6 +63,7 @@ const whereClause = (
54
63
  const orderClause = (
55
64
  input:
56
65
  | {
66
+ readonly model?: string;
57
67
  readonly orderBy?: readonly {
58
68
  readonly direction: "asc" | "desc";
59
69
  readonly field: string;
@@ -67,6 +77,11 @@ const orderClause = (
67
77
  return sql` order by ${sql.join(
68
78
  clauses.map((clause) => {
69
79
  const field = sql.ref(clause.field);
80
+ if (input?.model === "bundle_events") {
81
+ return clause.direction === "asc"
82
+ ? sql`${field} asc`
83
+ : sql`${field} desc`;
84
+ }
70
85
  const nulls =
71
86
  clause.nulls ?? (clause.direction === "asc" ? "last" : "first");
72
87
  const nullOrder =
@@ -110,6 +125,55 @@ const insertRow = async (
110
125
  return Number(result.numAffectedRows ?? 0) > 0;
111
126
  };
112
127
 
128
+ export const recordKyselyInsights = async (
129
+ executor: QueryExecutorProvider,
130
+ provider: Exclude<ORMSQLProvider, "mssql">,
131
+ { event }: InsightsRecordEventInput,
132
+ ): Promise<void> => {
133
+ const entries = Object.entries(toStoredBundleEventRow(event, provider));
134
+ const insert = sql`insert into bundle_events (${sql.join(entries.map(([field]) => sql.ref(field)))}) values (${sql.join(entries.map(([, value]) => value))})`;
135
+ await (
136
+ provider === "mysql"
137
+ ? sql`${insert} on duplicate key update id = id`
138
+ : sql`${insert} on conflict (id) do nothing`
139
+ ).execute(executor);
140
+
141
+ const fields = [
142
+ "install_id",
143
+ "user_id",
144
+ "platform",
145
+ "channel",
146
+ "type",
147
+ "from_bundle_id",
148
+ "to_bundle_id",
149
+ "id",
150
+ "received_at_ms",
151
+ ];
152
+ const columns = sql.join(fields.map((field) => sql.ref(field)));
153
+ const headInsert = sql`insert into bundle_event_heads (${columns}) select ${columns} from bundle_events where id = ${event.id}`;
154
+ const newer =
155
+ provider === "mysql"
156
+ ? sql`(values(received_at_ms) > bundle_event_heads.received_at_ms or (values(received_at_ms) = bundle_event_heads.received_at_ms and values(id) > bundle_event_heads.id))`
157
+ : sql`(excluded.received_at_ms > bundle_event_heads.received_at_ms or (excluded.received_at_ms = bundle_event_heads.received_at_ms and excluded.id > bundle_event_heads.id))`;
158
+ // MySQL evaluates assignments in order: keep the timestamp comparison intact
159
+ // until the other fields and the event ID have been replaced.
160
+ const assignments = sql.join(
161
+ fields
162
+ .filter((field) => field !== "install_id")
163
+ .map((field) => {
164
+ const column = sql.ref(field);
165
+ return provider === "mysql"
166
+ ? sql`${column} = if(${newer}, values(${column}), ${sql.ref(`bundle_event_heads.${field}`)})`
167
+ : sql`${column} = ${sql.ref(`excluded.${field}`)}`;
168
+ }),
169
+ );
170
+ await (
171
+ provider === "mysql"
172
+ ? sql`${headInsert} on duplicate key update ${assignments}`
173
+ : sql`${headInsert} on conflict (install_id) do update set ${assignments} where ${newer}`
174
+ ).execute(executor);
175
+ };
176
+
113
177
  const updateBundle = async (
114
178
  executor: QueryExecutorProvider,
115
179
  id: string,
@@ -226,7 +290,37 @@ export const createKyselyCrud = (
226
290
  provider: Exclude<ORMSQLProvider, "mssql">,
227
291
  relationMode: RelationMode = "foreign-keys",
228
292
  ): TransactionDatabasePluginImplementation &
229
- Pick<DatabasePluginImplementation, "deleteChannel" | "insertChannel"> => ({
293
+ Pick<
294
+ DatabasePluginImplementation,
295
+ | "deleteChannel"
296
+ | "insertChannel"
297
+ | "findLatestInsightsEvents"
298
+ | "countLatestInsightsEvents"
299
+ > => ({
300
+ async findLatestInsightsEvents(input) {
301
+ const where = buildKyselyWhere(provider, latestInsightsWhere(input));
302
+ const result =
303
+ await sql<StoredBundleEventRow>`SELECT canonical.* FROM (SELECT id, install_id FROM bundle_event_heads WHERE ${where} ORDER BY install_id ASC LIMIT ${"installId" in input ? 1 : input.limit}) AS head JOIN bundle_events AS canonical ON canonical.id = head.id ORDER BY head.install_id ASC`.execute(
304
+ executor,
305
+ );
306
+ return result.rows.map(fromStoredBundleEventRow);
307
+ },
308
+ async countLatestInsightsEvents(input) {
309
+ const groups = latestInsightsCountGroups(input).map(
310
+ (where) => sql`(${buildKyselyWhere(provider, where)})`,
311
+ );
312
+ // NULL from_bundle_id must not exclude a later matching group.
313
+ const query =
314
+ provider === "mysql" && groups.length === 2
315
+ ? sql<{
316
+ count: string | number;
317
+ }>`SELECT (SELECT COUNT(*) FROM bundle_event_heads WHERE ${groups[0]}) + (SELECT COUNT(*) FROM bundle_event_heads WHERE ${groups[1]} AND ${groups[0]} IS NOT TRUE) AS count`
318
+ : sql<{
319
+ count: string | number;
320
+ }>`SELECT COUNT(*) AS count FROM bundle_event_heads WHERE (${sql.join(groups, sql` OR `)})`;
321
+ const result = await query.execute(executor);
322
+ return Number(result.rows[0]?.count ?? 0);
323
+ },
230
324
  async deleteChannel({ id }) {
231
325
  const existing = await sql<ChannelRow>`select ${sql.ref("id")}, ${sql.ref(
232
326
  "name",
@@ -296,8 +390,14 @@ export const createKyselyCrud = (
296
390
  await insertRow(executor, "bundle_patches", input.data, provider);
297
391
  return input.data;
298
392
  case "bundle_events":
299
- await insertRow(executor, "bundle_events", input.data, provider);
393
+ await insertRow(
394
+ executor,
395
+ "bundle_events",
396
+ toStoredBundleEventRow(input.data, provider),
397
+ provider,
398
+ );
300
399
  return input.data;
400
+
301
401
  case "releases":
302
402
  await insertRow(
303
403
  executor,
@@ -452,6 +552,12 @@ export const createKyselyCrud = (
452
552
  throw new DatabasePluginInputError("invalid-operation");
453
553
  }
454
554
  switch (input.model) {
555
+ case "bundle_events":
556
+ return countRows(
557
+ executor,
558
+ "bundle_events",
559
+ buildKyselyWhere(provider, input.where),
560
+ );
455
561
  case "bundles":
456
562
  return countRows(
457
563
  executor,
@@ -546,11 +652,13 @@ export const createKyselyCrud = (
546
652
  case "bundle_events": {
547
653
  const where = whereClause(buildKyselyWhere(provider, input.where));
548
654
  const order = orderClause(input);
549
- const result = await sql<BundleEventRow>`select * from ${sql.table(
550
- "bundle_events",
551
- )}${where}${order}${pagination}`.execute(executor);
552
- return [...result.rows];
655
+ const result =
656
+ await sql<StoredBundleEventRow>`select * from ${sql.table(
657
+ "bundle_events",
658
+ )}${where}${order}${pagination}`.execute(executor);
659
+ return result.rows.map(fromStoredBundleEventRow);
553
660
  }
661
+
554
662
  case "api_keys": {
555
663
  const where = whereClause(buildKyselyWhere(provider, input.where));
556
664
  const order = orderClause(input);
@@ -0,0 +1,168 @@
1
+ import { DatabaseSync, type SqliteValue } from "node:sqlite";
2
+
3
+ import { PGlite } from "@electric-sql/pglite";
4
+ import { Kysely, SqliteDialect, sql } from "kysely";
5
+ import { PGliteDialect } from "kysely-pglite-dialect";
6
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
7
+
8
+ import { createBundleEventRowFixture } from "../../../test-utils/src/databaseTestFixtures";
9
+ import { createKyselyMigrator } from "../db/fixedMigrator";
10
+ import type { DatabaseAdapterWithCapabilities } from "../db/types";
11
+ import { kyselyAdapter } from "./kysely";
12
+
13
+ describe.each(["postgresql", "sqlite"] as const)(
14
+ "Kysely %s Insights heads",
15
+ (provider) => {
16
+ let db: Kysely<object>;
17
+ let postgres: PGlite | undefined;
18
+ let adapter: DatabaseAdapterWithCapabilities;
19
+
20
+ beforeEach(async () => {
21
+ if (provider === "postgresql") {
22
+ postgres = new PGlite();
23
+ db = new Kysely<object>({ dialect: new PGliteDialect(postgres) });
24
+ } else {
25
+ const sqlite = new DatabaseSync(":memory:");
26
+ db = new Kysely<object>({
27
+ dialect: new SqliteDialect({
28
+ database: {
29
+ close: () => sqlite.close(),
30
+ prepare: (query) => {
31
+ const statement = sqlite.prepare(query);
32
+ return {
33
+ reader: statement.columns().length > 0,
34
+ all: (parameters) =>
35
+ statement.all(...(parameters as SqliteValue[])),
36
+ run: (parameters) => {
37
+ const result = statement.run(
38
+ ...(parameters as SqliteValue[]),
39
+ );
40
+ return {
41
+ changes: result.changes,
42
+ lastInsertRowid: result.lastInsertRowid,
43
+ };
44
+ },
45
+ iterate: (parameters) =>
46
+ statement.iterate(...(parameters as SqliteValue[])),
47
+ };
48
+ },
49
+ },
50
+ }),
51
+ });
52
+ }
53
+ adapter = kyselyAdapter({ db, provider });
54
+ await (
55
+ await createKyselyMigrator({ db, provider }).migrateToLatest()
56
+ ).execute();
57
+ });
58
+
59
+ afterEach(async () => {
60
+ await db.destroy();
61
+ await postgres?.close();
62
+ postgres = undefined;
63
+ });
64
+
65
+ it("rolls back the canonical event when the head write fails and permits retry", async () => {
66
+ const insights = adapter.models.insights;
67
+ const previous = createBundleEventRowFixture("811", 100);
68
+ const next = {
69
+ ...createBundleEventRowFixture("812", 200),
70
+ install_id: previous.install_id,
71
+ };
72
+ await insights.recordEvent({ event: previous });
73
+ await sql`alter table bundle_event_heads rename to unavailable_heads`.execute(
74
+ db,
75
+ );
76
+
77
+ await expect(insights.recordEvent({ event: next })).rejects.toThrow();
78
+ expect(
79
+ (await sql`select id from bundle_events order by id`.execute(db)).rows,
80
+ ).toEqual([{ id: previous.id }]);
81
+
82
+ await sql`alter table unavailable_heads rename to bundle_event_heads`.execute(
83
+ db,
84
+ );
85
+ await expect(
86
+ insights.findLatestEvents({ installId: previous.install_id }),
87
+ ).resolves.toEqual([previous]);
88
+ await insights.recordEvent({ event: next });
89
+ await expect(
90
+ insights.findLatestEvents({ installId: previous.install_id }),
91
+ ).resolves.toEqual([next]);
92
+ });
93
+
94
+ it("keeps delayed and altered duplicate reports from replacing the canonical head", async () => {
95
+ const insights = adapter.models.insights;
96
+ const previous = {
97
+ ...createBundleEventRowFixture("821", 100),
98
+ user_id: "previous-user",
99
+ };
100
+ const newest = {
101
+ ...createBundleEventRowFixture("823", 200),
102
+ install_id: previous.install_id,
103
+ user_id: "current-user",
104
+ channel: "preview",
105
+ metadata: { ...previous.metadata, device: { locale: "ko-KR" } },
106
+ };
107
+ const tied = {
108
+ ...createBundleEventRowFixture("822", 200),
109
+ install_id: previous.install_id,
110
+ };
111
+ await insights.recordEvent({ event: newest });
112
+ await Promise.all([
113
+ insights.recordEvent({ event: tied }),
114
+ insights.recordEvent({ event: previous }),
115
+ ]);
116
+ await insights.recordEvent({
117
+ event: {
118
+ ...previous,
119
+ install_id: "changed-retry-installation",
120
+ user_id: "changed-retry-user",
121
+ received_at_ms: 300,
122
+ },
123
+ });
124
+
125
+ await expect(
126
+ insights.findLatestEvents({ installId: previous.install_id }),
127
+ ).resolves.toEqual([newest]);
128
+ await expect(
129
+ insights.findLatestEvents({ userId: "current-user", limit: 10 }),
130
+ ).resolves.toEqual([newest]);
131
+ await expect(
132
+ insights.findLatestEvents({ userId: "previous-user", limit: 10 }),
133
+ ).resolves.toEqual([]);
134
+ await expect(
135
+ insights.findLatestEvents({ installId: "changed-retry-installation" }),
136
+ ).resolves.toEqual([]);
137
+ await expect(
138
+ insights.countLatestEvents({
139
+ platform: "ios",
140
+ channel: "production",
141
+ sinceMs: 0,
142
+ }),
143
+ ).resolves.toBe(0);
144
+ await expect(
145
+ insights.countLatestEvents({
146
+ platform: "ios",
147
+ channel: "preview",
148
+ sinceMs: 0,
149
+ }),
150
+ ).resolves.toBe(1);
151
+ expect(
152
+ (await sql`select * from bundle_event_heads`.execute(db)).rows,
153
+ ).toEqual([
154
+ {
155
+ install_id: newest.install_id,
156
+ id: newest.id,
157
+ received_at_ms: newest.received_at_ms,
158
+ user_id: newest.user_id,
159
+ platform: newest.platform,
160
+ channel: newest.channel,
161
+ type: newest.type,
162
+ from_bundle_id: newest.from_bundle_id,
163
+ to_bundle_id: newest.to_bundle_id,
164
+ },
165
+ ]);
166
+ });
167
+ },
168
+ );