@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
package/dist/adapters/prisma.mjs
CHANGED
|
@@ -1,276 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { createPrismaOrderBy, createPrismaWhere } from "./prismaQuery.mjs";
|
|
6
|
-
import { PrismaAdapterError, getPrismaDelegate, parsePrismaApiKeyRow, parsePrismaBundleEventRow, parsePrismaBundleRow, parsePrismaChannelRow, parsePrismaInsightsInstallationRow, parsePrismaPatchRow, parsePrismaReleaseCatalogRow, parsePrismaReleaseRow, parsePrismaRows } from "./prismaRows.mjs";
|
|
7
|
-
import { DatabasePluginInputError, createDatabasePlugin } from "@hot-updater/plugin-core";
|
|
8
|
-
import { createDatabasePluginAdapter } from "@hot-updater/plugin-core/internal";
|
|
1
|
+
import { checkSqlProvider } from "./sqlProviders.mjs";
|
|
2
|
+
import { generatePrismaEngineSchema, prismaCollationStatements } from "../db/enginePrismaSchema.mjs";
|
|
3
|
+
import { prismaExecutor } from "./prismaExecutor.mjs";
|
|
4
|
+
import { coreSettings, coreTarget, createEngineDatabase, createSettingsMigrator, createSqlAdapter } from "@hot-updater/plugin-core";
|
|
9
5
|
//#region src/adapters/prisma.ts
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
where: createPrismaWhere(input.where, provider),
|
|
31
|
-
...orderBy ? { orderBy } : {},
|
|
32
|
-
skip: input.offset,
|
|
33
|
-
take: input.limit
|
|
34
|
-
});
|
|
35
|
-
switch (input.model) {
|
|
36
|
-
case "bundles": return parsePrismaRows(rows, parsePrismaBundleRow);
|
|
37
|
-
case "bundle_patches": return parsePrismaRows(rows, parsePrismaPatchRow);
|
|
38
|
-
case "bundle_events": return parsePrismaRows(rows, parsePrismaBundleEventRow);
|
|
39
|
-
case "bundle_installations": return parsePrismaRows(rows, parsePrismaInsightsInstallationRow);
|
|
40
|
-
case "channels": return parsePrismaRows(rows, parsePrismaChannelRow);
|
|
41
|
-
case "api_keys": return parsePrismaRows(rows, parsePrismaApiKeyRow);
|
|
42
|
-
case "releases": return parsePrismaRows(rows, parsePrismaReleaseRow);
|
|
43
|
-
case "release_catalogs": return parsePrismaRows(rows, parsePrismaReleaseCatalogRow);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const assertPatchReferences = async (client, bundleId, baseBundleId) => {
|
|
47
|
-
const ids = Array.from(/* @__PURE__ */ new Set([bundleId, baseBundleId]));
|
|
48
|
-
if (await getPrismaDelegate(client, "bundles").count({ where: { id: { in: ids } } }) !== ids.length) throw new PrismaAdapterError("patch references a missing bundle");
|
|
49
|
-
};
|
|
50
|
-
const createCrudImplementation = (client, provider, relationMode) => ({
|
|
51
|
-
deleteChannel: async ({ id }) => {
|
|
52
|
-
const channels = getPrismaDelegate(client, "channels");
|
|
53
|
-
if (await channels.findFirst({ where: { id } }) === null) return {
|
|
54
|
-
deleted: false,
|
|
55
|
-
reason: "not_found"
|
|
56
|
-
};
|
|
57
|
-
if (await getPrismaDelegate(client, "releases").count({ where: { channel_id: id } }) > 0) return {
|
|
58
|
-
deleted: false,
|
|
59
|
-
reason: "not_empty"
|
|
60
|
-
};
|
|
61
|
-
try {
|
|
62
|
-
await channels.deleteMany({ where: { id } });
|
|
63
|
-
} catch (error) {
|
|
64
|
-
if (isChannelDeleteReferencedError(error)) return {
|
|
65
|
-
deleted: false,
|
|
66
|
-
reason: "not_empty"
|
|
6
|
+
/**
|
|
7
|
+
* Hot Updater's database on a Prisma client: the storage engine through the
|
|
8
|
+
* shared SQL core, fenced by the schema settings. `db generate` writes the
|
|
9
|
+
* models Prisma applies; `db migrate` then sets the collations Prisma cannot
|
|
10
|
+
* declare and writes the settings rows the fence checks.
|
|
11
|
+
*/
|
|
12
|
+
const prismaAdapter = (config) => {
|
|
13
|
+
const provider = checkSqlProvider("prismaAdapter", config.provider);
|
|
14
|
+
const executor = prismaExecutor(config.prisma, provider);
|
|
15
|
+
return {
|
|
16
|
+
...createEngineDatabase({
|
|
17
|
+
name: "prisma",
|
|
18
|
+
adapter: createSqlAdapter({ executor })
|
|
19
|
+
}),
|
|
20
|
+
provider,
|
|
21
|
+
generateSchema: ((version, _name, { schema } = coreTarget) => {
|
|
22
|
+
if (version !== "latest" && version !== coreSettings["schema.core"]) throw new Error(`Invalid version ${version}`);
|
|
23
|
+
return {
|
|
24
|
+
code: generatePrismaEngineSchema(provider, schema),
|
|
25
|
+
path: "prisma/schema.prisma"
|
|
67
26
|
};
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
where: { name: input.row.name },
|
|
81
|
-
create: input.row,
|
|
82
|
-
update: {}
|
|
83
|
-
}));
|
|
84
|
-
return {
|
|
85
|
-
row,
|
|
86
|
-
inserted: row.id === input.row.id
|
|
87
|
-
};
|
|
88
|
-
},
|
|
89
|
-
create: async (input) => {
|
|
90
|
-
if (input.model === "bundle_patches") await assertPatchReferences(client, input.data.bundle_id, input.data.base_bundle_id);
|
|
91
|
-
const delegate = getPrismaDelegate(client, input.model);
|
|
92
|
-
let row;
|
|
93
|
-
if (input.onConflict === "ignore") {
|
|
94
|
-
const where = input.model === "channels" ? { name: input.data.name } : input.model === "api_keys" ? { hash: input.data.hash } : { install_id: input.data.install_id };
|
|
95
|
-
row = await delegate.upsert({
|
|
96
|
-
where,
|
|
97
|
-
create: input.data,
|
|
98
|
-
update: {}
|
|
99
|
-
});
|
|
100
|
-
} else row = await delegate.create({ data: input.data });
|
|
101
|
-
switch (input.model) {
|
|
102
|
-
case "bundles": return parsePrismaBundleRow(row);
|
|
103
|
-
case "bundle_patches": return parsePrismaPatchRow(row);
|
|
104
|
-
case "bundle_events": return parsePrismaBundleEventRow(row);
|
|
105
|
-
case "bundle_installations": return parsePrismaInsightsInstallationRow(row);
|
|
106
|
-
case "channels": return parsePrismaChannelRow(row);
|
|
107
|
-
case "api_keys": return parsePrismaApiKeyRow(row);
|
|
108
|
-
case "releases": return parsePrismaReleaseRow(row);
|
|
109
|
-
case "release_catalogs": return parsePrismaReleaseCatalogRow(row);
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
update: async (input) => {
|
|
113
|
-
if (input.model === "bundle_installations") {
|
|
114
|
-
const installId = input.where.find((item) => item.field === "install_id" && (item.operator === void 0 || item.operator === "eq") && typeof item.value === "string")?.value;
|
|
115
|
-
if (typeof installId !== "string") throw new PrismaAdapterError("bundle_installations update requires install_id");
|
|
116
|
-
const delegate = getPrismaDelegate(client, "bundle_installations");
|
|
117
|
-
if (delegate.updateMany === void 0) throw new PrismaAdapterError("model delegate \"bundle_installations\" requires updateMany");
|
|
118
|
-
if ((await delegate.updateMany({
|
|
119
|
-
where: createPrismaWhere(input.where, provider),
|
|
120
|
-
data: input.update
|
|
121
|
-
})).count === 0) return null;
|
|
122
|
-
const stored = await delegate.findFirst({ where: { install_id: installId } });
|
|
123
|
-
return stored === null ? null : parsePrismaInsightsInstallationRow(stored);
|
|
124
|
-
}
|
|
125
|
-
const id = input.where[0]?.value;
|
|
126
|
-
if (typeof id !== "string") throw new PrismaAdapterError(`${input.model} update requires a string id`);
|
|
127
|
-
if (input.model === "api_keys") {
|
|
128
|
-
const delegate = getPrismaDelegate(client, "api_keys");
|
|
129
|
-
if (await delegate.findFirst({ where: { id } }) === null) return null;
|
|
130
|
-
await delegate.update({
|
|
131
|
-
where: { id },
|
|
132
|
-
data: input.update
|
|
133
|
-
});
|
|
134
|
-
const stored = await delegate.findFirst({ where: { id } });
|
|
135
|
-
return stored === null ? null : parsePrismaApiKeyRow(stored);
|
|
136
|
-
}
|
|
137
|
-
if (input.model === "releases") {
|
|
138
|
-
const delegate = getPrismaDelegate(client, "releases");
|
|
139
|
-
if (await delegate.findFirst({ where: { id } }) === null) return null;
|
|
140
|
-
await delegate.update({
|
|
141
|
-
where: { id },
|
|
142
|
-
data: input.update
|
|
143
|
-
});
|
|
144
|
-
const stored = await delegate.findFirst({ where: { id } });
|
|
145
|
-
return stored === null ? null : parsePrismaReleaseRow(stored);
|
|
146
|
-
}
|
|
147
|
-
if (input.model === "release_catalogs") {
|
|
148
|
-
const delegate = getPrismaDelegate(client, "release_catalogs");
|
|
149
|
-
if (await delegate.findFirst({ where: { scope_key: id } }) === null) return null;
|
|
150
|
-
await delegate.update({
|
|
151
|
-
where: { scope_key: id },
|
|
152
|
-
data: input.update
|
|
27
|
+
}),
|
|
28
|
+
createMigrator: ({ schema, settings } = coreTarget) => {
|
|
29
|
+
const collations = prismaCollationStatements(provider, schema);
|
|
30
|
+
return createSettingsMigrator({
|
|
31
|
+
adapterName: "prisma",
|
|
32
|
+
executor,
|
|
33
|
+
settings,
|
|
34
|
+
applyTables: "`prisma db push` (or `prisma migrate`)",
|
|
35
|
+
...collations.length === 0 ? {} : { fixups: {
|
|
36
|
+
description: "Set the collations Prisma cannot declare, so strings compare by their bytes",
|
|
37
|
+
statements: collations
|
|
38
|
+
} }
|
|
153
39
|
});
|
|
154
|
-
const stored = await delegate.findFirst({ where: { scope_key: id } });
|
|
155
|
-
return stored === null ? null : parsePrismaReleaseCatalogRow(stored);
|
|
156
|
-
}
|
|
157
|
-
const delegate = getPrismaDelegate(client, "bundles");
|
|
158
|
-
if (delegate.updateMany === void 0) throw new PrismaAdapterError("model delegate \"bundles\" requires updateMany");
|
|
159
|
-
if (await delegate.findFirst({ where: { id } }) === null) return null;
|
|
160
|
-
await delegate.updateMany({
|
|
161
|
-
where: { id },
|
|
162
|
-
data: input.update
|
|
163
|
-
});
|
|
164
|
-
const stored = await delegate.findFirst({ where: { id } });
|
|
165
|
-
if (stored === null) return null;
|
|
166
|
-
return parsePrismaBundleRow(stored);
|
|
167
|
-
},
|
|
168
|
-
delete: async (input) => {
|
|
169
|
-
if (input.model === "bundles" && relationMode === "prisma") {
|
|
170
|
-
const rows = await getPrismaDelegate(client, "bundles").findMany({ where: createPrismaWhere(input.where, provider) });
|
|
171
|
-
const ids = parsePrismaRows(rows, parsePrismaBundleRow).map(({ id }) => id);
|
|
172
|
-
if (ids.length === 0) return;
|
|
173
|
-
await getPrismaDelegate(client, "bundle_patches").deleteMany({ where: { OR: [{ bundle_id: { in: ids } }, { base_bundle_id: { in: ids } }] } });
|
|
174
|
-
}
|
|
175
|
-
try {
|
|
176
|
-
await getPrismaDelegate(client, input.model).deleteMany({ where: createPrismaWhere(input.where, provider) });
|
|
177
|
-
} catch (error) {
|
|
178
|
-
if (input.model === "channels") translateChannelDeleteError(error);
|
|
179
|
-
throw error;
|
|
180
|
-
}
|
|
181
|
-
},
|
|
182
|
-
count: async (input) => {
|
|
183
|
-
if (input.distinct !== void 0) throw new DatabasePluginInputError("invalid-operation");
|
|
184
|
-
return getPrismaDelegate(client, input.model).count({ where: createPrismaWhere(input.where, provider) });
|
|
185
|
-
},
|
|
186
|
-
findOne: async (input) => {
|
|
187
|
-
const row = await getPrismaDelegate(client, input.model).findFirst({ where: createPrismaWhere(input.where, provider) });
|
|
188
|
-
if (row === null) return null;
|
|
189
|
-
switch (input.model) {
|
|
190
|
-
case "bundles": return parsePrismaBundleRow(row);
|
|
191
|
-
case "bundle_patches": return parsePrismaPatchRow(row);
|
|
192
|
-
case "channels": return parsePrismaChannelRow(row);
|
|
193
|
-
case "api_keys": return parsePrismaApiKeyRow(row);
|
|
194
|
-
case "releases": return parsePrismaReleaseRow(row);
|
|
195
|
-
case "release_catalogs": return parsePrismaReleaseCatalogRow(row);
|
|
196
|
-
case "bundle_installations": return parsePrismaInsightsInstallationRow(row);
|
|
197
40
|
}
|
|
198
|
-
},
|
|
199
|
-
findMany: (input) => findMany(client, input, provider)
|
|
200
|
-
});
|
|
201
|
-
const createPrismaImplementation = (client, relationMode, provider) => {
|
|
202
|
-
const crud = createCrudImplementation(client, provider, relationMode);
|
|
203
|
-
const implementation = {
|
|
204
|
-
...crud,
|
|
205
|
-
recordInsights: (input) => {
|
|
206
|
-
if (!hasCallbackTransaction(client)) throw new PrismaAdapterError("Insights recording requires callback transactions");
|
|
207
|
-
return runPrismaTransaction(client, "serializable", async (transactionClient) => {
|
|
208
|
-
const events = getPrismaDelegate(transactionClient, "bundle_events");
|
|
209
|
-
if (await events.findFirst({ where: { id: input.event.id } }) !== null) return;
|
|
210
|
-
await events.create({ data: input.event });
|
|
211
|
-
const installations = getPrismaDelegate(transactionClient, "bundle_installations");
|
|
212
|
-
const existing = await installations.findFirst({ where: { install_id: input.installation.install_id } });
|
|
213
|
-
if (existing === null) {
|
|
214
|
-
await installations.create({ data: input.installation });
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
const current = parsePrismaInsightsInstallationRow(existing);
|
|
218
|
-
if (current.received_at_ms > input.installation.received_at_ms || current.received_at_ms === input.installation.received_at_ms && current.id >= input.installation.id) return;
|
|
219
|
-
await installations.update({
|
|
220
|
-
where: { install_id: input.installation.install_id },
|
|
221
|
-
data: input.installation
|
|
222
|
-
});
|
|
223
|
-
}, true);
|
|
224
|
-
},
|
|
225
|
-
deleteChannel: (input) => {
|
|
226
|
-
if (!hasCallbackTransaction(client)) throw new PrismaAdapterError("channel deletion requires callback transactions");
|
|
227
|
-
return runPrismaTransaction(client, "serializable", (transactionClient) => createCrudImplementation(transactionClient, provider, relationMode).deleteChannel(input));
|
|
228
|
-
},
|
|
229
|
-
delete: (input) => {
|
|
230
|
-
if (input.model !== "bundles" || relationMode === "foreign-keys") return crud.delete(input);
|
|
231
|
-
if (!hasCallbackTransaction(client)) throw new PrismaAdapterError("relation mode \"prisma\" requires callback transactions");
|
|
232
|
-
return runPrismaTransaction(client, "serializable", (transactionClient) => createCrudImplementation(transactionClient, provider, relationMode).delete(input));
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
if (relationMode === "prisma" && !hasCallbackTransaction(client)) throw new PrismaAdapterError("relation mode \"prisma\" requires callback transactions");
|
|
236
|
-
if (!hasCallbackTransaction(client)) return implementation;
|
|
237
|
-
if (relationMode === "prisma") implementation.create = (input) => runPrismaTransaction(client, "serializable", (transactionClient) => createCrudImplementation(transactionClient, provider, relationMode).create(input));
|
|
238
|
-
implementation.update = (input) => runPrismaTransaction(client, relationMode === "prisma" ? "serializable" : "default", (transactionClient) => createCrudImplementation(transactionClient, provider, relationMode).update(input));
|
|
239
|
-
return {
|
|
240
|
-
...implementation,
|
|
241
|
-
transaction: (callback) => runPrismaTransaction(client, "serializable", async (transactionClient) => {
|
|
242
|
-
return callback(createCrudImplementation(transactionClient, provider, relationMode));
|
|
243
|
-
})
|
|
244
|
-
};
|
|
245
|
-
};
|
|
246
|
-
const prismaAdapter = (config) => {
|
|
247
|
-
assertPrismaProvider(config.provider);
|
|
248
|
-
const adapter = createDatabasePluginAdapter("prisma", createPrismaImplementation(config.prisma, config.relationMode ?? "foreign-keys", config.provider));
|
|
249
|
-
const unsupportedMssqlInsights = async () => {
|
|
250
|
-
throw new PrismaAdapterError("SQL Server Insights is unsupported: string keys pad trailing spaces and do not provide the required exact UTF-8 identity ordering");
|
|
251
41
|
};
|
|
252
|
-
const insights = config.provider === "mssql" ? {
|
|
253
|
-
record: unsupportedMssqlInsights,
|
|
254
|
-
listEvents: unsupportedMssqlInsights,
|
|
255
|
-
findInstallations: unsupportedMssqlInsights,
|
|
256
|
-
countInstallations: unsupportedMssqlInsights,
|
|
257
|
-
countEvents: unsupportedMssqlInsights
|
|
258
|
-
} : adapter.models.insights;
|
|
259
|
-
return Object.assign(createDatabasePlugin({
|
|
260
|
-
name: "prisma",
|
|
261
|
-
models: {
|
|
262
|
-
...adapter.models,
|
|
263
|
-
insights
|
|
264
|
-
},
|
|
265
|
-
commit: adapter.commit
|
|
266
|
-
}), {
|
|
267
|
-
adapterName: "prisma",
|
|
268
|
-
provider: config.provider,
|
|
269
|
-
generateSchema: ((version) => ({
|
|
270
|
-
code: generatePrismaSchema(config.provider, version === "latest" ? hotUpdaterSchema : getHotUpdaterSchemaVersion(version)),
|
|
271
|
-
path: "./prisma/schema/hot_updater.prisma"
|
|
272
|
-
}))
|
|
273
|
-
});
|
|
274
42
|
};
|
|
275
43
|
//#endregion
|
|
276
44
|
export { prismaAdapter };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
|
|
2
|
+
//#region src/adapters/prismaExecutor.ts
|
|
3
|
+
/**
|
|
4
|
+
* Prisma reports a raw query's database error as P2010 with the database's
|
|
5
|
+
* code in `meta.code`, and a failed transaction as P2034. The error thrown on
|
|
6
|
+
* carries that code, as the SQL core classifies it, with Prisma's as `cause`.
|
|
7
|
+
*/
|
|
8
|
+
const withDatabaseCode = (dialect, error) => {
|
|
9
|
+
const { code, meta } = error ?? {};
|
|
10
|
+
const database = code === "P2034" ? "40001" : meta?.code === void 0 ? void 0 : String(meta.code);
|
|
11
|
+
if (database === void 0) return error;
|
|
12
|
+
return Object.assign(new Error(String(meta?.message ?? error.message), { cause: error }), { code: dialect === "sqlite" ? `SQLITE:${database}` : database });
|
|
13
|
+
};
|
|
14
|
+
/** MySQL's ASCII keys are `VarBinary` in Prisma's schema, and read back as bytes. */
|
|
15
|
+
const decoder = new TextDecoder();
|
|
16
|
+
const decodeRow = (row) => {
|
|
17
|
+
for (const [column, value] of Object.entries(row)) if (value instanceof Uint8Array) row[column] = decoder.decode(value);
|
|
18
|
+
return row;
|
|
19
|
+
};
|
|
20
|
+
const over = (dialect, client) => ({ async execute({ sql, params }) {
|
|
21
|
+
try {
|
|
22
|
+
if (/^\s*SELECT\b/iu.test(sql)) return {
|
|
23
|
+
rows: (await client.$queryRawUnsafe(sql, ...params)).map(decodeRow),
|
|
24
|
+
changes: 0
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
rows: [],
|
|
28
|
+
changes: Number(await client.$executeRawUnsafe(sql, ...params))
|
|
29
|
+
};
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw withDatabaseCode(dialect, error);
|
|
32
|
+
}
|
|
33
|
+
} });
|
|
34
|
+
/** Taking the write lock first makes a SQLite transaction behave as BEGIN IMMEDIATE. */
|
|
35
|
+
const writeLock = (dialect) => `UPDATE ${(0, _hot_updater_plugin_core.quoteSql)(dialect, _hot_updater_plugin_core.SETTINGS_TABLE.name)} SET ${(0, _hot_updater_plugin_core.quoteSql)(dialect, "_v")} = ${(0, _hot_updater_plugin_core.quoteSql)(dialect, "_v")} WHERE 0 = 1`;
|
|
36
|
+
/**
|
|
37
|
+
* The SQL core's executor over a Prisma client, through raw queries and
|
|
38
|
+
* interactive transactions. A SQLite transaction takes the write lock with
|
|
39
|
+
* its first statement, as BEGIN IMMEDIATE would.
|
|
40
|
+
*/
|
|
41
|
+
const prismaExecutor = (client, dialect) => ({
|
|
42
|
+
dialect,
|
|
43
|
+
...over(dialect, client),
|
|
44
|
+
async transaction(fn) {
|
|
45
|
+
try {
|
|
46
|
+
return await client.$transaction(async (tx) => {
|
|
47
|
+
const connection = over(dialect, tx);
|
|
48
|
+
if (dialect === "sqlite") await connection.execute({
|
|
49
|
+
sql: writeLock(dialect),
|
|
50
|
+
params: []
|
|
51
|
+
});
|
|
52
|
+
return fn(connection);
|
|
53
|
+
}, {
|
|
54
|
+
maxWait: 1e4,
|
|
55
|
+
timeout: 3e4
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw withDatabaseCode(dialect, error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
//#endregion
|
|
63
|
+
exports.prismaExecutor = prismaExecutor;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { SETTINGS_TABLE, quoteSql } from "@hot-updater/plugin-core";
|
|
2
|
+
//#region src/adapters/prismaExecutor.ts
|
|
3
|
+
/**
|
|
4
|
+
* Prisma reports a raw query's database error as P2010 with the database's
|
|
5
|
+
* code in `meta.code`, and a failed transaction as P2034. The error thrown on
|
|
6
|
+
* carries that code, as the SQL core classifies it, with Prisma's as `cause`.
|
|
7
|
+
*/
|
|
8
|
+
const withDatabaseCode = (dialect, error) => {
|
|
9
|
+
const { code, meta } = error ?? {};
|
|
10
|
+
const database = code === "P2034" ? "40001" : meta?.code === void 0 ? void 0 : String(meta.code);
|
|
11
|
+
if (database === void 0) return error;
|
|
12
|
+
return Object.assign(new Error(String(meta?.message ?? error.message), { cause: error }), { code: dialect === "sqlite" ? `SQLITE:${database}` : database });
|
|
13
|
+
};
|
|
14
|
+
/** MySQL's ASCII keys are `VarBinary` in Prisma's schema, and read back as bytes. */
|
|
15
|
+
const decoder = new TextDecoder();
|
|
16
|
+
const decodeRow = (row) => {
|
|
17
|
+
for (const [column, value] of Object.entries(row)) if (value instanceof Uint8Array) row[column] = decoder.decode(value);
|
|
18
|
+
return row;
|
|
19
|
+
};
|
|
20
|
+
const over = (dialect, client) => ({ async execute({ sql, params }) {
|
|
21
|
+
try {
|
|
22
|
+
if (/^\s*SELECT\b/iu.test(sql)) return {
|
|
23
|
+
rows: (await client.$queryRawUnsafe(sql, ...params)).map(decodeRow),
|
|
24
|
+
changes: 0
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
rows: [],
|
|
28
|
+
changes: Number(await client.$executeRawUnsafe(sql, ...params))
|
|
29
|
+
};
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw withDatabaseCode(dialect, error);
|
|
32
|
+
}
|
|
33
|
+
} });
|
|
34
|
+
/** Taking the write lock first makes a SQLite transaction behave as BEGIN IMMEDIATE. */
|
|
35
|
+
const writeLock = (dialect) => `UPDATE ${quoteSql(dialect, SETTINGS_TABLE.name)} SET ${quoteSql(dialect, "_v")} = ${quoteSql(dialect, "_v")} WHERE 0 = 1`;
|
|
36
|
+
/**
|
|
37
|
+
* The SQL core's executor over a Prisma client, through raw queries and
|
|
38
|
+
* interactive transactions. A SQLite transaction takes the write lock with
|
|
39
|
+
* its first statement, as BEGIN IMMEDIATE would.
|
|
40
|
+
*/
|
|
41
|
+
const prismaExecutor = (client, dialect) => ({
|
|
42
|
+
dialect,
|
|
43
|
+
...over(dialect, client),
|
|
44
|
+
async transaction(fn) {
|
|
45
|
+
try {
|
|
46
|
+
return await client.$transaction(async (tx) => {
|
|
47
|
+
const connection = over(dialect, tx);
|
|
48
|
+
if (dialect === "sqlite") await connection.execute({
|
|
49
|
+
sql: writeLock(dialect),
|
|
50
|
+
params: []
|
|
51
|
+
});
|
|
52
|
+
return fn(connection);
|
|
53
|
+
}, {
|
|
54
|
+
maxWait: 1e4,
|
|
55
|
+
timeout: 3e4
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw withDatabaseCode(dialect, error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
//#endregion
|
|
63
|
+
export { prismaExecutor };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/adapters/sqlProviders.ts
|
|
2
|
+
/** The dialects Hot Updater's SQL core runs on. */
|
|
3
|
+
const sqlProviders = [
|
|
4
|
+
"sqlite",
|
|
5
|
+
"mysql",
|
|
6
|
+
"postgresql"
|
|
7
|
+
];
|
|
8
|
+
/** The provider an adapter was given, when Hot Updater's SQL core runs on it. */
|
|
9
|
+
const checkSqlProvider = (adapter, provider) => {
|
|
10
|
+
if (!sqlProviders.includes(provider)) throw new Error(`${adapter}: provider "${String(provider)}" is not supported. Use ${sqlProviders.map((name) => `"${name}"`).join(", ")}; CockroachDB and SQL Server were dropped in 1.0.`);
|
|
11
|
+
return provider;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
exports.checkSqlProvider = checkSqlProvider;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/adapters/sqlProviders.ts
|
|
2
|
+
/** The dialects Hot Updater's SQL core runs on. */
|
|
3
|
+
const sqlProviders = [
|
|
4
|
+
"sqlite",
|
|
5
|
+
"mysql",
|
|
6
|
+
"postgresql"
|
|
7
|
+
];
|
|
8
|
+
/** The provider an adapter was given, when Hot Updater's SQL core runs on it. */
|
|
9
|
+
const checkSqlProvider = (adapter, provider) => {
|
|
10
|
+
if (!sqlProviders.includes(provider)) throw new Error(`${adapter}: provider "${String(provider)}" is not supported. Use ${sqlProviders.map((name) => `"${name}"`).join(", ")}; CockroachDB and SQL Server were dropped in 1.0.`);
|
|
11
|
+
return provider;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { checkSqlProvider };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
const require_reads = require("../core/reads.cjs");
|
|
2
|
+
const require_api = require("../core/api.cjs");
|
|
3
|
+
const require_official = require("../plugins/official.cjs");
|
|
4
|
+
const require_clientPlugins = require("./clientPlugins.cjs");
|
|
5
|
+
let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
|
|
6
|
+
//#region src/assembly/assemblePlugins.ts
|
|
7
|
+
const PLUGIN_KEYS = /* @__PURE__ */ new Set([
|
|
8
|
+
"id",
|
|
9
|
+
"provides",
|
|
10
|
+
"schemaVersion",
|
|
11
|
+
"schema",
|
|
12
|
+
"namespace",
|
|
13
|
+
"init",
|
|
14
|
+
"cli"
|
|
15
|
+
]);
|
|
16
|
+
const CLI_KEYS = /* @__PURE__ */ new Set(["clientCredential", "clientPlugin"]);
|
|
17
|
+
const INSTANCE_KEYS = /* @__PURE__ */ new Set([
|
|
18
|
+
"api",
|
|
19
|
+
"endpoints",
|
|
20
|
+
"clientAuth"
|
|
21
|
+
]);
|
|
22
|
+
const METHODS = /* @__PURE__ */ new Set([
|
|
23
|
+
"GET",
|
|
24
|
+
"POST",
|
|
25
|
+
"PUT",
|
|
26
|
+
"PATCH",
|
|
27
|
+
"DELETE"
|
|
28
|
+
]);
|
|
29
|
+
/** An id that prefixes table names is a SQL name; one that does not may use camelCase. */
|
|
30
|
+
const PREFIX_ID = /^[a-z][a-z0-9_]*$/u;
|
|
31
|
+
const UNPREFIXED_ID = /^[a-z][A-Za-z0-9_]*$/u;
|
|
32
|
+
const PATH = /^(\/(:?[A-Za-z0-9_.~-]+))+$/u;
|
|
33
|
+
const fail = (message) => {
|
|
34
|
+
throw new _hot_updater_plugin_core.HotUpdaterConfigError(message);
|
|
35
|
+
};
|
|
36
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
37
|
+
const checkPlugin = (value, at) => {
|
|
38
|
+
if (!isRecord(value)) return fail(`${at} is not a plugin.`);
|
|
39
|
+
if ("kind" in value) fail(`${at} has a kind; core is built in and is never passed as a plugin.`);
|
|
40
|
+
const unknown = Object.keys(value).find((key) => !PLUGIN_KEYS.has(key));
|
|
41
|
+
if (unknown !== void 0) fail(`${at} has an unknown key "${unknown}".`);
|
|
42
|
+
const plugin = value;
|
|
43
|
+
if (plugin.namespace !== void 0 && plugin.namespace !== false) fail(`${at} may only set namespace: false.`);
|
|
44
|
+
const pattern = plugin.namespace === false ? UNPREFIXED_ID : PREFIX_ID;
|
|
45
|
+
if (typeof plugin.id !== "string" || !pattern.test(plugin.id)) fail(`${at} needs an id matching ${pattern.source}.`);
|
|
46
|
+
if (plugin.id === "core") fail(`${at} uses the id "core", which is core's own.`);
|
|
47
|
+
require_official.checkReservedId(value, at);
|
|
48
|
+
if (typeof plugin.schemaVersion !== "string" || !isRecord(plugin.schema) || typeof plugin.init !== "function") fail(`Plugin "${plugin.id}" needs a schemaVersion, a schema, and init.`);
|
|
49
|
+
const provides = plugin.provides;
|
|
50
|
+
if (provides !== void 0 && (!isRecord(provides) || Object.entries(provides).some(([key, flag]) => key !== "clientAuth" || flag !== true))) fail(`Plugin "${plugin.id}" may only declare provides: { clientAuth: true }.`);
|
|
51
|
+
const cli = value.cli;
|
|
52
|
+
if (cli !== void 0) {
|
|
53
|
+
if (!isRecord(cli) || Object.keys(cli).some((key) => !CLI_KEYS.has(key))) fail(`Plugin "${plugin.id}" cli may only hold clientCredential and clientPlugin.`);
|
|
54
|
+
if (cli.clientCredential !== void 0 && plugin.provides?.clientAuth !== true) fail(`Plugin "${plugin.id}" adds cli.clientCredential without declaring provides: { clientAuth: true }.`);
|
|
55
|
+
}
|
|
56
|
+
return plugin;
|
|
57
|
+
};
|
|
58
|
+
const checkEndpoint = (id, value) => {
|
|
59
|
+
const endpoint = isRecord(value) ? value : {};
|
|
60
|
+
if (!METHODS.has(endpoint.method) || typeof endpoint.path !== "string" || !PATH.test(endpoint.path) || endpoint.access !== "client" && endpoint.access !== "admin" || typeof endpoint.handler !== "function") fail(`Plugin "${id}" has an endpoint that needs a method, a path like /name/:param, access "client" or "admin", and a handler.`);
|
|
61
|
+
return {
|
|
62
|
+
...endpoint,
|
|
63
|
+
plugin: id
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
const checkInstance = (plugin, instance) => {
|
|
67
|
+
const { id } = plugin;
|
|
68
|
+
if (!isRecord(instance)) return fail(`Plugin "${id}" init returned no instance.`);
|
|
69
|
+
if (typeof instance.then === "function") fail(`Plugin "${id}" init returned a promise; init must be synchronous.`);
|
|
70
|
+
const unknown = Object.keys(instance).find((key) => !INSTANCE_KEYS.has(key));
|
|
71
|
+
if (unknown !== void 0 || !("api" in instance)) fail(`Plugin "${id}" init must return { api, endpoints?, clientAuth? }${unknown === void 0 ? "" : `, not "${unknown}"`}.`);
|
|
72
|
+
const declared = plugin.provides?.clientAuth === true;
|
|
73
|
+
const clientAuth = instance.clientAuth;
|
|
74
|
+
if (declared !== (clientAuth !== void 0)) fail(declared ? `Plugin "${id}" declares provides: { clientAuth: true } but its instance has no clientAuth.` : `Plugin "${id}" returns clientAuth without declaring provides: { clientAuth: true }.`);
|
|
75
|
+
if (clientAuth !== void 0 && (typeof clientAuth.authenticate !== "function" || !Array.isArray(clientAuth.varyHeaders) || !clientAuth.varyHeaders.every((header) => typeof header === "string"))) fail(`Plugin "${id}" clientAuth needs varyHeaders and authenticate.`);
|
|
76
|
+
const endpoints = instance.endpoints ?? [];
|
|
77
|
+
if (!Array.isArray(endpoints)) fail(`Plugin "${id}" endpoints must be an array.`);
|
|
78
|
+
return {
|
|
79
|
+
api: instance.api,
|
|
80
|
+
clientAuth,
|
|
81
|
+
endpoints: endpoints.map((endpoint) => checkEndpoint(id, endpoint))
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Checks every plugin, runs each `init` once against one engine over the
|
|
86
|
+
* database, and collects APIs, endpoints, and clientAuth. Core's reads run
|
|
87
|
+
* on the same engine, so plugins read core through `ctx.core`.
|
|
88
|
+
*/
|
|
89
|
+
const assemblePlugins = (value, database, { now = Date.now, storage = { resolveFileUrl: async () => null } } = {}) => {
|
|
90
|
+
if (!Array.isArray(value)) return fail("plugins must be an array of plugins.");
|
|
91
|
+
const plugins = value.map((plugin, position) => checkPlugin(plugin, `plugins[${position}]`));
|
|
92
|
+
const ids = /* @__PURE__ */ new Set();
|
|
93
|
+
for (const { id } of plugins) {
|
|
94
|
+
if (ids.has(id)) fail(`Plugin "${id}" is registered twice.`);
|
|
95
|
+
ids.add(id);
|
|
96
|
+
}
|
|
97
|
+
require_clientPlugins.clientPluginsOf(plugins);
|
|
98
|
+
const engine = (0, _hot_updater_plugin_core.createEngine)(database, {
|
|
99
|
+
plugins,
|
|
100
|
+
now
|
|
101
|
+
});
|
|
102
|
+
const { onCachedRoutesChange } = database;
|
|
103
|
+
const core = require_api.createCoreApi(engine.core, storage, {
|
|
104
|
+
now,
|
|
105
|
+
...onCachedRoutesChange === void 0 ? {} : { onCachedRoutesChange: () => onCachedRoutesChange.call(database) }
|
|
106
|
+
});
|
|
107
|
+
const reads = require_reads.createCoreReads(engine.core, storage);
|
|
108
|
+
const api = {};
|
|
109
|
+
const endpoints = [];
|
|
110
|
+
let clientAuth;
|
|
111
|
+
for (const plugin of plugins) {
|
|
112
|
+
const instance = checkInstance(plugin, plugin.init({
|
|
113
|
+
db: engine.database(plugin),
|
|
114
|
+
core: reads,
|
|
115
|
+
now
|
|
116
|
+
}));
|
|
117
|
+
if (instance.clientAuth !== void 0) {
|
|
118
|
+
if (clientAuth !== void 0) fail(`Plugins "${clientAuth.plugin}" and "${plugin.id}" both provide clientAuth; keep one.`);
|
|
119
|
+
const provided = instance.clientAuth;
|
|
120
|
+
clientAuth = {
|
|
121
|
+
plugin: plugin.id,
|
|
122
|
+
varyHeaders: provided.varyHeaders,
|
|
123
|
+
authenticate: (headers) => provided.authenticate(headers)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
endpoints.push(...instance.endpoints);
|
|
127
|
+
api[plugin.id] = instance.api;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
core,
|
|
131
|
+
api,
|
|
132
|
+
endpoints,
|
|
133
|
+
...clientAuth === void 0 ? {} : { clientAuth },
|
|
134
|
+
flush: engine.flush
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
//#endregion
|
|
138
|
+
Object.defineProperty(exports, "HotUpdaterConfigError", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
get: function() {
|
|
141
|
+
return _hot_updater_plugin_core.HotUpdaterConfigError;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
exports.assemblePlugins = assemblePlugins;
|