@hot-updater/server 1.0.0-rc.2 → 1.0.0-rc.21
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/LICENSE +12 -14
- package/dist/adapters/drizzle.cjs +32 -75
- package/dist/adapters/drizzle.d.cts +13 -5
- package/dist/adapters/drizzle.d.mts +13 -5
- package/dist/adapters/drizzle.mjs +32 -77
- package/dist/adapters/drizzleExecutor.cjs +114 -0
- package/dist/adapters/drizzleExecutor.d.cts +9 -0
- package/dist/adapters/drizzleExecutor.d.mts +9 -0
- package/dist/adapters/drizzleExecutor.mjs +112 -0
- package/dist/adapters/kysely.cjs +22 -31
- package/dist/adapters/kysely.d.cts +10 -6
- package/dist/adapters/kysely.d.mts +10 -6
- package/dist/adapters/kysely.mjs +23 -33
- package/dist/adapters/kyselyExecutor.cjs +32 -0
- package/dist/adapters/kyselyExecutor.d.cts +11 -0
- package/dist/adapters/kyselyExecutor.d.mts +11 -0
- package/dist/adapters/kyselyExecutor.mjs +32 -0
- package/dist/adapters/mongodb.cjs +18 -54
- package/dist/adapters/mongodb.d.cts +10 -5
- package/dist/adapters/mongodb.d.mts +10 -5
- package/dist/adapters/mongodb.mjs +19 -56
- package/dist/adapters/mongodbAdapter.cjs +192 -0
- package/dist/adapters/mongodbAdapter.d.cts +9 -0
- package/dist/adapters/mongodbAdapter.d.mts +9 -0
- package/dist/adapters/mongodbAdapter.mjs +191 -0
- package/dist/adapters/prisma.cjs +35 -267
- package/dist/adapters/prisma.d.cts +12 -7
- package/dist/adapters/prisma.d.mts +12 -7
- package/dist/adapters/prisma.mjs +36 -268
- package/dist/adapters/prismaExecutor.cjs +63 -0
- package/dist/adapters/prismaExecutor.mjs +63 -0
- package/dist/adapters/sqlProviders.cjs +14 -0
- package/dist/adapters/sqlProviders.mjs +14 -0
- package/dist/assembly/assemblePlugins.cjs +144 -0
- package/dist/assembly/assemblePlugins.d.cts +5 -0
- package/dist/assembly/assemblePlugins.d.mts +5 -0
- package/dist/assembly/assemblePlugins.mjs +139 -0
- package/dist/assembly/clientPlugins.cjs +88 -0
- package/dist/assembly/clientPlugins.mjs +88 -0
- package/dist/assembly/configError.d.cts +2 -0
- package/dist/assembly/configError.d.mts +2 -0
- package/dist/assembly/configError.mjs +2 -0
- package/dist/core/api.cjs +51 -0
- package/dist/core/api.d.cts +54 -0
- package/dist/core/api.d.mts +54 -0
- package/dist/core/api.mjs +51 -0
- package/dist/core/baseCandidates.cjs +28 -0
- package/dist/core/baseCandidates.mjs +28 -0
- package/dist/core/operations.cjs +351 -0
- package/dist/core/operations.mjs +351 -0
- package/dist/core/reads.cjs +273 -0
- package/dist/core/reads.d.cts +9 -0
- package/dist/core/reads.d.mts +9 -0
- package/dist/core/reads.mjs +267 -0
- package/dist/core/releases.cjs +193 -0
- package/dist/core/releases.mjs +190 -0
- package/dist/core/writes.cjs +96 -0
- package/dist/core/writes.mjs +91 -0
- package/dist/createHotUpdaterCore.cjs +72 -91
- package/dist/createHotUpdaterCore.d.cts +106 -38
- package/dist/createHotUpdaterCore.d.mts +106 -38
- package/dist/createHotUpdaterCore.mjs +74 -89
- package/dist/db/engineDrizzleSchema.cjs +53 -0
- package/dist/db/engineDrizzleSchema.mjs +53 -0
- package/dist/db/enginePrismaSchema.cjs +84 -0
- package/dist/db/enginePrismaSchema.d.cts +5 -0
- package/dist/db/enginePrismaSchema.d.mts +5 -0
- package/dist/db/enginePrismaSchema.mjs +83 -0
- package/dist/db/releaseCatalog.cjs +24 -47
- package/dist/db/releaseCatalog.mjs +20 -44
- package/dist/db/updateArtifacts.cjs +43 -72
- package/dist/db/updateArtifacts.mjs +40 -69
- package/dist/handler.cjs +59 -43
- package/dist/handler.d.cts +3 -5
- package/dist/handler.d.mts +3 -5
- package/dist/handler.mjs +61 -43
- package/dist/handlerAdminRoutes.cjs +374 -0
- package/dist/handlerAdminRoutes.mjs +372 -0
- package/dist/handlerParameters.cjs +6 -11
- package/dist/handlerParameters.mjs +6 -10
- package/dist/handlerReleaseCatalogRoutes.cjs +32 -15
- package/dist/handlerReleaseCatalogRoutes.mjs +32 -15
- package/dist/handlerTypes.d.cts +2 -55
- package/dist/handlerTypes.d.mts +2 -55
- package/dist/handlerVersionRoutes.cjs +20 -4
- package/dist/handlerVersionRoutes.mjs +20 -4
- package/dist/index.cjs +9 -9
- package/dist/index.d.cts +8 -9
- package/dist/index.d.mts +8 -9
- package/dist/index.mjs +3 -4
- package/dist/node.cjs +3 -6
- package/dist/node.d.cts +2 -2
- package/dist/node.d.mts +2 -2
- package/dist/node.mjs +4 -4
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/plugins/api-keys/index.cjs +9 -0
- package/dist/plugins/api-keys/index.d.cts +1 -0
- package/dist/plugins/api-keys/index.d.mts +1 -0
- package/dist/plugins/api-keys/index.mjs +2 -0
- package/dist/plugins/definePlugin.d.cts +2 -0
- package/dist/plugins/definePlugin.d.mts +2 -0
- package/dist/plugins/insights/index.cjs +9 -0
- package/dist/plugins/insights/index.d.cts +1 -0
- package/dist/plugins/insights/index.d.mts +1 -0
- package/dist/plugins/insights/index.mjs +2 -0
- package/dist/plugins/official.cjs +30 -0
- package/dist/plugins/official.mjs +29 -0
- package/dist/storageAccess.cjs +13 -10
- package/dist/storageAccess.mjs +13 -10
- package/dist/types/index.d.cts +2 -28
- package/dist/types/index.d.mts +2 -28
- package/package.json +26 -14
- package/dist/_virtual/_rolldown/runtime.cjs +0 -23
- package/dist/adapters/databaseConstraintErrors.cjs +0 -22
- package/dist/adapters/databaseConstraintErrors.mjs +0 -21
- package/dist/adapters/databasePluginUtils.cjs +0 -116
- package/dist/adapters/databasePluginUtils.mjs +0 -106
- package/dist/adapters/drizzleCrud.cjs +0 -311
- package/dist/adapters/drizzleCrud.mjs +0 -308
- package/dist/adapters/drizzleLazyDB.cjs +0 -106
- package/dist/adapters/drizzleLazyDB.mjs +0 -106
- package/dist/adapters/drizzleQuery.cjs +0 -69
- package/dist/adapters/drizzleQuery.mjs +0 -69
- package/dist/adapters/insightsTransaction.cjs +0 -22
- package/dist/adapters/insightsTransaction.mjs +0 -22
- package/dist/adapters/kyselyCrud.cjs +0 -305
- package/dist/adapters/kyselyCrud.mjs +0 -304
- package/dist/adapters/kyselyQuery.cjs +0 -56
- package/dist/adapters/kyselyQuery.mjs +0 -56
- package/dist/adapters/mongodbCollections.cjs +0 -38
- package/dist/adapters/mongodbCollections.mjs +0 -32
- package/dist/adapters/mongodbQuery.cjs +0 -94
- package/dist/adapters/mongodbQuery.mjs +0 -86
- package/dist/adapters/mongodbReads.cjs +0 -210
- package/dist/adapters/mongodbReads.mjs +0 -210
- package/dist/adapters/mongodbWrites.cjs +0 -148
- package/dist/adapters/mongodbWrites.mjs +0 -148
- package/dist/adapters/prismaQuery.cjs +0 -57
- package/dist/adapters/prismaQuery.mjs +0 -56
- package/dist/adapters/prismaRows.cjs +0 -219
- package/dist/adapters/prismaRows.mjs +0 -209
- package/dist/apiKeys.cjs +0 -143
- package/dist/apiKeys.d.cts +0 -49
- package/dist/apiKeys.d.mts +0 -49
- package/dist/apiKeys.mjs +0 -133
- package/dist/db/createBundleDiff.cjs +0 -144
- package/dist/db/createBundleDiff.d.cts +0 -16
- package/dist/db/createBundleDiff.d.mts +0 -16
- package/dist/db/createBundleDiff.mjs +0 -141
- package/dist/db/databasePluginCore.cjs +0 -137
- package/dist/db/databasePluginCore.mjs +0 -137
- package/dist/db/fixedMigrator.cjs +0 -79
- package/dist/db/fixedMigrator.mjs +0 -78
- package/dist/db/fixedMigratorMongo.cjs +0 -321
- package/dist/db/fixedMigratorMongo.mjs +0 -321
- package/dist/db/fixedMigratorShared.cjs +0 -23
- package/dist/db/fixedMigratorShared.mjs +0 -18
- package/dist/db/index.cjs +0 -27
- package/dist/db/index.d.cts +0 -12
- package/dist/db/index.d.mts +0 -12
- package/dist/db/index.mjs +0 -22
- package/dist/db/mongoMigrationExecution.cjs +0 -11
- package/dist/db/mongoMigrationExecution.mjs +0 -11
- package/dist/db/releaseCatalog.d.cts +0 -16
- package/dist/db/releaseCatalog.d.mts +0 -16
- package/dist/db/schema/insightsCollation.cjs +0 -14
- package/dist/db/schema/insightsCollation.mjs +0 -14
- package/dist/db/schema/mongodb.cjs +0 -25
- package/dist/db/schema/mongodb.mjs +0 -25
- package/dist/db/schema/registry.cjs +0 -21
- package/dist/db/schema/registry.mjs +0 -17
- package/dist/db/schema/sql.cjs +0 -105
- package/dist/db/schema/sql.mjs +0 -97
- package/dist/db/schema/sqlOperations.cjs +0 -27
- package/dist/db/schema/sqlOperations.mjs +0 -26
- package/dist/db/schemaGenerators.cjs +0 -275
- package/dist/db/schemaGenerators.mjs +0 -273
- package/dist/db/schemaReadiness.cjs +0 -26
- package/dist/db/schemaReadiness.d.cts +0 -9
- package/dist/db/schemaReadiness.d.mts +0 -9
- package/dist/db/schemaReadiness.mjs +0 -25
- package/dist/db/sqlMigrationExecution.cjs +0 -67
- package/dist/db/sqlMigrationExecution.mjs +0 -67
- package/dist/db/types.cjs +0 -22
- package/dist/db/types.d.cts +0 -118
- package/dist/db/types.d.mts +0 -118
- package/dist/db/types.mjs +0 -19
- package/dist/handlerBundleRoutes.cjs +0 -155
- package/dist/handlerBundleRoutes.mjs +0 -155
- package/dist/handlerReleaseManagementRoutes.cjs +0 -124
- package/dist/handlerReleaseManagementRoutes.mjs +0 -124
- package/dist/insights/domain.d.cts +0 -89
- package/dist/insights/domain.d.mts +0 -89
- package/dist/insights/errors.cjs +0 -15
- package/dist/insights/errors.mjs +0 -14
- package/dist/insights/eventInput.cjs +0 -155
- package/dist/insights/eventInput.mjs +0 -153
- package/dist/insights/provider.cjs +0 -314
- package/dist/insights/provider.d.cts +0 -6
- package/dist/insights/provider.d.mts +0 -6
- package/dist/insights/provider.mjs +0 -314
- package/dist/insights/queryInput.cjs +0 -93
- package/dist/insights/queryInput.mjs +0 -91
- package/dist/insights/routes.cjs +0 -58
- package/dist/insights/routes.mjs +0 -56
- package/dist/insights/types.d.cts +0 -32
- package/dist/insights/types.d.mts +0 -32
- package/dist/schema/dsl.cjs +0 -103
- package/dist/schema/dsl.mjs +0 -87
- package/dist/schema/index.cjs +0 -20
- package/dist/schema/index.mjs +0 -7
- package/dist/schema/settings.cjs +0 -9
- package/dist/schema/settings.mjs +0 -9
- package/dist/schema/types.cjs +0 -8
- package/dist/schema/types.mjs +0 -6
- package/dist/schema/v1_0_0.cjs +0 -406
- package/dist/schema/v1_0_0.mjs +0 -398
- package/src/adapters/databaseConstraintErrors.ts +0 -25
- package/src/adapters/databasePluginTestDatabase.ts +0 -120
- package/src/adapters/databasePluginUtils.spec.ts +0 -29
- package/src/adapters/databasePluginUtils.ts +0 -214
- package/src/adapters/drizzle.spec.ts +0 -336
- package/src/adapters/drizzle.ts +0 -162
- package/src/adapters/drizzleCrud.ts +0 -571
- package/src/adapters/drizzleLazyDB.ts +0 -271
- package/src/adapters/drizzleQuery.ts +0 -164
- package/src/adapters/insightsTransaction.ts +0 -32
- package/src/adapters/kysely.spec.ts +0 -174
- package/src/adapters/kysely.ts +0 -110
- package/src/adapters/kyselyCrud.ts +0 -709
- package/src/adapters/kyselyQuery.ts +0 -128
- package/src/adapters/mongodb.integration.spec.ts +0 -210
- package/src/adapters/mongodb.spec.ts +0 -209
- package/src/adapters/mongodb.ts +0 -138
- package/src/adapters/mongodbCollections.ts +0 -67
- package/src/adapters/mongodbQuery.ts +0 -255
- package/src/adapters/mongodbReads.ts +0 -380
- package/src/adapters/mongodbRows.spec.ts +0 -64
- package/src/adapters/mongodbRows.ts +0 -118
- package/src/adapters/mongodbTestClient.ts +0 -377
- package/src/adapters/mongodbTestFilter.ts +0 -169
- package/src/adapters/mongodbWrites.ts +0 -287
- package/src/adapters/prisma.spec.ts +0 -293
- package/src/adapters/prisma.ts +0 -565
- package/src/adapters/prismaQuery.ts +0 -129
- package/src/adapters/prismaRows.spec.ts +0 -98
- package/src/adapters/prismaRows.ts +0 -337
- package/src/adapters/prismaTestClient.ts +0 -413
- package/src/apiKeys.integration.spec.ts +0 -192
- package/src/apiKeys.spec.ts +0 -162
- package/src/apiKeys.ts +0 -220
- package/src/calculatePagination.ts +0 -34
- package/src/createHotUpdaterCore.ts +0 -246
- package/src/db/bundleRows.spec.ts +0 -164
- package/src/db/bundleRows.ts +0 -10
- package/src/db/createBundleDiff.spec.ts +0 -450
- package/src/db/createBundleDiff.ts +0 -364
- package/src/db/databasePluginCore.spec.ts +0 -62
- package/src/db/databasePluginCore.testFixtures.ts +0 -76
- package/src/db/databasePluginCore.ts +0 -185
- package/src/db/databasePluginUpdateCheck.bench.ts +0 -110
- package/src/db/fixedMigrator.spec.ts +0 -296
- package/src/db/fixedMigrator.ts +0 -147
- package/src/db/fixedMigratorMongo.spec.ts +0 -239
- package/src/db/fixedMigratorMongo.ts +0 -425
- package/src/db/fixedMigratorShared.ts +0 -36
- package/src/db/hotUpdaterSchema.ts +0 -5
- package/src/db/index.spec.ts +0 -1269
- package/src/db/index.ts +0 -47
- package/src/db/mongoMigrationExecution.ts +0 -23
- package/src/db/node-sqlite.d.ts +0 -25
- package/src/db/releaseCatalog.ts +0 -148
- package/src/db/schema/definitions.ts +0 -1
- package/src/db/schema/insightsCollation.ts +0 -21
- package/src/db/schema/mongodb.ts +0 -40
- package/src/db/schema/registry.ts +0 -55
- package/src/db/schema/sql.ts +0 -244
- package/src/db/schema/sqlOperations.ts +0 -63
- package/src/db/schema/types.ts +0 -1
- package/src/db/schemaGenerators.ts +0 -494
- package/src/db/schemaReadiness.ts +0 -34
- package/src/db/sqlMigrationExecution.spec.ts +0 -65
- package/src/db/sqlMigrationExecution.ts +0 -108
- package/src/db/types.spec.ts +0 -63
- package/src/db/types.ts +0 -228
- package/src/db/updateArtifacts.spec.ts +0 -359
- package/src/db/updateArtifacts.ts +0 -541
- package/src/handler-standalone.integration.spec.ts +0 -528
- package/src/handler.spec.ts +0 -190
- package/src/handler.testFixtures.ts +0 -30
- package/src/handler.ts +0 -245
- package/src/handlerAuth.integration.spec.ts +0 -33
- package/src/handlerBundleRoutes.ts +0 -259
- package/src/handlerErrors.ts +0 -7
- package/src/handlerManagement.spec.ts +0 -262
- package/src/handlerParameters.ts +0 -105
- package/src/handlerReleaseCatalogRoutes.ts +0 -154
- package/src/handlerReleaseManagementRoutes.ts +0 -213
- package/src/handlerTypes.ts +0 -96
- package/src/handlerVersionRoutes.ts +0 -12
- package/src/index.ts +0 -30
- package/src/insights/domain.ts +0 -103
- package/src/insights/errors.ts +0 -11
- package/src/insights/eventInput.ts +0 -225
- package/src/insights/insights.integration.spec.ts +0 -381
- package/src/insights/provider.spec.ts +0 -385
- package/src/insights/provider.ts +0 -591
- package/src/insights/queryInput.ts +0 -141
- package/src/insights/routes.ts +0 -104
- package/src/insights/types.ts +0 -55
- package/src/internalRouter.ts +0 -104
- package/src/node.spec.ts +0 -119
- package/src/node.ts +0 -111
- package/src/releaseCatalogRoutes.integration.spec.ts +0 -238
- package/src/runtime.spec.ts +0 -194
- package/src/runtime.testFixtures.ts +0 -71
- package/src/schema/dsl-all-versions.spec.ts +0 -37
- package/src/schema/dsl.ts +0 -187
- package/src/schema/index.ts +0 -10
- package/src/schema/settings.ts +0 -15
- package/src/schema/types.ts +0 -79
- package/src/schema/v1_0_0.spec.ts +0 -220
- package/src/schema/v1_0_0.ts +0 -529
- package/src/storageAccess.spec.ts +0 -152
- package/src/storageAccess.ts +0 -141
- package/src/types/index.ts +0 -34
- package/src/version.ts +0 -3
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
DatabaseModel,
|
|
3
|
-
DatabaseWhere,
|
|
4
|
-
} from "@hot-updater/plugin-core/internal";
|
|
5
|
-
import { sql, type RawBuilder } from "kysely";
|
|
6
|
-
|
|
7
|
-
import type { ORMSQLProvider } from "../db/types";
|
|
8
|
-
import { escapeGlobPattern, escapeLikePattern } from "./databasePluginUtils";
|
|
9
|
-
|
|
10
|
-
class InvalidDatabasePredicateError extends Error {
|
|
11
|
-
readonly name = "InvalidDatabasePredicateError";
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const stringPredicate = (
|
|
15
|
-
provider: ORMSQLProvider,
|
|
16
|
-
field: string,
|
|
17
|
-
operator: "contains" | "ends_with" | "starts_with",
|
|
18
|
-
value: string,
|
|
19
|
-
insensitive: boolean,
|
|
20
|
-
): RawBuilder<boolean> => {
|
|
21
|
-
const column = sql.ref(field);
|
|
22
|
-
if (provider === "sqlite" && !insensitive) {
|
|
23
|
-
const literal = escapeGlobPattern(value);
|
|
24
|
-
const pattern =
|
|
25
|
-
operator === "contains"
|
|
26
|
-
? `*${literal}*`
|
|
27
|
-
: operator === "starts_with"
|
|
28
|
-
? `${literal}*`
|
|
29
|
-
: `*${literal}`;
|
|
30
|
-
return sql<boolean>`${column} glob ${pattern}`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const literal = escapeLikePattern(value);
|
|
34
|
-
const pattern =
|
|
35
|
-
operator === "contains"
|
|
36
|
-
? `%${literal}%`
|
|
37
|
-
: operator === "starts_with"
|
|
38
|
-
? `${literal}%`
|
|
39
|
-
: `%${literal}`;
|
|
40
|
-
if (insensitive) {
|
|
41
|
-
return sql<boolean>`lower(${column}) like lower(${pattern}) escape '\\'`;
|
|
42
|
-
}
|
|
43
|
-
if (provider === "mysql") {
|
|
44
|
-
return sql<boolean>`binary ${column} like binary ${pattern} escape '\\'`;
|
|
45
|
-
}
|
|
46
|
-
return sql<boolean>`${column} like ${pattern} escape '\\'`;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const predicate = <TModel extends DatabaseModel>(
|
|
50
|
-
provider: ORMSQLProvider,
|
|
51
|
-
condition: DatabaseWhere<TModel>,
|
|
52
|
-
): RawBuilder<boolean> => {
|
|
53
|
-
const column = sql.ref(condition.field);
|
|
54
|
-
const operator = condition.operator ?? "eq";
|
|
55
|
-
switch (operator) {
|
|
56
|
-
case "eq":
|
|
57
|
-
case "ne": {
|
|
58
|
-
if (condition.value === null) {
|
|
59
|
-
return operator === "eq"
|
|
60
|
-
? sql<boolean>`${column} is null`
|
|
61
|
-
: sql<boolean>`${column} is not null`;
|
|
62
|
-
}
|
|
63
|
-
const insensitive =
|
|
64
|
-
"mode" in condition && condition.mode === "insensitive";
|
|
65
|
-
if (insensitive) {
|
|
66
|
-
return operator === "eq"
|
|
67
|
-
? sql<boolean>`lower(${column}) = lower(${condition.value})`
|
|
68
|
-
: sql<boolean>`lower(${column}) <> lower(${condition.value})`;
|
|
69
|
-
}
|
|
70
|
-
return operator === "eq"
|
|
71
|
-
? sql<boolean>`${column} = ${condition.value}`
|
|
72
|
-
: sql<boolean>`${column} <> ${condition.value}`;
|
|
73
|
-
}
|
|
74
|
-
case "gt":
|
|
75
|
-
return sql<boolean>`${column} > ${condition.value}`;
|
|
76
|
-
case "gte":
|
|
77
|
-
return sql<boolean>`${column} >= ${condition.value}`;
|
|
78
|
-
case "lt":
|
|
79
|
-
return sql<boolean>`${column} < ${condition.value}`;
|
|
80
|
-
case "lte":
|
|
81
|
-
return sql<boolean>`${column} <= ${condition.value}`;
|
|
82
|
-
case "in":
|
|
83
|
-
case "not_in": {
|
|
84
|
-
if (!Array.isArray(condition.value)) {
|
|
85
|
-
throw new InvalidDatabasePredicateError();
|
|
86
|
-
}
|
|
87
|
-
if (operator === "in") {
|
|
88
|
-
return condition.value.length === 0
|
|
89
|
-
? sql<boolean>`false`
|
|
90
|
-
: sql<boolean>`${column} in (${sql.join(condition.value)})`;
|
|
91
|
-
}
|
|
92
|
-
return condition.value.length === 0
|
|
93
|
-
? sql<boolean>`true`
|
|
94
|
-
: sql<boolean>`${column} not in (${sql.join(condition.value)})`;
|
|
95
|
-
}
|
|
96
|
-
case "contains":
|
|
97
|
-
case "starts_with":
|
|
98
|
-
case "ends_with": {
|
|
99
|
-
if (typeof condition.value !== "string") {
|
|
100
|
-
throw new InvalidDatabasePredicateError();
|
|
101
|
-
}
|
|
102
|
-
return stringPredicate(
|
|
103
|
-
provider,
|
|
104
|
-
condition.field,
|
|
105
|
-
operator,
|
|
106
|
-
condition.value,
|
|
107
|
-
"mode" in condition && condition.mode === "insensitive",
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
export const buildKyselyWhere = <TModel extends DatabaseModel>(
|
|
114
|
-
provider: ORMSQLProvider,
|
|
115
|
-
where: readonly DatabaseWhere<TModel>[] | undefined,
|
|
116
|
-
): RawBuilder<boolean> | undefined => {
|
|
117
|
-
const [first, ...rest] = where ?? [];
|
|
118
|
-
if (first === undefined) return undefined;
|
|
119
|
-
let expression = predicate(provider, first);
|
|
120
|
-
for (const condition of rest) {
|
|
121
|
-
const next = predicate(provider, condition);
|
|
122
|
-
expression =
|
|
123
|
-
condition.connector === "OR"
|
|
124
|
-
? sql<boolean>`(${expression} or ${next})`
|
|
125
|
-
: sql<boolean>`(${expression} and ${next})`;
|
|
126
|
-
}
|
|
127
|
-
return expression;
|
|
128
|
-
};
|
|
@@ -1,210 +0,0 @@
|
|
|
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,209 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createDatabaseClient,
|
|
3
|
-
toInsightsInstallationRow,
|
|
4
|
-
} from "@hot-updater/plugin-core";
|
|
5
|
-
import { describe, expect, it } from "vitest";
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
createBundlePatchRowFixture,
|
|
9
|
-
createBundleRowFixture,
|
|
10
|
-
createBundleEventRowFixture,
|
|
11
|
-
} from "../../../test-utils/src/databaseTestFixtures";
|
|
12
|
-
import { setupDatabasePluginTestSuite } from "../../../test-utils/src/setupDatabasePluginTestSuite";
|
|
13
|
-
import { mongoAdapter } from "./mongodb";
|
|
14
|
-
import { createMongoBundleWhere } from "./mongodbQuery";
|
|
15
|
-
import { createMongoTestHarness } from "./mongodbTestClient";
|
|
16
|
-
|
|
17
|
-
const harness = createMongoTestHarness();
|
|
18
|
-
|
|
19
|
-
setupDatabasePluginTestSuite({
|
|
20
|
-
name: "mongoAdapter v2",
|
|
21
|
-
migrate: () => undefined,
|
|
22
|
-
createPlugin: () =>
|
|
23
|
-
mongoAdapter({ client: harness.client, transactions: true }),
|
|
24
|
-
reset: () => harness.reset(),
|
|
25
|
-
dispose: () => harness.close(),
|
|
26
|
-
});
|
|
27
|
-
|
|
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
|
-
|
|
93
|
-
it("returns an adapter without an unsafe atomic-batch fallback", () => {
|
|
94
|
-
const plugin = mongoAdapter({ client: harness.client });
|
|
95
|
-
expect(plugin.name).toBe("mongodb");
|
|
96
|
-
expect(plugin.adapterName).toBe("mongodb");
|
|
97
|
-
expect(plugin.provider).toBe("mongodb");
|
|
98
|
-
expect(Reflect.has(plugin, "transaction")).toBe(false);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("rejects a cross-table commit when transactions are disabled", async () => {
|
|
102
|
-
const plugin = mongoAdapter({ client: harness.client });
|
|
103
|
-
const owner = createBundleRowFixture("970");
|
|
104
|
-
const patch = createBundlePatchRowFixture(
|
|
105
|
-
"971",
|
|
106
|
-
owner.id,
|
|
107
|
-
createBundleRowFixture("972").id,
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
await expect(
|
|
111
|
-
plugin.commit({
|
|
112
|
-
changes: [
|
|
113
|
-
{
|
|
114
|
-
model: "bundles",
|
|
115
|
-
operation: "insert",
|
|
116
|
-
row: owner,
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
model: "bundlePatches",
|
|
120
|
-
operation: "insert",
|
|
121
|
-
row: patch,
|
|
122
|
-
},
|
|
123
|
-
],
|
|
124
|
-
}),
|
|
125
|
-
).rejects.toMatchObject({ name: "DatabaseAtomicCommitUnsupportedError" });
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("recovers a tombstoned bundle when an aggregate delete is retried", async () => {
|
|
129
|
-
harness.reset();
|
|
130
|
-
const client = createDatabaseClient(
|
|
131
|
-
mongoAdapter({ client: harness.client }),
|
|
132
|
-
);
|
|
133
|
-
const bundle = {
|
|
134
|
-
id: "bundle-retry",
|
|
135
|
-
platform: "ios" as const,
|
|
136
|
-
fileHash: "bundle-retry-hash",
|
|
137
|
-
gitCommitHash: null,
|
|
138
|
-
storageUri: "storage://bundle-retry",
|
|
139
|
-
archiveByteSize: 3_000_000_001,
|
|
140
|
-
};
|
|
141
|
-
await client.insertBundle(bundle);
|
|
142
|
-
|
|
143
|
-
harness.failNextBundleTombstone();
|
|
144
|
-
await expect(client.deleteBundleById(bundle.id)).rejects.toThrow(
|
|
145
|
-
"injected tombstone failure",
|
|
146
|
-
);
|
|
147
|
-
await expect(client.deleteBundleById(bundle.id)).resolves.toBeUndefined();
|
|
148
|
-
await expect(client.insertBundle(bundle)).resolves.toBeUndefined();
|
|
149
|
-
await expect(client.getBundleById(bundle.id)).resolves.toMatchObject({
|
|
150
|
-
id: bundle.id,
|
|
151
|
-
fileHash: bundle.fileHash,
|
|
152
|
-
storageUri: bundle.storageUri,
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe("MongoDB low-level predicate translation", () => {
|
|
158
|
-
it("composes connectors left to right", () => {
|
|
159
|
-
expect(
|
|
160
|
-
createMongoBundleWhere([
|
|
161
|
-
{ field: "id", value: "first" },
|
|
162
|
-
{ field: "id", value: "second", connector: "OR" },
|
|
163
|
-
{ field: "platform", value: "ios", connector: "AND" },
|
|
164
|
-
]),
|
|
165
|
-
).toEqual({
|
|
166
|
-
$and: [
|
|
167
|
-
{
|
|
168
|
-
$or: [
|
|
169
|
-
{ $expr: { $eq: ["$id", { $literal: "first" }] } },
|
|
170
|
-
{ $expr: { $eq: ["$id", { $literal: "second" }] } },
|
|
171
|
-
],
|
|
172
|
-
},
|
|
173
|
-
{ $expr: { $eq: ["$platform", { $literal: "ios" }] } },
|
|
174
|
-
],
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("escapes insensitive string patterns", () => {
|
|
179
|
-
expect(
|
|
180
|
-
createMongoBundleWhere([
|
|
181
|
-
{
|
|
182
|
-
field: "storage_uri",
|
|
183
|
-
operator: "contains",
|
|
184
|
-
value: "release.*",
|
|
185
|
-
mode: "insensitive",
|
|
186
|
-
},
|
|
187
|
-
]),
|
|
188
|
-
).toEqual({
|
|
189
|
-
$expr: {
|
|
190
|
-
$regexMatch: {
|
|
191
|
-
input: { $ifNull: ["$storage_uri", ""] },
|
|
192
|
-
regex: { $literal: "release\\.\\*" },
|
|
193
|
-
options: "i",
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
});
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it("preserves empty set semantics", () => {
|
|
200
|
-
expect(
|
|
201
|
-
createMongoBundleWhere([{ field: "id", operator: "in", value: [] }]),
|
|
202
|
-
).toEqual({ $expr: { $in: ["$id", { $literal: [] }] } });
|
|
203
|
-
expect(
|
|
204
|
-
createMongoBundleWhere([{ field: "id", operator: "not_in", value: [] }]),
|
|
205
|
-
).toEqual({
|
|
206
|
-
$expr: { $not: [{ $in: ["$id", { $literal: [] }] }] },
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
});
|
package/src/adapters/mongodb.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
compareInsightsText,
|
|
3
|
-
createDatabasePlugin,
|
|
4
|
-
} from "@hot-updater/plugin-core";
|
|
5
|
-
import {
|
|
6
|
-
createDatabasePluginAdapter,
|
|
7
|
-
type DatabasePluginImplementation,
|
|
8
|
-
type TransactionDatabasePluginImplementation,
|
|
9
|
-
} from "@hot-updater/plugin-core/internal";
|
|
10
|
-
import {
|
|
11
|
-
MongoServerError,
|
|
12
|
-
type ClientSession,
|
|
13
|
-
type MongoClient,
|
|
14
|
-
} from "mongodb";
|
|
15
|
-
|
|
16
|
-
import { createMongoMigrator } from "../db/fixedMigrator";
|
|
17
|
-
import type { DatabaseAdapterWithCapabilities } from "../db/types";
|
|
18
|
-
import { createMongoCollections } from "./mongodbCollections";
|
|
19
|
-
import { createMongoReads } from "./mongodbReads";
|
|
20
|
-
import { createMongoWrites } from "./mongodbWrites";
|
|
21
|
-
|
|
22
|
-
export interface MongoDBConfig {
|
|
23
|
-
readonly client: MongoClient;
|
|
24
|
-
/** Enables atomic catalog commits. Insights always requires MongoDB 5+ on a replica set or sharded cluster. */
|
|
25
|
-
readonly transactions?: boolean;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const createMongoImplementation = (
|
|
29
|
-
client: MongoClient,
|
|
30
|
-
session?: ClientSession,
|
|
31
|
-
): DatabasePluginImplementation => {
|
|
32
|
-
const collections = createMongoCollections(client);
|
|
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
|
-
},
|
|
90
|
-
...createMongoWrites(collections, session),
|
|
91
|
-
...createMongoReads(collections, session),
|
|
92
|
-
};
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const createTransactionalMongoImplementation = (
|
|
96
|
-
client: MongoClient,
|
|
97
|
-
): DatabasePluginImplementation => ({
|
|
98
|
-
...createMongoImplementation(client),
|
|
99
|
-
deleteChannel: (input) =>
|
|
100
|
-
client.withSession((session) =>
|
|
101
|
-
session.withTransaction(() =>
|
|
102
|
-
createMongoImplementation(client, session).deleteChannel(input),
|
|
103
|
-
),
|
|
104
|
-
),
|
|
105
|
-
transaction: <TResult>(
|
|
106
|
-
callback: (
|
|
107
|
-
transaction: TransactionDatabasePluginImplementation,
|
|
108
|
-
) => Promise<TResult>,
|
|
109
|
-
): Promise<TResult> =>
|
|
110
|
-
client.withSession((session) =>
|
|
111
|
-
session.withTransaction(() =>
|
|
112
|
-
callback(createMongoImplementation(client, session)),
|
|
113
|
-
),
|
|
114
|
-
),
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
export const mongoAdapter = (
|
|
118
|
-
config: MongoDBConfig,
|
|
119
|
-
): DatabaseAdapterWithCapabilities => {
|
|
120
|
-
const adapter = createDatabasePluginAdapter(
|
|
121
|
-
"mongodb",
|
|
122
|
-
config.transactions === true
|
|
123
|
-
? createTransactionalMongoImplementation(config.client)
|
|
124
|
-
: createMongoImplementation(config.client),
|
|
125
|
-
);
|
|
126
|
-
return Object.assign(
|
|
127
|
-
createDatabasePlugin({
|
|
128
|
-
name: "mongodb",
|
|
129
|
-
models: adapter.models,
|
|
130
|
-
commit: adapter.commit,
|
|
131
|
-
}),
|
|
132
|
-
{
|
|
133
|
-
adapterName: "mongodb",
|
|
134
|
-
provider: "mongodb" as const,
|
|
135
|
-
createMigrator: () => createMongoMigrator(config.client),
|
|
136
|
-
},
|
|
137
|
-
);
|
|
138
|
-
};
|