@hot-updater/plugin-core 1.0.0-rc.17 → 1.0.0-rc.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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/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 +20 -5
- package/dist/database/adapter.d.cts +25 -8
- package/dist/database/adapter.d.mts +25 -8
- package/dist/database/adapter.mjs +21 -4
- package/dist/database/distinct.cjs +12 -6
- package/dist/database/distinct.d.cts +13 -11
- package/dist/database/distinct.d.mts +13 -11
- package/dist/database/distinct.mjs +13 -6
- package/dist/database/memoryAdapter.cjs +2 -10
- package/dist/database/memoryAdapter.d.cts +2 -4
- package/dist/database/memoryAdapter.d.mts +2 -4
- package/dist/database/memoryAdapter.mjs +2 -10
- package/dist/database/values.cjs +5 -1
- package/dist/database/values.d.cts +8 -21
- package/dist/database/values.d.mts +8 -21
- package/dist/database/values.mjs +5 -1
- package/dist/database/verifyAdapter.cjs +1 -0
- package/dist/database/verifyAdapter.d.cts +8 -0
- package/dist/database/verifyAdapter.d.mts +8 -0
- package/dist/database/verifyAdapter.mjs +1 -0
- package/dist/databaseErrors.cjs +4 -4
- package/dist/databaseErrors.d.cts +6 -6
- package/dist/databaseErrors.d.mts +6 -6
- package/dist/databaseErrors.mjs +4 -4
- package/dist/databaseJsonValue.cjs +6 -0
- package/dist/databaseJsonValue.d.cts +6 -0
- package/dist/databaseJsonValue.d.mts +6 -0
- package/dist/databaseJsonValue.mjs +6 -0
- package/dist/databaseMetadata.cjs +3 -3
- package/dist/databaseMetadata.mjs +3 -3
- package/dist/databaseRows.cjs +6 -6
- package/dist/databaseRows.d.cts +1 -1
- package/dist/databaseRows.d.mts +1 -1
- package/dist/databaseRows.mjs +1 -1
- 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 +86 -7
- package/dist/index.d.cts +36 -6
- package/dist/index.d.mts +36 -6
- package/dist/index.mjs +26 -4
- 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 +26 -25
- package/dist/releaseCatalogCompiler.d.cts +1 -2
- package/dist/releaseCatalogCompiler.d.mts +2 -3
- package/dist/releaseCatalogCompiler.mjs +3 -7
- 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 +1 -1
- package/dist/types/databaseConfig.d.cts +8 -1
- package/dist/types/databaseConfig.d.mts +8 -1
- package/dist/types/databaseConfig.mjs +1 -1
- package/dist/types/databaseRows.d.cts +1 -1
- package/dist/types/databaseRows.d.mts +1 -1
- package/dist/types/index.d.cts +26 -14
- package/dist/types/index.d.mts +26 -14
- package/dist/types/public.d.cts +1 -1
- package/dist/types/public.d.mts +1 -1
- 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/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/internal.cjs +0 -38
- package/dist/internal.d.cts +0 -12
- package/dist/internal.d.mts +0 -12
- package/dist/internal.mjs +0 -8
- package/dist/types/internal.d.cts +0 -4
- package/dist/types/internal.d.mts +0 -4
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @hot-updater/plugin-core
|
|
2
|
+
|
|
3
|
+
The kit that extends the Hot Updater server: the storage and database adapter
|
|
4
|
+
contracts and their helpers, such as `createStorageAdapter`, the release
|
|
5
|
+
catalog compiler, the server plugin authoring API (`definePlugin`, the schema
|
|
6
|
+
DSL, the typed database handle, and the database errors), and the storage
|
|
7
|
+
engine every database runs on, with `createEngine` and `meterReads`. Adapter
|
|
8
|
+
and server plugin authors import it from here and list it in
|
|
9
|
+
`peerDependencies`, as Hot Updater's own plugin packages do.
|
|
10
|
+
|
|
11
|
+
## Boundary with `@hot-updater/protocol`
|
|
12
|
+
|
|
13
|
+
- `@hot-updater/protocol` is the device-safe, zero-dependency package for what
|
|
14
|
+
crosses a boundary: the formats and pure computations the app and the server
|
|
15
|
+
share, and the contracts between a host and its extensions on the device,
|
|
16
|
+
such as the client plugin contract. React Native imports it and the built-in
|
|
17
|
+
Insights client, never plugin-core or the server.
|
|
18
|
+
- `@hot-updater/plugin-core` is the kit that extends the server: the adapter
|
|
19
|
+
and server plugin contracts and their helpers. It runs wherever the server
|
|
20
|
+
runs, in Node, Workers, Deno, or Lambda, depends on `@hot-updater/protocol`,
|
|
21
|
+
never the reverse, and the app never imports it. When both sides need
|
|
22
|
+
something here, move it to `@hot-updater/protocol` and re-export it from this
|
|
23
|
+
package.
|
|
24
|
+
|
|
25
|
+
The public API is the package root, and it has no other entry. Hot Updater's
|
|
26
|
+
own packages use it the way any adapter or plugin does: a name another package
|
|
27
|
+
needs is on the root, with a JSDoc line that says why, and everything else stays
|
|
28
|
+
private to this package.
|
|
29
|
+
|
|
30
|
+
Core's tables are part of that API: `CoreSchema` types the `core` handle of
|
|
31
|
+
`createEngine`, and `coreSchema` and `coreTarget` are what tooling creates. A
|
|
32
|
+
change to core's schema is a release of this package, and it ships with the
|
|
33
|
+
migration that `schema.core` versions.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const require_range_DvX_Y6iv = require("./node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.cjs");
|
|
2
|
+
let _hot_updater_protocol = require("@hot-updater/protocol");
|
|
3
|
+
//#region src/baseCandidateKey.ts
|
|
4
|
+
/**
|
|
5
|
+
* A new bundle's base-candidate key, which `findBaseBundleIds` on core's API
|
|
6
|
+
* and on a plugin's `ctx.core` takes: the Release Catalog scope its devices
|
|
7
|
+
* update from, and for an app version its normalized range; null for a
|
|
8
|
+
* target without a valid range. The CLI's deploy produces it this way.
|
|
9
|
+
*/
|
|
10
|
+
const targetBaseCandidateKey = (target) => {
|
|
11
|
+
const channelKey = (0, _hot_updater_protocol.encodeChannelKey)(target.channel);
|
|
12
|
+
if (target.fingerprintHash) return JSON.stringify([(0, _hot_updater_protocol.createReleaseCatalogScopeKey)({
|
|
13
|
+
channelKey,
|
|
14
|
+
fingerprintHash: target.fingerprintHash,
|
|
15
|
+
platform: target.platform,
|
|
16
|
+
strategy: "FINGERPRINT"
|
|
17
|
+
})]);
|
|
18
|
+
const range = target.appVersion === null ? null : require_range_DvX_Y6iv.l(target.appVersion);
|
|
19
|
+
return range === null ? null : JSON.stringify([(0, _hot_updater_protocol.createReleaseCatalogScopeKey)({
|
|
20
|
+
channelKey,
|
|
21
|
+
platform: target.platform,
|
|
22
|
+
strategy: "APP_VERSION"
|
|
23
|
+
}), range]);
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The scope and range a base-candidate key names, null for any other
|
|
27
|
+
* string: what an implementation of `findBaseBundleIds` reads the key with.
|
|
28
|
+
*/
|
|
29
|
+
const parseBaseCandidateKey = (key) => {
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(key);
|
|
32
|
+
if (!Array.isArray(parsed) || typeof parsed[0] !== "string") return null;
|
|
33
|
+
const [scopeKey, text] = parsed;
|
|
34
|
+
const { strategy } = (0, _hot_updater_protocol.parseReleaseCatalogScopeKey)(scopeKey);
|
|
35
|
+
if (strategy === "FINGERPRINT") return parsed.length === 1 ? {
|
|
36
|
+
scopeKey,
|
|
37
|
+
range: null
|
|
38
|
+
} : null;
|
|
39
|
+
const range = parsed.length === 2 && typeof text === "string" ? require_range_DvX_Y6iv.l(text) : null;
|
|
40
|
+
return range === null ? null : {
|
|
41
|
+
scopeKey,
|
|
42
|
+
range
|
|
43
|
+
};
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
exports.parseBaseCandidateKey = parseBaseCandidateKey;
|
|
50
|
+
exports.targetBaseCandidateKey = targetBaseCandidateKey;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/baseCandidateKey.d.ts
|
|
2
|
+
/** A new bundle's auto-patch target: where it is released, and to whom. */
|
|
3
|
+
interface BaseCandidateTarget {
|
|
4
|
+
readonly channel: string;
|
|
5
|
+
readonly platform: "ios" | "android";
|
|
6
|
+
readonly fingerprintHash: string | null;
|
|
7
|
+
readonly appVersion: string | null;
|
|
8
|
+
}
|
|
9
|
+
/** What a base-candidate key names: a catalog scope, and an app-version scope's target range. */
|
|
10
|
+
interface BaseCandidateQuery {
|
|
11
|
+
readonly scopeKey: string;
|
|
12
|
+
readonly range: string | null;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A new bundle's base-candidate key, which `findBaseBundleIds` on core's API
|
|
16
|
+
* and on a plugin's `ctx.core` takes: the Release Catalog scope its devices
|
|
17
|
+
* update from, and for an app version its normalized range; null for a
|
|
18
|
+
* target without a valid range. The CLI's deploy produces it this way.
|
|
19
|
+
*/
|
|
20
|
+
declare const targetBaseCandidateKey: (target: BaseCandidateTarget) => string | null;
|
|
21
|
+
/**
|
|
22
|
+
* The scope and range a base-candidate key names, null for any other
|
|
23
|
+
* string: what an implementation of `findBaseBundleIds` reads the key with.
|
|
24
|
+
*/
|
|
25
|
+
declare const parseBaseCandidateKey: (key: string) => BaseCandidateQuery | null;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { BaseCandidateQuery, BaseCandidateTarget, parseBaseCandidateKey, targetBaseCandidateKey };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/baseCandidateKey.d.ts
|
|
2
|
+
/** A new bundle's auto-patch target: where it is released, and to whom. */
|
|
3
|
+
interface BaseCandidateTarget {
|
|
4
|
+
readonly channel: string;
|
|
5
|
+
readonly platform: "ios" | "android";
|
|
6
|
+
readonly fingerprintHash: string | null;
|
|
7
|
+
readonly appVersion: string | null;
|
|
8
|
+
}
|
|
9
|
+
/** What a base-candidate key names: a catalog scope, and an app-version scope's target range. */
|
|
10
|
+
interface BaseCandidateQuery {
|
|
11
|
+
readonly scopeKey: string;
|
|
12
|
+
readonly range: string | null;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A new bundle's base-candidate key, which `findBaseBundleIds` on core's API
|
|
16
|
+
* and on a plugin's `ctx.core` takes: the Release Catalog scope its devices
|
|
17
|
+
* update from, and for an app version its normalized range; null for a
|
|
18
|
+
* target without a valid range. The CLI's deploy produces it this way.
|
|
19
|
+
*/
|
|
20
|
+
declare const targetBaseCandidateKey: (target: BaseCandidateTarget) => string | null;
|
|
21
|
+
/**
|
|
22
|
+
* The scope and range a base-candidate key names, null for any other
|
|
23
|
+
* string: what an implementation of `findBaseBundleIds` reads the key with.
|
|
24
|
+
*/
|
|
25
|
+
declare const parseBaseCandidateKey: (key: string) => BaseCandidateQuery | null;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { BaseCandidateQuery, BaseCandidateTarget, parseBaseCandidateKey, targetBaseCandidateKey };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { l as normalizeRange } from "./node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.mjs";
|
|
2
|
+
import { createReleaseCatalogScopeKey, encodeChannelKey, parseReleaseCatalogScopeKey } from "@hot-updater/protocol";
|
|
3
|
+
//#region src/baseCandidateKey.ts
|
|
4
|
+
/**
|
|
5
|
+
* A new bundle's base-candidate key, which `findBaseBundleIds` on core's API
|
|
6
|
+
* and on a plugin's `ctx.core` takes: the Release Catalog scope its devices
|
|
7
|
+
* update from, and for an app version its normalized range; null for a
|
|
8
|
+
* target without a valid range. The CLI's deploy produces it this way.
|
|
9
|
+
*/
|
|
10
|
+
const targetBaseCandidateKey = (target) => {
|
|
11
|
+
const channelKey = encodeChannelKey(target.channel);
|
|
12
|
+
if (target.fingerprintHash) return JSON.stringify([createReleaseCatalogScopeKey({
|
|
13
|
+
channelKey,
|
|
14
|
+
fingerprintHash: target.fingerprintHash,
|
|
15
|
+
platform: target.platform,
|
|
16
|
+
strategy: "FINGERPRINT"
|
|
17
|
+
})]);
|
|
18
|
+
const range = target.appVersion === null ? null : normalizeRange(target.appVersion);
|
|
19
|
+
return range === null ? null : JSON.stringify([createReleaseCatalogScopeKey({
|
|
20
|
+
channelKey,
|
|
21
|
+
platform: target.platform,
|
|
22
|
+
strategy: "APP_VERSION"
|
|
23
|
+
}), range]);
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The scope and range a base-candidate key names, null for any other
|
|
27
|
+
* string: what an implementation of `findBaseBundleIds` reads the key with.
|
|
28
|
+
*/
|
|
29
|
+
const parseBaseCandidateKey = (key) => {
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(key);
|
|
32
|
+
if (!Array.isArray(parsed) || typeof parsed[0] !== "string") return null;
|
|
33
|
+
const [scopeKey, text] = parsed;
|
|
34
|
+
const { strategy } = parseReleaseCatalogScopeKey(scopeKey);
|
|
35
|
+
if (strategy === "FINGERPRINT") return parsed.length === 1 ? {
|
|
36
|
+
scopeKey,
|
|
37
|
+
range: null
|
|
38
|
+
} : null;
|
|
39
|
+
const range = parsed.length === 2 && typeof text === "string" ? normalizeRange(text) : null;
|
|
40
|
+
return range === null ? null : {
|
|
41
|
+
scopeKey,
|
|
42
|
+
range
|
|
43
|
+
};
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { parseBaseCandidateKey, targetBaseCandidateKey };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/createStorageAdapter.ts
|
|
2
|
+
const createStorageAdapter = (options) => ({ ...options });
|
|
3
|
+
function assertStorageOperations(adapter, operations) {
|
|
4
|
+
for (const operation of operations) if (typeof adapter[operation] !== "function") throw new Error(`Storage adapter "${adapter.name}" does not implement ${operation}.`);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
exports.assertStorageOperations = assertStorageOperations;
|
|
8
|
+
exports.createStorageAdapter = createStorageAdapter;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { StorageAdapter } from "./types/index.cjs";
|
|
2
|
+
//#region src/createStorageAdapter.d.ts
|
|
3
|
+
type CreateStorageAdapterOptions = StorageAdapter;
|
|
4
|
+
type StorageOperation = Exclude<keyof StorageAdapter, "name" | "protocol">;
|
|
5
|
+
type StorageAdapterWith<TOperation extends StorageOperation> = StorageAdapter & Required<Pick<StorageAdapter, TOperation>>;
|
|
6
|
+
declare const createStorageAdapter: <const TOptions extends CreateStorageAdapterOptions>(options: TOptions) => TOptions;
|
|
7
|
+
declare function assertStorageOperations<const TOperations extends readonly StorageOperation[]>(adapter: StorageAdapter, operations: TOperations): asserts adapter is StorageAdapterWith<TOperations[number]>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { CreateStorageAdapterOptions, StorageAdapterWith, StorageOperation, assertStorageOperations, createStorageAdapter };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { StorageAdapter } from "./types/index.mjs";
|
|
2
|
+
//#region src/createStorageAdapter.d.ts
|
|
3
|
+
type CreateStorageAdapterOptions = StorageAdapter;
|
|
4
|
+
type StorageOperation = Exclude<keyof StorageAdapter, "name" | "protocol">;
|
|
5
|
+
type StorageAdapterWith<TOperation extends StorageOperation> = StorageAdapter & Required<Pick<StorageAdapter, TOperation>>;
|
|
6
|
+
declare const createStorageAdapter: <const TOptions extends CreateStorageAdapterOptions>(options: TOptions) => TOptions;
|
|
7
|
+
declare function assertStorageOperations<const TOperations extends readonly StorageOperation[]>(adapter: StorageAdapter, operations: TOperations): asserts adapter is StorageAdapterWith<TOperations[number]>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { CreateStorageAdapterOptions, StorageAdapterWith, StorageOperation, assertStorageOperations, createStorageAdapter };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/createStorageAdapter.ts
|
|
2
|
+
const createStorageAdapter = (options) => ({ ...options });
|
|
3
|
+
function assertStorageOperations(adapter, operations) {
|
|
4
|
+
for (const operation of operations) if (typeof adapter[operation] !== "function") throw new Error(`Storage adapter "${adapter.name}" does not implement ${operation}.`);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { assertStorageOperations, createStorageAdapter };
|
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
//#region src/database/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* The version column every stored row carries, which a guarded write
|
|
4
|
+
* compares and bumps. An adapter that is neither SQL nor key-value, such as
|
|
5
|
+
* the MongoDB adapter, stores and checks it itself.
|
|
6
|
+
*/
|
|
2
7
|
const DATABASE_VERSION_COLUMN = "_v";
|
|
3
|
-
const DATABASE_MAX_QUERY_LIMIT = 500;
|
|
4
|
-
const DATABASE_MAX_MULTI_VALUES = 16;
|
|
5
8
|
var DatabaseSchemaError = class extends Error {
|
|
6
9
|
name = "DatabaseSchemaError";
|
|
7
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* The column `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
13
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, reads a
|
|
14
|
+
* column's type.
|
|
15
|
+
*/
|
|
8
16
|
const findPhysicalColumn = (table, name) => {
|
|
9
17
|
const column = table.columns.find((candidate) => candidate.name === name);
|
|
10
18
|
if (column === void 0) throw new DatabaseSchemaError(`${table.name} has no column ${name}.`);
|
|
11
19
|
return column;
|
|
12
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* The index `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
23
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, resolves a
|
|
24
|
+
* query's `index`.
|
|
25
|
+
*/
|
|
13
26
|
const findPhysicalIndex = (table, name) => {
|
|
14
27
|
const index = table.indexes.find((candidate) => candidate.name === name);
|
|
15
28
|
if (index === void 0) throw new DatabaseSchemaError(`${table.name} has no index ${name}.`);
|
|
@@ -28,15 +41,17 @@ const expiresAt = (table, row) => {
|
|
|
28
41
|
const time = table.retention && row[table.retention.column];
|
|
29
42
|
return typeof time === "number" ? time + table.retention.ms : void 0;
|
|
30
43
|
};
|
|
31
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* The primary key of a row, in key-column order: what `get` takes for it.
|
|
46
|
+
* An adapter that is neither SQL nor key-value, such as the MongoDB adapter,
|
|
47
|
+
* addresses rows by it.
|
|
48
|
+
*/
|
|
32
49
|
const rowKey = (table, row) => table.key.map((column) => {
|
|
33
50
|
const value = row[column];
|
|
34
51
|
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") throw new DatabaseSchemaError(`${table.name}.${column} is a key column and must be a string, number, or boolean.`);
|
|
35
52
|
return value;
|
|
36
53
|
});
|
|
37
54
|
//#endregion
|
|
38
|
-
exports.DATABASE_MAX_MULTI_VALUES = DATABASE_MAX_MULTI_VALUES;
|
|
39
|
-
exports.DATABASE_MAX_QUERY_LIMIT = DATABASE_MAX_QUERY_LIMIT;
|
|
40
55
|
exports.DATABASE_VERSION_COLUMN = DATABASE_VERSION_COLUMN;
|
|
41
56
|
exports.DatabaseSchemaError = DatabaseSchemaError;
|
|
42
57
|
exports.expiresAt = expiresAt;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
//#region src/database/adapter.d.ts
|
|
2
2
|
/**
|
|
3
|
-
* The
|
|
3
|
+
* The database adapter contract. An adapter implements `get`, `query`, and
|
|
4
4
|
* `write` with its backend's native features and knows nothing about Hot
|
|
5
|
-
* Updater's domain or plugins. Adapter authors import it from
|
|
6
|
-
* `@hot-updater/
|
|
5
|
+
* Updater's domain or plugins. Adapter authors import it from
|
|
6
|
+
* `@hot-updater/plugin-core`.
|
|
7
7
|
*/
|
|
8
8
|
type DatabaseJson = null | boolean | number | string | readonly DatabaseJson[] | {
|
|
9
9
|
readonly [key: string]: DatabaseJson;
|
|
@@ -153,19 +153,32 @@ interface DatabaseAdapter {
|
|
|
153
153
|
* deleted. A backend that expires rows natively leaves it out.
|
|
154
154
|
*/
|
|
155
155
|
prune?(table: PhysicalTable, before: number, limit: number): Promise<number>;
|
|
156
|
-
/** Called only by
|
|
156
|
+
/** Called only by migration tooling, such as `hot-updater db migrate`. */
|
|
157
157
|
readonly migrations?: {
|
|
158
158
|
apply(tables: readonly PhysicalTable[]): Promise<void>;
|
|
159
159
|
};
|
|
160
160
|
dispose?(): Promise<void>;
|
|
161
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* The version column every stored row carries, which a guarded write
|
|
164
|
+
* compares and bumps. An adapter that is neither SQL nor key-value, such as
|
|
165
|
+
* the MongoDB adapter, stores and checks it itself.
|
|
166
|
+
*/
|
|
162
167
|
declare const DATABASE_VERSION_COLUMN = "_v";
|
|
163
|
-
declare const DATABASE_MAX_QUERY_LIMIT = 500;
|
|
164
|
-
declare const DATABASE_MAX_MULTI_VALUES = 16;
|
|
165
168
|
declare class DatabaseSchemaError extends Error {
|
|
166
169
|
readonly name = "DatabaseSchemaError";
|
|
167
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* The column `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
173
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, reads a
|
|
174
|
+
* column's type.
|
|
175
|
+
*/
|
|
168
176
|
declare const findPhysicalColumn: (table: PhysicalTable, name: string) => PhysicalColumn;
|
|
177
|
+
/**
|
|
178
|
+
* The index `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
179
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, resolves a
|
|
180
|
+
* query's `index`.
|
|
181
|
+
*/
|
|
169
182
|
declare const findPhysicalIndex: (table: PhysicalTable, name: string) => PhysicalIndex;
|
|
170
183
|
/**
|
|
171
184
|
* The columns that order an index read and close its cursors: the sort
|
|
@@ -177,7 +190,11 @@ declare const indexOrderColumns: (table: PhysicalTable, index: PhysicalIndex) =>
|
|
|
177
190
|
* undefined when the table keeps its rows or the row's time is null.
|
|
178
191
|
*/
|
|
179
192
|
declare const expiresAt: (table: PhysicalTable, row: StoredRow) => number | undefined;
|
|
180
|
-
/**
|
|
193
|
+
/**
|
|
194
|
+
* The primary key of a row, in key-column order: what `get` takes for it.
|
|
195
|
+
* An adapter that is neither SQL nor key-value, such as the MongoDB adapter,
|
|
196
|
+
* addresses rows by it.
|
|
197
|
+
*/
|
|
181
198
|
declare const rowKey: (table: PhysicalTable, row: StoredRow) => DatabaseKey;
|
|
182
199
|
//#endregion
|
|
183
|
-
export {
|
|
200
|
+
export { DATABASE_VERSION_COLUMN, DatabaseAdapter, DatabaseJson, DatabaseKey, DatabaseKeyValue, DatabaseSchemaError, DatabaseValue, PhysicalColumn, PhysicalColumnType, PhysicalIndex, PhysicalRetention, PhysicalTable, QueryBound, QueryRequest, StoredRow, WriteGuard, WriteOp, WriteResult, expiresAt, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
//#region src/database/adapter.d.ts
|
|
2
2
|
/**
|
|
3
|
-
* The
|
|
3
|
+
* The database adapter contract. An adapter implements `get`, `query`, and
|
|
4
4
|
* `write` with its backend's native features and knows nothing about Hot
|
|
5
|
-
* Updater's domain or plugins. Adapter authors import it from
|
|
6
|
-
* `@hot-updater/
|
|
5
|
+
* Updater's domain or plugins. Adapter authors import it from
|
|
6
|
+
* `@hot-updater/plugin-core`.
|
|
7
7
|
*/
|
|
8
8
|
type DatabaseJson = null | boolean | number | string | readonly DatabaseJson[] | {
|
|
9
9
|
readonly [key: string]: DatabaseJson;
|
|
@@ -153,19 +153,32 @@ interface DatabaseAdapter {
|
|
|
153
153
|
* deleted. A backend that expires rows natively leaves it out.
|
|
154
154
|
*/
|
|
155
155
|
prune?(table: PhysicalTable, before: number, limit: number): Promise<number>;
|
|
156
|
-
/** Called only by
|
|
156
|
+
/** Called only by migration tooling, such as `hot-updater db migrate`. */
|
|
157
157
|
readonly migrations?: {
|
|
158
158
|
apply(tables: readonly PhysicalTable[]): Promise<void>;
|
|
159
159
|
};
|
|
160
160
|
dispose?(): Promise<void>;
|
|
161
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* The version column every stored row carries, which a guarded write
|
|
164
|
+
* compares and bumps. An adapter that is neither SQL nor key-value, such as
|
|
165
|
+
* the MongoDB adapter, stores and checks it itself.
|
|
166
|
+
*/
|
|
162
167
|
declare const DATABASE_VERSION_COLUMN = "_v";
|
|
163
|
-
declare const DATABASE_MAX_QUERY_LIMIT = 500;
|
|
164
|
-
declare const DATABASE_MAX_MULTI_VALUES = 16;
|
|
165
168
|
declare class DatabaseSchemaError extends Error {
|
|
166
169
|
readonly name = "DatabaseSchemaError";
|
|
167
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* The column `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
173
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, reads a
|
|
174
|
+
* column's type.
|
|
175
|
+
*/
|
|
168
176
|
declare const findPhysicalColumn: (table: PhysicalTable, name: string) => PhysicalColumn;
|
|
177
|
+
/**
|
|
178
|
+
* The index `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
179
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, resolves a
|
|
180
|
+
* query's `index`.
|
|
181
|
+
*/
|
|
169
182
|
declare const findPhysicalIndex: (table: PhysicalTable, name: string) => PhysicalIndex;
|
|
170
183
|
/**
|
|
171
184
|
* The columns that order an index read and close its cursors: the sort
|
|
@@ -177,7 +190,11 @@ declare const indexOrderColumns: (table: PhysicalTable, index: PhysicalIndex) =>
|
|
|
177
190
|
* undefined when the table keeps its rows or the row's time is null.
|
|
178
191
|
*/
|
|
179
192
|
declare const expiresAt: (table: PhysicalTable, row: StoredRow) => number | undefined;
|
|
180
|
-
/**
|
|
193
|
+
/**
|
|
194
|
+
* The primary key of a row, in key-column order: what `get` takes for it.
|
|
195
|
+
* An adapter that is neither SQL nor key-value, such as the MongoDB adapter,
|
|
196
|
+
* addresses rows by it.
|
|
197
|
+
*/
|
|
181
198
|
declare const rowKey: (table: PhysicalTable, row: StoredRow) => DatabaseKey;
|
|
182
199
|
//#endregion
|
|
183
|
-
export {
|
|
200
|
+
export { DATABASE_VERSION_COLUMN, DatabaseAdapter, DatabaseJson, DatabaseKey, DatabaseKeyValue, DatabaseSchemaError, DatabaseValue, PhysicalColumn, PhysicalColumnType, PhysicalIndex, PhysicalRetention, PhysicalTable, QueryBound, QueryRequest, StoredRow, WriteGuard, WriteOp, WriteResult, expiresAt, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
//#region src/database/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* The version column every stored row carries, which a guarded write
|
|
4
|
+
* compares and bumps. An adapter that is neither SQL nor key-value, such as
|
|
5
|
+
* the MongoDB adapter, stores and checks it itself.
|
|
6
|
+
*/
|
|
2
7
|
const DATABASE_VERSION_COLUMN = "_v";
|
|
3
|
-
const DATABASE_MAX_QUERY_LIMIT = 500;
|
|
4
|
-
const DATABASE_MAX_MULTI_VALUES = 16;
|
|
5
8
|
var DatabaseSchemaError = class extends Error {
|
|
6
9
|
name = "DatabaseSchemaError";
|
|
7
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* The column `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
13
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, reads a
|
|
14
|
+
* column's type.
|
|
15
|
+
*/
|
|
8
16
|
const findPhysicalColumn = (table, name) => {
|
|
9
17
|
const column = table.columns.find((candidate) => candidate.name === name);
|
|
10
18
|
if (column === void 0) throw new DatabaseSchemaError(`${table.name} has no column ${name}.`);
|
|
11
19
|
return column;
|
|
12
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* The index `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
23
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, resolves a
|
|
24
|
+
* query's `index`.
|
|
25
|
+
*/
|
|
13
26
|
const findPhysicalIndex = (table, name) => {
|
|
14
27
|
const index = table.indexes.find((candidate) => candidate.name === name);
|
|
15
28
|
if (index === void 0) throw new DatabaseSchemaError(`${table.name} has no index ${name}.`);
|
|
@@ -28,11 +41,15 @@ const expiresAt = (table, row) => {
|
|
|
28
41
|
const time = table.retention && row[table.retention.column];
|
|
29
42
|
return typeof time === "number" ? time + table.retention.ms : void 0;
|
|
30
43
|
};
|
|
31
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* The primary key of a row, in key-column order: what `get` takes for it.
|
|
46
|
+
* An adapter that is neither SQL nor key-value, such as the MongoDB adapter,
|
|
47
|
+
* addresses rows by it.
|
|
48
|
+
*/
|
|
32
49
|
const rowKey = (table, row) => table.key.map((column) => {
|
|
33
50
|
const value = row[column];
|
|
34
51
|
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") throw new DatabaseSchemaError(`${table.name}.${column} is a key column and must be a string, number, or boolean.`);
|
|
35
52
|
return value;
|
|
36
53
|
});
|
|
37
54
|
//#endregion
|
|
38
|
-
export {
|
|
55
|
+
export { DATABASE_VERSION_COLUMN, DatabaseSchemaError, expiresAt, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -43,14 +43,20 @@ const distinctRegister = (identity) => {
|
|
|
43
43
|
character: String.fromCharCode(rank + OFFSET)
|
|
44
44
|
};
|
|
45
45
|
};
|
|
46
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
48
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
49
|
+
*/
|
|
47
50
|
const addDistinct = (summary, identity) => {
|
|
48
51
|
const registers = decode(summary);
|
|
49
52
|
const { index, rank } = distinctRegister(identity);
|
|
50
53
|
registers[index] = Math.max(registers[index], rank);
|
|
51
54
|
return encode(registers);
|
|
52
55
|
};
|
|
53
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
58
|
+
* `distinct` column across the aggregate rows it reads.
|
|
59
|
+
*/
|
|
54
60
|
const mergeDistinct = (summaries) => {
|
|
55
61
|
const merged = new Uint8Array(REGISTER_COUNT);
|
|
56
62
|
for (const summary of summaries) {
|
|
@@ -59,7 +65,10 @@ const mergeDistinct = (summaries) => {
|
|
|
59
65
|
}
|
|
60
66
|
return encode(merged);
|
|
61
67
|
};
|
|
62
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
70
|
+
* reads a `distinct` column.
|
|
71
|
+
*/
|
|
63
72
|
const countDistinct = (summary) => {
|
|
64
73
|
const registers = decode(summary);
|
|
65
74
|
let inverseSum = 0;
|
|
@@ -72,11 +81,8 @@ const countDistinct = (summary) => {
|
|
|
72
81
|
const estimate = raw <= 2.5 * REGISTER_COUNT && empty > 0 ? REGISTER_COUNT * Math.log(REGISTER_COUNT / empty) : raw;
|
|
73
82
|
return Math.max(0, Math.round(estimate));
|
|
74
83
|
};
|
|
75
|
-
/** A sketch that holds no value. */
|
|
76
|
-
const emptyDistinct = () => encode(new Uint8Array(REGISTER_COUNT));
|
|
77
84
|
//#endregion
|
|
78
85
|
exports.addDistinct = addDistinct;
|
|
79
86
|
exports.countDistinct = countDistinct;
|
|
80
87
|
exports.distinctRegister = distinctRegister;
|
|
81
|
-
exports.emptyDistinct = emptyDistinct;
|
|
82
88
|
exports.mergeDistinct = mergeDistinct;
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
//#region src/database/distinct.d.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
/** The sketch with `identity` added; a missing sketch is the empty one. */
|
|
2
|
+
/**
|
|
3
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
4
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
5
|
+
*/
|
|
8
6
|
declare const addDistinct: (summary: string | null | undefined, identity: string) => string;
|
|
9
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
9
|
+
* `distinct` column across the aggregate rows it reads.
|
|
10
|
+
*/
|
|
10
11
|
declare const mergeDistinct: (summaries: readonly (string | null | undefined)[]) => string;
|
|
11
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
14
|
+
* reads a `distinct` column.
|
|
15
|
+
*/
|
|
12
16
|
declare const countDistinct: (summary: string | null | undefined) => number;
|
|
13
|
-
/** A sketch that holds no value. */
|
|
14
|
-
declare const emptyDistinct: () => string;
|
|
15
17
|
//#endregion
|
|
16
|
-
export { addDistinct, countDistinct,
|
|
18
|
+
export { addDistinct, countDistinct, mergeDistinct };
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
//#region src/database/distinct.d.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
/** The sketch with `identity` added; a missing sketch is the empty one. */
|
|
2
|
+
/**
|
|
3
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
4
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
5
|
+
*/
|
|
8
6
|
declare const addDistinct: (summary: string | null | undefined, identity: string) => string;
|
|
9
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
9
|
+
* `distinct` column across the aggregate rows it reads.
|
|
10
|
+
*/
|
|
10
11
|
declare const mergeDistinct: (summaries: readonly (string | null | undefined)[]) => string;
|
|
11
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
14
|
+
* reads a `distinct` column.
|
|
15
|
+
*/
|
|
12
16
|
declare const countDistinct: (summary: string | null | undefined) => number;
|
|
13
|
-
/** A sketch that holds no value. */
|
|
14
|
-
declare const emptyDistinct: () => string;
|
|
15
17
|
//#endregion
|
|
16
|
-
export { addDistinct, countDistinct,
|
|
18
|
+
export { addDistinct, countDistinct, mergeDistinct };
|
|
@@ -43,14 +43,20 @@ const distinctRegister = (identity) => {
|
|
|
43
43
|
character: String.fromCharCode(rank + OFFSET)
|
|
44
44
|
};
|
|
45
45
|
};
|
|
46
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
48
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
49
|
+
*/
|
|
47
50
|
const addDistinct = (summary, identity) => {
|
|
48
51
|
const registers = decode(summary);
|
|
49
52
|
const { index, rank } = distinctRegister(identity);
|
|
50
53
|
registers[index] = Math.max(registers[index], rank);
|
|
51
54
|
return encode(registers);
|
|
52
55
|
};
|
|
53
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
58
|
+
* `distinct` column across the aggregate rows it reads.
|
|
59
|
+
*/
|
|
54
60
|
const mergeDistinct = (summaries) => {
|
|
55
61
|
const merged = new Uint8Array(REGISTER_COUNT);
|
|
56
62
|
for (const summary of summaries) {
|
|
@@ -59,7 +65,10 @@ const mergeDistinct = (summaries) => {
|
|
|
59
65
|
}
|
|
60
66
|
return encode(merged);
|
|
61
67
|
};
|
|
62
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
70
|
+
* reads a `distinct` column.
|
|
71
|
+
*/
|
|
63
72
|
const countDistinct = (summary) => {
|
|
64
73
|
const registers = decode(summary);
|
|
65
74
|
let inverseSum = 0;
|
|
@@ -72,7 +81,5 @@ const countDistinct = (summary) => {
|
|
|
72
81
|
const estimate = raw <= 2.5 * REGISTER_COUNT && empty > 0 ? REGISTER_COUNT * Math.log(REGISTER_COUNT / empty) : raw;
|
|
73
82
|
return Math.max(0, Math.round(estimate));
|
|
74
83
|
};
|
|
75
|
-
/** A sketch that holds no value. */
|
|
76
|
-
const emptyDistinct = () => encode(new Uint8Array(REGISTER_COUNT));
|
|
77
84
|
//#endregion
|
|
78
|
-
export { addDistinct, countDistinct, distinctRegister,
|
|
85
|
+
export { addDistinct, countDistinct, distinctRegister, mergeDistinct };
|
|
@@ -47,7 +47,7 @@ const createMemoryAdapter = (options = {}) => {
|
|
|
47
47
|
const rowsOf = (table) => state.get(prefix + table.name) ?? /* @__PURE__ */ new Map();
|
|
48
48
|
return {
|
|
49
49
|
id: "memory",
|
|
50
|
-
fits: (
|
|
50
|
+
fits: () => true,
|
|
51
51
|
async get(table, keys) {
|
|
52
52
|
const rows = rowsOf(table);
|
|
53
53
|
return keys.map((key) => {
|
|
@@ -58,15 +58,7 @@ const createMemoryAdapter = (options = {}) => {
|
|
|
58
58
|
async query(table, request) {
|
|
59
59
|
const index = require_adapter.findPhysicalIndex(table, request.index);
|
|
60
60
|
const direction = request.order === "asc" ? 1 : -1;
|
|
61
|
-
|
|
62
|
-
const pageSize = options.nativePageSize ?? request.limit;
|
|
63
|
-
const rows = [];
|
|
64
|
-
while (rows.length < request.limit) {
|
|
65
|
-
const page = ordered.slice(rows.length, rows.length + Math.min(pageSize, request.limit - rows.length));
|
|
66
|
-
if (page.length === 0) break;
|
|
67
|
-
rows.push(...page);
|
|
68
|
-
}
|
|
69
|
-
return rows.map((row) => structuredClone(row));
|
|
61
|
+
return [...rowsOf(table).values()].filter((row) => require_values.matchesQuery(table, index, request, row)).sort((left, right) => direction * require_values.compareTuples(require_values.indexOrderTuple(table, index, left), require_values.indexOrderTuple(table, index, right))).slice(0, request.limit).map((row) => structuredClone(row));
|
|
70
62
|
},
|
|
71
63
|
async write(ops) {
|
|
72
64
|
const draft = new Map(state);
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { DatabaseAdapter } from "./adapter.cjs";
|
|
2
2
|
//#region src/database/memoryAdapter.d.ts
|
|
3
|
+
/** `createMemoryAdapter`'s options. */
|
|
3
4
|
interface MemoryAdapterOptions {
|
|
5
|
+
/** Prefixes every table's name, so several servers can share one store. */
|
|
4
6
|
readonly tablePrefix?: string;
|
|
5
|
-
/** Test-only: the most ops `fits()` accepts. */
|
|
6
|
-
readonly maxOps?: number;
|
|
7
|
-
/** Test-only: rows per internal page, so `query` has to loop pages. */
|
|
8
|
-
readonly nativePageSize?: number;
|
|
9
7
|
}
|
|
10
8
|
/**
|
|
11
9
|
* The reference adapter: every read is a consistent snapshot and every write
|