@hot-updater/plugin-core 1.0.0-rc.2 → 1.0.0-rc.22
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,68 @@
|
|
|
1
|
+
require("../../database/adapter.cjs");
|
|
2
|
+
const require_aggregateBatching = require("./aggregateBatching.cjs");
|
|
3
|
+
const require_engineTransaction = require("./engineTransaction.cjs");
|
|
4
|
+
const require_engine = require("./engine.cjs");
|
|
5
|
+
//#region src/engine/database/database.ts
|
|
6
|
+
/** The engine plus `database(module)`, which hands a module its typed handle. */
|
|
7
|
+
const createDatabaseEngine = ({ batching, now = Date.now, meter, ...options }) => {
|
|
8
|
+
const adapter = meter ? meter.wrap(options.adapter) : options.adapter;
|
|
9
|
+
const engine = require_engine.createStorageEngine({
|
|
10
|
+
...options,
|
|
11
|
+
adapter
|
|
12
|
+
});
|
|
13
|
+
const { reads } = engine;
|
|
14
|
+
meter?.attach(reads.reads);
|
|
15
|
+
const batches = batching && require_aggregateBatching.createAggregateBatches({
|
|
16
|
+
engine,
|
|
17
|
+
adapter,
|
|
18
|
+
schema: options.schema,
|
|
19
|
+
batching,
|
|
20
|
+
now
|
|
21
|
+
});
|
|
22
|
+
const transaction = batches?.transaction ?? engine.transaction;
|
|
23
|
+
const outside = (read) => (...args) => {
|
|
24
|
+
require_engineTransaction.assertOutsideTransaction();
|
|
25
|
+
return read(...args);
|
|
26
|
+
};
|
|
27
|
+
/** No read outside a transaction returns `_v`, which a key-value index copy lacks. */
|
|
28
|
+
const bare = (row) => row && Object.fromEntries(Object.entries(row).filter(([column]) => column !== "_v"));
|
|
29
|
+
const barePage = async (page) => {
|
|
30
|
+
const { rows, next } = await page;
|
|
31
|
+
return {
|
|
32
|
+
rows: rows.map(bare),
|
|
33
|
+
...next === void 0 ? {} : { next }
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
...engine,
|
|
38
|
+
/** Applies batched aggregate changes still pending: this process's buffer and the log. */
|
|
39
|
+
flush: async () => {
|
|
40
|
+
await batches?.flush();
|
|
41
|
+
},
|
|
42
|
+
dispose: async () => {
|
|
43
|
+
await batches?.dispose();
|
|
44
|
+
},
|
|
45
|
+
database(module) {
|
|
46
|
+
const name = (model) => module.namespace ? `${module.namespace}_${model}` : model;
|
|
47
|
+
return {
|
|
48
|
+
findOne: outside(async (model, lookup) => bare(await reads.findOne(name(model), lookup))),
|
|
49
|
+
findByKeys: outside(async (model, keys) => (await reads.findByKeys(name(model), keys)).map(bare)),
|
|
50
|
+
findMany: outside((model, input) => barePage(reads.findMany(name(model), input))),
|
|
51
|
+
findAggregates: outside(async (model, input) => {
|
|
52
|
+
await batches?.beforeRead(name(model));
|
|
53
|
+
return await barePage(reads.findAggregates(name(model), input));
|
|
54
|
+
}),
|
|
55
|
+
transaction: (fn) => transaction((tx) => fn({
|
|
56
|
+
findOne: (model, lookup) => tx.findOne(name(model), lookup),
|
|
57
|
+
findMany: (model, input) => tx.findMany(name(model), input),
|
|
58
|
+
create: (model, row) => tx.create(name(model), row),
|
|
59
|
+
update: (model, row, set) => tx.update(name(model), row, set),
|
|
60
|
+
delete: (model, row) => tx.delete(name(model), row),
|
|
61
|
+
aggregate: (model, identity, changes, options) => tx.aggregate(name(model), identity, changes, options)
|
|
62
|
+
}))
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
//#endregion
|
|
68
|
+
exports.createDatabaseEngine = createDatabaseEngine;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import "../../database/adapter.mjs";
|
|
2
|
+
import { createAggregateBatches } from "./aggregateBatching.mjs";
|
|
3
|
+
import { assertOutsideTransaction } from "./engineTransaction.mjs";
|
|
4
|
+
import { createStorageEngine } from "./engine.mjs";
|
|
5
|
+
//#region src/engine/database/database.ts
|
|
6
|
+
/** The engine plus `database(module)`, which hands a module its typed handle. */
|
|
7
|
+
const createDatabaseEngine = ({ batching, now = Date.now, meter, ...options }) => {
|
|
8
|
+
const adapter = meter ? meter.wrap(options.adapter) : options.adapter;
|
|
9
|
+
const engine = createStorageEngine({
|
|
10
|
+
...options,
|
|
11
|
+
adapter
|
|
12
|
+
});
|
|
13
|
+
const { reads } = engine;
|
|
14
|
+
meter?.attach(reads.reads);
|
|
15
|
+
const batches = batching && createAggregateBatches({
|
|
16
|
+
engine,
|
|
17
|
+
adapter,
|
|
18
|
+
schema: options.schema,
|
|
19
|
+
batching,
|
|
20
|
+
now
|
|
21
|
+
});
|
|
22
|
+
const transaction = batches?.transaction ?? engine.transaction;
|
|
23
|
+
const outside = (read) => (...args) => {
|
|
24
|
+
assertOutsideTransaction();
|
|
25
|
+
return read(...args);
|
|
26
|
+
};
|
|
27
|
+
/** No read outside a transaction returns `_v`, which a key-value index copy lacks. */
|
|
28
|
+
const bare = (row) => row && Object.fromEntries(Object.entries(row).filter(([column]) => column !== "_v"));
|
|
29
|
+
const barePage = async (page) => {
|
|
30
|
+
const { rows, next } = await page;
|
|
31
|
+
return {
|
|
32
|
+
rows: rows.map(bare),
|
|
33
|
+
...next === void 0 ? {} : { next }
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
...engine,
|
|
38
|
+
/** Applies batched aggregate changes still pending: this process's buffer and the log. */
|
|
39
|
+
flush: async () => {
|
|
40
|
+
await batches?.flush();
|
|
41
|
+
},
|
|
42
|
+
dispose: async () => {
|
|
43
|
+
await batches?.dispose();
|
|
44
|
+
},
|
|
45
|
+
database(module) {
|
|
46
|
+
const name = (model) => module.namespace ? `${module.namespace}_${model}` : model;
|
|
47
|
+
return {
|
|
48
|
+
findOne: outside(async (model, lookup) => bare(await reads.findOne(name(model), lookup))),
|
|
49
|
+
findByKeys: outside(async (model, keys) => (await reads.findByKeys(name(model), keys)).map(bare)),
|
|
50
|
+
findMany: outside((model, input) => barePage(reads.findMany(name(model), input))),
|
|
51
|
+
findAggregates: outside(async (model, input) => {
|
|
52
|
+
await batches?.beforeRead(name(model));
|
|
53
|
+
return await barePage(reads.findAggregates(name(model), input));
|
|
54
|
+
}),
|
|
55
|
+
transaction: (fn) => transaction((tx) => fn({
|
|
56
|
+
findOne: (model, lookup) => tx.findOne(name(model), lookup),
|
|
57
|
+
findMany: (model, input) => tx.findMany(name(model), input),
|
|
58
|
+
create: (model, row) => tx.create(name(model), row),
|
|
59
|
+
update: (model, row, set) => tx.update(name(model), row, set),
|
|
60
|
+
delete: (model, row) => tx.delete(name(model), row),
|
|
61
|
+
aggregate: (model, identity, changes, options) => tx.aggregate(name(model), identity, changes, options)
|
|
62
|
+
}))
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
//#endregion
|
|
68
|
+
export { createDatabaseEngine };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const require_verifyAdapter = require("../../database/verifyAdapter.cjs");
|
|
2
|
+
const require_engineReads = require("./engineReads.cjs");
|
|
3
|
+
const require_engineTransaction = require("./engineTransaction.cjs");
|
|
4
|
+
//#region src/engine/database/engine.ts
|
|
5
|
+
/** Storage semantics over one adapter, addressed by physical table name. */
|
|
6
|
+
const createStorageEngine = ({ adapter: given, schema, maxPageSize, verify, retry }) => {
|
|
7
|
+
const verified = verify ? require_verifyAdapter.verifyAdapter(given) : void 0;
|
|
8
|
+
const adapter = verified ?? given;
|
|
9
|
+
const reads = require_engineReads.createEngineReads({
|
|
10
|
+
adapter,
|
|
11
|
+
schema,
|
|
12
|
+
maxPageSize
|
|
13
|
+
});
|
|
14
|
+
const { transaction } = require_engineTransaction.createTransactions({
|
|
15
|
+
adapter,
|
|
16
|
+
schema,
|
|
17
|
+
reads,
|
|
18
|
+
retry
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
reads,
|
|
22
|
+
transaction,
|
|
23
|
+
/** Runs `read` and reports what it read at both boundaries (verify mode only). */
|
|
24
|
+
async measureReads(read) {
|
|
25
|
+
if (verified === void 0) throw new Error("measureReads needs an engine created with verify: true.");
|
|
26
|
+
verified.reads.reset();
|
|
27
|
+
reads.reads.reset();
|
|
28
|
+
return {
|
|
29
|
+
result: await read(),
|
|
30
|
+
adapter: verified.reads.total(),
|
|
31
|
+
engine: reads.reads.total()
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
//#endregion
|
|
37
|
+
exports.createStorageEngine = createStorageEngine;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { verifyAdapter } from "../../database/verifyAdapter.mjs";
|
|
2
|
+
import { createEngineReads } from "./engineReads.mjs";
|
|
3
|
+
import { createTransactions } from "./engineTransaction.mjs";
|
|
4
|
+
//#region src/engine/database/engine.ts
|
|
5
|
+
/** Storage semantics over one adapter, addressed by physical table name. */
|
|
6
|
+
const createStorageEngine = ({ adapter: given, schema, maxPageSize, verify, retry }) => {
|
|
7
|
+
const verified = verify ? verifyAdapter(given) : void 0;
|
|
8
|
+
const adapter = verified ?? given;
|
|
9
|
+
const reads = createEngineReads({
|
|
10
|
+
adapter,
|
|
11
|
+
schema,
|
|
12
|
+
maxPageSize
|
|
13
|
+
});
|
|
14
|
+
const { transaction } = createTransactions({
|
|
15
|
+
adapter,
|
|
16
|
+
schema,
|
|
17
|
+
reads,
|
|
18
|
+
retry
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
reads,
|
|
22
|
+
transaction,
|
|
23
|
+
/** Runs `read` and reports what it read at both boundaries (verify mode only). */
|
|
24
|
+
async measureReads(read) {
|
|
25
|
+
if (verified === void 0) throw new Error("measureReads needs an engine created with verify: true.");
|
|
26
|
+
verified.reads.reset();
|
|
27
|
+
reads.reads.reset();
|
|
28
|
+
return {
|
|
29
|
+
result: await read(),
|
|
30
|
+
adapter: verified.reads.total(),
|
|
31
|
+
engine: reads.reads.total()
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
//#endregion
|
|
37
|
+
export { createStorageEngine };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
const require_errors = require("../../serverPlugin/errors.cjs");
|
|
2
|
+
const require_distinct = require("../../database/distinct.cjs");
|
|
3
|
+
require("../../database/adapter.cjs");
|
|
4
|
+
const require_values = require("../../database/values.cjs");
|
|
5
|
+
const require_cursor = require("./cursor.cjs");
|
|
6
|
+
//#region src/engine/database/engineAggregates.ts
|
|
7
|
+
const definitionOf = (model) => model.definition;
|
|
8
|
+
/** Every change keyed by one value lands on one shard. */
|
|
9
|
+
const shardOf = (value, shards) => require_cursor.fnv1a(value) % shards;
|
|
10
|
+
/** Merges one `tx.aggregate` call into its shard row's pending changes. */
|
|
11
|
+
const recordAggregate = (changes, model, identity, values, shardBy) => {
|
|
12
|
+
const { key: fields, counters, gauges, distinct, shards } = definitionOf(model);
|
|
13
|
+
const fail = (message) => new require_errors.DatabaseTransactionError(`${model.table.name}: ${message}`);
|
|
14
|
+
if (Object.keys(identity).length !== fields.length || !fields.every((name) => require_values.isKeyValue(identity[name]))) throw fail(`aggregate takes exactly its identity fields: ${fields.join(", ")}.`);
|
|
15
|
+
if (shardBy === void 0 && shards > 1 && gauges.some((gauge) => gauge in values)) throw fail("gauges of a sharded aggregate need shardBy.");
|
|
16
|
+
const shard = shardBy === void 0 ? Math.floor(Math.random() * shards) : shardOf(shardBy, shards);
|
|
17
|
+
const key = [...fields.map((name) => identity[name]), shard];
|
|
18
|
+
const id = JSON.stringify([model.table.name, key]);
|
|
19
|
+
const change = changes.get(id) ?? {
|
|
20
|
+
model,
|
|
21
|
+
key,
|
|
22
|
+
deltas: {},
|
|
23
|
+
sketches: {}
|
|
24
|
+
};
|
|
25
|
+
changes.set(id, change);
|
|
26
|
+
for (const [metric, value] of Object.entries(values)) if (distinct.includes(metric) && typeof value === "string") change.sketches[metric] = require_distinct.mergeDistinct([change.sketches[metric], value]);
|
|
27
|
+
else if ((counters.includes(metric) || gauges.includes(metric)) && Number.isSafeInteger(value)) change.deltas[metric] = (change.deltas[metric] ?? 0) + Number(value);
|
|
28
|
+
else throw fail(`${metric} is not a counter, gauge, or sketch of this aggregate.`);
|
|
29
|
+
};
|
|
30
|
+
/** A shard row before its first change: every metric zero or empty. */
|
|
31
|
+
const blank = (model, key) => {
|
|
32
|
+
const { counters, gauges, distinct } = definitionOf(model);
|
|
33
|
+
return Object.fromEntries([
|
|
34
|
+
...model.table.key.map((column, position) => [column, key[position]]),
|
|
35
|
+
...[...counters, ...gauges].map((name) => [name, 0]),
|
|
36
|
+
...distinct.map((name) => [name, null]),
|
|
37
|
+
["_v", 0]
|
|
38
|
+
]);
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* A gauge computed below zero. Deltas from a row another writer has since
|
|
42
|
+
* moved do this, so the transaction reruns; it is thrown only when it lasts.
|
|
43
|
+
*/
|
|
44
|
+
var NegativeGaugeError = class extends require_errors.DatabaseTransactionError {};
|
|
45
|
+
/** The guarded write that merges `change` into `current`: insert, patch, or delete at zero. */
|
|
46
|
+
const rewrite = ({ model, key, deltas, sketches }, current) => {
|
|
47
|
+
const { table } = model;
|
|
48
|
+
const { counters, gauges, distinct } = definitionOf(model);
|
|
49
|
+
const base = current ?? blank(model, key);
|
|
50
|
+
const set = {};
|
|
51
|
+
for (const [metric, delta] of Object.entries(deltas)) set[metric] = Number(base[metric] ?? 0) + delta;
|
|
52
|
+
for (const [metric, sketch] of Object.entries(sketches)) set[metric] = require_distinct.mergeDistinct([base[metric], sketch]);
|
|
53
|
+
if (current && Object.entries(set).every(([metric, value]) => current[metric] === value)) return;
|
|
54
|
+
const row = {
|
|
55
|
+
...base,
|
|
56
|
+
...set
|
|
57
|
+
};
|
|
58
|
+
if (gauges.some((gauge) => Number(row[gauge]) < 0)) {
|
|
59
|
+
if (current === null && table.retention) return void 0;
|
|
60
|
+
throw new NegativeGaugeError(`${table.name}: a gauge went below 0.`);
|
|
61
|
+
}
|
|
62
|
+
const zero = distinct.length === 0 && [...counters, ...gauges].every((metric) => Number(row[metric]) === 0);
|
|
63
|
+
if (current === null) return zero ? void 0 : {
|
|
64
|
+
type: "insert",
|
|
65
|
+
table,
|
|
66
|
+
row
|
|
67
|
+
};
|
|
68
|
+
const guard = { v: Number(current["_v"]) };
|
|
69
|
+
return zero ? {
|
|
70
|
+
type: "delete",
|
|
71
|
+
table,
|
|
72
|
+
key,
|
|
73
|
+
guard,
|
|
74
|
+
previous: current
|
|
75
|
+
} : {
|
|
76
|
+
type: "patch",
|
|
77
|
+
table,
|
|
78
|
+
key,
|
|
79
|
+
set,
|
|
80
|
+
guard,
|
|
81
|
+
previous: current
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Counter-only rows become blind increments that create the row when it is
|
|
86
|
+
* missing. Rows with gauges or sketches are read in one parallel batch per
|
|
87
|
+
* table, merged, and written back under a guard, so a conflict resends only
|
|
88
|
+
* them.
|
|
89
|
+
*/
|
|
90
|
+
const compileAggregates = async (adapter, changes) => {
|
|
91
|
+
const ops = [];
|
|
92
|
+
const merged = /* @__PURE__ */ new Map();
|
|
93
|
+
for (const change of changes) {
|
|
94
|
+
const { model, key, deltas, sketches } = change;
|
|
95
|
+
if (Object.keys(sketches).length > 0 || definitionOf(model).gauges.some((gauge) => (deltas[gauge] ?? 0) !== 0)) merged.set(model, [...merged.get(model) ?? [], change]);
|
|
96
|
+
else if (Object.values(deltas).some((delta) => delta !== 0)) {
|
|
97
|
+
const { table } = model;
|
|
98
|
+
const init = blank(model, key);
|
|
99
|
+
ops.push({
|
|
100
|
+
type: "increment",
|
|
101
|
+
table,
|
|
102
|
+
key,
|
|
103
|
+
by: deltas,
|
|
104
|
+
init
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const groups = [...merged.values()];
|
|
109
|
+
const current = await Promise.all(groups.map((group) => adapter.get(group[0].model.table, group.map(({ key }) => key))));
|
|
110
|
+
groups.forEach((group, index) => group.forEach((change, position) => {
|
|
111
|
+
const op = rewrite(change, current[index][position] ?? null);
|
|
112
|
+
if (op !== void 0) ops.push(op);
|
|
113
|
+
}));
|
|
114
|
+
return ops;
|
|
115
|
+
};
|
|
116
|
+
//#endregion
|
|
117
|
+
exports.NegativeGaugeError = NegativeGaugeError;
|
|
118
|
+
exports.compileAggregates = compileAggregates;
|
|
119
|
+
exports.recordAggregate = recordAggregate;
|
|
120
|
+
exports.shardOf = shardOf;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { DatabaseTransactionError } from "../../serverPlugin/errors.mjs";
|
|
2
|
+
import { mergeDistinct } from "../../database/distinct.mjs";
|
|
3
|
+
import "../../database/adapter.mjs";
|
|
4
|
+
import { isKeyValue } from "../../database/values.mjs";
|
|
5
|
+
import { fnv1a } from "./cursor.mjs";
|
|
6
|
+
//#region src/engine/database/engineAggregates.ts
|
|
7
|
+
const definitionOf = (model) => model.definition;
|
|
8
|
+
/** Every change keyed by one value lands on one shard. */
|
|
9
|
+
const shardOf = (value, shards) => fnv1a(value) % shards;
|
|
10
|
+
/** Merges one `tx.aggregate` call into its shard row's pending changes. */
|
|
11
|
+
const recordAggregate = (changes, model, identity, values, shardBy) => {
|
|
12
|
+
const { key: fields, counters, gauges, distinct, shards } = definitionOf(model);
|
|
13
|
+
const fail = (message) => new DatabaseTransactionError(`${model.table.name}: ${message}`);
|
|
14
|
+
if (Object.keys(identity).length !== fields.length || !fields.every((name) => isKeyValue(identity[name]))) throw fail(`aggregate takes exactly its identity fields: ${fields.join(", ")}.`);
|
|
15
|
+
if (shardBy === void 0 && shards > 1 && gauges.some((gauge) => gauge in values)) throw fail("gauges of a sharded aggregate need shardBy.");
|
|
16
|
+
const shard = shardBy === void 0 ? Math.floor(Math.random() * shards) : shardOf(shardBy, shards);
|
|
17
|
+
const key = [...fields.map((name) => identity[name]), shard];
|
|
18
|
+
const id = JSON.stringify([model.table.name, key]);
|
|
19
|
+
const change = changes.get(id) ?? {
|
|
20
|
+
model,
|
|
21
|
+
key,
|
|
22
|
+
deltas: {},
|
|
23
|
+
sketches: {}
|
|
24
|
+
};
|
|
25
|
+
changes.set(id, change);
|
|
26
|
+
for (const [metric, value] of Object.entries(values)) if (distinct.includes(metric) && typeof value === "string") change.sketches[metric] = mergeDistinct([change.sketches[metric], value]);
|
|
27
|
+
else if ((counters.includes(metric) || gauges.includes(metric)) && Number.isSafeInteger(value)) change.deltas[metric] = (change.deltas[metric] ?? 0) + Number(value);
|
|
28
|
+
else throw fail(`${metric} is not a counter, gauge, or sketch of this aggregate.`);
|
|
29
|
+
};
|
|
30
|
+
/** A shard row before its first change: every metric zero or empty. */
|
|
31
|
+
const blank = (model, key) => {
|
|
32
|
+
const { counters, gauges, distinct } = definitionOf(model);
|
|
33
|
+
return Object.fromEntries([
|
|
34
|
+
...model.table.key.map((column, position) => [column, key[position]]),
|
|
35
|
+
...[...counters, ...gauges].map((name) => [name, 0]),
|
|
36
|
+
...distinct.map((name) => [name, null]),
|
|
37
|
+
["_v", 0]
|
|
38
|
+
]);
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* A gauge computed below zero. Deltas from a row another writer has since
|
|
42
|
+
* moved do this, so the transaction reruns; it is thrown only when it lasts.
|
|
43
|
+
*/
|
|
44
|
+
var NegativeGaugeError = class extends DatabaseTransactionError {};
|
|
45
|
+
/** The guarded write that merges `change` into `current`: insert, patch, or delete at zero. */
|
|
46
|
+
const rewrite = ({ model, key, deltas, sketches }, current) => {
|
|
47
|
+
const { table } = model;
|
|
48
|
+
const { counters, gauges, distinct } = definitionOf(model);
|
|
49
|
+
const base = current ?? blank(model, key);
|
|
50
|
+
const set = {};
|
|
51
|
+
for (const [metric, delta] of Object.entries(deltas)) set[metric] = Number(base[metric] ?? 0) + delta;
|
|
52
|
+
for (const [metric, sketch] of Object.entries(sketches)) set[metric] = mergeDistinct([base[metric], sketch]);
|
|
53
|
+
if (current && Object.entries(set).every(([metric, value]) => current[metric] === value)) return;
|
|
54
|
+
const row = {
|
|
55
|
+
...base,
|
|
56
|
+
...set
|
|
57
|
+
};
|
|
58
|
+
if (gauges.some((gauge) => Number(row[gauge]) < 0)) {
|
|
59
|
+
if (current === null && table.retention) return void 0;
|
|
60
|
+
throw new NegativeGaugeError(`${table.name}: a gauge went below 0.`);
|
|
61
|
+
}
|
|
62
|
+
const zero = distinct.length === 0 && [...counters, ...gauges].every((metric) => Number(row[metric]) === 0);
|
|
63
|
+
if (current === null) return zero ? void 0 : {
|
|
64
|
+
type: "insert",
|
|
65
|
+
table,
|
|
66
|
+
row
|
|
67
|
+
};
|
|
68
|
+
const guard = { v: Number(current["_v"]) };
|
|
69
|
+
return zero ? {
|
|
70
|
+
type: "delete",
|
|
71
|
+
table,
|
|
72
|
+
key,
|
|
73
|
+
guard,
|
|
74
|
+
previous: current
|
|
75
|
+
} : {
|
|
76
|
+
type: "patch",
|
|
77
|
+
table,
|
|
78
|
+
key,
|
|
79
|
+
set,
|
|
80
|
+
guard,
|
|
81
|
+
previous: current
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Counter-only rows become blind increments that create the row when it is
|
|
86
|
+
* missing. Rows with gauges or sketches are read in one parallel batch per
|
|
87
|
+
* table, merged, and written back under a guard, so a conflict resends only
|
|
88
|
+
* them.
|
|
89
|
+
*/
|
|
90
|
+
const compileAggregates = async (adapter, changes) => {
|
|
91
|
+
const ops = [];
|
|
92
|
+
const merged = /* @__PURE__ */ new Map();
|
|
93
|
+
for (const change of changes) {
|
|
94
|
+
const { model, key, deltas, sketches } = change;
|
|
95
|
+
if (Object.keys(sketches).length > 0 || definitionOf(model).gauges.some((gauge) => (deltas[gauge] ?? 0) !== 0)) merged.set(model, [...merged.get(model) ?? [], change]);
|
|
96
|
+
else if (Object.values(deltas).some((delta) => delta !== 0)) {
|
|
97
|
+
const { table } = model;
|
|
98
|
+
const init = blank(model, key);
|
|
99
|
+
ops.push({
|
|
100
|
+
type: "increment",
|
|
101
|
+
table,
|
|
102
|
+
key,
|
|
103
|
+
by: deltas,
|
|
104
|
+
init
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const groups = [...merged.values()];
|
|
109
|
+
const current = await Promise.all(groups.map((group) => adapter.get(group[0].model.table, group.map(({ key }) => key))));
|
|
110
|
+
groups.forEach((group, index) => group.forEach((change, position) => {
|
|
111
|
+
const op = rewrite(change, current[index][position] ?? null);
|
|
112
|
+
if (op !== void 0) ops.push(op);
|
|
113
|
+
}));
|
|
114
|
+
return ops;
|
|
115
|
+
};
|
|
116
|
+
//#endregion
|
|
117
|
+
export { NegativeGaugeError, compileAggregates, recordAggregate, shardOf };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
const require_errors = require("../../serverPlugin/errors.cjs");
|
|
2
|
+
const require_distinct = require("../../database/distinct.cjs");
|
|
3
|
+
const require_adapter = require("../../database/adapter.cjs");
|
|
4
|
+
const require_values = require("../../database/values.cjs");
|
|
5
|
+
const require_cursor = require("./cursor.cjs");
|
|
6
|
+
const require_resolveSchema = require("./resolveSchema.cjs");
|
|
7
|
+
//#region src/engine/database/engineReads.ts
|
|
8
|
+
const sameFields = (left, right) => left.length === right.length && left.every((field) => right.includes(field));
|
|
9
|
+
const keyValue = (value, field) => {
|
|
10
|
+
if (require_values.isKeyValue(value)) return value;
|
|
11
|
+
throw new require_errors.DatabaseQueryError(`${field} needs a string, number, or boolean.`);
|
|
12
|
+
};
|
|
13
|
+
const bound = (exclusive, inclusive, columns) => {
|
|
14
|
+
if (exclusive !== void 0 && inclusive !== void 0) throw new require_errors.DatabaseQueryError(`range on ${columns[0]} sets both bounds of one side.`);
|
|
15
|
+
const value = exclusive ?? inclusive;
|
|
16
|
+
if (value === void 0) return void 0;
|
|
17
|
+
const values = Array.isArray(value) ? value : [value];
|
|
18
|
+
if (values.length === 0 || values.length > columns.length) throw new require_errors.DatabaseQueryError(`range on ${columns[0]} takes 1–${columns.length} values of ${columns.join(", ")}.`);
|
|
19
|
+
return {
|
|
20
|
+
values: values.map((item, position) => keyValue(item, columns[position])),
|
|
21
|
+
inclusive: inclusive !== void 0
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
const createEngineReads = ({ adapter, schema, maxPageSize: requested = 500 }) => {
|
|
25
|
+
const maxPageSize = Math.min(requested, 500);
|
|
26
|
+
let count = {
|
|
27
|
+
calls: 0,
|
|
28
|
+
rows: 0
|
|
29
|
+
};
|
|
30
|
+
const returned = (rows) => {
|
|
31
|
+
const size = Array.isArray(rows) ? rows.length : rows === null ? 0 : 1;
|
|
32
|
+
count = {
|
|
33
|
+
calls: count.calls + 1,
|
|
34
|
+
rows: count.rows + size
|
|
35
|
+
};
|
|
36
|
+
return rows;
|
|
37
|
+
};
|
|
38
|
+
/** A model of `kind` by physical table name, for reads and transactions. */
|
|
39
|
+
const modelOf = (name, kind = "table") => {
|
|
40
|
+
const model = schema.models.get(name);
|
|
41
|
+
if (model?.definition.kind === kind) return model;
|
|
42
|
+
throw new require_errors.DatabaseQueryError(model === void 0 ? `Unknown model ${name}.` : kind === "table" ? `${name} is an aggregate; read it with findAggregates and change it with aggregate.` : `${name} is a table; read it with findMany and change it with create, update, or delete.`);
|
|
43
|
+
};
|
|
44
|
+
const plan = ({ table }, input) => {
|
|
45
|
+
const index = table.indexes.find(({ name }) => name === input.index);
|
|
46
|
+
if (index === void 0) throw new require_errors.DatabaseQueryError(`${table.name} has no index ${input.index}.`);
|
|
47
|
+
const where = input.where ?? {};
|
|
48
|
+
if (!sameFields(Object.keys(where), index.eq)) throw new require_errors.DatabaseQueryError(`${table.name}.${index.name} reads need where on exactly: ${index.eq.join(", ") || "(none)"}.`);
|
|
49
|
+
const { limit, order = "asc", range } = input;
|
|
50
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > maxPageSize) throw new require_errors.DatabaseQueryError(`limit must be 1–${maxPageSize}.`);
|
|
51
|
+
const columns = require_adapter.indexOrderColumns(table, index);
|
|
52
|
+
const eq = index.eq.map((field) => keyValue(where[field], `${table.name}.${field}`));
|
|
53
|
+
const scope = require_cursor.cursorScope([
|
|
54
|
+
table.name,
|
|
55
|
+
index.name,
|
|
56
|
+
eq,
|
|
57
|
+
order,
|
|
58
|
+
range
|
|
59
|
+
]);
|
|
60
|
+
let lower = bound(range?.gt, range?.gte, columns);
|
|
61
|
+
let upper = bound(range?.lt, range?.lte, columns);
|
|
62
|
+
if (input.cursor !== void 0) {
|
|
63
|
+
const values = require_cursor.decodeCursor(scope, input.cursor);
|
|
64
|
+
if (order === "asc") lower = {
|
|
65
|
+
values,
|
|
66
|
+
inclusive: false
|
|
67
|
+
};
|
|
68
|
+
else upper = {
|
|
69
|
+
values,
|
|
70
|
+
inclusive: false
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
request: {
|
|
75
|
+
index: index.name,
|
|
76
|
+
eq,
|
|
77
|
+
order,
|
|
78
|
+
limit,
|
|
79
|
+
...lower === void 0 ? {} : { lower },
|
|
80
|
+
...upper === void 0 ? {} : { upper }
|
|
81
|
+
},
|
|
82
|
+
index,
|
|
83
|
+
scope
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
/** Reads rows by key in one batch; the results follow the keys' order. */
|
|
87
|
+
const findByKeys = async (name, lookups) => {
|
|
88
|
+
const { table } = modelOf(name, "table");
|
|
89
|
+
const keys = lookups.map((lookup) => {
|
|
90
|
+
if (!sameFields(Object.keys(lookup), table.key)) throw new require_errors.DatabaseQueryError(`findByKeys on ${name} takes its key: ${table.key.join(", ")}.`);
|
|
91
|
+
return table.key.map((field) => keyValue(lookup[field], `${name}.${field}`));
|
|
92
|
+
});
|
|
93
|
+
const rows = keys.length === 0 ? [] : await adapter.get(table, keys);
|
|
94
|
+
returned(rows.filter((row) => row !== null));
|
|
95
|
+
return rows.map((row) => row ?? null);
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
maxPageSize,
|
|
99
|
+
modelOf,
|
|
100
|
+
reads: {
|
|
101
|
+
total: () => count,
|
|
102
|
+
reset: () => {
|
|
103
|
+
count = {
|
|
104
|
+
calls: 0,
|
|
105
|
+
rows: 0
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
findByKeys,
|
|
110
|
+
/** Reads one row by its key or by the eq fields of a unique index. */
|
|
111
|
+
async findOne(name, lookup) {
|
|
112
|
+
const { table } = modelOf(name, "table");
|
|
113
|
+
const fields = Object.keys(lookup);
|
|
114
|
+
if (sameFields(fields, table.key)) return (await findByKeys(name, [lookup]))[0];
|
|
115
|
+
const index = table.indexes.find(({ unique, eq }) => unique && sameFields(fields, eq));
|
|
116
|
+
if (index === void 0) throw new require_errors.DatabaseQueryError(`findOne on ${name} needs its key or a unique field, not ${fields.join(", ")}.`);
|
|
117
|
+
const [row] = await adapter.query(table, {
|
|
118
|
+
index: index.name,
|
|
119
|
+
eq: index.eq.map((field) => keyValue(lookup[field], `${name}.${field}`)),
|
|
120
|
+
order: "asc",
|
|
121
|
+
limit: 1
|
|
122
|
+
});
|
|
123
|
+
return returned(row ?? null);
|
|
124
|
+
},
|
|
125
|
+
/** Reads one page of an index range; `next` resumes right after it. */
|
|
126
|
+
async findMany(name, input) {
|
|
127
|
+
const model = modelOf(name, "table");
|
|
128
|
+
const { request, index, scope } = plan(model, input);
|
|
129
|
+
const rows = returned(await adapter.query(model.table, request));
|
|
130
|
+
const last = rows.at(-1);
|
|
131
|
+
return rows.length === input.limit && last !== void 0 ? {
|
|
132
|
+
rows,
|
|
133
|
+
next: require_cursor.encodeCursor(scope, require_values.indexOrderTuple(model.table, index, last))
|
|
134
|
+
} : { rows };
|
|
135
|
+
},
|
|
136
|
+
/**
|
|
137
|
+
* Reads one page of logical aggregate rows, summing counters and gauges
|
|
138
|
+
* and merging sketches across each row's shards. A row cut off by the
|
|
139
|
+
* limit is completed by reading only its remaining shards.
|
|
140
|
+
*/
|
|
141
|
+
async findAggregates(name, input) {
|
|
142
|
+
const model = modelOf(name, "aggregate");
|
|
143
|
+
const { table, definition } = model;
|
|
144
|
+
if (definition.kind !== "aggregate") throw new require_errors.DatabaseQueryError(name);
|
|
145
|
+
const { request, index, scope } = plan(model, input);
|
|
146
|
+
const rows = await adapter.query(table, request);
|
|
147
|
+
const logicalKey = (row) => definition.key.map((column) => keyValue(row[column], column));
|
|
148
|
+
const groups = /* @__PURE__ */ new Map();
|
|
149
|
+
for (const row of rows) {
|
|
150
|
+
const key = JSON.stringify(logicalKey(row));
|
|
151
|
+
groups.set(key, [...groups.get(key) ?? [], row]);
|
|
152
|
+
}
|
|
153
|
+
const full = rows.length === input.limit;
|
|
154
|
+
const last = [...groups.values()].at(-1);
|
|
155
|
+
if (full && last !== void 0) {
|
|
156
|
+
const seen = new Set(last.map((row) => row[require_resolveSchema.SHARD_COLUMN]));
|
|
157
|
+
const missing = Array.from({ length: definition.shards }, (_, shard) => shard).filter((shard) => !seen.has(shard));
|
|
158
|
+
if (missing.length > 0) {
|
|
159
|
+
const key = logicalKey(last[0]);
|
|
160
|
+
const rest = await adapter.get(table, missing.map((shard) => [...key, shard]));
|
|
161
|
+
last.push(...rest.filter((row) => row !== null));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const merged = [...groups.values()].map((shards) => {
|
|
165
|
+
const row = Object.fromEntries(definition.key.map((field) => [field, shards[0][field]]));
|
|
166
|
+
for (const metric of [...definition.counters, ...definition.gauges]) row[metric] = shards.reduce((sum, shard) => sum + Number(shard[metric] ?? 0), 0);
|
|
167
|
+
for (const metric of definition.distinct) row[metric] = require_distinct.mergeDistinct(shards.map((shard) => shard[metric]));
|
|
168
|
+
return row;
|
|
169
|
+
});
|
|
170
|
+
returned(merged);
|
|
171
|
+
if (!full || last === void 0) return { rows: merged };
|
|
172
|
+
const tuple = require_values.indexOrderTuple(table, index, last[0]);
|
|
173
|
+
return {
|
|
174
|
+
rows: merged,
|
|
175
|
+
next: require_cursor.encodeCursor(scope, tuple.slice(0, -1))
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
//#endregion
|
|
181
|
+
exports.DatabaseQueryError = require_errors.DatabaseQueryError;
|
|
182
|
+
exports.createEngineReads = createEngineReads;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Page } from "../../serverPlugin/databaseHandle.cjs";
|
|
2
|
+
import { DatabaseQueryError } from "../../serverPlugin/errors.cjs";
|
|
3
|
+
//#region src/engine/database/engineReads.d.ts
|
|
4
|
+
/** Reads and the rows (logical rows for aggregates) the engine returned to its callers. */
|
|
5
|
+
interface EngineReadCount {
|
|
6
|
+
readonly calls: number;
|
|
7
|
+
readonly rows: number;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { EngineReadCount };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Page } from "../../serverPlugin/databaseHandle.mjs";
|
|
2
|
+
import { DatabaseQueryError } from "../../serverPlugin/errors.mjs";
|
|
3
|
+
//#region src/engine/database/engineReads.d.ts
|
|
4
|
+
/** Reads and the rows (logical rows for aggregates) the engine returned to its callers. */
|
|
5
|
+
interface EngineReadCount {
|
|
6
|
+
readonly calls: number;
|
|
7
|
+
readonly rows: number;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { EngineReadCount };
|