@hot-updater/plugin-core 1.0.0-rc.3 → 1.0.0-rc.30
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/README.md +33 -0
- package/dist/baseCandidateKey.cjs +50 -0
- package/dist/baseCandidateKey.d.cts +27 -0
- package/dist/baseCandidateKey.d.mts +27 -0
- package/dist/baseCandidateKey.mjs +49 -0
- package/dist/bundleStorageLayout.cjs +7 -0
- package/dist/bundleStorageLayout.d.cts +5 -1
- package/dist/bundleStorageLayout.d.mts +5 -1
- package/dist/bundleStorageLayout.mjs +7 -1
- package/dist/contentType.cjs +9 -0
- package/dist/contentType.d.cts +4 -0
- package/dist/contentType.d.mts +4 -0
- package/dist/contentType.mjs +7 -0
- package/dist/coreApi.d.cts +131 -0
- package/dist/coreApi.d.mts +131 -0
- package/dist/createStorageAdapter.cjs +8 -0
- package/dist/createStorageAdapter.d.cts +9 -0
- package/dist/createStorageAdapter.d.mts +9 -0
- package/dist/createStorageAdapter.mjs +7 -0
- package/dist/database/adapter.cjs +61 -0
- package/dist/database/adapter.d.cts +200 -0
- package/dist/database/adapter.d.mts +200 -0
- package/dist/database/adapter.mjs +55 -0
- package/dist/database/distinct.cjs +88 -0
- package/dist/database/distinct.d.cts +18 -0
- package/dist/database/distinct.d.mts +18 -0
- package/dist/database/distinct.mjs +85 -0
- package/dist/database/memoryAdapter.cjs +107 -0
- package/dist/database/memoryAdapter.d.cts +14 -0
- package/dist/database/memoryAdapter.d.mts +14 -0
- package/dist/database/memoryAdapter.mjs +107 -0
- package/dist/database/values.cjs +114 -0
- package/dist/database/values.d.cts +17 -0
- package/dist/database/values.d.mts +17 -0
- package/dist/database/values.mjs +104 -0
- package/dist/database/verifyAdapter.cjs +137 -0
- package/dist/database/verifyAdapter.d.cts +37 -0
- package/dist/database/verifyAdapter.d.mts +37 -0
- package/dist/database/verifyAdapter.mjs +136 -0
- package/dist/databaseErrors.cjs +30 -0
- package/dist/databaseErrors.d.cts +21 -0
- package/dist/databaseErrors.d.mts +21 -0
- package/dist/databaseErrors.mjs +28 -0
- package/dist/databaseJsonValue.cjs +6 -0
- package/dist/databaseJsonValue.d.cts +9 -2
- package/dist/databaseJsonValue.d.mts +9 -2
- package/dist/databaseJsonValue.mjs +6 -0
- package/dist/databaseMetadata.cjs +4 -4
- package/dist/databaseMetadata.mjs +3 -3
- package/dist/databaseRows.cjs +6 -12
- package/dist/databaseRows.d.cts +1 -1
- package/dist/databaseRows.d.mts +1 -1
- package/dist/databaseRows.mjs +1 -7
- package/dist/engine/core/schema.cjs +279 -0
- package/dist/engine/core/schema.d.cts +312 -0
- package/dist/engine/core/schema.d.mts +312 -0
- package/dist/engine/core/schema.mjs +277 -0
- package/dist/engine/createEngine.cjs +72 -0
- package/dist/engine/createEngine.d.cts +45 -0
- package/dist/engine/createEngine.d.mts +45 -0
- package/dist/engine/createEngine.mjs +72 -0
- package/dist/engine/database/aggregateBatching.cjs +627 -0
- package/dist/engine/database/aggregateBatching.mjs +626 -0
- package/dist/engine/database/cursor.cjs +42 -0
- package/dist/engine/database/cursor.mjs +38 -0
- package/dist/engine/database/database.cjs +68 -0
- package/dist/engine/database/database.mjs +68 -0
- package/dist/engine/database/engine.cjs +37 -0
- package/dist/engine/database/engine.mjs +37 -0
- package/dist/engine/database/engineAggregates.cjs +120 -0
- package/dist/engine/database/engineAggregates.mjs +117 -0
- package/dist/engine/database/engineReads.cjs +182 -0
- package/dist/engine/database/engineReads.d.cts +10 -0
- package/dist/engine/database/engineReads.d.mts +10 -0
- package/dist/engine/database/engineReads.mjs +181 -0
- package/dist/engine/database/engineTransaction.cjs +399 -0
- package/dist/engine/database/engineTransaction.d.cts +18 -0
- package/dist/engine/database/engineTransaction.d.mts +18 -0
- package/dist/engine/database/engineTransaction.mjs +398 -0
- package/dist/engine/database/fence.cjs +156 -0
- package/dist/engine/database/fence.d.cts +41 -0
- package/dist/engine/database/fence.d.mts +41 -0
- package/dist/engine/database/fence.mjs +149 -0
- package/dist/engine/database/kv/kvAdapter.cjs +293 -0
- package/dist/engine/database/kv/kvAdapter.d.cts +116 -0
- package/dist/engine/database/kv/kvAdapter.d.mts +116 -0
- package/dist/engine/database/kv/kvAdapter.mjs +292 -0
- package/dist/engine/database/readMeter.cjs +80 -0
- package/dist/engine/database/readMeter.d.cts +34 -0
- package/dist/engine/database/readMeter.d.mts +34 -0
- package/dist/engine/database/readMeter.mjs +79 -0
- package/dist/engine/database/resolveSchema.cjs +197 -0
- package/dist/engine/database/resolveSchema.d.cts +35 -0
- package/dist/engine/database/resolveSchema.d.mts +35 -0
- package/dist/engine/database/resolveSchema.mjs +195 -0
- package/dist/engine/database/retention.cjs +100 -0
- package/dist/engine/database/retention.mjs +98 -0
- package/dist/engine/database/sql/sqlAdapter.cjs +290 -0
- package/dist/engine/database/sql/sqlAdapter.d.cts +45 -0
- package/dist/engine/database/sql/sqlAdapter.d.mts +45 -0
- package/dist/engine/database/sql/sqlAdapter.mjs +288 -0
- package/dist/engine/database/sql/sqlSchema.cjs +175 -0
- package/dist/engine/database/sql/sqlSchema.d.cts +45 -0
- package/dist/engine/database/sql/sqlSchema.d.mts +45 -0
- package/dist/engine/database/sql/sqlSchema.mjs +169 -0
- package/dist/engine/db/coreDatabase.cjs +94 -0
- package/dist/engine/db/coreDatabase.d.cts +78 -0
- package/dist/engine/db/coreDatabase.d.mts +78 -0
- package/dist/engine/db/coreDatabase.mjs +86 -0
- package/dist/engine/db/engineMigrator.cjs +53 -0
- package/dist/engine/db/engineMigrator.mjs +53 -0
- package/dist/engine/db/engineSql.cjs +26 -0
- package/dist/engine/db/engineSql.d.cts +16 -0
- package/dist/engine/db/engineSql.d.mts +16 -0
- package/dist/engine/db/engineSql.mjs +25 -0
- package/dist/engine/db/engineSqlMigrator.cjs +67 -0
- package/dist/engine/db/engineSqlMigrator.d.cts +21 -0
- package/dist/engine/db/engineSqlMigrator.d.mts +21 -0
- package/dist/engine/db/engineSqlMigrator.mjs +67 -0
- package/dist/engine/db/fixedMigratorShared.cjs +12 -0
- package/dist/engine/db/fixedMigratorShared.mjs +11 -0
- package/dist/engine/db/schemaSettings.cjs +63 -0
- package/dist/engine/db/schemaSettings.d.cts +7 -0
- package/dist/engine/db/schemaSettings.d.mts +7 -0
- package/dist/engine/db/schemaSettings.mjs +62 -0
- package/dist/engine/db/settingsMigrator.cjs +94 -0
- package/dist/engine/db/settingsMigrator.d.cts +25 -0
- package/dist/engine/db/settingsMigrator.d.mts +25 -0
- package/dist/engine/db/settingsMigrator.mjs +91 -0
- package/dist/engine/db/types.d.cts +78 -0
- package/dist/engine/db/types.d.mts +78 -0
- package/dist/index.cjs +97 -49
- package/dist/index.d.cts +46 -22
- package/dist/index.d.mts +46 -22
- package/dist/index.mjs +32 -17
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/comparison-DenM3wCn.cjs +0 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/comparison-DenM3wCn.mjs +1 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.cjs +8 -0
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.mjs +9 -2
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/set-CC5YeoYX.cjs +4 -0
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/set-CC5YeoYX.mjs +4 -1
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/version-CQ98ZBpL.cjs +0 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/version-CQ98ZBpL.mjs +2 -6
- package/dist/releaseCatalogCompiler.cjs +27 -26
- package/dist/releaseCatalogCompiler.d.cts +1 -2
- package/dist/releaseCatalogCompiler.d.mts +2 -3
- package/dist/releaseCatalogCompiler.mjs +5 -9
- package/dist/releaseCatalogMutation.cjs +0 -292
- package/dist/releaseCatalogMutation.d.cts +4 -45
- package/dist/releaseCatalogMutation.d.mts +4 -45
- package/dist/releaseCatalogMutation.mjs +1 -288
- package/dist/releaseManagement.cjs +0 -221
- package/dist/releaseManagement.d.cts +4 -28
- package/dist/releaseManagement.d.mts +4 -28
- package/dist/releaseManagement.mjs +1 -216
- package/dist/remoteBundleSigning.d.cts +2 -2
- package/dist/remoteBundleSigning.d.mts +2 -2
- package/dist/serverPlugin/busy.cjs +36 -0
- package/dist/serverPlugin/busy.d.cts +12 -0
- package/dist/serverPlugin/busy.d.mts +12 -0
- package/dist/serverPlugin/busy.mjs +36 -0
- package/dist/serverPlugin/configError.cjs +7 -0
- package/dist/serverPlugin/configError.d.cts +7 -0
- package/dist/serverPlugin/configError.d.mts +7 -0
- package/dist/serverPlugin/configError.mjs +7 -0
- package/dist/serverPlugin/coreReads.d.cts +48 -0
- package/dist/serverPlugin/coreReads.d.mts +48 -0
- package/dist/serverPlugin/databaseHandle.d.cts +105 -0
- package/dist/serverPlugin/databaseHandle.d.mts +105 -0
- package/dist/serverPlugin/definePlugin.cjs +5 -0
- package/dist/serverPlugin/definePlugin.d.cts +130 -0
- package/dist/serverPlugin/definePlugin.d.mts +130 -0
- package/dist/serverPlugin/definePlugin.mjs +5 -0
- package/dist/serverPlugin/definitions.d.cts +76 -0
- package/dist/serverPlugin/definitions.d.mts +76 -0
- package/dist/serverPlugin/errors.cjs +37 -0
- package/dist/serverPlugin/errors.d.cts +29 -0
- package/dist/serverPlugin/errors.d.mts +29 -0
- package/dist/serverPlugin/errors.mjs +32 -0
- package/dist/serverPlugin/schema.cjs +24 -0
- package/dist/serverPlugin/schema.d.cts +90 -0
- package/dist/serverPlugin/schema.d.mts +90 -0
- package/dist/serverPlugin/schema.mjs +23 -0
- package/dist/types/databaseConfig.cjs +5 -0
- package/dist/types/databaseConfig.d.cts +68 -0
- package/dist/types/databaseConfig.d.mts +68 -0
- package/dist/types/databaseConfig.mjs +5 -0
- package/dist/types/databaseOperations.d.cts +5 -180
- package/dist/types/databaseOperations.d.mts +5 -180
- package/dist/types/databaseRows.d.cts +5 -56
- package/dist/types/databaseRows.d.mts +5 -56
- package/dist/types/index.cjs +2 -0
- package/dist/types/index.d.cts +31 -97
- package/dist/types/index.d.mts +31 -97
- package/dist/types/index.mjs +2 -1
- package/dist/types/models.d.cts +14 -0
- package/dist/types/models.d.mts +14 -0
- package/dist/types/public.d.cts +4 -3
- package/dist/types/public.d.mts +4 -3
- package/dist/uuidv7.cjs +3 -3
- package/dist/uuidv7.d.cts +1 -1
- package/dist/uuidv7.d.mts +1 -1
- package/dist/uuidv7.mjs +1 -1
- package/package.json +10 -7
- package/dist/calculatePagination.cjs +0 -25
- package/dist/calculatePagination.d.cts +0 -12
- package/dist/calculatePagination.d.mts +0 -12
- package/dist/calculatePagination.mjs +0 -25
- package/dist/compressionFormat.cjs +0 -62
- package/dist/compressionFormat.d.cts +0 -33
- package/dist/compressionFormat.d.mts +0 -33
- package/dist/compressionFormat.mjs +0 -58
- package/dist/createDatabasePlugin.cjs +0 -920
- package/dist/createDatabasePlugin.d.cts +0 -28
- package/dist/createDatabasePlugin.d.mts +0 -28
- package/dist/createDatabasePlugin.mjs +0 -916
- package/dist/createStoragePlugin.cjs +0 -8
- package/dist/createStoragePlugin.d.cts +0 -9
- package/dist/createStoragePlugin.d.mts +0 -9
- package/dist/createStoragePlugin.mjs +0 -7
- package/dist/databaseClient.cjs +0 -117
- package/dist/databaseClient.d.cts +0 -32
- package/dist/databaseClient.d.mts +0 -32
- package/dist/databaseClient.mjs +0 -114
- package/dist/databaseClientReads.cjs +0 -115
- package/dist/databaseClientReads.mjs +0 -113
- package/dist/databaseClientUpdates.cjs +0 -40
- package/dist/databaseClientUpdates.d.cts +0 -11
- package/dist/databaseClientUpdates.d.mts +0 -11
- package/dist/databaseClientUpdates.mjs +0 -38
- package/dist/databasePluginCrud.cjs +0 -88
- package/dist/databasePluginCrud.d.cts +0 -4
- package/dist/databasePluginCrud.d.mts +0 -4
- package/dist/databasePluginCrud.mjs +0 -87
- package/dist/databasePluginCrudValidation.d.cts +0 -6
- package/dist/databasePluginCrudValidation.d.mts +0 -6
- package/dist/databasePluginCrudValidationErrors.cjs +0 -11
- package/dist/databasePluginCrudValidationErrors.d.cts +0 -9
- package/dist/databasePluginCrudValidationErrors.d.mts +0 -9
- package/dist/databasePluginCrudValidationErrors.mjs +0 -11
- package/dist/databasePluginCrudValidationFields.cjs +0 -321
- package/dist/databasePluginCrudValidationFields.d.cts +0 -1
- package/dist/databasePluginCrudValidationFields.d.mts +0 -1
- package/dist/databasePluginCrudValidationFields.mjs +0 -309
- package/dist/databasePluginCrudValidationMutations.cjs +0 -107
- package/dist/databasePluginCrudValidationMutations.d.cts +0 -1
- package/dist/databasePluginCrudValidationMutations.d.mts +0 -1
- package/dist/databasePluginCrudValidationMutations.mjs +0 -100
- package/dist/databasePluginCrudValidationQueries.cjs +0 -103
- package/dist/databasePluginCrudValidationQueries.d.cts +0 -2
- package/dist/databasePluginCrudValidationQueries.d.mts +0 -2
- package/dist/databasePluginCrudValidationQueries.mjs +0 -97
- package/dist/databasePluginCrudValidationRows.cjs +0 -55
- package/dist/databasePluginCrudValidationRows.d.cts +0 -1
- package/dist/databasePluginCrudValidationRows.d.mts +0 -1
- package/dist/databasePluginCrudValidationRows.mjs +0 -53
- package/dist/databasePluginTransaction.cjs +0 -7
- package/dist/databasePluginTransaction.mjs +0 -7
- package/dist/insightsContract.cjs +0 -157
- package/dist/insightsContract.d.cts +0 -11
- package/dist/insightsContract.d.mts +0 -11
- package/dist/insightsContract.mjs +0 -153
- package/dist/internal.cjs +0 -6
- package/dist/internal.d.cts +0 -10
- package/dist/internal.d.mts +0 -10
- package/dist/internal.mjs +0 -3
- package/dist/paginateBundles.cjs +0 -53
- package/dist/paginateBundles.d.cts +0 -13
- package/dist/paginateBundles.d.mts +0 -13
- package/dist/paginateBundles.mjs +0 -53
- package/dist/queryBundles.cjs +0 -34
- package/dist/queryBundles.d.cts +0 -9
- package/dist/queryBundles.d.mts +0 -9
- package/dist/queryBundles.mjs +0 -32
- package/dist/requestBundleCache.cjs +0 -22
- package/dist/requestBundleCache.d.cts +0 -8
- package/dist/requestBundleCache.d.mts +0 -8
- package/dist/requestBundleCache.mjs +0 -22
- package/dist/types/database.d.cts +0 -5
- package/dist/types/database.d.mts +0 -5
- package/dist/types/databaseFields.cjs +0 -105
- package/dist/types/databaseFields.d.cts +0 -13
- package/dist/types/databaseFields.d.mts +0 -13
- package/dist/types/databaseFields.mjs +0 -105
- package/dist/types/databasePlugin.d.cts +0 -298
- package/dist/types/databasePlugin.d.mts +0 -298
- package/dist/types/databaseQuery.d.cts +0 -46
- package/dist/types/databaseQuery.d.mts +0 -46
- package/dist/types/internal.d.cts +0 -8
- package/dist/types/internal.d.mts +0 -8
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
//#region src/engine/database/fence.ts
|
|
2
|
+
/**
|
|
3
|
+
* The settings rows migrations write, last, and the fence reads; retention
|
|
4
|
+
* passes keep their lease here too. Tooling that creates the tables creates
|
|
5
|
+
* this one with them: an ORM's schema generator, a provider's setup, and an
|
|
6
|
+
* access policy that names the tables a server reads.
|
|
7
|
+
*/
|
|
8
|
+
const SETTINGS_TABLE = {
|
|
9
|
+
name: "private_hot_updater_settings",
|
|
10
|
+
columns: [
|
|
11
|
+
{
|
|
12
|
+
name: "key",
|
|
13
|
+
type: "string",
|
|
14
|
+
nullable: false,
|
|
15
|
+
maxLength: 255
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: "value",
|
|
19
|
+
type: "string",
|
|
20
|
+
nullable: false,
|
|
21
|
+
maxLength: 255
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "_v",
|
|
25
|
+
type: "integer",
|
|
26
|
+
nullable: false,
|
|
27
|
+
default: 0
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
key: ["key"],
|
|
31
|
+
indexes: []
|
|
32
|
+
};
|
|
33
|
+
/** The engine's storage-layout generation; it changes only with a migration. */
|
|
34
|
+
const ENGINE_SCHEMA_KEY = "schema.engine";
|
|
35
|
+
const CORE_SCHEMA_KEY = "schema.core";
|
|
36
|
+
const stored = ({ key, expected, found }) => `"${key}" is ${found === null ? "missing" : `"${found}"`}; expected "${expected}"`;
|
|
37
|
+
const settingMessage = (adapterName, { key, expected, found }) => `Hot Updater schema setting "${key}" for ${adapterName} is ${found === null ? "missing" : `"${found}"`}; expected "${expected}". ${key === "schema.engine" ? "Create a new empty database and run `hot-updater db migrate`; databases from before the storage engine are not converted." : "Run `hot-updater db migrate`."}`;
|
|
38
|
+
/** The plugin a `schema.<id>` setting belongs to; undefined for core's and the engine's. */
|
|
39
|
+
const pluginOf = ({ key }) => key.startsWith("schema.") && key !== "schema.engine" && key !== CORE_SCHEMA_KEY ? key.slice(7) : void 0;
|
|
40
|
+
const listed = (items) => items.length < 2 ? items.join("") : `${items.slice(0, -1).join(", ")} and ${items.at(-1)}`;
|
|
41
|
+
/** How to create plugins' tables when the fence was given no fix. */
|
|
42
|
+
const PLUGIN_TABLES_FIX = "Run `hot-updater db migrate`. A managed server runs only its provider's plugins, and rerunning `hot-updater init --provider <provider>` applies the provider's migrations. Run `hot-updater db` commands in the server's project: they load the server file named on the command line, or src/hotUpdater.* or src/db.*, and don't read hot-updater.config.ts.";
|
|
43
|
+
const pluginsMessage = (adapterName, plugins, settings, fix) => `The tables of ${plugins.length === 1 ? "plugin" : "plugins"} ${listed(plugins.map((id) => `"${id}"`))} are not migrated on ${adapterName}: schema ${settings.length === 1 ? "setting" : "settings"} ${settings.map(stored).join(", and ")}. ${fix}`;
|
|
44
|
+
/** The database needs `hot-updater db migrate`, or a new database; the handler answers 503. */
|
|
45
|
+
var HotUpdaterSchemaMigrationRequiredError = class extends Error {
|
|
46
|
+
adapterName;
|
|
47
|
+
currentVersion;
|
|
48
|
+
setting;
|
|
49
|
+
/** Every setting the fence found missing or different; `setting` is the first. */
|
|
50
|
+
settings;
|
|
51
|
+
/**
|
|
52
|
+
* The ids of the plugins whose `schema.<id>` setting is among them: the
|
|
53
|
+
* plugins whose tables a migration has yet to create or change.
|
|
54
|
+
*/
|
|
55
|
+
plugins;
|
|
56
|
+
constructor(adapterName, currentVersion, setting, options) {
|
|
57
|
+
const settings = options?.settings ?? (setting === void 0 ? [] : [setting]);
|
|
58
|
+
const plugins = settings.flatMap((mismatch) => pluginOf(mismatch) ?? []);
|
|
59
|
+
super(setting === void 0 ? currentVersion === void 0 ? `Hot Updater database schema is not initialized for ${adapterName}. Run \`hot-updater db migrate\` before using this adapter.` : `Hot Updater v1 cannot migrate schema ${currentVersion} in place. Create a new empty database and run \`hot-updater db migrate\`.` : plugins.length > 0 && plugins.length === settings.length ? pluginsMessage(adapterName, plugins, settings, options?.fix ?? PLUGIN_TABLES_FIX) : settingMessage(adapterName, setting), options?.cause === void 0 ? void 0 : { cause: options.cause });
|
|
60
|
+
this.adapterName = adapterName;
|
|
61
|
+
this.currentVersion = currentVersion;
|
|
62
|
+
this.setting = setting;
|
|
63
|
+
this.name = "HotUpdaterSchemaMigrationRequiredError";
|
|
64
|
+
this.settings = settings;
|
|
65
|
+
this.plugins = plugins;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
/** Driver codes for a table or column the database lacks. */
|
|
69
|
+
const MISSING_SCHEMA_CODES = /* @__PURE__ */ new Set([
|
|
70
|
+
"42P01",
|
|
71
|
+
"42703",
|
|
72
|
+
"ER_NO_SUCH_TABLE",
|
|
73
|
+
"ER_BAD_FIELD_ERROR"
|
|
74
|
+
]);
|
|
75
|
+
const MISSING_SCHEMA_MESSAGE = /no such (?:table|column)|(?:relation|column) .+ does not exist|table .+ doesn't exist|unknown column/iu;
|
|
76
|
+
/**
|
|
77
|
+
* Whether a read failed because a table or column is missing, so the
|
|
78
|
+
* database needs migrations. ORMs may wrap the driver's error in `cause`.
|
|
79
|
+
*/
|
|
80
|
+
const isMissingSchemaError = (error) => {
|
|
81
|
+
for (let depth = 0; error instanceof Object && depth < 8; depth += 1) {
|
|
82
|
+
const { code, message, cause } = error;
|
|
83
|
+
if (MISSING_SCHEMA_CODES.has(String(code)) || typeof message === "string" && MISSING_SCHEMA_MESSAGE.test(message)) return true;
|
|
84
|
+
error = cause;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
};
|
|
88
|
+
const readSettings = (adapter, keys) => adapter.get(SETTINGS_TABLE, keys.map((key) => [key]));
|
|
89
|
+
/**
|
|
90
|
+
* Throws unless every expected setting is stored with its value: one batch
|
|
91
|
+
* read. The error lists every setting missing or different; `fix` says how
|
|
92
|
+
* to create the plugins' tables on this database.
|
|
93
|
+
*/
|
|
94
|
+
const checkSchemaFence = async (adapter, adapterName, expected, fix) => {
|
|
95
|
+
const keys = Object.keys(expected);
|
|
96
|
+
let rows;
|
|
97
|
+
try {
|
|
98
|
+
rows = await readSettings(adapter, keys);
|
|
99
|
+
} catch (cause) {
|
|
100
|
+
if (!isMissingSchemaError(cause)) throw cause;
|
|
101
|
+
throw new HotUpdaterSchemaMigrationRequiredError(adapterName, void 0, {
|
|
102
|
+
key: ENGINE_SCHEMA_KEY,
|
|
103
|
+
expected: "1",
|
|
104
|
+
found: null
|
|
105
|
+
}, { cause });
|
|
106
|
+
}
|
|
107
|
+
const settings = keys.flatMap((key, position) => {
|
|
108
|
+
const found = rows[position]?.value;
|
|
109
|
+
return found === expected[key] ? [] : [{
|
|
110
|
+
key,
|
|
111
|
+
expected: expected[key],
|
|
112
|
+
found: typeof found === "string" ? found : null
|
|
113
|
+
}];
|
|
114
|
+
});
|
|
115
|
+
const [first] = settings;
|
|
116
|
+
if (first !== void 0) throw new HotUpdaterSchemaMigrationRequiredError(adapterName, void 0, first, {
|
|
117
|
+
settings,
|
|
118
|
+
...fix === void 0 ? {} : { fix }
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
/** The database name a fenced adapter carries, so a server can fence its plugins' rows too. */
|
|
122
|
+
const FENCED = Symbol.for("@hot-updater/server/schema-fence");
|
|
123
|
+
/** The database name an adapter behind the schema fence reports under. */
|
|
124
|
+
const fencedName = (adapter) => adapter[FENCED];
|
|
125
|
+
/**
|
|
126
|
+
* The adapter behind the schema fence: a process's first read or write
|
|
127
|
+
* checks the settings first. Success is kept; a failure is checked again on
|
|
128
|
+
* the next call.
|
|
129
|
+
*/
|
|
130
|
+
const withSchemaFence = (adapter, adapterName, expected, fix) => {
|
|
131
|
+
let ready;
|
|
132
|
+
const fence = () => {
|
|
133
|
+
ready ??= checkSchemaFence(adapter, adapterName, expected, fix).catch((error) => {
|
|
134
|
+
ready = void 0;
|
|
135
|
+
throw error;
|
|
136
|
+
});
|
|
137
|
+
return ready;
|
|
138
|
+
};
|
|
139
|
+
return {
|
|
140
|
+
...adapter,
|
|
141
|
+
[FENCED]: adapterName,
|
|
142
|
+
get: (table, keys) => fence().then(() => adapter.get(table, keys)),
|
|
143
|
+
query: (table, request) => fence().then(() => adapter.query(table, request)),
|
|
144
|
+
write: (ops) => fence().then(() => adapter.write(ops)),
|
|
145
|
+
...adapter.prune && { prune: (table, before, limit) => fence().then(() => adapter.prune(table, before, limit)) }
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
//#endregion
|
|
149
|
+
export { ENGINE_SCHEMA_KEY, HotUpdaterSchemaMigrationRequiredError, SETTINGS_TABLE, checkSchemaFence, fencedName, isMissingSchemaError, readSettings, withSchemaFence };
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
const require_adapter = require("../../../database/adapter.cjs");
|
|
2
|
+
const require_values = require("../../../database/values.cjs");
|
|
3
|
+
const require_verifyAdapter = require("../../../database/verifyAdapter.cjs");
|
|
4
|
+
//#region src/engine/database/kv/kvAdapter.ts
|
|
5
|
+
const SEPARATOR = "";
|
|
6
|
+
/**
|
|
7
|
+
* Order-preserving text for key values; each value ends in the lowest code
|
|
8
|
+
* point any encoding uses. `createKvAdapter` stores a row as the item with
|
|
9
|
+
* the table's name as `pk` and its key's text as `sk`, so tooling that
|
|
10
|
+
* writes a store's items itself, as the AWS setup's settings items do, keys
|
|
11
|
+
* them with it.
|
|
12
|
+
*/
|
|
13
|
+
const encodeKvKey = (values) => values.map((value) => {
|
|
14
|
+
if (typeof value === "boolean") return `${Number(value)}${SEPARATOR}`;
|
|
15
|
+
if (typeof value === "number") {
|
|
16
|
+
const view = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(8));
|
|
17
|
+
view.setFloat64(0, value === 0 ? 0 : value);
|
|
18
|
+
const mask = view.getUint32(0) >>> 31 ? [-1, -1] : [1 << 31, 0];
|
|
19
|
+
return `${[0, 4].map((at, word) => ((view.getUint32(at) ^ mask[word]) >>> 0).toString(16).padStart(8, "0")).join("")}${SEPARATOR}`;
|
|
20
|
+
}
|
|
21
|
+
let text = "";
|
|
22
|
+
for (const char of value) text += char < "" ? `\u0002${String.fromCharCode(char.charCodeAt(0) + 1)}` : char;
|
|
23
|
+
return `${text}${SEPARATOR}`;
|
|
24
|
+
}).join("");
|
|
25
|
+
/** The first sort key after every key that starts with `prefix`. */
|
|
26
|
+
const pastPrefix = (prefix) => `${prefix.slice(0, -1)}\u0002`;
|
|
27
|
+
/** A unique item's sort key, and an index item's when its order is empty. */
|
|
28
|
+
const ONLY = "#";
|
|
29
|
+
/** An index whose eq and order columns are the key, in order, reads the row items. */
|
|
30
|
+
const inKeyOrder = (table, index) => {
|
|
31
|
+
const columns = [...index.eq, ...require_adapter.indexOrderColumns(table, index)];
|
|
32
|
+
return !index.unique && columns.length === table.key.length && columns.every((column, position) => column === table.key[position]);
|
|
33
|
+
};
|
|
34
|
+
const same = (left, right, columns) => columns.every((name) => require_values.canonicalJson(left[name]) === require_values.canonicalJson(right[name]));
|
|
35
|
+
/**
|
|
36
|
+
* The database adapter over a key-value store. A row is one item at
|
|
37
|
+
* `pk = <table>`, `sk = enc(key)`; each index not in key order adds an item
|
|
38
|
+
* per entry at `pk = <table>#<index>#enc(eq)`, `sk = enc(order)`, and each
|
|
39
|
+
* unique entry one item at `sk = "#"`, written only where no other row holds
|
|
40
|
+
* it. Index items hold a copy of the row without `_v` or counters, so an
|
|
41
|
+
* increment, which changes only those, never makes them stale.
|
|
42
|
+
*/
|
|
43
|
+
const createKvAdapter = ({ store, tablePrefix = "" }) => {
|
|
44
|
+
const layouts = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
const layoutOf = (table) => {
|
|
46
|
+
const known = layouts.get(table);
|
|
47
|
+
if (known) return known;
|
|
48
|
+
const counters = table.columns.filter((column) => column.default !== void 0 && column.name !== "_v").map((column) => column.name);
|
|
49
|
+
const layout = {
|
|
50
|
+
table,
|
|
51
|
+
rows: tablePrefix + table.name,
|
|
52
|
+
copied: table.columns.map((column) => column.name).filter((name) => name !== "_v" && !counters.includes(name)),
|
|
53
|
+
counters,
|
|
54
|
+
indexed: table.indexes.filter((index) => !inKeyOrder(table, index))
|
|
55
|
+
};
|
|
56
|
+
layouts.set(table, layout);
|
|
57
|
+
return layout;
|
|
58
|
+
};
|
|
59
|
+
const rowItem = (layout, key) => ({
|
|
60
|
+
pk: layout.rows,
|
|
61
|
+
sk: encodeKvKey(key)
|
|
62
|
+
});
|
|
63
|
+
const partition = (layout, index, eq) => `${layout.rows}#${index.name}#${encodeKvKey(eq)}`;
|
|
64
|
+
const copyOf = (layout, row) => Object.fromEntries(layout.copied.map((name) => [name, row[name] ?? null]));
|
|
65
|
+
/** The expiry every item of `row` carries, for a table with retention. */
|
|
66
|
+
const stamp = (table, row) => {
|
|
67
|
+
const at = row && require_adapter.expiresAt(table, row);
|
|
68
|
+
return at === void 0 ? {} : { expiresAt: at };
|
|
69
|
+
};
|
|
70
|
+
/** Every index and unique item a row has, by address. */
|
|
71
|
+
const itemsOf = (layout, row) => {
|
|
72
|
+
const items = /* @__PURE__ */ new Map();
|
|
73
|
+
for (const index of row === null ? [] : layout.indexed) for (const eq of require_values.indexEntries(layout.table, index, row)) {
|
|
74
|
+
const pk = partition(layout, index, eq);
|
|
75
|
+
const sk = index.unique ? ONLY : encodeKvKey(require_values.indexOrderTuple(layout.table, index, row)) || ONLY;
|
|
76
|
+
items.set(JSON.stringify([pk, sk]), {
|
|
77
|
+
key: {
|
|
78
|
+
pk,
|
|
79
|
+
sk
|
|
80
|
+
},
|
|
81
|
+
unique: index.unique === true
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return items;
|
|
85
|
+
};
|
|
86
|
+
/** Moves a row's index items from `before` to `after`; a unique entry is taken only where no row holds it. */
|
|
87
|
+
const indexOps = (layout, before, after) => {
|
|
88
|
+
const old = itemsOf(layout, before);
|
|
89
|
+
const next = itemsOf(layout, after);
|
|
90
|
+
const ops = [...old].filter(([id]) => !next.has(id)).map(([, item]) => ({
|
|
91
|
+
type: "delete",
|
|
92
|
+
key: item.key
|
|
93
|
+
}));
|
|
94
|
+
const copy = after && copyOf(layout, after);
|
|
95
|
+
const unchanged = before !== null && copy !== null && same(before, copy, layout.copied);
|
|
96
|
+
for (const [id, item] of next) {
|
|
97
|
+
if (old.has(id) && unchanged) continue;
|
|
98
|
+
const taken = item.unique && !old.has(id);
|
|
99
|
+
ops.push({
|
|
100
|
+
type: "put",
|
|
101
|
+
key: item.key,
|
|
102
|
+
value: copy,
|
|
103
|
+
condition: taken ? { exists: false } : void 0,
|
|
104
|
+
...stamp(layout.table, after)
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return ops;
|
|
108
|
+
};
|
|
109
|
+
/** The item ops of one adapter op: its row item first, then its index items. */
|
|
110
|
+
const plan = (op) => {
|
|
111
|
+
const layout = layoutOf(op.table);
|
|
112
|
+
const key = rowItem(layout, op.type === "insert" ? require_adapter.rowKey(op.table, op.row) : op.key);
|
|
113
|
+
const guard = op.type === "insert" ? { exists: false } : op.guard;
|
|
114
|
+
const condition = guard && ("v" in guard ? { v: guard.v } : guard);
|
|
115
|
+
switch (op.type) {
|
|
116
|
+
case "check": return [{
|
|
117
|
+
type: "check",
|
|
118
|
+
key,
|
|
119
|
+
condition
|
|
120
|
+
}];
|
|
121
|
+
case "increment":
|
|
122
|
+
if (layout.indexed.length > 0 && (op.init !== void 0 || Object.keys(op.by).some((name) => layout.copied.includes(name)))) throw new require_verifyAdapter.DatabaseAdapterContractError(`${op.table.name} has index copies: an increment may change only counters of an existing row.`);
|
|
123
|
+
return [{
|
|
124
|
+
type: "add",
|
|
125
|
+
key,
|
|
126
|
+
by: {
|
|
127
|
+
...op.by,
|
|
128
|
+
["_v"]: 1
|
|
129
|
+
},
|
|
130
|
+
init: op.init,
|
|
131
|
+
condition: condition ?? (op.init ? void 0 : { exists: true }),
|
|
132
|
+
...stamp(op.table, op.init)
|
|
133
|
+
}];
|
|
134
|
+
case "delete": return [{
|
|
135
|
+
type: "delete",
|
|
136
|
+
key,
|
|
137
|
+
condition
|
|
138
|
+
}, ...indexOps(layout, op.previous, null)];
|
|
139
|
+
default: {
|
|
140
|
+
const before = op.type === "insert" ? null : op.previous;
|
|
141
|
+
const row = op.type === "insert" ? op.row : {
|
|
142
|
+
...op.previous,
|
|
143
|
+
...op.set,
|
|
144
|
+
["_v"]: op.guard.v + 1
|
|
145
|
+
};
|
|
146
|
+
return [{
|
|
147
|
+
type: "put",
|
|
148
|
+
key,
|
|
149
|
+
value: row,
|
|
150
|
+
condition,
|
|
151
|
+
...stamp(op.table, row)
|
|
152
|
+
}, ...indexOps(layout, before, row)];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* One op per item. Only unique items of two rows can meet: one op frees
|
|
158
|
+
* the value and another takes it. That becomes one unconditional put, as
|
|
159
|
+
* the freeing op's guard already holds the value's owner. Two rows taking
|
|
160
|
+
* one value fail at the later op.
|
|
161
|
+
*/
|
|
162
|
+
const compile = (ops) => {
|
|
163
|
+
const items = /* @__PURE__ */ new Map();
|
|
164
|
+
for (const [origin, op] of ops.entries()) for (const kv of plan(op)) {
|
|
165
|
+
const id = JSON.stringify([kv.key.pk, kv.key.sk]);
|
|
166
|
+
const earlier = items.get(id);
|
|
167
|
+
const put = [earlier, {
|
|
168
|
+
op: kv,
|
|
169
|
+
origin
|
|
170
|
+
}].find((planned) => planned?.op.type === "put");
|
|
171
|
+
if (earlier === void 0) items.set(id, {
|
|
172
|
+
op: kv,
|
|
173
|
+
origin
|
|
174
|
+
});
|
|
175
|
+
else if (put?.op.type === "put" && [earlier.op, kv].some(({ type }) => type === "delete")) items.set(id, {
|
|
176
|
+
op: {
|
|
177
|
+
...put.op,
|
|
178
|
+
condition: void 0
|
|
179
|
+
},
|
|
180
|
+
origin
|
|
181
|
+
});
|
|
182
|
+
else return { failedOp: origin };
|
|
183
|
+
}
|
|
184
|
+
return { items: [...items.values()] };
|
|
185
|
+
};
|
|
186
|
+
const encoder = new TextEncoder();
|
|
187
|
+
const sizeOf = ({ op }) => encoder.encode(op.key.pk + op.key.sk + JSON.stringify(op.type === "put" ? op.value : op.type === "add" ? op.init : null)).length;
|
|
188
|
+
/** Fills counters from the rows; a row that still equals its copy is returned whole, `_v` included. */
|
|
189
|
+
const complete = async (layout, copies) => {
|
|
190
|
+
if (layout.counters.length === 0 || copies.length === 0) return copies;
|
|
191
|
+
const rows = await store.get(copies.map((copy) => rowItem(layout, require_adapter.rowKey(layout.table, copy))));
|
|
192
|
+
return copies.map((copy, at) => {
|
|
193
|
+
const row = rows[at];
|
|
194
|
+
if (row && same(row, copy, layout.copied)) return row;
|
|
195
|
+
const counters = layout.counters.map((name) => [name, row?.[name] ?? 0]);
|
|
196
|
+
return {
|
|
197
|
+
...copy,
|
|
198
|
+
...Object.fromEntries(counters)
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* A request's sort keys within its partition, from `gte` up to `lt`; null
|
|
204
|
+
* when none can match. Every encoding ends in the separator, so the keys
|
|
205
|
+
* that start with a prefix end right before `pastPrefix` of it.
|
|
206
|
+
*/
|
|
207
|
+
const rangeOf = (prefix, request) => {
|
|
208
|
+
const edge = (bound, lower) => {
|
|
209
|
+
if (bound === void 0) return prefix === "" ? void 0 : lower ? prefix : pastPrefix(prefix);
|
|
210
|
+
const text = prefix + encodeKvKey(bound.values);
|
|
211
|
+
if (text === "") return bound.inclusive ? void 0 : null;
|
|
212
|
+
return lower === bound.inclusive ? text : pastPrefix(text);
|
|
213
|
+
};
|
|
214
|
+
const gte = edge(request.lower, true);
|
|
215
|
+
const lt = edge(request.upper, false);
|
|
216
|
+
if (gte === null || lt === null) return null;
|
|
217
|
+
if (gte !== void 0 && lt !== void 0 && require_values.compareUtf8(gte, lt) >= 0) return null;
|
|
218
|
+
if (lt === void 0) return {
|
|
219
|
+
gte,
|
|
220
|
+
lt
|
|
221
|
+
};
|
|
222
|
+
return {
|
|
223
|
+
gte,
|
|
224
|
+
lt,
|
|
225
|
+
lte: lt.endsWith(SEPARATOR) ? lt.slice(0, -1) : lt
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
return {
|
|
229
|
+
id: store.id,
|
|
230
|
+
fits(ops) {
|
|
231
|
+
const { items } = compile(ops);
|
|
232
|
+
if (items === void 0) return true;
|
|
233
|
+
const { items: most, bytes, itemBytes = bytes, keyBytes } = store.limits;
|
|
234
|
+
const sizes = items.map(sizeOf);
|
|
235
|
+
const length = (text) => encoder.encode(text).length;
|
|
236
|
+
return items.length <= most && sizes.reduce((sum, size) => sum + size, 0) <= bytes && sizes.every((size) => size <= itemBytes) && (keyBytes === void 0 || items.every(({ op }) => length(op.key.pk) <= keyBytes.pk && length(op.key.sk) <= keyBytes.sk));
|
|
237
|
+
},
|
|
238
|
+
get: (table, keys) => store.get(keys.map((key) => rowItem(layoutOf(table), key))),
|
|
239
|
+
async query(table, request) {
|
|
240
|
+
const layout = layoutOf(table);
|
|
241
|
+
const index = require_adapter.findPhysicalIndex(table, request.index);
|
|
242
|
+
if (index.unique) {
|
|
243
|
+
const key = {
|
|
244
|
+
pk: partition(layout, index, request.eq),
|
|
245
|
+
sk: ONLY
|
|
246
|
+
};
|
|
247
|
+
const [copy] = await store.get([key]);
|
|
248
|
+
const found = copy && require_values.matchesQuery(table, index, request, copy);
|
|
249
|
+
return complete(layout, found ? [copy] : []);
|
|
250
|
+
}
|
|
251
|
+
const own = inKeyOrder(table, index);
|
|
252
|
+
const range = rangeOf(own ? encodeKvKey(request.eq) : "", request);
|
|
253
|
+
if (range === null) return [];
|
|
254
|
+
const pk = own ? layout.rows : partition(layout, index, request.eq);
|
|
255
|
+
const rows = [];
|
|
256
|
+
let after;
|
|
257
|
+
while (rows.length < request.limit) {
|
|
258
|
+
const { items, more } = await store.query({
|
|
259
|
+
pk,
|
|
260
|
+
...range,
|
|
261
|
+
order: request.order,
|
|
262
|
+
limit: request.limit - rows.length,
|
|
263
|
+
after
|
|
264
|
+
});
|
|
265
|
+
rows.push(...items.map((item) => item.value));
|
|
266
|
+
if (!more || items.length === 0) break;
|
|
267
|
+
after = items.at(-1).sk;
|
|
268
|
+
}
|
|
269
|
+
return own ? rows : complete(layout, rows);
|
|
270
|
+
},
|
|
271
|
+
async write(ops) {
|
|
272
|
+
const { items, failedOp } = compile(ops);
|
|
273
|
+
if (items === void 0) return {
|
|
274
|
+
ok: false,
|
|
275
|
+
failedOp
|
|
276
|
+
};
|
|
277
|
+
const result = await store.write(items.map(({ op }) => op));
|
|
278
|
+
return !result.ok && "failedOp" in result ? {
|
|
279
|
+
ok: false,
|
|
280
|
+
failedOp: items[result.failedOp].origin
|
|
281
|
+
} : result;
|
|
282
|
+
},
|
|
283
|
+
...store.deleteConsumed === void 0 ? {} : { deleteConsumed: (table, rows) => {
|
|
284
|
+
const layout = layoutOf(table);
|
|
285
|
+
return store.deleteConsumed(rows.flatMap((row) => [rowItem(layout, require_adapter.rowKey(table, row)), ...[...itemsOf(layout, row).values()].map(({ key }) => key)]));
|
|
286
|
+
} },
|
|
287
|
+
migrations: { apply: async () => store.migrations?.apply() },
|
|
288
|
+
...store.dispose === void 0 ? {} : { dispose: () => store.dispose() }
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
//#endregion
|
|
292
|
+
exports.createKvAdapter = createKvAdapter;
|
|
293
|
+
exports.encodeKvKey = encodeKvKey;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { DatabaseAdapter, DatabaseKeyValue, StoredRow, WriteResult } from "../../../database/adapter.cjs";
|
|
2
|
+
//#region src/engine/database/kv/kvAdapter.d.ts
|
|
3
|
+
/** An item's address: a partition, and a sort key within it. */
|
|
4
|
+
interface KvKey {
|
|
5
|
+
readonly pk: string;
|
|
6
|
+
readonly sk: string;
|
|
7
|
+
}
|
|
8
|
+
/** What an op requires of its item before the write: that it exists or not, or that its `_v` is `v`. */
|
|
9
|
+
type KvCondition = {
|
|
10
|
+
readonly exists: boolean;
|
|
11
|
+
} | {
|
|
12
|
+
readonly v: number;
|
|
13
|
+
};
|
|
14
|
+
type KvOp = {
|
|
15
|
+
readonly key: KvKey;
|
|
16
|
+
readonly condition?: KvCondition;
|
|
17
|
+
/**
|
|
18
|
+
* When a put's or a created item expires, in epoch milliseconds, which
|
|
19
|
+
* the store maps to its native TTL. Every item of a row of a table with
|
|
20
|
+
* retention carries it, index copies included.
|
|
21
|
+
*/
|
|
22
|
+
readonly expiresAt?: number;
|
|
23
|
+
} & ({
|
|
24
|
+
readonly type: "put";
|
|
25
|
+
readonly value: StoredRow;
|
|
26
|
+
} | {
|
|
27
|
+
readonly type: "delete";
|
|
28
|
+
} | {
|
|
29
|
+
readonly type: "check";
|
|
30
|
+
} |
|
|
31
|
+
/** Adds to numeric attributes, creating the item from `init` when it is missing. */
|
|
32
|
+
{
|
|
33
|
+
readonly type: "add";
|
|
34
|
+
readonly by: Readonly<Record<string, number>>;
|
|
35
|
+
readonly init?: StoredRow;
|
|
36
|
+
});
|
|
37
|
+
/** Sort keys from `gte` up to `lt` in one partition, resuming past `after`. */
|
|
38
|
+
interface KvRange {
|
|
39
|
+
readonly pk: string;
|
|
40
|
+
readonly gte?: string;
|
|
41
|
+
readonly lt?: string;
|
|
42
|
+
/**
|
|
43
|
+
* `lt` as an inclusive bound, with `lt`: every key the helper writes is
|
|
44
|
+
* below `lt` exactly when it is at most `lte`. A store whose two-sided
|
|
45
|
+
* range is inclusive (DynamoDB's `BETWEEN`) uses it to read nothing past
|
|
46
|
+
* the range.
|
|
47
|
+
*/
|
|
48
|
+
readonly lte?: string;
|
|
49
|
+
readonly order: "asc" | "desc";
|
|
50
|
+
readonly limit: number;
|
|
51
|
+
readonly after?: string;
|
|
52
|
+
}
|
|
53
|
+
interface KvItem {
|
|
54
|
+
readonly sk: string;
|
|
55
|
+
readonly value: StoredRow;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A key-value backend (DynamoDB, Firestore): strongly consistent point
|
|
59
|
+
* reads, one partition's sort-key range per page, and atomic writes whose
|
|
60
|
+
* conditions all see the state before the write. Sort keys compare as UTF-8.
|
|
61
|
+
*/
|
|
62
|
+
interface KeyValueStore {
|
|
63
|
+
readonly id: string;
|
|
64
|
+
/**
|
|
65
|
+
* The most items, and bytes, one atomic write takes; `itemBytes` caps each
|
|
66
|
+
* item, and `keyBytes` the partition and sort keys the store indexes whole.
|
|
67
|
+
*/
|
|
68
|
+
readonly limits: {
|
|
69
|
+
readonly items: number;
|
|
70
|
+
readonly bytes: number;
|
|
71
|
+
readonly itemBytes?: number;
|
|
72
|
+
readonly keyBytes?: {
|
|
73
|
+
readonly pk: number;
|
|
74
|
+
readonly sk: number;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
get(keys: readonly KvKey[]): Promise<readonly (StoredRow | null)[]>;
|
|
78
|
+
/** One native page; `more` when the range may go on past it. An empty page ends the range. */
|
|
79
|
+
query(range: KvRange): Promise<{
|
|
80
|
+
readonly items: readonly KvItem[];
|
|
81
|
+
readonly more: boolean;
|
|
82
|
+
}>;
|
|
83
|
+
/** Applies every op or none; `failedOp` names an op whose condition failed. */
|
|
84
|
+
write(ops: readonly KvOp[]): Promise<WriteResult>;
|
|
85
|
+
/** Deletes items with no condition and not atomically, a batch at a time. */
|
|
86
|
+
deleteConsumed?(keys: readonly KvKey[]): Promise<void>;
|
|
87
|
+
/** Prepares the store, such as its one table, for `db migrate`. */
|
|
88
|
+
readonly migrations?: {
|
|
89
|
+
apply(): Promise<void>;
|
|
90
|
+
};
|
|
91
|
+
dispose?(): Promise<void>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Order-preserving text for key values; each value ends in the lowest code
|
|
95
|
+
* point any encoding uses. `createKvAdapter` stores a row as the item with
|
|
96
|
+
* the table's name as `pk` and its key's text as `sk`, so tooling that
|
|
97
|
+
* writes a store's items itself, as the AWS setup's settings items do, keys
|
|
98
|
+
* them with it.
|
|
99
|
+
*/
|
|
100
|
+
declare const encodeKvKey: (values: readonly DatabaseKeyValue[]) => string;
|
|
101
|
+
/** `createKvAdapter`'s options. */
|
|
102
|
+
interface KvAdapterOptions {
|
|
103
|
+
readonly store: KeyValueStore;
|
|
104
|
+
readonly tablePrefix?: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The database adapter over a key-value store. A row is one item at
|
|
108
|
+
* `pk = <table>`, `sk = enc(key)`; each index not in key order adds an item
|
|
109
|
+
* per entry at `pk = <table>#<index>#enc(eq)`, `sk = enc(order)`, and each
|
|
110
|
+
* unique entry one item at `sk = "#"`, written only where no other row holds
|
|
111
|
+
* it. Index items hold a copy of the row without `_v` or counters, so an
|
|
112
|
+
* increment, which changes only those, never makes them stale.
|
|
113
|
+
*/
|
|
114
|
+
declare const createKvAdapter: ({ store, tablePrefix }: KvAdapterOptions) => DatabaseAdapter;
|
|
115
|
+
//#endregion
|
|
116
|
+
export { KeyValueStore, KvAdapterOptions, KvCondition, KvItem, KvKey, KvOp, KvRange, createKvAdapter, encodeKvKey };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { DatabaseAdapter, DatabaseKeyValue, StoredRow, WriteResult } from "../../../database/adapter.mjs";
|
|
2
|
+
//#region src/engine/database/kv/kvAdapter.d.ts
|
|
3
|
+
/** An item's address: a partition, and a sort key within it. */
|
|
4
|
+
interface KvKey {
|
|
5
|
+
readonly pk: string;
|
|
6
|
+
readonly sk: string;
|
|
7
|
+
}
|
|
8
|
+
/** What an op requires of its item before the write: that it exists or not, or that its `_v` is `v`. */
|
|
9
|
+
type KvCondition = {
|
|
10
|
+
readonly exists: boolean;
|
|
11
|
+
} | {
|
|
12
|
+
readonly v: number;
|
|
13
|
+
};
|
|
14
|
+
type KvOp = {
|
|
15
|
+
readonly key: KvKey;
|
|
16
|
+
readonly condition?: KvCondition;
|
|
17
|
+
/**
|
|
18
|
+
* When a put's or a created item expires, in epoch milliseconds, which
|
|
19
|
+
* the store maps to its native TTL. Every item of a row of a table with
|
|
20
|
+
* retention carries it, index copies included.
|
|
21
|
+
*/
|
|
22
|
+
readonly expiresAt?: number;
|
|
23
|
+
} & ({
|
|
24
|
+
readonly type: "put";
|
|
25
|
+
readonly value: StoredRow;
|
|
26
|
+
} | {
|
|
27
|
+
readonly type: "delete";
|
|
28
|
+
} | {
|
|
29
|
+
readonly type: "check";
|
|
30
|
+
} |
|
|
31
|
+
/** Adds to numeric attributes, creating the item from `init` when it is missing. */
|
|
32
|
+
{
|
|
33
|
+
readonly type: "add";
|
|
34
|
+
readonly by: Readonly<Record<string, number>>;
|
|
35
|
+
readonly init?: StoredRow;
|
|
36
|
+
});
|
|
37
|
+
/** Sort keys from `gte` up to `lt` in one partition, resuming past `after`. */
|
|
38
|
+
interface KvRange {
|
|
39
|
+
readonly pk: string;
|
|
40
|
+
readonly gte?: string;
|
|
41
|
+
readonly lt?: string;
|
|
42
|
+
/**
|
|
43
|
+
* `lt` as an inclusive bound, with `lt`: every key the helper writes is
|
|
44
|
+
* below `lt` exactly when it is at most `lte`. A store whose two-sided
|
|
45
|
+
* range is inclusive (DynamoDB's `BETWEEN`) uses it to read nothing past
|
|
46
|
+
* the range.
|
|
47
|
+
*/
|
|
48
|
+
readonly lte?: string;
|
|
49
|
+
readonly order: "asc" | "desc";
|
|
50
|
+
readonly limit: number;
|
|
51
|
+
readonly after?: string;
|
|
52
|
+
}
|
|
53
|
+
interface KvItem {
|
|
54
|
+
readonly sk: string;
|
|
55
|
+
readonly value: StoredRow;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A key-value backend (DynamoDB, Firestore): strongly consistent point
|
|
59
|
+
* reads, one partition's sort-key range per page, and atomic writes whose
|
|
60
|
+
* conditions all see the state before the write. Sort keys compare as UTF-8.
|
|
61
|
+
*/
|
|
62
|
+
interface KeyValueStore {
|
|
63
|
+
readonly id: string;
|
|
64
|
+
/**
|
|
65
|
+
* The most items, and bytes, one atomic write takes; `itemBytes` caps each
|
|
66
|
+
* item, and `keyBytes` the partition and sort keys the store indexes whole.
|
|
67
|
+
*/
|
|
68
|
+
readonly limits: {
|
|
69
|
+
readonly items: number;
|
|
70
|
+
readonly bytes: number;
|
|
71
|
+
readonly itemBytes?: number;
|
|
72
|
+
readonly keyBytes?: {
|
|
73
|
+
readonly pk: number;
|
|
74
|
+
readonly sk: number;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
get(keys: readonly KvKey[]): Promise<readonly (StoredRow | null)[]>;
|
|
78
|
+
/** One native page; `more` when the range may go on past it. An empty page ends the range. */
|
|
79
|
+
query(range: KvRange): Promise<{
|
|
80
|
+
readonly items: readonly KvItem[];
|
|
81
|
+
readonly more: boolean;
|
|
82
|
+
}>;
|
|
83
|
+
/** Applies every op or none; `failedOp` names an op whose condition failed. */
|
|
84
|
+
write(ops: readonly KvOp[]): Promise<WriteResult>;
|
|
85
|
+
/** Deletes items with no condition and not atomically, a batch at a time. */
|
|
86
|
+
deleteConsumed?(keys: readonly KvKey[]): Promise<void>;
|
|
87
|
+
/** Prepares the store, such as its one table, for `db migrate`. */
|
|
88
|
+
readonly migrations?: {
|
|
89
|
+
apply(): Promise<void>;
|
|
90
|
+
};
|
|
91
|
+
dispose?(): Promise<void>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Order-preserving text for key values; each value ends in the lowest code
|
|
95
|
+
* point any encoding uses. `createKvAdapter` stores a row as the item with
|
|
96
|
+
* the table's name as `pk` and its key's text as `sk`, so tooling that
|
|
97
|
+
* writes a store's items itself, as the AWS setup's settings items do, keys
|
|
98
|
+
* them with it.
|
|
99
|
+
*/
|
|
100
|
+
declare const encodeKvKey: (values: readonly DatabaseKeyValue[]) => string;
|
|
101
|
+
/** `createKvAdapter`'s options. */
|
|
102
|
+
interface KvAdapterOptions {
|
|
103
|
+
readonly store: KeyValueStore;
|
|
104
|
+
readonly tablePrefix?: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The database adapter over a key-value store. A row is one item at
|
|
108
|
+
* `pk = <table>`, `sk = enc(key)`; each index not in key order adds an item
|
|
109
|
+
* per entry at `pk = <table>#<index>#enc(eq)`, `sk = enc(order)`, and each
|
|
110
|
+
* unique entry one item at `sk = "#"`, written only where no other row holds
|
|
111
|
+
* it. Index items hold a copy of the row without `_v` or counters, so an
|
|
112
|
+
* increment, which changes only those, never makes them stale.
|
|
113
|
+
*/
|
|
114
|
+
declare const createKvAdapter: ({ store, tablePrefix }: KvAdapterOptions) => DatabaseAdapter;
|
|
115
|
+
//#endregion
|
|
116
|
+
export { KeyValueStore, KvAdapterOptions, KvCondition, KvItem, KvKey, KvOp, KvRange, createKvAdapter, encodeKvKey };
|