@hot-updater/server 1.0.0-rc.1 → 1.0.0-rc.2
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.
- package/dist/_virtual/_rolldown/runtime.cjs +1 -1
- package/dist/adapters/databaseConstraintErrors.cjs +0 -1
- package/dist/adapters/databasePluginUtils.cjs +0 -1
- package/dist/adapters/drizzle.cjs +10 -3
- package/dist/adapters/drizzle.d.cts +0 -1
- package/dist/adapters/drizzle.d.mts +0 -1
- package/dist/adapters/drizzle.mjs +11 -3
- package/dist/adapters/drizzleCrud.cjs +62 -1
- package/dist/adapters/drizzleCrud.mjs +61 -2
- package/dist/adapters/drizzleLazyDB.cjs +6 -1
- package/dist/adapters/drizzleLazyDB.mjs +6 -1
- package/dist/adapters/drizzleQuery.cjs +3 -2
- package/dist/adapters/drizzleQuery.mjs +3 -1
- package/dist/adapters/insightsTransaction.cjs +22 -0
- package/dist/adapters/insightsTransaction.mjs +22 -0
- package/dist/adapters/kysely.cjs +2 -1
- package/dist/adapters/kysely.d.cts +0 -1
- package/dist/adapters/kysely.d.mts +0 -1
- package/dist/adapters/kysely.mjs +3 -1
- package/dist/adapters/kyselyCrud.cjs +54 -3
- package/dist/adapters/kyselyCrud.mjs +54 -3
- package/dist/adapters/kyselyQuery.cjs +2 -2
- package/dist/adapters/kyselyQuery.mjs +2 -1
- package/dist/adapters/mongodb.cjs +27 -1
- package/dist/adapters/mongodb.d.cts +1 -1
- package/dist/adapters/mongodb.d.mts +1 -1
- package/dist/adapters/mongodb.mjs +28 -1
- package/dist/adapters/mongodbCollections.cjs +2 -0
- package/dist/adapters/mongodbCollections.mjs +2 -0
- package/dist/adapters/mongodbQuery.cjs +21 -4
- package/dist/adapters/mongodbQuery.mjs +21 -5
- package/dist/adapters/mongodbReads.cjs +91 -29
- package/dist/adapters/mongodbReads.mjs +92 -29
- package/dist/adapters/mongodbWrites.cjs +16 -2
- package/dist/adapters/mongodbWrites.mjs +18 -3
- package/dist/adapters/prisma.cjs +55 -7
- package/dist/adapters/prisma.d.cts +0 -1
- package/dist/adapters/prisma.d.mts +0 -1
- package/dist/adapters/prisma.mjs +56 -7
- package/dist/adapters/prismaQuery.cjs +0 -1
- package/dist/adapters/prismaRows.cjs +22 -1
- package/dist/adapters/prismaRows.mjs +22 -1
- package/dist/apiKeys.cjs +6 -2
- package/dist/apiKeys.d.cts +0 -1
- package/dist/apiKeys.d.mts +0 -1
- package/dist/apiKeys.mjs +3 -3
- package/dist/createHotUpdaterCore.cjs +24 -9
- package/dist/createHotUpdaterCore.d.cts +1 -1
- package/dist/createHotUpdaterCore.d.mts +1 -1
- package/dist/createHotUpdaterCore.mjs +23 -9
- package/dist/db/createBundleDiff.cjs +2 -2
- package/dist/db/createBundleDiff.d.cts +1 -5
- package/dist/db/createBundleDiff.d.mts +1 -5
- package/dist/db/databasePluginCore.cjs +0 -1
- package/dist/db/fixedMigrator.cjs +2 -2
- package/dist/db/fixedMigrator.mjs +2 -2
- package/dist/db/fixedMigratorMongo.cjs +6 -4
- package/dist/db/fixedMigratorMongo.mjs +6 -4
- package/dist/db/index.d.cts +0 -1
- package/dist/db/index.d.mts +0 -1
- package/dist/db/releaseCatalog.cjs +0 -1
- package/dist/db/releaseCatalog.d.cts +0 -1
- package/dist/db/releaseCatalog.d.mts +0 -1
- package/dist/db/schema/insightsCollation.cjs +14 -0
- package/dist/db/schema/insightsCollation.mjs +14 -0
- package/dist/db/schema/sql.cjs +11 -7
- package/dist/db/schema/sql.mjs +8 -8
- package/dist/db/schemaGenerators.cjs +28 -9
- package/dist/db/schemaGenerators.mjs +28 -9
- package/dist/db/schemaReadiness.cjs +2 -0
- package/dist/db/schemaReadiness.d.cts +1 -0
- package/dist/db/schemaReadiness.d.mts +1 -0
- package/dist/db/schemaReadiness.mjs +2 -0
- package/dist/db/sqlMigrationExecution.cjs +1 -1
- package/dist/db/sqlMigrationExecution.mjs +1 -0
- package/dist/db/types.cjs +3 -1
- package/dist/db/types.d.cts +3 -6
- package/dist/db/types.d.mts +3 -6
- package/dist/db/types.mjs +2 -2
- package/dist/db/updateArtifacts.cjs +0 -1
- package/dist/handler.d.cts +1 -2
- package/dist/handler.d.mts +1 -2
- package/dist/handlerReleaseCatalogRoutes.cjs +0 -1
- package/dist/handlerReleaseManagementRoutes.cjs +0 -1
- package/dist/handlerTypes.d.cts +0 -1
- package/dist/handlerTypes.d.mts +0 -1
- package/dist/handlerVersionRoutes.d.cts +1 -0
- package/dist/handlerVersionRoutes.d.mts +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +1 -1
- package/dist/insights/domain.d.cts +37 -69
- package/dist/insights/domain.d.mts +37 -69
- package/dist/insights/errors.cjs +1 -8
- package/dist/insights/errors.mjs +2 -8
- package/dist/insights/eventInput.cjs +49 -5
- package/dist/insights/eventInput.mjs +48 -6
- package/dist/insights/provider.cjs +314 -0
- package/dist/insights/provider.d.cts +6 -0
- package/dist/insights/provider.d.mts +6 -0
- package/dist/insights/provider.mjs +314 -0
- package/dist/insights/queryInput.cjs +78 -43
- package/dist/insights/queryInput.mjs +76 -40
- package/dist/insights/routes.cjs +20 -25
- package/dist/insights/routes.mjs +22 -27
- package/dist/insights/types.d.cts +27 -12
- package/dist/insights/types.d.mts +27 -12
- package/dist/node.d.cts +0 -1
- package/dist/node.d.mts +0 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/schema/index.cjs +17 -3
- package/dist/schema/index.mjs +4 -4
- package/dist/schema/v1_0_0.cjs +72 -30
- package/dist/schema/v1_0_0.mjs +72 -31
- package/dist/storageAccess.cjs +0 -1
- package/dist/types/index.d.cts +0 -1
- package/dist/types/index.d.mts +0 -1
- package/package.json +7 -7
- package/src/adapters/databasePluginTestDatabase.ts +14 -0
- package/src/adapters/drizzle.spec.ts +99 -0
- package/src/adapters/drizzle.ts +21 -3
- package/src/adapters/drizzleCrud.ts +154 -2
- package/src/adapters/drizzleLazyDB.ts +17 -3
- package/src/adapters/insightsTransaction.ts +32 -0
- package/src/adapters/kysely.spec.ts +29 -0
- package/src/adapters/kysely.ts +10 -1
- package/src/adapters/kyselyCrud.ts +121 -0
- package/src/adapters/mongodb.integration.spec.ts +210 -0
- package/src/adapters/mongodb.spec.ts +69 -1
- package/src/adapters/mongodb.ts +66 -2
- package/src/adapters/mongodbCollections.ts +5 -0
- package/src/adapters/mongodbQuery.ts +32 -3
- package/src/adapters/mongodbReads.ts +69 -0
- package/src/adapters/mongodbTestClient.ts +41 -6
- package/src/adapters/mongodbTestFilter.ts +4 -1
- package/src/adapters/mongodbWrites.ts +27 -0
- package/src/adapters/prisma.spec.ts +97 -0
- package/src/adapters/prisma.ts +103 -3
- package/src/adapters/prismaRows.ts +37 -1
- package/src/adapters/prismaTestClient.ts +22 -5
- package/src/apiKeys.integration.spec.ts +1 -1
- package/src/createHotUpdaterCore.ts +21 -7
- package/src/db/databasePluginUpdateCheck.bench.ts +3 -0
- package/src/db/fixedMigrator.spec.ts +65 -0
- package/src/db/fixedMigratorMongo.spec.ts +56 -9
- package/src/db/fixedMigratorMongo.ts +10 -10
- package/src/db/index.spec.ts +8 -1
- package/src/db/schema/insightsCollation.ts +21 -0
- package/src/db/schema/sql.ts +8 -5
- package/src/db/schemaGenerators.ts +41 -13
- package/src/db/types.spec.ts +8 -0
- package/src/db/types.ts +10 -4
- package/src/handler-standalone.integration.spec.ts +5 -7
- package/src/index.ts +2 -2
- package/src/insights/domain.ts +39 -75
- package/src/insights/errors.ts +0 -7
- package/src/insights/eventInput.ts +60 -3
- package/src/insights/insights.integration.spec.ts +254 -17
- package/src/insights/provider.spec.ts +385 -0
- package/src/insights/provider.ts +591 -0
- package/src/insights/queryInput.ts +112 -73
- package/src/insights/routes.ts +37 -56
- package/src/insights/types.ts +47 -36
- package/src/schema/dsl-all-versions.spec.ts +3 -1
- package/src/schema/v1_0_0.spec.ts +77 -3
- package/src/schema/v1_0_0.ts +94 -30
- package/dist/insights/bounded/activeOverview.cjs +0 -87
- package/dist/insights/bounded/activeOverview.mjs +0 -85
- package/dist/insights/bounded/installationSearch.cjs +0 -42
- package/dist/insights/bounded/installationSearch.mjs +0 -42
- package/dist/insights/bounded/persistence.cjs +0 -39
- package/dist/insights/bounded/persistence.mjs +0 -38
- package/dist/insights/bounded/provider.cjs +0 -170
- package/dist/insights/bounded/provider.d.cts +0 -7
- package/dist/insights/bounded/provider.d.mts +0 -7
- package/dist/insights/bounded/provider.mjs +0 -170
- package/dist/insights/bounded/scan.cjs +0 -144
- package/dist/insights/bounded/scan.mjs +0 -138
- package/dist/insights/persistence.d.cts +0 -6
- package/dist/insights/persistence.d.mts +0 -6
- package/src/insights/bounded/activeOverview.spec.ts +0 -189
- package/src/insights/bounded/activeOverview.ts +0 -148
- package/src/insights/bounded/installationSearch.spec.ts +0 -130
- package/src/insights/bounded/installationSearch.ts +0 -69
- package/src/insights/bounded/persistence.ts +0 -43
- package/src/insights/bounded/provider.spec.ts +0 -219
- package/src/insights/bounded/provider.ts +0 -229
- package/src/insights/bounded/scan.ts +0 -235
- package/src/insights/persistence.ts +0 -10
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import net from "node:net";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
toInsightsInstallationRow,
|
|
6
|
+
type BundleEventRow,
|
|
7
|
+
} from "@hot-updater/plugin-core";
|
|
8
|
+
import { execa } from "execa";
|
|
9
|
+
import { MongoClient } from "mongodb";
|
|
10
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
11
|
+
|
|
12
|
+
import { createBundleEventRowFixture } from "../../../test-utils/src/databaseTestFixtures";
|
|
13
|
+
import { mongoAdapter } from "./mongodb";
|
|
14
|
+
|
|
15
|
+
const availablePort = (): Promise<number> =>
|
|
16
|
+
new Promise((resolve, reject) => {
|
|
17
|
+
const server = net.createServer();
|
|
18
|
+
server.once("error", reject);
|
|
19
|
+
server.listen(0, "127.0.0.1", () => {
|
|
20
|
+
const address = server.address();
|
|
21
|
+
if (address === null || typeof address === "string") {
|
|
22
|
+
server.close();
|
|
23
|
+
reject(new Error("Failed to allocate MongoDB test port"));
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
server.close((error) => (error ? reject(error) : resolve(address.port)));
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("MongoDB native Insights transactions", () => {
|
|
31
|
+
let client: MongoClient;
|
|
32
|
+
let composeEnvironment: Record<string, string>;
|
|
33
|
+
const composeFile = path.resolve(
|
|
34
|
+
import.meta.dirname,
|
|
35
|
+
"../../../../examples-server/hono-mongodb/docker-compose.yml",
|
|
36
|
+
);
|
|
37
|
+
const compose = (args: string[]) =>
|
|
38
|
+
execa("docker", ["compose", "-f", composeFile, ...args], {
|
|
39
|
+
env: composeEnvironment,
|
|
40
|
+
});
|
|
41
|
+
const insights = () => mongoAdapter({ client }).models.insights;
|
|
42
|
+
const record = (event: BundleEventRow) =>
|
|
43
|
+
insights().record({
|
|
44
|
+
event,
|
|
45
|
+
installation: toInsightsInstallationRow(event),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
beforeAll(async () => {
|
|
49
|
+
const port = await availablePort();
|
|
50
|
+
composeEnvironment = {
|
|
51
|
+
COMPOSE_PROJECT_NAME: `hot-updater-insights-${process.pid}`,
|
|
52
|
+
HOT_UPDATER_E2E_MONGODB_PORT: String(port),
|
|
53
|
+
};
|
|
54
|
+
await compose(["up", "-d", "--wait"]);
|
|
55
|
+
client = new MongoClient(
|
|
56
|
+
`mongodb://127.0.0.1:${port}/insights_native?replicaSet=rs0&directConnection=true`,
|
|
57
|
+
);
|
|
58
|
+
await client.connect();
|
|
59
|
+
const adapter = mongoAdapter({ client });
|
|
60
|
+
await (await adapter.createMigrator!().migrateToLatest()).execute();
|
|
61
|
+
}, 120_000);
|
|
62
|
+
|
|
63
|
+
beforeEach(async () => {
|
|
64
|
+
await client.db().collection("bundle_events").deleteMany({});
|
|
65
|
+
await client.db().collection("bundle_installations").deleteMany({});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterAll(async () => {
|
|
69
|
+
await client?.close();
|
|
70
|
+
if (composeEnvironment) await compose(["down", "-v", "--remove-orphans"]);
|
|
71
|
+
}, 60_000);
|
|
72
|
+
|
|
73
|
+
it("rolls back both canonical changes after a native installation validation error", async () => {
|
|
74
|
+
const previous = {
|
|
75
|
+
...createBundleEventRowFixture("801", 100),
|
|
76
|
+
user_id: "original",
|
|
77
|
+
};
|
|
78
|
+
await record(previous);
|
|
79
|
+
await client.db().command({
|
|
80
|
+
collMod: "bundle_installations",
|
|
81
|
+
validator: { user_id: { $ne: "reject" } },
|
|
82
|
+
});
|
|
83
|
+
const failed = {
|
|
84
|
+
...createBundleEventRowFixture("802", 200),
|
|
85
|
+
install_id: previous.install_id,
|
|
86
|
+
user_id: "reject",
|
|
87
|
+
};
|
|
88
|
+
try {
|
|
89
|
+
await expect(record(failed)).rejects.toThrow();
|
|
90
|
+
expect(
|
|
91
|
+
await client.db().collection("bundle_events").countDocuments({}),
|
|
92
|
+
).toBe(1);
|
|
93
|
+
await expect(
|
|
94
|
+
insights().findInstallations({ installId: previous.install_id }),
|
|
95
|
+
).resolves.toEqual([toInsightsInstallationRow(previous)]);
|
|
96
|
+
} finally {
|
|
97
|
+
await client
|
|
98
|
+
.db()
|
|
99
|
+
.command({ collMod: "bundle_installations", validator: {} });
|
|
100
|
+
}
|
|
101
|
+
await record(failed);
|
|
102
|
+
await record(failed);
|
|
103
|
+
expect(
|
|
104
|
+
await client.db().collection("bundle_events").countDocuments({}),
|
|
105
|
+
).toBe(2);
|
|
106
|
+
await expect(
|
|
107
|
+
insights().findInstallations({ installId: previous.install_id }),
|
|
108
|
+
).resolves.toEqual([toInsightsInstallationRow(failed)]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("retries native conflicts without dropping events or letting duplicate IDs rewrite state", async () => {
|
|
112
|
+
const events = Array.from({ length: 12 }, (_, index) => ({
|
|
113
|
+
...createBundleEventRowFixture(String(810 + index), 100),
|
|
114
|
+
install_id: "concurrent-installation",
|
|
115
|
+
user_id: index === 11 ? null : "old-user",
|
|
116
|
+
}));
|
|
117
|
+
await Promise.all(events.map(record));
|
|
118
|
+
const winner = events.at(-1)!;
|
|
119
|
+
await Promise.all(Array.from({ length: 4 }, () => record(winner)));
|
|
120
|
+
const conflictingDuplicate = {
|
|
121
|
+
...events[0]!,
|
|
122
|
+
install_id: "different-installation",
|
|
123
|
+
received_at_ms: 1000,
|
|
124
|
+
};
|
|
125
|
+
await record(conflictingDuplicate);
|
|
126
|
+
expect(
|
|
127
|
+
await client.db().collection("bundle_events").countDocuments({}),
|
|
128
|
+
).toBe(12);
|
|
129
|
+
await expect(
|
|
130
|
+
insights().findInstallations({ installId: winner.install_id }),
|
|
131
|
+
).resolves.toEqual([toInsightsInstallationRow(winner)]);
|
|
132
|
+
await expect(
|
|
133
|
+
insights().findInstallations({
|
|
134
|
+
installId: conflictingDuplicate.install_id,
|
|
135
|
+
}),
|
|
136
|
+
).resolves.toEqual([]);
|
|
137
|
+
await expect(
|
|
138
|
+
insights().findInstallations({ userId: "old-user", limit: 10 }),
|
|
139
|
+
).resolves.toEqual([]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("accepts concurrent retries of a previously unseen event ID exactly once", async () => {
|
|
143
|
+
const event = createBundleEventRowFixture("825", 100);
|
|
144
|
+
await Promise.all(Array.from({ length: 12 }, () => record(event)));
|
|
145
|
+
expect(
|
|
146
|
+
await client.db().collection("bundle_events").countDocuments({}),
|
|
147
|
+
).toBe(1);
|
|
148
|
+
await expect(
|
|
149
|
+
insights().findInstallations({ installId: event.install_id }),
|
|
150
|
+
).resolves.toEqual([toInsightsInstallationRow(event)]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("uses raw recovery predicates consistently in native counts and indexed lists", async () => {
|
|
154
|
+
const applied = createBundleEventRowFixture("831", 100);
|
|
155
|
+
const recovered: BundleEventRow = {
|
|
156
|
+
...createBundleEventRowFixture("832", 200),
|
|
157
|
+
type: "RECOVERED",
|
|
158
|
+
update_strategy: "appVersion",
|
|
159
|
+
install_id: applied.install_id,
|
|
160
|
+
from_bundle_id: applied.to_bundle_id,
|
|
161
|
+
to_bundle_id: applied.from_bundle_id!,
|
|
162
|
+
};
|
|
163
|
+
await record(applied);
|
|
164
|
+
await record(recovered);
|
|
165
|
+
const filter = {
|
|
166
|
+
type: "RECOVERED" as const,
|
|
167
|
+
platform: "ios" as const,
|
|
168
|
+
channel: "production",
|
|
169
|
+
fromBundleId: applied.to_bundle_id,
|
|
170
|
+
};
|
|
171
|
+
await expect(
|
|
172
|
+
insights().countEvents({ filter, sinceMs: 200, beforeReceivedAtMs: 201 }),
|
|
173
|
+
).resolves.toBe(1);
|
|
174
|
+
await expect(
|
|
175
|
+
insights().listEvents({
|
|
176
|
+
filter: { kind: "bundle", ...filter },
|
|
177
|
+
sinceMs: 200,
|
|
178
|
+
beforeReceivedAtMs: 201,
|
|
179
|
+
limit: 10,
|
|
180
|
+
}),
|
|
181
|
+
).resolves.toEqual([recovered]);
|
|
182
|
+
await expect(
|
|
183
|
+
insights().countEvents({ filter, sinceMs: 100, beforeReceivedAtMs: 200 }),
|
|
184
|
+
).resolves.toBe(0);
|
|
185
|
+
await expect(
|
|
186
|
+
insights().countInstallations({
|
|
187
|
+
platform: "ios",
|
|
188
|
+
channel: "production",
|
|
189
|
+
bundleId: applied.to_bundle_id,
|
|
190
|
+
sinceMs: 0,
|
|
191
|
+
}),
|
|
192
|
+
).resolves.toBe(0);
|
|
193
|
+
const explanation = await client
|
|
194
|
+
.db()
|
|
195
|
+
.collection("bundle_events")
|
|
196
|
+
.find({
|
|
197
|
+
type: "RECOVERED",
|
|
198
|
+
platform: "ios",
|
|
199
|
+
channel: "production",
|
|
200
|
+
from_bundle_id: applied.to_bundle_id,
|
|
201
|
+
received_at_ms: { $gte: 200, $lt: 201 },
|
|
202
|
+
})
|
|
203
|
+
.collation({ locale: "simple" })
|
|
204
|
+
.sort({ received_at_ms: -1, id: -1 })
|
|
205
|
+
.hint("bundle_events_from_bundle_idx")
|
|
206
|
+
.explain("executionStats");
|
|
207
|
+
expect(explanation.executionStats.nReturned).toBe(1);
|
|
208
|
+
expect(explanation.executionStats.totalKeysExamined).toBeLessThanOrEqual(2);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createDatabaseClient,
|
|
3
|
+
toInsightsInstallationRow,
|
|
4
|
+
} from "@hot-updater/plugin-core";
|
|
2
5
|
import { describe, expect, it } from "vitest";
|
|
3
6
|
|
|
4
7
|
import {
|
|
5
8
|
createBundlePatchRowFixture,
|
|
6
9
|
createBundleRowFixture,
|
|
10
|
+
createBundleEventRowFixture,
|
|
7
11
|
} from "../../../test-utils/src/databaseTestFixtures";
|
|
8
12
|
import { setupDatabasePluginTestSuite } from "../../../test-utils/src/setupDatabasePluginTestSuite";
|
|
9
13
|
import { mongoAdapter } from "./mongodb";
|
|
@@ -22,6 +26,70 @@ setupDatabasePluginTestSuite({
|
|
|
22
26
|
});
|
|
23
27
|
|
|
24
28
|
describe("mongoAdapter capabilities", () => {
|
|
29
|
+
it("records Insights atomically even without legacy catalog transactions enabled", async () => {
|
|
30
|
+
harness.reset();
|
|
31
|
+
const insights = mongoAdapter({ client: harness.client }).models.insights;
|
|
32
|
+
const event = createBundleEventRowFixture("981", 100);
|
|
33
|
+
const input = { event, installation: toInsightsInstallationRow(event) };
|
|
34
|
+
harness.failNextInstallationWrite();
|
|
35
|
+
await expect(insights.record(input)).rejects.toThrow(
|
|
36
|
+
"injected installation write failure",
|
|
37
|
+
);
|
|
38
|
+
await expect(
|
|
39
|
+
insights.listEvents({
|
|
40
|
+
filter: { kind: "all" },
|
|
41
|
+
beforeReceivedAtMs: 200,
|
|
42
|
+
limit: 10,
|
|
43
|
+
}),
|
|
44
|
+
).resolves.toEqual([]);
|
|
45
|
+
await expect(
|
|
46
|
+
insights.findInstallations({ installId: event.install_id }),
|
|
47
|
+
).resolves.toEqual([]);
|
|
48
|
+
await insights.record(input);
|
|
49
|
+
await insights.record(input);
|
|
50
|
+
await expect(
|
|
51
|
+
insights.listEvents({
|
|
52
|
+
filter: { kind: "all" },
|
|
53
|
+
beforeReceivedAtMs: 200,
|
|
54
|
+
limit: 10,
|
|
55
|
+
}),
|
|
56
|
+
).resolves.toEqual([event]);
|
|
57
|
+
await expect(
|
|
58
|
+
insights.findInstallations({ installId: event.install_id }),
|
|
59
|
+
).resolves.toEqual([input.installation]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("preserves all concurrent events while advancing one installation and clearing its user", async () => {
|
|
63
|
+
harness.reset();
|
|
64
|
+
const insights = mongoAdapter({ client: harness.client }).models.insights;
|
|
65
|
+
const events = ["990", "993", "991", "992"].map((suffix) => ({
|
|
66
|
+
...createBundleEventRowFixture(suffix, 100),
|
|
67
|
+
install_id: "concurrent-installation",
|
|
68
|
+
user_id: suffix === "993" ? null : "previous-user",
|
|
69
|
+
}));
|
|
70
|
+
await Promise.all(
|
|
71
|
+
events.map((event) =>
|
|
72
|
+
insights.record({
|
|
73
|
+
event,
|
|
74
|
+
installation: toInsightsInstallationRow(event),
|
|
75
|
+
}),
|
|
76
|
+
),
|
|
77
|
+
);
|
|
78
|
+
await expect(
|
|
79
|
+
insights.findInstallations({ installId: "concurrent-installation" }),
|
|
80
|
+
).resolves.toEqual([toInsightsInstallationRow(events[1]!)]);
|
|
81
|
+
await expect(
|
|
82
|
+
insights.findInstallations({ userId: "previous-user", limit: 10 }),
|
|
83
|
+
).resolves.toEqual([]);
|
|
84
|
+
await expect(
|
|
85
|
+
insights.listEvents({
|
|
86
|
+
filter: { kind: "all" },
|
|
87
|
+
beforeReceivedAtMs: 200,
|
|
88
|
+
limit: 10,
|
|
89
|
+
}),
|
|
90
|
+
).resolves.toHaveLength(4);
|
|
91
|
+
});
|
|
92
|
+
|
|
25
93
|
it("returns an adapter without an unsafe atomic-batch fallback", () => {
|
|
26
94
|
const plugin = mongoAdapter({ client: harness.client });
|
|
27
95
|
expect(plugin.name).toBe("mongodb");
|
package/src/adapters/mongodb.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
compareInsightsText,
|
|
3
|
+
createDatabasePlugin,
|
|
4
|
+
} from "@hot-updater/plugin-core";
|
|
2
5
|
import {
|
|
3
6
|
createDatabasePluginAdapter,
|
|
4
7
|
type DatabasePluginImplementation,
|
|
5
8
|
type TransactionDatabasePluginImplementation,
|
|
6
9
|
} from "@hot-updater/plugin-core/internal";
|
|
7
|
-
import
|
|
10
|
+
import {
|
|
11
|
+
MongoServerError,
|
|
12
|
+
type ClientSession,
|
|
13
|
+
type MongoClient,
|
|
14
|
+
} from "mongodb";
|
|
8
15
|
|
|
9
16
|
import { createMongoMigrator } from "../db/fixedMigrator";
|
|
10
17
|
import type { DatabaseAdapterWithCapabilities } from "../db/types";
|
|
@@ -14,6 +21,7 @@ import { createMongoWrites } from "./mongodbWrites";
|
|
|
14
21
|
|
|
15
22
|
export interface MongoDBConfig {
|
|
16
23
|
readonly client: MongoClient;
|
|
24
|
+
/** Enables atomic catalog commits. Insights always requires MongoDB 5+ on a replica set or sharded cluster. */
|
|
17
25
|
readonly transactions?: boolean;
|
|
18
26
|
}
|
|
19
27
|
|
|
@@ -23,6 +31,62 @@ const createMongoImplementation = (
|
|
|
23
31
|
): DatabasePluginImplementation => {
|
|
24
32
|
const collections = createMongoCollections(client);
|
|
25
33
|
return {
|
|
34
|
+
recordInsights: async (input) => {
|
|
35
|
+
const record = () =>
|
|
36
|
+
client.withSession((recordSession) =>
|
|
37
|
+
recordSession.withTransaction(
|
|
38
|
+
async () => {
|
|
39
|
+
const records = createMongoCollections(client);
|
|
40
|
+
const options = {
|
|
41
|
+
session: recordSession,
|
|
42
|
+
collation: { locale: "simple" },
|
|
43
|
+
};
|
|
44
|
+
const inserted = await records.bundleEvents.updateOne(
|
|
45
|
+
{ id: input.event.id },
|
|
46
|
+
{ $setOnInsert: input.event },
|
|
47
|
+
{ ...options, upsert: true },
|
|
48
|
+
);
|
|
49
|
+
if (inserted.upsertedCount === 0) return;
|
|
50
|
+
const current = await records.bundleInstallations.findOne(
|
|
51
|
+
{ install_id: input.installation.install_id },
|
|
52
|
+
options,
|
|
53
|
+
);
|
|
54
|
+
if (current === null) {
|
|
55
|
+
await records.bundleInstallations.insertOne(
|
|
56
|
+
{ ...input.installation },
|
|
57
|
+
{
|
|
58
|
+
session: recordSession,
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
} else if (
|
|
62
|
+
input.installation.received_at_ms > current.received_at_ms ||
|
|
63
|
+
(input.installation.received_at_ms === current.received_at_ms &&
|
|
64
|
+
compareInsightsText(input.installation.id, current.id) > 0)
|
|
65
|
+
) {
|
|
66
|
+
await records.bundleInstallations.updateOne(
|
|
67
|
+
{ install_id: input.installation.install_id },
|
|
68
|
+
{ $set: input.installation },
|
|
69
|
+
options,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
readConcern: { level: "snapshot" },
|
|
75
|
+
readPreference: "primary",
|
|
76
|
+
writeConcern: { w: "majority" },
|
|
77
|
+
},
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
try {
|
|
81
|
+
await record();
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (!(error instanceof MongoServerError) || error.code !== 11000)
|
|
84
|
+
throw error;
|
|
85
|
+
// A concurrent insert won a canonical key. Read that committed winner
|
|
86
|
+
// in a new transaction; some MongoDB versions do not retry E11000.
|
|
87
|
+
await record();
|
|
88
|
+
}
|
|
89
|
+
},
|
|
26
90
|
...createMongoWrites(collections, session),
|
|
27
91
|
...createMongoReads(collections, session),
|
|
28
92
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BundleEventRow,
|
|
3
|
+
InsightsInstallationRow,
|
|
3
4
|
BundlePatchRow,
|
|
4
5
|
BundleRow,
|
|
5
6
|
ChannelRow,
|
|
@@ -37,6 +38,7 @@ export type MongoCollections = {
|
|
|
37
38
|
readonly bundles: Collection<MongoBundleDocument>;
|
|
38
39
|
readonly bundlePatches: Collection<BundlePatchRow>;
|
|
39
40
|
readonly bundleEvents: Collection<BundleEventRow>;
|
|
41
|
+
readonly bundleInstallations: Collection<InsightsInstallationRow>;
|
|
40
42
|
readonly channels: Collection<ChannelRow>;
|
|
41
43
|
readonly apiKeys: Collection<ApiKeyRow>;
|
|
42
44
|
readonly releases: Collection<ReleaseRow>;
|
|
@@ -51,6 +53,9 @@ export const createMongoCollections = (
|
|
|
51
53
|
bundles: database.collection<MongoBundleDocument>("bundles"),
|
|
52
54
|
bundlePatches: database.collection<BundlePatchRow>("bundle_patches"),
|
|
53
55
|
bundleEvents: database.collection<BundleEventRow>("bundle_events"),
|
|
56
|
+
bundleInstallations: database.collection<InsightsInstallationRow>(
|
|
57
|
+
"bundle_installations",
|
|
58
|
+
),
|
|
54
59
|
channels: database.collection<ChannelRow>("channels"),
|
|
55
60
|
apiKeys: database.collection<ApiKeyRow>("api_keys"),
|
|
56
61
|
releases: database.collection<ReleaseRow>("releases"),
|
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
BundlePatchRow,
|
|
5
5
|
BundleRow,
|
|
6
6
|
ChannelRow,
|
|
7
|
+
InsightsInstallationRow,
|
|
7
8
|
ReleaseCatalogRow,
|
|
8
9
|
ReleaseRow,
|
|
9
10
|
} from "@hot-updater/plugin-core";
|
|
@@ -121,20 +122,39 @@ const predicate = (where: AnyDatabaseWhere): Document => {
|
|
|
121
122
|
|
|
122
123
|
const createMongoWhereDocument = (
|
|
123
124
|
where: readonly AnyDatabaseWhere[] | undefined,
|
|
125
|
+
toPredicate: (condition: AnyDatabaseWhere) => Document = predicate,
|
|
124
126
|
): Document => {
|
|
125
127
|
const items = Array.isArray(where) ? where : [];
|
|
126
128
|
const first = items[0];
|
|
127
129
|
if (first === undefined) return {};
|
|
128
130
|
|
|
129
|
-
let result =
|
|
131
|
+
let result = toPredicate(first);
|
|
130
132
|
for (const item of items.slice(1)) {
|
|
131
133
|
result = {
|
|
132
|
-
[item.connector === "OR" ? "$or" : "$and"]: [result,
|
|
134
|
+
[item.connector === "OR" ? "$or" : "$and"]: [result, toPredicate(item)],
|
|
133
135
|
};
|
|
134
136
|
}
|
|
135
137
|
return result;
|
|
136
138
|
};
|
|
137
139
|
|
|
140
|
+
// Fixed Insights predicates use native fields so equality/range indexes apply.
|
|
141
|
+
const insightsPredicate = (where: AnyDatabaseWhere): Document => {
|
|
142
|
+
if ("mode" in where && where.mode === "insensitive") return predicate(where);
|
|
143
|
+
switch (where.operator) {
|
|
144
|
+
case undefined:
|
|
145
|
+
case "eq":
|
|
146
|
+
return { [where.field]: { $eq: where.value } };
|
|
147
|
+
case "gt":
|
|
148
|
+
case "gte":
|
|
149
|
+
case "lt":
|
|
150
|
+
case "lte":
|
|
151
|
+
case "in":
|
|
152
|
+
return { [where.field]: { [`$${where.operator}`]: where.value } };
|
|
153
|
+
default:
|
|
154
|
+
return predicate(where);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
138
158
|
export function createMongoBundleWhere(
|
|
139
159
|
where: readonly DatabaseWhere<"bundles">[] | undefined,
|
|
140
160
|
): Filter<BundleRow>;
|
|
@@ -159,7 +179,16 @@ export function createMongoEventWhere(
|
|
|
159
179
|
export function createMongoEventWhere(
|
|
160
180
|
where: readonly DatabaseWhere<"bundle_events">[] | undefined,
|
|
161
181
|
): Document {
|
|
162
|
-
return createMongoWhereDocument(where);
|
|
182
|
+
return createMongoWhereDocument(where, insightsPredicate);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function createMongoInstallationWhere(
|
|
186
|
+
where: readonly DatabaseWhere<"bundle_installations">[] | undefined,
|
|
187
|
+
): Filter<InsightsInstallationRow>;
|
|
188
|
+
export function createMongoInstallationWhere(
|
|
189
|
+
where: readonly DatabaseWhere<"bundle_installations">[] | undefined,
|
|
190
|
+
): Document {
|
|
191
|
+
return createMongoWhereDocument(where, insightsPredicate);
|
|
163
192
|
}
|
|
164
193
|
|
|
165
194
|
export function createMongoApiKeyWhere(
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
createMongoChannelWhere,
|
|
20
20
|
createMongoApiKeyWhere,
|
|
21
21
|
createMongoEventWhere,
|
|
22
|
+
createMongoInstallationWhere,
|
|
22
23
|
createMongoPatchWhere,
|
|
23
24
|
createMongoReleaseCatalogWhere,
|
|
24
25
|
createMongoReleaseWhere,
|
|
@@ -91,6 +92,8 @@ const findMongoRows = async (
|
|
|
91
92
|
.find(createMongoEventWhere(input.where), {
|
|
92
93
|
projection: WITHOUT_MONGO_ID,
|
|
93
94
|
...mongoSessionOptions(session),
|
|
95
|
+
collation: { locale: "simple" },
|
|
96
|
+
readPreference: "primary",
|
|
94
97
|
})
|
|
95
98
|
.skip(input.offset)
|
|
96
99
|
.limit(input.limit);
|
|
@@ -100,6 +103,38 @@ const findMongoRows = async (
|
|
|
100
103
|
.find(createMongoEventWhere(input.where), {
|
|
101
104
|
projection: WITHOUT_MONGO_ID,
|
|
102
105
|
...mongoSessionOptions(session),
|
|
106
|
+
collation: { locale: "simple" },
|
|
107
|
+
readPreference: "primary",
|
|
108
|
+
})
|
|
109
|
+
.toArray();
|
|
110
|
+
return sortRowsByOrder(rows, rawOrderBy).slice(
|
|
111
|
+
input.offset,
|
|
112
|
+
input.offset + input.limit,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const sort = createMongoSort(input);
|
|
116
|
+
return sort === undefined
|
|
117
|
+
? cursor.toArray()
|
|
118
|
+
: cursor.sort(sort).toArray();
|
|
119
|
+
}
|
|
120
|
+
case "bundle_installations": {
|
|
121
|
+
const cursor = collections.bundleInstallations
|
|
122
|
+
.find(createMongoInstallationWhere(input.where), {
|
|
123
|
+
projection: WITHOUT_MONGO_ID,
|
|
124
|
+
...mongoSessionOptions(session),
|
|
125
|
+
collation: { locale: "simple" },
|
|
126
|
+
readPreference: "primary",
|
|
127
|
+
})
|
|
128
|
+
.skip(input.offset)
|
|
129
|
+
.limit(input.limit);
|
|
130
|
+
if (rawOrderBy === undefined) return cursor.toArray();
|
|
131
|
+
if (needsInMemoryOrder) {
|
|
132
|
+
const rows = await collections.bundleInstallations
|
|
133
|
+
.find(createMongoInstallationWhere(input.where), {
|
|
134
|
+
projection: WITHOUT_MONGO_ID,
|
|
135
|
+
...mongoSessionOptions(session),
|
|
136
|
+
collation: { locale: "simple" },
|
|
137
|
+
readPreference: "primary",
|
|
103
138
|
})
|
|
104
139
|
.toArray();
|
|
105
140
|
return sortRowsByOrder(rows, rawOrderBy).slice(
|
|
@@ -248,6 +283,30 @@ export const createMongoReads = (
|
|
|
248
283
|
createMongoReleaseWhere(input.where),
|
|
249
284
|
mongoSessionOptions(session),
|
|
250
285
|
);
|
|
286
|
+
case "bundle_installations":
|
|
287
|
+
return collections.bundleInstallations.countDocuments(
|
|
288
|
+
createMongoInstallationWhere(input.where),
|
|
289
|
+
{
|
|
290
|
+
...mongoSessionOptions(session),
|
|
291
|
+
collation: { locale: "simple" },
|
|
292
|
+
readPreference: "primary",
|
|
293
|
+
...(session === undefined
|
|
294
|
+
? { readConcern: { level: "snapshot" } }
|
|
295
|
+
: {}),
|
|
296
|
+
},
|
|
297
|
+
);
|
|
298
|
+
case "bundle_events":
|
|
299
|
+
return collections.bundleEvents.countDocuments(
|
|
300
|
+
createMongoEventWhere(input.where),
|
|
301
|
+
{
|
|
302
|
+
...mongoSessionOptions(session),
|
|
303
|
+
collation: { locale: "simple" },
|
|
304
|
+
readPreference: "primary",
|
|
305
|
+
...(session === undefined
|
|
306
|
+
? { readConcern: { level: "snapshot" } }
|
|
307
|
+
: {}),
|
|
308
|
+
},
|
|
309
|
+
);
|
|
251
310
|
}
|
|
252
311
|
},
|
|
253
312
|
findOne: async (input) => {
|
|
@@ -300,6 +359,16 @@ export const createMongoReads = (
|
|
|
300
359
|
...mongoSessionOptions(session),
|
|
301
360
|
},
|
|
302
361
|
);
|
|
362
|
+
case "bundle_installations":
|
|
363
|
+
return collections.bundleInstallations.findOne(
|
|
364
|
+
createMongoInstallationWhere(input.where),
|
|
365
|
+
{
|
|
366
|
+
projection: WITHOUT_MONGO_ID,
|
|
367
|
+
...mongoSessionOptions(session),
|
|
368
|
+
collation: { locale: "simple" },
|
|
369
|
+
readPreference: "primary",
|
|
370
|
+
},
|
|
371
|
+
);
|
|
303
372
|
}
|
|
304
373
|
},
|
|
305
374
|
findMany: (input) => {
|