@hot-updater/server 0.36.6 → 1.0.0-rc.0
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/adapters/databaseConstraintErrors.cjs +23 -0
- package/dist/adapters/databaseConstraintErrors.mjs +21 -0
- package/dist/adapters/databasePluginUtils.cjs +117 -0
- package/dist/adapters/databasePluginUtils.mjs +106 -0
- package/dist/adapters/drizzle.cjs +53 -176
- package/dist/adapters/drizzle.d.cts +6 -4
- package/dist/adapters/drizzle.d.mts +6 -4
- package/dist/adapters/drizzle.mjs +54 -177
- package/dist/adapters/drizzleCrud.cjs +250 -0
- package/dist/adapters/drizzleCrud.mjs +249 -0
- package/dist/adapters/drizzleLazyDB.cjs +84 -36
- package/dist/adapters/drizzleLazyDB.mjs +84 -36
- package/dist/adapters/drizzleQuery.cjs +68 -0
- package/dist/adapters/drizzleQuery.mjs +67 -0
- package/dist/adapters/kysely.cjs +18 -138
- package/dist/adapters/kysely.d.cts +3 -12
- package/dist/adapters/kysely.d.mts +3 -12
- package/dist/adapters/kysely.mjs +19 -139
- package/dist/adapters/kyselyCrud.cjs +254 -0
- package/dist/adapters/kyselyCrud.mjs +253 -0
- package/dist/adapters/kyselyQuery.cjs +56 -0
- package/dist/adapters/kyselyQuery.mjs +55 -0
- package/dist/adapters/mongodb.cjs +21 -174
- package/dist/adapters/mongodb.d.cts +3 -2
- package/dist/adapters/mongodb.d.mts +3 -2
- package/dist/adapters/mongodb.mjs +21 -174
- package/dist/adapters/mongodbCollections.cjs +36 -0
- package/dist/adapters/mongodbCollections.mjs +30 -0
- package/dist/adapters/mongodbQuery.cjs +77 -0
- package/dist/adapters/mongodbQuery.mjs +70 -0
- package/dist/adapters/mongodbReads.cjs +148 -0
- package/dist/adapters/mongodbReads.mjs +147 -0
- package/dist/adapters/mongodbWrites.cjs +134 -0
- package/dist/adapters/mongodbWrites.mjs +133 -0
- package/dist/adapters/prisma.cjs +205 -180
- package/dist/adapters/prisma.d.cts +3 -3
- package/dist/adapters/prisma.d.mts +3 -3
- package/dist/adapters/prisma.mjs +206 -181
- package/dist/adapters/prismaQuery.cjs +58 -0
- package/dist/adapters/prismaQuery.mjs +56 -0
- package/dist/adapters/prismaRows.cjs +198 -0
- package/dist/adapters/prismaRows.mjs +188 -0
- package/dist/analytics/bounded/activeOverview.cjs +87 -0
- package/dist/analytics/bounded/activeOverview.mjs +85 -0
- package/dist/analytics/bounded/installationSearch.cjs +42 -0
- package/dist/analytics/bounded/installationSearch.mjs +42 -0
- package/dist/analytics/bounded/persistence.cjs +39 -0
- package/dist/analytics/bounded/persistence.mjs +38 -0
- package/dist/analytics/bounded/provider.cjs +170 -0
- package/dist/analytics/bounded/provider.d.cts +7 -0
- package/dist/analytics/bounded/provider.d.mts +7 -0
- package/dist/analytics/bounded/provider.mjs +170 -0
- package/dist/analytics/bounded/scan.cjs +144 -0
- package/dist/analytics/bounded/scan.mjs +138 -0
- package/dist/analytics/domain.d.cts +121 -0
- package/dist/analytics/domain.d.mts +121 -0
- package/dist/analytics/errors.cjs +22 -0
- package/dist/analytics/errors.mjs +20 -0
- package/dist/analytics/eventInput.cjs +111 -0
- package/dist/analytics/eventInput.mjs +111 -0
- package/dist/analytics/persistence.d.cts +6 -0
- package/dist/analytics/persistence.d.mts +6 -0
- package/dist/analytics/queryInput.cjs +58 -0
- package/dist/analytics/queryInput.mjs +55 -0
- package/dist/analytics/routes.cjs +63 -0
- package/dist/analytics/routes.mjs +61 -0
- package/dist/analytics/types.d.cts +17 -0
- package/dist/analytics/types.d.mts +17 -0
- package/dist/apiKeys.cjs +139 -0
- package/dist/apiKeys.d.cts +50 -0
- package/dist/apiKeys.d.mts +50 -0
- package/dist/apiKeys.mjs +133 -0
- package/dist/createHotUpdaterCore.cjs +48 -26
- package/dist/createHotUpdaterCore.d.cts +49 -17
- package/dist/createHotUpdaterCore.d.mts +49 -17
- package/dist/createHotUpdaterCore.mjs +51 -29
- package/dist/db/createBundleDiff.cjs +45 -68
- package/dist/db/createBundleDiff.d.cts +3 -3
- package/dist/db/createBundleDiff.d.mts +3 -3
- package/dist/db/createBundleDiff.mjs +47 -68
- package/dist/db/databasePluginCore.cjs +138 -0
- package/dist/db/databasePluginCore.mjs +137 -0
- package/dist/db/fixedMigrator.cjs +36 -93
- package/dist/db/fixedMigrator.mjs +36 -93
- package/dist/db/fixedMigratorMongo.cjs +319 -0
- package/dist/db/fixedMigratorMongo.mjs +319 -0
- package/dist/db/fixedMigratorShared.cjs +23 -0
- package/dist/db/fixedMigratorShared.mjs +18 -0
- package/dist/db/mongoMigrationExecution.cjs +11 -0
- package/dist/db/mongoMigrationExecution.mjs +11 -0
- package/dist/db/releaseCatalog.cjs +66 -0
- package/dist/db/releaseCatalog.d.cts +17 -0
- package/dist/db/releaseCatalog.d.mts +17 -0
- package/dist/db/releaseCatalog.mjs +64 -0
- package/dist/db/schema/mongodb.cjs +14 -6
- package/dist/db/schema/mongodb.mjs +14 -6
- package/dist/db/schema/registry.cjs +0 -2
- package/dist/db/schema/registry.mjs +1 -2
- package/dist/db/schema/sql.cjs +31 -14
- package/dist/db/schema/sql.mjs +32 -13
- package/dist/db/schema/sqlOperations.cjs +4 -4
- package/dist/db/schema/sqlOperations.mjs +5 -5
- package/dist/db/schemaGenerators.cjs +52 -18
- package/dist/db/schemaGenerators.mjs +52 -18
- package/dist/db/schemaReadiness.cjs +4 -3
- package/dist/db/schemaReadiness.mjs +4 -3
- package/dist/db/sqlMigrationExecution.cjs +67 -0
- package/dist/db/sqlMigrationExecution.mjs +66 -0
- package/dist/db/types.cjs +2 -6
- package/dist/db/types.d.cts +60 -16
- package/dist/db/types.d.mts +60 -16
- package/dist/db/types.mjs +3 -6
- package/dist/db/updateArtifacts.cjs +114 -60
- package/dist/db/updateArtifacts.mjs +117 -62
- package/dist/handler.cjs +108 -282
- package/dist/handler.d.cts +3 -46
- package/dist/handler.d.mts +3 -46
- package/dist/handler.mjs +107 -281
- package/dist/handlerBundleRoutes.cjs +155 -0
- package/dist/handlerBundleRoutes.mjs +155 -0
- package/dist/handlerErrors.cjs +9 -0
- package/dist/handlerErrors.mjs +9 -0
- package/dist/handlerParameters.cjs +39 -0
- package/dist/handlerParameters.mjs +34 -0
- package/dist/handlerReleaseCatalogRoutes.cjs +111 -0
- package/dist/handlerReleaseCatalogRoutes.mjs +110 -0
- package/dist/handlerReleaseManagementRoutes.cjs +125 -0
- package/dist/handlerReleaseManagementRoutes.mjs +124 -0
- package/dist/handlerTypes.d.cts +63 -0
- package/dist/handlerTypes.d.mts +63 -0
- package/dist/handlerVersionRoutes.cjs +10 -0
- package/dist/handlerVersionRoutes.d.cts +4 -0
- package/dist/handlerVersionRoutes.d.mts +4 -0
- package/dist/handlerVersionRoutes.mjs +9 -0
- package/dist/index.cjs +10 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.mts +9 -3
- package/dist/index.mjs +5 -2
- package/dist/node.cjs +19 -12
- package/dist/node.d.cts +15 -10
- package/dist/node.d.mts +15 -10
- package/dist/node.mjs +19 -13
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/schema/dsl.cjs +19 -2
- package/dist/schema/dsl.mjs +17 -3
- package/dist/schema/index.cjs +1 -8
- package/dist/schema/index.mjs +2 -8
- package/dist/schema/types.cjs +3 -1
- package/dist/schema/types.mjs +3 -2
- package/dist/schema/v1_0_0.cjs +364 -0
- package/dist/schema/v1_0_0.mjs +357 -0
- package/dist/storageAccess.cjs +69 -25
- package/dist/storageAccess.mjs +68 -25
- package/dist/types/index.d.cts +4 -3
- package/dist/types/index.d.mts +5 -4
- package/package.json +8 -7
- package/src/adapters/databaseConstraintErrors.ts +25 -0
- package/src/adapters/databasePluginTestDatabase.ts +106 -0
- package/src/adapters/databasePluginUtils.spec.ts +29 -0
- package/src/adapters/databasePluginUtils.ts +214 -0
- package/src/adapters/drizzle.spec.ts +208 -47
- package/src/adapters/drizzle.ts +106 -293
- package/src/adapters/drizzleCrud.ts +419 -0
- package/src/adapters/drizzleLazyDB.ts +206 -100
- package/src/adapters/drizzleQuery.ts +164 -0
- package/src/adapters/kysely.spec.ts +126 -88
- package/src/adapters/kysely.ts +76 -334
- package/src/adapters/kyselyCrud.ts +588 -0
- package/src/adapters/kyselyQuery.ts +128 -0
- package/src/adapters/mongodb.spec.ts +141 -0
- package/src/adapters/mongodb.ts +60 -288
- package/src/adapters/mongodbCollections.ts +62 -0
- package/src/adapters/mongodbQuery.ts +226 -0
- package/src/adapters/mongodbReads.ts +311 -0
- package/src/adapters/mongodbRows.spec.ts +64 -0
- package/src/adapters/mongodbRows.ts +118 -0
- package/src/adapters/mongodbTestClient.ts +342 -0
- package/src/adapters/mongodbTestFilter.ts +166 -0
- package/src/adapters/mongodbWrites.ts +260 -0
- package/src/adapters/prisma.spec.ts +196 -0
- package/src/adapters/prisma.ts +417 -292
- package/src/adapters/prismaQuery.ts +129 -0
- package/src/adapters/prismaRows.spec.ts +98 -0
- package/src/adapters/prismaRows.ts +301 -0
- package/src/adapters/prismaTestClient.ts +396 -0
- package/src/analytics/analytics.integration.spec.ts +144 -0
- package/src/analytics/bounded/activeOverview.spec.ts +189 -0
- package/src/analytics/bounded/activeOverview.ts +148 -0
- package/src/analytics/bounded/installationSearch.spec.ts +130 -0
- package/src/analytics/bounded/installationSearch.ts +69 -0
- package/src/analytics/bounded/persistence.ts +43 -0
- package/src/analytics/bounded/provider.spec.ts +219 -0
- package/src/analytics/bounded/provider.ts +229 -0
- package/src/analytics/bounded/scan.ts +235 -0
- package/src/analytics/domain.ts +139 -0
- package/src/analytics/errors.ts +18 -0
- package/src/analytics/eventInput.ts +168 -0
- package/src/analytics/persistence.ts +10 -0
- package/src/analytics/queryInput.ts +102 -0
- package/src/analytics/routes.ts +123 -0
- package/src/analytics/types.ts +44 -0
- package/src/apiKeys.integration.spec.ts +192 -0
- package/src/apiKeys.spec.ts +162 -0
- package/src/apiKeys.ts +220 -0
- package/src/createHotUpdaterCore.ts +164 -100
- package/src/db/bundleRows.spec.ts +164 -0
- package/src/db/bundleRows.ts +10 -140
- package/src/db/createBundleDiff.spec.ts +172 -127
- package/src/db/createBundleDiff.ts +91 -110
- package/src/db/databasePluginCore.spec.ts +62 -0
- package/src/db/databasePluginCore.testFixtures.ts +76 -0
- package/src/db/databasePluginCore.ts +185 -0
- package/src/db/databasePluginUpdateCheck.bench.ts +107 -0
- package/src/db/fixedMigrator.spec.ts +184 -42
- package/src/db/fixedMigrator.ts +64 -205
- package/src/db/fixedMigratorMongo.spec.ts +192 -0
- package/src/db/fixedMigratorMongo.ts +425 -0
- package/src/db/fixedMigratorShared.ts +36 -0
- package/src/db/hotUpdaterSchema.ts +0 -1
- package/src/db/index.spec.ts +333 -1025
- package/src/db/mongoMigrationExecution.ts +23 -0
- package/src/db/node-sqlite.d.ts +25 -0
- package/src/db/releaseCatalog.ts +148 -0
- package/src/db/schema/mongodb.ts +20 -6
- package/src/db/schema/sql.ts +52 -11
- package/src/db/schema/sqlOperations.ts +5 -4
- package/src/db/schemaGenerators.ts +107 -23
- package/src/db/schemaReadiness.ts +2 -1
- package/src/db/sqlMigrationExecution.spec.ts +65 -0
- package/src/db/sqlMigrationExecution.ts +108 -0
- package/src/db/types.spec.ts +55 -0
- package/src/db/types.ts +129 -65
- package/src/db/updateArtifacts.spec.ts +359 -0
- package/src/db/updateArtifacts.ts +290 -126
- package/src/handler-standalone.integration.spec.ts +401 -364
- package/src/handler.spec.ts +137 -582
- package/src/handler.testFixtures.ts +30 -0
- package/src/handler.ts +218 -616
- package/src/handlerAuth.integration.spec.ts +33 -0
- package/src/handlerBundleRoutes.ts +259 -0
- package/src/handlerErrors.ts +7 -0
- package/src/handlerManagement.spec.ts +262 -0
- package/src/handlerParameters.ts +105 -0
- package/src/handlerReleaseCatalogRoutes.ts +154 -0
- package/src/handlerReleaseManagementRoutes.ts +213 -0
- package/src/handlerTypes.ts +96 -0
- package/src/handlerVersionRoutes.ts +12 -0
- package/src/index.ts +22 -2
- package/src/node.spec.ts +79 -10
- package/src/node.ts +20 -17
- package/src/releaseCatalogRoutes.integration.spec.ts +238 -0
- package/src/runtime.spec.ts +126 -959
- package/src/runtime.testFixtures.ts +71 -0
- package/src/schema/dsl-all-versions.spec.ts +26 -17
- package/src/schema/dsl.ts +41 -2
- package/src/schema/index.ts +3 -9
- package/src/schema/types.ts +9 -3
- package/src/schema/v1_0_0.spec.ts +146 -0
- package/src/schema/v1_0_0.ts +465 -0
- package/src/storageAccess.spec.ts +127 -32
- package/src/storageAccess.ts +106 -55
- package/src/types/index.ts +2 -1
- package/dist/db/bundleRows.cjs +0 -82
- package/dist/db/bundleRows.d.cts +0 -33
- package/dist/db/bundleRows.d.mts +0 -33
- package/dist/db/bundleRows.mjs +0 -79
- package/dist/db/pluginCore.cjs +0 -242
- package/dist/db/pluginCore.mjs +0 -241
- package/dist/db/schema/sqlMigrations.cjs +0 -74
- package/dist/db/schema/sqlMigrations.mjs +0 -73
- package/dist/db/schemaEnhancements.cjs +0 -20
- package/dist/db/schemaEnhancements.mjs +0 -19
- package/dist/route.cjs +0 -7
- package/dist/route.mjs +0 -7
- package/dist/schema/v0_21_0.cjs +0 -38
- package/dist/schema/v0_21_0.mjs +0 -37
- package/dist/schema/v0_29_0.cjs +0 -45
- package/dist/schema/v0_29_0.mjs +0 -44
- package/dist/schema/v0_31_0.cjs +0 -82
- package/dist/schema/v0_31_0.mjs +0 -80
- package/src/db/pluginCore.spec.ts +0 -1095
- package/src/db/pluginCore.ts +0 -475
- package/src/db/pluginUpdateCheck.bench.ts +0 -254
- package/src/db/schema/sqlMigrations.ts +0 -219
- package/src/db/schemaEnhancements.ts +0 -55
- package/src/route.ts +0 -7
- package/src/schema/v0_21_0.ts +0 -52
- package/src/schema/v0_29_0.ts +0 -62
- package/src/schema/v0_31_0.spec.ts +0 -73
- package/src/schema/v0_31_0.ts +0 -120
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { isChannelDeleteReferencedError, translateChannelDeleteError } from "./databaseConstraintErrors.mjs";
|
|
2
|
+
import { fromStoredBundleRow, fromStoredReleaseCatalogRow, fromStoredReleaseRow, toStoredBundleRow, toStoredBundleUpdate, toStoredReleaseRow, toStoredReleaseUpdate } from "./databasePluginUtils.mjs";
|
|
3
|
+
import { buildDrizzleWhere } from "./drizzleQuery.mjs";
|
|
4
|
+
import { DatabasePluginInputError } from "@hot-updater/plugin-core";
|
|
5
|
+
import { asc, desc, eq, sql } from "drizzle-orm";
|
|
6
|
+
//#region src/adapters/drizzleCrud.ts
|
|
7
|
+
var MissingDrizzleTableError = class extends Error {
|
|
8
|
+
name = "MissingDrizzleTableError";
|
|
9
|
+
constructor(table) {
|
|
10
|
+
super(`Drizzle schema is missing table "${table}".`);
|
|
11
|
+
this.table = table;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var DrizzleAdapterInvariantError = class extends Error {
|
|
15
|
+
name = "DrizzleAdapterInvariantError";
|
|
16
|
+
};
|
|
17
|
+
const getDrizzleTable = (db, name) => {
|
|
18
|
+
const table = db._.fullSchema[name];
|
|
19
|
+
if (table === void 0) throw new MissingDrizzleTableError(name);
|
|
20
|
+
return table;
|
|
21
|
+
};
|
|
22
|
+
const isSQLWrapper = (value) => typeof value === "object" && value !== null && "getSQL" in value && typeof value.getSQL === "function";
|
|
23
|
+
const getDrizzleColumn = (table, name) => {
|
|
24
|
+
const value = table[name];
|
|
25
|
+
if (!isSQLWrapper(value)) throw new MissingDrizzleTableError(`${name} column`);
|
|
26
|
+
return value;
|
|
27
|
+
};
|
|
28
|
+
const executeInsert = async (db, provider, table, data, onConflict) => {
|
|
29
|
+
const builder = db.insert(table);
|
|
30
|
+
const insert = builder.values(data);
|
|
31
|
+
if (onConflict === void 0) {
|
|
32
|
+
await insert.execute();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const ignored = provider === "mysql" ? builder.ignore?.().values(data) : insert.onConflictDoNothing?.();
|
|
36
|
+
if (ignored === void 0) throw new DrizzleAdapterInvariantError();
|
|
37
|
+
await ignored.execute();
|
|
38
|
+
};
|
|
39
|
+
const toOrderBy = (table, input) => {
|
|
40
|
+
return input.orderBy?.flatMap((clause) => {
|
|
41
|
+
const column = getDrizzleColumn(table, clause.field);
|
|
42
|
+
return [(clause.nulls ?? (clause.direction === "asc" ? "last" : "first")) === "first" ? sql`${column} is null desc` : sql`${column} is null asc`, clause.direction === "asc" ? asc(column) : desc(column)];
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
const createDrizzleCrud = (db, provider) => {
|
|
46
|
+
const bundles = getDrizzleTable(db, "bundles");
|
|
47
|
+
const patches = getDrizzleTable(db, "bundle_patches");
|
|
48
|
+
const events = getDrizzleTable(db, "bundle_events");
|
|
49
|
+
const releases = getDrizzleTable(db, "releases");
|
|
50
|
+
const releaseCatalogs = getDrizzleTable(db, "release_catalogs");
|
|
51
|
+
const channels = getDrizzleTable(db, "channels");
|
|
52
|
+
const apiKeys = getDrizzleTable(db, "api_keys");
|
|
53
|
+
return {
|
|
54
|
+
async deleteChannel({ id }) {
|
|
55
|
+
const idPredicate = eq(getDrizzleColumn(channels, "id"), id);
|
|
56
|
+
if (await db.query.channels.findFirst({ where: idPredicate }) === void 0) return {
|
|
57
|
+
deleted: false,
|
|
58
|
+
reason: "not_found"
|
|
59
|
+
};
|
|
60
|
+
if (await db.$count(releases, eq(getDrizzleColumn(releases, "channel_id"), id)) > 0) return {
|
|
61
|
+
deleted: false,
|
|
62
|
+
reason: "not_empty"
|
|
63
|
+
};
|
|
64
|
+
try {
|
|
65
|
+
await db.delete(channels).where(idPredicate).execute();
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (isChannelDeleteReferencedError(error)) return {
|
|
68
|
+
deleted: false,
|
|
69
|
+
reason: "not_empty"
|
|
70
|
+
};
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
return { deleted: true };
|
|
74
|
+
},
|
|
75
|
+
async insertChannel(input) {
|
|
76
|
+
const before = await db.query.channels.findFirst({ where: eq(getDrizzleColumn(channels, "name"), input.row.name) });
|
|
77
|
+
if (before !== void 0) return {
|
|
78
|
+
row: before,
|
|
79
|
+
inserted: false
|
|
80
|
+
};
|
|
81
|
+
await executeInsert(db, provider, channels, input.row, "ignore");
|
|
82
|
+
const row = await db.query.channels.findFirst({ where: eq(getDrizzleColumn(channels, "name"), input.row.name) });
|
|
83
|
+
if (row === void 0) throw new DrizzleAdapterInvariantError();
|
|
84
|
+
return {
|
|
85
|
+
row,
|
|
86
|
+
inserted: row.id === input.row.id
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
async create(input) {
|
|
90
|
+
switch (input.model) {
|
|
91
|
+
case "bundles":
|
|
92
|
+
await executeInsert(db, provider, bundles, toStoredBundleRow(input.data, provider), void 0);
|
|
93
|
+
return input.data;
|
|
94
|
+
case "bundle_patches":
|
|
95
|
+
await executeInsert(db, provider, patches, input.data, void 0);
|
|
96
|
+
return input.data;
|
|
97
|
+
case "bundle_events":
|
|
98
|
+
await executeInsert(db, provider, events, input.data, void 0);
|
|
99
|
+
return input.data;
|
|
100
|
+
case "releases":
|
|
101
|
+
await executeInsert(db, provider, releases, toStoredReleaseRow(input.data, provider), void 0);
|
|
102
|
+
return input.data;
|
|
103
|
+
case "release_catalogs":
|
|
104
|
+
await executeInsert(db, provider, releaseCatalogs, input.data, void 0);
|
|
105
|
+
return input.data;
|
|
106
|
+
case "api_keys":
|
|
107
|
+
await executeInsert(db, provider, apiKeys, input.data, input.onConflict);
|
|
108
|
+
return input.data;
|
|
109
|
+
case "channels":
|
|
110
|
+
await executeInsert(db, provider, channels, input.data, input.onConflict);
|
|
111
|
+
return input.data;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
async update(input) {
|
|
115
|
+
const selector = input.where[0];
|
|
116
|
+
if (selector === void 0 || typeof selector.value !== "string") throw new DrizzleAdapterInvariantError();
|
|
117
|
+
if (input.model === "api_keys") {
|
|
118
|
+
const idPredicate = eq(getDrizzleColumn(apiKeys, "id"), selector.value);
|
|
119
|
+
await db.update(apiKeys).set(input.update).where(idPredicate).execute();
|
|
120
|
+
return await db.query.api_keys.findFirst({ where: idPredicate }) ?? null;
|
|
121
|
+
}
|
|
122
|
+
if (input.model === "releases") {
|
|
123
|
+
const idPredicate = eq(getDrizzleColumn(releases, "id"), selector.value);
|
|
124
|
+
await db.update(releases).set(toStoredReleaseUpdate(input.update, provider)).where(idPredicate).execute();
|
|
125
|
+
const row = await db.query.releases.findFirst({ where: idPredicate });
|
|
126
|
+
return row === void 0 ? null : fromStoredReleaseRow(row);
|
|
127
|
+
}
|
|
128
|
+
if (input.model === "release_catalogs") {
|
|
129
|
+
const scopePredicate = eq(getDrizzleColumn(releaseCatalogs, "scope_key"), selector.value);
|
|
130
|
+
await db.update(releaseCatalogs).set(input.update).where(scopePredicate).execute();
|
|
131
|
+
const row = await db.query.release_catalogs.findFirst({ where: scopePredicate });
|
|
132
|
+
return row === void 0 ? null : fromStoredReleaseCatalogRow(row);
|
|
133
|
+
}
|
|
134
|
+
const idPredicate = eq(getDrizzleColumn(bundles, "id"), selector.value);
|
|
135
|
+
await db.update(bundles).set(toStoredBundleUpdate(input.update, provider)).where(idPredicate).execute();
|
|
136
|
+
const stored = await db.query.bundles.findFirst({ where: idPredicate });
|
|
137
|
+
if (stored === void 0) return null;
|
|
138
|
+
return fromStoredBundleRow(stored);
|
|
139
|
+
},
|
|
140
|
+
async delete(input) {
|
|
141
|
+
switch (input.model) {
|
|
142
|
+
case "bundles": {
|
|
143
|
+
const where = buildDrizzleWhere(provider, bundles, input.where);
|
|
144
|
+
if (where === void 0) throw new DrizzleAdapterInvariantError();
|
|
145
|
+
await db.delete(bundles).where(where).execute();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
case "bundle_patches": {
|
|
149
|
+
const where = buildDrizzleWhere(provider, patches, input.where);
|
|
150
|
+
if (where === void 0) throw new DrizzleAdapterInvariantError();
|
|
151
|
+
await db.delete(patches).where(where).execute();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
case "releases": {
|
|
155
|
+
const where = buildDrizzleWhere(provider, releases, input.where);
|
|
156
|
+
if (where === void 0) throw new DrizzleAdapterInvariantError();
|
|
157
|
+
await db.delete(releases).where(where).execute();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
case "channels": {
|
|
161
|
+
const where = buildDrizzleWhere(provider, channels, input.where);
|
|
162
|
+
if (where === void 0) throw new DrizzleAdapterInvariantError();
|
|
163
|
+
try {
|
|
164
|
+
await db.delete(channels).where(where).execute();
|
|
165
|
+
} catch (error) {
|
|
166
|
+
translateChannelDeleteError(error);
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
async count(input) {
|
|
173
|
+
if (input.distinct !== void 0) throw new DatabasePluginInputError("invalid-operation");
|
|
174
|
+
switch (input.model) {
|
|
175
|
+
case "bundles": return db.$count(bundles, buildDrizzleWhere(provider, bundles, input.where));
|
|
176
|
+
case "bundle_patches": return db.$count(patches, buildDrizzleWhere(provider, patches, input.where));
|
|
177
|
+
case "releases": return db.$count(releases, buildDrizzleWhere(provider, releases, input.where));
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
async findOne(input) {
|
|
181
|
+
switch (input.model) {
|
|
182
|
+
case "bundles": {
|
|
183
|
+
const row = await db.query.bundles.findFirst({ where: buildDrizzleWhere(provider, bundles, input.where) });
|
|
184
|
+
return row === void 0 ? null : fromStoredBundleRow(row);
|
|
185
|
+
}
|
|
186
|
+
case "bundle_patches": return await db.query.bundle_patches.findFirst({ where: buildDrizzleWhere(provider, patches, input.where) }) ?? null;
|
|
187
|
+
case "api_keys": return await db.query.api_keys.findFirst({ where: buildDrizzleWhere(provider, apiKeys, input.where) }) ?? null;
|
|
188
|
+
case "channels": return await db.query.channels.findFirst({ where: buildDrizzleWhere(provider, channels, input.where) }) ?? null;
|
|
189
|
+
case "releases": {
|
|
190
|
+
const row = await db.query.releases.findFirst({ where: buildDrizzleWhere(provider, releases, input.where) });
|
|
191
|
+
return row === void 0 ? null : fromStoredReleaseRow(row);
|
|
192
|
+
}
|
|
193
|
+
case "release_catalogs": {
|
|
194
|
+
const row = await db.query.release_catalogs.findFirst({ where: buildDrizzleWhere(provider, releaseCatalogs, input.where) });
|
|
195
|
+
return row === void 0 ? null : fromStoredReleaseCatalogRow(row);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
async findMany(input) {
|
|
200
|
+
if (input.distinctOn !== void 0) throw new DatabasePluginInputError("invalid-operation");
|
|
201
|
+
switch (input.model) {
|
|
202
|
+
case "bundles": return (await db.query.bundles.findMany({
|
|
203
|
+
where: buildDrizzleWhere(provider, bundles, input.where),
|
|
204
|
+
orderBy: toOrderBy(bundles, input),
|
|
205
|
+
limit: input.limit,
|
|
206
|
+
offset: input.offset
|
|
207
|
+
})).map(fromStoredBundleRow);
|
|
208
|
+
case "bundle_events": return db.query.bundle_events.findMany({
|
|
209
|
+
where: buildDrizzleWhere(provider, events, input.where),
|
|
210
|
+
orderBy: toOrderBy(events, input),
|
|
211
|
+
limit: input.limit,
|
|
212
|
+
offset: input.offset
|
|
213
|
+
});
|
|
214
|
+
case "api_keys": return db.query.api_keys.findMany({
|
|
215
|
+
where: buildDrizzleWhere(provider, apiKeys, input.where),
|
|
216
|
+
orderBy: toOrderBy(apiKeys, input),
|
|
217
|
+
limit: input.limit,
|
|
218
|
+
offset: input.offset
|
|
219
|
+
});
|
|
220
|
+
case "bundle_patches": return db.query.bundle_patches.findMany({
|
|
221
|
+
where: buildDrizzleWhere(provider, patches, input.where),
|
|
222
|
+
orderBy: toOrderBy(patches, input),
|
|
223
|
+
limit: input.limit,
|
|
224
|
+
offset: input.offset
|
|
225
|
+
});
|
|
226
|
+
case "channels": return db.query.channels.findMany({
|
|
227
|
+
where: buildDrizzleWhere(provider, channels, input.where),
|
|
228
|
+
orderBy: toOrderBy(channels, input),
|
|
229
|
+
limit: input.limit,
|
|
230
|
+
offset: input.offset
|
|
231
|
+
});
|
|
232
|
+
case "releases": return (await db.query.releases.findMany({
|
|
233
|
+
where: buildDrizzleWhere(provider, releases, input.where),
|
|
234
|
+
orderBy: toOrderBy(releases, input),
|
|
235
|
+
limit: input.limit,
|
|
236
|
+
offset: input.offset
|
|
237
|
+
})).map(fromStoredReleaseRow);
|
|
238
|
+
case "release_catalogs": return (await db.query.release_catalogs.findMany({
|
|
239
|
+
where: buildDrizzleWhere(provider, releaseCatalogs, input.where),
|
|
240
|
+
orderBy: toOrderBy(releaseCatalogs, input),
|
|
241
|
+
limit: input.limit,
|
|
242
|
+
offset: input.offset
|
|
243
|
+
})).map(fromStoredReleaseCatalogRow);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
//#endregion
|
|
249
|
+
export { createDrizzleCrud };
|
|
@@ -1,52 +1,100 @@
|
|
|
1
1
|
//#region src/adapters/drizzleLazyDB.ts
|
|
2
|
+
var InvalidDrizzleDatabaseError = class extends Error {
|
|
3
|
+
name = "InvalidDrizzleDatabaseError";
|
|
4
|
+
constructor(reason) {
|
|
5
|
+
super(`[hot-updater] ${reason}`);
|
|
6
|
+
this.reason = reason;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
10
|
+
const hasFunction = (value, key) => typeof value[key] === "function";
|
|
11
|
+
const isDrizzleQuery = (value) => isRecord(value) && hasFunction(value, "findFirst") && hasFunction(value, "findMany");
|
|
12
|
+
const isDrizzleDB = (value) => {
|
|
13
|
+
if (!isRecord(value)) return false;
|
|
14
|
+
const metadata = value["_"];
|
|
15
|
+
if (!isRecord(metadata) || !isRecord(metadata["fullSchema"])) return false;
|
|
16
|
+
const query = value["query"];
|
|
17
|
+
if (!isRecord(query) || !isDrizzleQuery(query["bundle_events"]) || !isDrizzleQuery(query["bundle_patches"]) || !isDrizzleQuery(query["bundles"]) || !isDrizzleQuery(query["channels"]) || !isDrizzleQuery(query["api_keys"]) || !isDrizzleQuery(query["releases"]) || !isDrizzleQuery(query["release_catalogs"])) return false;
|
|
18
|
+
if (value["transaction"] !== void 0 && !hasFunction(value, "transaction")) return false;
|
|
19
|
+
return hasFunction(value, "$count") && hasFunction(value, "delete") && hasFunction(value, "insert") && hasFunction(value, "update");
|
|
20
|
+
};
|
|
2
21
|
const asDB = (db) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return typed;
|
|
22
|
+
if (!isDrizzleDB(db)) throw new InvalidDrizzleDatabaseError("Drizzle adapter requires query mode with schema.");
|
|
23
|
+
return db;
|
|
6
24
|
};
|
|
7
25
|
const isDBFactory = (db) => typeof db === "function";
|
|
26
|
+
const parseSchema = (schema) => {
|
|
27
|
+
const parsed = {};
|
|
28
|
+
for (const [name, table] of Object.entries(schema)) {
|
|
29
|
+
if (!isRecord(table)) throw new InvalidDrizzleDatabaseError(`Drizzle schema table "${name}" is invalid.`);
|
|
30
|
+
parsed[name] = table;
|
|
31
|
+
}
|
|
32
|
+
return parsed;
|
|
33
|
+
};
|
|
8
34
|
const createLazyDB = (config) => {
|
|
9
|
-
|
|
10
|
-
if (!
|
|
11
|
-
|
|
35
|
+
if (!isDBFactory(config.db)) return asDB(config.db);
|
|
36
|
+
if (!config.schema) throw new InvalidDrizzleDatabaseError("Drizzle adapter requires schema when db is lazy.");
|
|
37
|
+
const source = config.db;
|
|
12
38
|
let resolvedDB;
|
|
13
|
-
const getDB =
|
|
14
|
-
resolvedDB ??= Promise.resolve(
|
|
39
|
+
const getDB = () => {
|
|
40
|
+
resolvedDB ??= Promise.resolve(source()).then(asDB);
|
|
15
41
|
return resolvedDB;
|
|
16
42
|
};
|
|
17
|
-
const fullSchema = config.schema;
|
|
18
|
-
const runInserted = async (table, value, operation) => {
|
|
19
|
-
return operation((await getDB()).insert(table).values(value));
|
|
20
|
-
};
|
|
21
43
|
return {
|
|
22
|
-
_: { fullSchema },
|
|
44
|
+
_: { fullSchema: parseSchema(config.schema) },
|
|
23
45
|
$count: async (table, where) => (await getDB()).$count(table, where),
|
|
24
|
-
delete: (table) => ({ where: async (
|
|
25
|
-
insert: (table) => ({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
delete: (table) => ({ where: (condition) => ({ execute: async () => (await getDB()).delete(table).where(condition).execute() }) }),
|
|
47
|
+
insert: (table) => ({
|
|
48
|
+
ignore: () => ({ values: (value) => ({ execute: async () => {
|
|
49
|
+
const insert = (await getDB()).insert(table).ignore;
|
|
50
|
+
if (insert === void 0) throw new InvalidDrizzleDatabaseError("The resolved Drizzle database does not support insert ignore.");
|
|
51
|
+
return insert().values(value).execute();
|
|
52
|
+
} }) }),
|
|
53
|
+
values: (value) => ({
|
|
54
|
+
execute: async () => (await getDB()).insert(table).values(value).execute(),
|
|
55
|
+
onConflictDoNothing: () => ({ execute: async () => {
|
|
56
|
+
const insert = (await getDB()).insert(table).values(value);
|
|
57
|
+
if (insert.onConflictDoNothing === void 0) throw new InvalidDrizzleDatabaseError("The resolved Drizzle database does not support conflict-ignore inserts.");
|
|
58
|
+
return insert.onConflictDoNothing().execute();
|
|
59
|
+
} })
|
|
37
60
|
})
|
|
38
|
-
})
|
|
39
|
-
query:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
}),
|
|
62
|
+
query: {
|
|
63
|
+
bundle_events: {
|
|
64
|
+
findFirst: async (args) => (await getDB()).query.bundle_events.findFirst(args),
|
|
65
|
+
findMany: async (args) => (await getDB()).query.bundle_events.findMany(args)
|
|
66
|
+
},
|
|
67
|
+
bundle_patches: {
|
|
68
|
+
findFirst: async (args) => (await getDB()).query.bundle_patches.findFirst(args),
|
|
69
|
+
findMany: async (args) => (await getDB()).query.bundle_patches.findMany(args)
|
|
70
|
+
},
|
|
71
|
+
bundles: {
|
|
72
|
+
findFirst: async (args) => (await getDB()).query.bundles.findFirst(args),
|
|
73
|
+
findMany: async (args) => (await getDB()).query.bundles.findMany(args)
|
|
74
|
+
},
|
|
75
|
+
channels: {
|
|
76
|
+
findFirst: async (args) => (await getDB()).query.channels.findFirst(args),
|
|
77
|
+
findMany: async (args) => (await getDB()).query.channels.findMany(args)
|
|
78
|
+
},
|
|
79
|
+
api_keys: {
|
|
80
|
+
findFirst: async (args) => (await getDB()).query.api_keys.findFirst(args),
|
|
81
|
+
findMany: async (args) => (await getDB()).query.api_keys.findMany(args)
|
|
82
|
+
},
|
|
83
|
+
releases: {
|
|
84
|
+
findFirst: async (args) => (await getDB()).query.releases.findFirst(args),
|
|
85
|
+
findMany: async (args) => (await getDB()).query.releases.findMany(args)
|
|
86
|
+
},
|
|
87
|
+
release_catalogs: {
|
|
88
|
+
findFirst: async (args) => (await getDB()).query.release_catalogs.findFirst(args),
|
|
89
|
+
findMany: async (args) => (await getDB()).query.release_catalogs.findMany(args)
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
update: (table) => ({ set: (values) => ({ where: (condition) => ({ execute: async () => (await getDB()).update(table).set(values).where(condition).execute() }) }) }),
|
|
93
|
+
...config.transaction === true ? { transaction: async (operation) => {
|
|
46
94
|
const db = await getDB();
|
|
47
|
-
if (
|
|
95
|
+
if (db.transaction === void 0) throw new InvalidDrizzleDatabaseError("The resolved Drizzle database does not support transactions.");
|
|
48
96
|
return db.transaction(operation);
|
|
49
|
-
}
|
|
97
|
+
} } : {}
|
|
50
98
|
};
|
|
51
99
|
};
|
|
52
100
|
//#endregion
|
|
@@ -1,52 +1,100 @@
|
|
|
1
1
|
//#region src/adapters/drizzleLazyDB.ts
|
|
2
|
+
var InvalidDrizzleDatabaseError = class extends Error {
|
|
3
|
+
name = "InvalidDrizzleDatabaseError";
|
|
4
|
+
constructor(reason) {
|
|
5
|
+
super(`[hot-updater] ${reason}`);
|
|
6
|
+
this.reason = reason;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
10
|
+
const hasFunction = (value, key) => typeof value[key] === "function";
|
|
11
|
+
const isDrizzleQuery = (value) => isRecord(value) && hasFunction(value, "findFirst") && hasFunction(value, "findMany");
|
|
12
|
+
const isDrizzleDB = (value) => {
|
|
13
|
+
if (!isRecord(value)) return false;
|
|
14
|
+
const metadata = value["_"];
|
|
15
|
+
if (!isRecord(metadata) || !isRecord(metadata["fullSchema"])) return false;
|
|
16
|
+
const query = value["query"];
|
|
17
|
+
if (!isRecord(query) || !isDrizzleQuery(query["bundle_events"]) || !isDrizzleQuery(query["bundle_patches"]) || !isDrizzleQuery(query["bundles"]) || !isDrizzleQuery(query["channels"]) || !isDrizzleQuery(query["api_keys"]) || !isDrizzleQuery(query["releases"]) || !isDrizzleQuery(query["release_catalogs"])) return false;
|
|
18
|
+
if (value["transaction"] !== void 0 && !hasFunction(value, "transaction")) return false;
|
|
19
|
+
return hasFunction(value, "$count") && hasFunction(value, "delete") && hasFunction(value, "insert") && hasFunction(value, "update");
|
|
20
|
+
};
|
|
2
21
|
const asDB = (db) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return typed;
|
|
22
|
+
if (!isDrizzleDB(db)) throw new InvalidDrizzleDatabaseError("Drizzle adapter requires query mode with schema.");
|
|
23
|
+
return db;
|
|
6
24
|
};
|
|
7
25
|
const isDBFactory = (db) => typeof db === "function";
|
|
26
|
+
const parseSchema = (schema) => {
|
|
27
|
+
const parsed = {};
|
|
28
|
+
for (const [name, table] of Object.entries(schema)) {
|
|
29
|
+
if (!isRecord(table)) throw new InvalidDrizzleDatabaseError(`Drizzle schema table "${name}" is invalid.`);
|
|
30
|
+
parsed[name] = table;
|
|
31
|
+
}
|
|
32
|
+
return parsed;
|
|
33
|
+
};
|
|
8
34
|
const createLazyDB = (config) => {
|
|
9
|
-
|
|
10
|
-
if (!
|
|
11
|
-
|
|
35
|
+
if (!isDBFactory(config.db)) return asDB(config.db);
|
|
36
|
+
if (!config.schema) throw new InvalidDrizzleDatabaseError("Drizzle adapter requires schema when db is lazy.");
|
|
37
|
+
const source = config.db;
|
|
12
38
|
let resolvedDB;
|
|
13
|
-
const getDB =
|
|
14
|
-
resolvedDB ??= Promise.resolve(
|
|
39
|
+
const getDB = () => {
|
|
40
|
+
resolvedDB ??= Promise.resolve(source()).then(asDB);
|
|
15
41
|
return resolvedDB;
|
|
16
42
|
};
|
|
17
|
-
const fullSchema = config.schema;
|
|
18
|
-
const runInserted = async (table, value, operation) => {
|
|
19
|
-
return operation((await getDB()).insert(table).values(value));
|
|
20
|
-
};
|
|
21
43
|
return {
|
|
22
|
-
_: { fullSchema },
|
|
44
|
+
_: { fullSchema: parseSchema(config.schema) },
|
|
23
45
|
$count: async (table, where) => (await getDB()).$count(table, where),
|
|
24
|
-
delete: (table) => ({ where: async (
|
|
25
|
-
insert: (table) => ({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
delete: (table) => ({ where: (condition) => ({ execute: async () => (await getDB()).delete(table).where(condition).execute() }) }),
|
|
47
|
+
insert: (table) => ({
|
|
48
|
+
ignore: () => ({ values: (value) => ({ execute: async () => {
|
|
49
|
+
const insert = (await getDB()).insert(table).ignore;
|
|
50
|
+
if (insert === void 0) throw new InvalidDrizzleDatabaseError("The resolved Drizzle database does not support insert ignore.");
|
|
51
|
+
return insert().values(value).execute();
|
|
52
|
+
} }) }),
|
|
53
|
+
values: (value) => ({
|
|
54
|
+
execute: async () => (await getDB()).insert(table).values(value).execute(),
|
|
55
|
+
onConflictDoNothing: () => ({ execute: async () => {
|
|
56
|
+
const insert = (await getDB()).insert(table).values(value);
|
|
57
|
+
if (insert.onConflictDoNothing === void 0) throw new InvalidDrizzleDatabaseError("The resolved Drizzle database does not support conflict-ignore inserts.");
|
|
58
|
+
return insert.onConflictDoNothing().execute();
|
|
59
|
+
} })
|
|
37
60
|
})
|
|
38
|
-
})
|
|
39
|
-
query:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
}),
|
|
62
|
+
query: {
|
|
63
|
+
bundle_events: {
|
|
64
|
+
findFirst: async (args) => (await getDB()).query.bundle_events.findFirst(args),
|
|
65
|
+
findMany: async (args) => (await getDB()).query.bundle_events.findMany(args)
|
|
66
|
+
},
|
|
67
|
+
bundle_patches: {
|
|
68
|
+
findFirst: async (args) => (await getDB()).query.bundle_patches.findFirst(args),
|
|
69
|
+
findMany: async (args) => (await getDB()).query.bundle_patches.findMany(args)
|
|
70
|
+
},
|
|
71
|
+
bundles: {
|
|
72
|
+
findFirst: async (args) => (await getDB()).query.bundles.findFirst(args),
|
|
73
|
+
findMany: async (args) => (await getDB()).query.bundles.findMany(args)
|
|
74
|
+
},
|
|
75
|
+
channels: {
|
|
76
|
+
findFirst: async (args) => (await getDB()).query.channels.findFirst(args),
|
|
77
|
+
findMany: async (args) => (await getDB()).query.channels.findMany(args)
|
|
78
|
+
},
|
|
79
|
+
api_keys: {
|
|
80
|
+
findFirst: async (args) => (await getDB()).query.api_keys.findFirst(args),
|
|
81
|
+
findMany: async (args) => (await getDB()).query.api_keys.findMany(args)
|
|
82
|
+
},
|
|
83
|
+
releases: {
|
|
84
|
+
findFirst: async (args) => (await getDB()).query.releases.findFirst(args),
|
|
85
|
+
findMany: async (args) => (await getDB()).query.releases.findMany(args)
|
|
86
|
+
},
|
|
87
|
+
release_catalogs: {
|
|
88
|
+
findFirst: async (args) => (await getDB()).query.release_catalogs.findFirst(args),
|
|
89
|
+
findMany: async (args) => (await getDB()).query.release_catalogs.findMany(args)
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
update: (table) => ({ set: (values) => ({ where: (condition) => ({ execute: async () => (await getDB()).update(table).set(values).where(condition).execute() }) }) }),
|
|
93
|
+
...config.transaction === true ? { transaction: async (operation) => {
|
|
46
94
|
const db = await getDB();
|
|
47
|
-
if (
|
|
95
|
+
if (db.transaction === void 0) throw new InvalidDrizzleDatabaseError("The resolved Drizzle database does not support transactions.");
|
|
48
96
|
return db.transaction(operation);
|
|
49
|
-
}
|
|
97
|
+
} } : {}
|
|
50
98
|
};
|
|
51
99
|
};
|
|
52
100
|
//#endregion
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require_databasePluginUtils = require("./databasePluginUtils.cjs");
|
|
3
|
+
let drizzle_orm = require("drizzle-orm");
|
|
4
|
+
//#region src/adapters/drizzleQuery.ts
|
|
5
|
+
var InvalidDatabasePredicateError = class extends Error {
|
|
6
|
+
name = "InvalidDatabasePredicateError";
|
|
7
|
+
};
|
|
8
|
+
var MissingDrizzleColumnError = class extends Error {
|
|
9
|
+
name = "MissingDrizzleColumnError";
|
|
10
|
+
constructor(column) {
|
|
11
|
+
super(`Drizzle schema is missing column "${column}".`);
|
|
12
|
+
this.column = column;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const isSQLWrapper = (value) => typeof value === "object" && value !== null && "getSQL" in value && typeof value.getSQL === "function";
|
|
16
|
+
const column = (table, field) => {
|
|
17
|
+
const value = table[field];
|
|
18
|
+
if (!isSQLWrapper(value)) throw new MissingDrizzleColumnError(field);
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
21
|
+
const stringPredicate = (provider, field, operator, value, insensitive) => {
|
|
22
|
+
if (provider === "sqlite" && !insensitive) {
|
|
23
|
+
const literal = require_databasePluginUtils.escapeGlobPattern(value);
|
|
24
|
+
return drizzle_orm.sql`${field} glob ${operator === "contains" ? `*${literal}*` : operator === "starts_with" ? `${literal}*` : `*${literal}`}`;
|
|
25
|
+
}
|
|
26
|
+
const literal = require_databasePluginUtils.escapeLikePattern(value);
|
|
27
|
+
const pattern = operator === "contains" ? `%${literal}%` : operator === "starts_with" ? `${literal}%` : `%${literal}`;
|
|
28
|
+
if (insensitive) return drizzle_orm.sql`lower(${field}) like lower(${pattern}) escape '\\'`;
|
|
29
|
+
if (provider === "mysql") return drizzle_orm.sql`binary ${field} like binary ${pattern} escape '\\'`;
|
|
30
|
+
return drizzle_orm.sql`${field} like ${pattern} escape '\\'`;
|
|
31
|
+
};
|
|
32
|
+
const predicate = (provider, table, condition) => {
|
|
33
|
+
const field = column(table, condition.field);
|
|
34
|
+
const operator = condition.operator ?? "eq";
|
|
35
|
+
switch (operator) {
|
|
36
|
+
case "eq":
|
|
37
|
+
case "ne":
|
|
38
|
+
if (condition.value === null) return operator === "eq" ? (0, drizzle_orm.isNull)(field) : (0, drizzle_orm.isNotNull)(field);
|
|
39
|
+
if ("mode" in condition && condition.mode === "insensitive") return operator === "eq" ? drizzle_orm.sql`lower(${field}) = lower(${condition.value})` : drizzle_orm.sql`lower(${field}) <> lower(${condition.value})`;
|
|
40
|
+
return operator === "eq" ? (0, drizzle_orm.eq)(field, condition.value) : (0, drizzle_orm.ne)(field, condition.value);
|
|
41
|
+
case "gt": return (0, drizzle_orm.gt)(field, condition.value);
|
|
42
|
+
case "gte": return (0, drizzle_orm.gte)(field, condition.value);
|
|
43
|
+
case "lt": return (0, drizzle_orm.lt)(field, condition.value);
|
|
44
|
+
case "lte": return (0, drizzle_orm.lte)(field, condition.value);
|
|
45
|
+
case "in":
|
|
46
|
+
case "not_in":
|
|
47
|
+
if (!Array.isArray(condition.value)) throw new InvalidDatabasePredicateError();
|
|
48
|
+
if (operator === "in") return condition.value.length === 0 ? drizzle_orm.sql`false` : (0, drizzle_orm.inArray)(field, condition.value);
|
|
49
|
+
return condition.value.length === 0 ? drizzle_orm.sql`true` : (0, drizzle_orm.notInArray)(field, condition.value);
|
|
50
|
+
case "contains":
|
|
51
|
+
case "starts_with":
|
|
52
|
+
case "ends_with":
|
|
53
|
+
if (typeof condition.value !== "string") throw new InvalidDatabasePredicateError();
|
|
54
|
+
return stringPredicate(provider, field, operator, condition.value, "mode" in condition && condition.mode === "insensitive");
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const buildDrizzleWhere = (provider, table, where) => {
|
|
58
|
+
const [first, ...rest] = Array.isArray(where) ? where : [];
|
|
59
|
+
if (first === void 0) return void 0;
|
|
60
|
+
let expression = predicate(provider, table, first);
|
|
61
|
+
for (const condition of rest) {
|
|
62
|
+
const next = predicate(provider, table, condition);
|
|
63
|
+
expression = condition.connector === "OR" ? drizzle_orm.sql`(${expression} or ${next})` : drizzle_orm.sql`(${expression} and ${next})`;
|
|
64
|
+
}
|
|
65
|
+
return expression;
|
|
66
|
+
};
|
|
67
|
+
//#endregion
|
|
68
|
+
exports.buildDrizzleWhere = buildDrizzleWhere;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { escapeGlobPattern, escapeLikePattern } from "./databasePluginUtils.mjs";
|
|
2
|
+
import { eq, gt, gte, inArray, isNotNull, isNull, lt, lte, ne, notInArray, sql } from "drizzle-orm";
|
|
3
|
+
//#region src/adapters/drizzleQuery.ts
|
|
4
|
+
var InvalidDatabasePredicateError = class extends Error {
|
|
5
|
+
name = "InvalidDatabasePredicateError";
|
|
6
|
+
};
|
|
7
|
+
var MissingDrizzleColumnError = class extends Error {
|
|
8
|
+
name = "MissingDrizzleColumnError";
|
|
9
|
+
constructor(column) {
|
|
10
|
+
super(`Drizzle schema is missing column "${column}".`);
|
|
11
|
+
this.column = column;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const isSQLWrapper = (value) => typeof value === "object" && value !== null && "getSQL" in value && typeof value.getSQL === "function";
|
|
15
|
+
const column = (table, field) => {
|
|
16
|
+
const value = table[field];
|
|
17
|
+
if (!isSQLWrapper(value)) throw new MissingDrizzleColumnError(field);
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
const stringPredicate = (provider, field, operator, value, insensitive) => {
|
|
21
|
+
if (provider === "sqlite" && !insensitive) {
|
|
22
|
+
const literal = escapeGlobPattern(value);
|
|
23
|
+
return sql`${field} glob ${operator === "contains" ? `*${literal}*` : operator === "starts_with" ? `${literal}*` : `*${literal}`}`;
|
|
24
|
+
}
|
|
25
|
+
const literal = escapeLikePattern(value);
|
|
26
|
+
const pattern = operator === "contains" ? `%${literal}%` : operator === "starts_with" ? `${literal}%` : `%${literal}`;
|
|
27
|
+
if (insensitive) return sql`lower(${field}) like lower(${pattern}) escape '\\'`;
|
|
28
|
+
if (provider === "mysql") return sql`binary ${field} like binary ${pattern} escape '\\'`;
|
|
29
|
+
return sql`${field} like ${pattern} escape '\\'`;
|
|
30
|
+
};
|
|
31
|
+
const predicate = (provider, table, condition) => {
|
|
32
|
+
const field = column(table, condition.field);
|
|
33
|
+
const operator = condition.operator ?? "eq";
|
|
34
|
+
switch (operator) {
|
|
35
|
+
case "eq":
|
|
36
|
+
case "ne":
|
|
37
|
+
if (condition.value === null) return operator === "eq" ? isNull(field) : isNotNull(field);
|
|
38
|
+
if ("mode" in condition && condition.mode === "insensitive") return operator === "eq" ? sql`lower(${field}) = lower(${condition.value})` : sql`lower(${field}) <> lower(${condition.value})`;
|
|
39
|
+
return operator === "eq" ? eq(field, condition.value) : ne(field, condition.value);
|
|
40
|
+
case "gt": return gt(field, condition.value);
|
|
41
|
+
case "gte": return gte(field, condition.value);
|
|
42
|
+
case "lt": return lt(field, condition.value);
|
|
43
|
+
case "lte": return lte(field, condition.value);
|
|
44
|
+
case "in":
|
|
45
|
+
case "not_in":
|
|
46
|
+
if (!Array.isArray(condition.value)) throw new InvalidDatabasePredicateError();
|
|
47
|
+
if (operator === "in") return condition.value.length === 0 ? sql`false` : inArray(field, condition.value);
|
|
48
|
+
return condition.value.length === 0 ? sql`true` : notInArray(field, condition.value);
|
|
49
|
+
case "contains":
|
|
50
|
+
case "starts_with":
|
|
51
|
+
case "ends_with":
|
|
52
|
+
if (typeof condition.value !== "string") throw new InvalidDatabasePredicateError();
|
|
53
|
+
return stringPredicate(provider, field, operator, condition.value, "mode" in condition && condition.mode === "insensitive");
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const buildDrizzleWhere = (provider, table, where) => {
|
|
57
|
+
const [first, ...rest] = Array.isArray(where) ? where : [];
|
|
58
|
+
if (first === void 0) return void 0;
|
|
59
|
+
let expression = predicate(provider, table, first);
|
|
60
|
+
for (const condition of rest) {
|
|
61
|
+
const next = predicate(provider, table, condition);
|
|
62
|
+
expression = condition.connector === "OR" ? sql`(${expression} or ${next})` : sql`(${expression} and ${next})`;
|
|
63
|
+
}
|
|
64
|
+
return expression;
|
|
65
|
+
};
|
|
66
|
+
//#endregion
|
|
67
|
+
export { buildDrizzleWhere };
|