@hot-updater/plugin-core 1.0.0-rc.15 → 1.0.0-rc.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/coreApi.d.cts +131 -0
- package/dist/coreApi.d.mts +131 -0
- package/dist/database/adapter.cjs +46 -0
- package/dist/database/adapter.d.cts +183 -0
- package/dist/database/adapter.d.mts +183 -0
- package/dist/database/adapter.mjs +38 -0
- package/dist/{insightsDistinctSummary.mjs → database/distinct.cjs} +25 -11
- package/dist/database/distinct.d.cts +16 -0
- package/dist/database/distinct.d.mts +16 -0
- package/dist/{insightsDistinctSummary.cjs → database/distinct.mjs} +21 -15
- package/dist/database/memoryAdapter.cjs +115 -0
- package/dist/database/memoryAdapter.d.cts +16 -0
- package/dist/database/memoryAdapter.d.mts +16 -0
- package/dist/database/memoryAdapter.mjs +115 -0
- package/dist/database/values.cjs +110 -0
- package/dist/database/values.d.cts +30 -0
- package/dist/database/values.d.mts +30 -0
- package/dist/database/values.mjs +100 -0
- package/dist/database/verifyAdapter.cjs +136 -0
- package/dist/database/verifyAdapter.d.cts +29 -0
- package/dist/database/verifyAdapter.d.mts +29 -0
- package/dist/database/verifyAdapter.mjs +135 -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 +0 -2
- package/dist/databaseJsonValue.d.cts +3 -3
- package/dist/databaseJsonValue.d.mts +3 -3
- package/dist/databaseJsonValue.mjs +1 -2
- package/dist/databaseMetadata.cjs +2 -2
- package/dist/databaseMetadata.mjs +1 -1
- package/dist/index.cjs +9 -39
- package/dist/index.d.cts +13 -19
- package/dist/index.d.mts +13 -19
- package/dist/index.mjs +7 -14
- package/dist/internal.cjs +37 -20
- package/dist/internal.d.cts +11 -12
- package/dist/internal.d.mts +11 -12
- package/dist/internal.mjs +8 -6
- package/dist/releaseCatalogCompiler.cjs +1 -1
- package/dist/releaseCatalogCompiler.mjs +2 -2
- 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/types/databaseConfig.cjs +5 -0
- package/dist/types/databaseConfig.d.cts +61 -0
- package/dist/types/databaseConfig.d.mts +61 -0
- package/dist/types/databaseConfig.mjs +5 -0
- package/dist/types/databaseOperations.d.cts +5 -174
- package/dist/types/databaseOperations.d.mts +5 -174
- package/dist/types/databaseRows.d.cts +1 -53
- package/dist/types/databaseRows.d.mts +1 -53
- package/dist/types/index.cjs +2 -0
- package/dist/types/index.d.cts +8 -74
- package/dist/types/index.d.mts +8 -74
- package/dist/types/index.mjs +2 -1
- package/dist/types/internal.d.cts +4 -8
- package/dist/types/internal.d.mts +4 -8
- 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/package.json +1 -1
- 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/createDatabasePlugin.cjs +0 -875
- package/dist/createDatabasePlugin.d.cts +0 -28
- package/dist/createDatabasePlugin.d.mts +0 -28
- package/dist/createDatabasePlugin.mjs +0 -871
- 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 -37
- package/dist/databaseClientUpdates.d.cts +0 -11
- package/dist/databaseClientUpdates.d.mts +0 -11
- package/dist/databaseClientUpdates.mjs +0 -35
- package/dist/databasePluginCrud.cjs +0 -87
- package/dist/databasePluginCrud.d.cts +0 -4
- package/dist/databasePluginCrud.d.mts +0 -4
- package/dist/databasePluginCrud.mjs +0 -86
- 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 -284
- package/dist/databasePluginCrudValidationFields.d.cts +0 -1
- package/dist/databasePluginCrudValidationFields.d.mts +0 -1
- package/dist/databasePluginCrudValidationFields.mjs +0 -272
- package/dist/databasePluginCrudValidationMutations.cjs +0 -88
- package/dist/databasePluginCrudValidationMutations.d.cts +0 -1
- package/dist/databasePluginCrudValidationMutations.d.mts +0 -1
- package/dist/databasePluginCrudValidationMutations.mjs +0 -82
- 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 -192
- package/dist/insightsContract.d.cts +0 -9
- package/dist/insightsContract.d.mts +0 -9
- package/dist/insightsContract.mjs +0 -189
- package/dist/insightsDistinctSummary.d.cts +0 -12
- package/dist/insightsDistinctSummary.d.mts +0 -12
- package/dist/insightsLatestQueries.cjs +0 -53
- package/dist/insightsLatestQueries.d.cts +0 -13
- package/dist/insightsLatestQueries.d.mts +0 -13
- package/dist/insightsLatestQueries.mjs +0 -52
- package/dist/insightsOverview.cjs +0 -139
- package/dist/insightsOverview.d.cts +0 -40
- package/dist/insightsOverview.d.mts +0 -40
- package/dist/insightsOverview.mjs +0 -135
- 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 -85
- package/dist/types/databaseFields.d.cts +0 -12
- package/dist/types/databaseFields.d.mts +0 -12
- package/dist/types/databaseFields.mjs +0 -85
- package/dist/types/databasePlugin.d.cts +0 -388
- package/dist/types/databasePlugin.d.mts +0 -388
- package/dist/types/databaseQuery.d.cts +0 -46
- package/dist/types/databaseQuery.d.mts +0 -46
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//#region src/database/adapter.ts
|
|
2
|
+
const DATABASE_VERSION_COLUMN = "_v";
|
|
3
|
+
const DATABASE_MAX_QUERY_LIMIT = 500;
|
|
4
|
+
const DATABASE_MAX_MULTI_VALUES = 16;
|
|
5
|
+
var DatabaseSchemaError = class extends Error {
|
|
6
|
+
name = "DatabaseSchemaError";
|
|
7
|
+
};
|
|
8
|
+
const findPhysicalColumn = (table, name) => {
|
|
9
|
+
const column = table.columns.find((candidate) => candidate.name === name);
|
|
10
|
+
if (column === void 0) throw new DatabaseSchemaError(`${table.name} has no column ${name}.`);
|
|
11
|
+
return column;
|
|
12
|
+
};
|
|
13
|
+
const findPhysicalIndex = (table, name) => {
|
|
14
|
+
const index = table.indexes.find((candidate) => candidate.name === name);
|
|
15
|
+
if (index === void 0) throw new DatabaseSchemaError(`${table.name} has no index ${name}.`);
|
|
16
|
+
return index;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The columns that order an index read and close its cursors: the sort
|
|
20
|
+
* columns, then the key columns that neither `eq` nor `sort` fixes.
|
|
21
|
+
*/
|
|
22
|
+
const indexOrderColumns = (table, index) => [...index.sort, ...table.key.filter((column) => !index.sort.includes(column) && !index.eq.includes(column))];
|
|
23
|
+
/**
|
|
24
|
+
* When a row of a table with retention expires, in epoch milliseconds, or
|
|
25
|
+
* undefined when the table keeps its rows or the row's time is null.
|
|
26
|
+
*/
|
|
27
|
+
const expiresAt = (table, row) => {
|
|
28
|
+
const time = table.retention && row[table.retention.column];
|
|
29
|
+
return typeof time === "number" ? time + table.retention.ms : void 0;
|
|
30
|
+
};
|
|
31
|
+
/** The primary key of a row, in key-column order. */
|
|
32
|
+
const rowKey = (table, row) => table.key.map((column) => {
|
|
33
|
+
const value = row[column];
|
|
34
|
+
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
|
+
return value;
|
|
36
|
+
});
|
|
37
|
+
//#endregion
|
|
38
|
+
export { DATABASE_MAX_MULTI_VALUES, DATABASE_MAX_QUERY_LIMIT, DATABASE_VERSION_COLUMN, DatabaseSchemaError, expiresAt, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/database/distinct.ts
|
|
2
|
+
/**
|
|
3
|
+
* HyperLogLog sketches, the storage engine's `distinct` aggregate metric: a
|
|
4
|
+
* sketch is 1,024 registers stored as a 1,024-character string, so adding a
|
|
5
|
+
* value, merging sketches, and counting one never need the values back.
|
|
6
|
+
*/
|
|
2
7
|
const PRECISION = 10;
|
|
3
8
|
const REGISTER_COUNT = 1024;
|
|
4
9
|
const MAX_RANK = 55;
|
|
@@ -13,17 +18,17 @@ const hash = (value) => {
|
|
|
13
18
|
const OFFSET = 33;
|
|
14
19
|
const decode = (summary) => {
|
|
15
20
|
if (!summary) return new Uint8Array(REGISTER_COUNT);
|
|
16
|
-
if (summary.length !== REGISTER_COUNT) throw new Error("Invalid
|
|
21
|
+
if (summary.length !== REGISTER_COUNT) throw new Error("Invalid distinct sketch");
|
|
17
22
|
const registers = new Uint8Array(REGISTER_COUNT);
|
|
18
23
|
for (let index = 0; index < REGISTER_COUNT; index += 1) {
|
|
19
24
|
const value = summary.charCodeAt(index) - OFFSET;
|
|
20
|
-
if (!Number.isInteger(value) || value < 0 || value > MAX_RANK) throw new Error("Invalid
|
|
25
|
+
if (!Number.isInteger(value) || value < 0 || value > MAX_RANK) throw new Error("Invalid distinct sketch");
|
|
21
26
|
registers[index] = value;
|
|
22
27
|
}
|
|
23
28
|
return registers;
|
|
24
29
|
};
|
|
25
30
|
const encode = (registers) => Array.from(registers, (value) => String.fromCharCode(value + OFFSET)).join("");
|
|
26
|
-
const
|
|
31
|
+
const distinctRegister = (identity) => {
|
|
27
32
|
const value = hash(identity);
|
|
28
33
|
const index = Number(value & BigInt(1023));
|
|
29
34
|
let remainder = value >> BigInt(PRECISION);
|
|
@@ -38,13 +43,15 @@ const getInsightsDistinctRegister = (identity) => {
|
|
|
38
43
|
character: String.fromCharCode(rank + OFFSET)
|
|
39
44
|
};
|
|
40
45
|
};
|
|
41
|
-
|
|
46
|
+
/** The sketch with `identity` added; a missing sketch is the empty one. */
|
|
47
|
+
const addDistinct = (summary, identity) => {
|
|
42
48
|
const registers = decode(summary);
|
|
43
|
-
const { index, rank } =
|
|
49
|
+
const { index, rank } = distinctRegister(identity);
|
|
44
50
|
registers[index] = Math.max(registers[index], rank);
|
|
45
51
|
return encode(registers);
|
|
46
52
|
};
|
|
47
|
-
|
|
53
|
+
/** The union of sketches, register by register. */
|
|
54
|
+
const mergeDistinct = (summaries) => {
|
|
48
55
|
const merged = new Uint8Array(REGISTER_COUNT);
|
|
49
56
|
for (const summary of summaries) {
|
|
50
57
|
const registers = decode(summary);
|
|
@@ -52,7 +59,8 @@ const mergeInsightsDistinct = (summaries) => {
|
|
|
52
59
|
}
|
|
53
60
|
return encode(merged);
|
|
54
61
|
};
|
|
55
|
-
|
|
62
|
+
/** The estimated number of distinct values a sketch holds. */
|
|
63
|
+
const countDistinct = (summary) => {
|
|
56
64
|
const registers = decode(summary);
|
|
57
65
|
let inverseSum = 0;
|
|
58
66
|
let empty = 0;
|
|
@@ -60,9 +68,15 @@ const countInsightsDistinct = (summary) => {
|
|
|
60
68
|
inverseSum += 2 ** -register;
|
|
61
69
|
if (register === 0) empty += 1;
|
|
62
70
|
}
|
|
63
|
-
const
|
|
71
|
+
const raw = .7213 / 1.0010537109375 * REGISTER_COUNT ** 2 / inverseSum;
|
|
72
|
+
const estimate = raw <= 2.5 * REGISTER_COUNT && empty > 0 ? REGISTER_COUNT * Math.log(REGISTER_COUNT / empty) : raw;
|
|
64
73
|
return Math.max(0, Math.round(estimate));
|
|
65
74
|
};
|
|
66
|
-
|
|
75
|
+
/** A sketch that holds no value. */
|
|
76
|
+
const emptyDistinct = () => encode(new Uint8Array(REGISTER_COUNT));
|
|
67
77
|
//#endregion
|
|
68
|
-
|
|
78
|
+
exports.addDistinct = addDistinct;
|
|
79
|
+
exports.countDistinct = countDistinct;
|
|
80
|
+
exports.distinctRegister = distinctRegister;
|
|
81
|
+
exports.emptyDistinct = emptyDistinct;
|
|
82
|
+
exports.mergeDistinct = mergeDistinct;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/database/distinct.d.ts
|
|
2
|
+
declare const distinctRegister: (identity: string) => {
|
|
3
|
+
index: number;
|
|
4
|
+
rank: number;
|
|
5
|
+
character: string;
|
|
6
|
+
};
|
|
7
|
+
/** The sketch with `identity` added; a missing sketch is the empty one. */
|
|
8
|
+
declare const addDistinct: (summary: string | null | undefined, identity: string) => string;
|
|
9
|
+
/** The union of sketches, register by register. */
|
|
10
|
+
declare const mergeDistinct: (summaries: readonly (string | null | undefined)[]) => string;
|
|
11
|
+
/** The estimated number of distinct values a sketch holds. */
|
|
12
|
+
declare const countDistinct: (summary: string | null | undefined) => number;
|
|
13
|
+
/** A sketch that holds no value. */
|
|
14
|
+
declare const emptyDistinct: () => string;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { addDistinct, countDistinct, distinctRegister, emptyDistinct, mergeDistinct };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/database/distinct.d.ts
|
|
2
|
+
declare const distinctRegister: (identity: string) => {
|
|
3
|
+
index: number;
|
|
4
|
+
rank: number;
|
|
5
|
+
character: string;
|
|
6
|
+
};
|
|
7
|
+
/** The sketch with `identity` added; a missing sketch is the empty one. */
|
|
8
|
+
declare const addDistinct: (summary: string | null | undefined, identity: string) => string;
|
|
9
|
+
/** The union of sketches, register by register. */
|
|
10
|
+
declare const mergeDistinct: (summaries: readonly (string | null | undefined)[]) => string;
|
|
11
|
+
/** The estimated number of distinct values a sketch holds. */
|
|
12
|
+
declare const countDistinct: (summary: string | null | undefined) => number;
|
|
13
|
+
/** A sketch that holds no value. */
|
|
14
|
+
declare const emptyDistinct: () => string;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { addDistinct, countDistinct, distinctRegister, emptyDistinct, mergeDistinct };
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/database/distinct.ts
|
|
2
|
+
/**
|
|
3
|
+
* HyperLogLog sketches, the storage engine's `distinct` aggregate metric: a
|
|
4
|
+
* sketch is 1,024 registers stored as a 1,024-character string, so adding a
|
|
5
|
+
* value, merging sketches, and counting one never need the values back.
|
|
6
|
+
*/
|
|
2
7
|
const PRECISION = 10;
|
|
3
8
|
const REGISTER_COUNT = 1024;
|
|
4
9
|
const MAX_RANK = 55;
|
|
@@ -13,17 +18,17 @@ const hash = (value) => {
|
|
|
13
18
|
const OFFSET = 33;
|
|
14
19
|
const decode = (summary) => {
|
|
15
20
|
if (!summary) return new Uint8Array(REGISTER_COUNT);
|
|
16
|
-
if (summary.length !== REGISTER_COUNT) throw new Error("Invalid
|
|
21
|
+
if (summary.length !== REGISTER_COUNT) throw new Error("Invalid distinct sketch");
|
|
17
22
|
const registers = new Uint8Array(REGISTER_COUNT);
|
|
18
23
|
for (let index = 0; index < REGISTER_COUNT; index += 1) {
|
|
19
24
|
const value = summary.charCodeAt(index) - OFFSET;
|
|
20
|
-
if (!Number.isInteger(value) || value < 0 || value > MAX_RANK) throw new Error("Invalid
|
|
25
|
+
if (!Number.isInteger(value) || value < 0 || value > MAX_RANK) throw new Error("Invalid distinct sketch");
|
|
21
26
|
registers[index] = value;
|
|
22
27
|
}
|
|
23
28
|
return registers;
|
|
24
29
|
};
|
|
25
30
|
const encode = (registers) => Array.from(registers, (value) => String.fromCharCode(value + OFFSET)).join("");
|
|
26
|
-
const
|
|
31
|
+
const distinctRegister = (identity) => {
|
|
27
32
|
const value = hash(identity);
|
|
28
33
|
const index = Number(value & BigInt(1023));
|
|
29
34
|
let remainder = value >> BigInt(PRECISION);
|
|
@@ -38,13 +43,15 @@ const getInsightsDistinctRegister = (identity) => {
|
|
|
38
43
|
character: String.fromCharCode(rank + OFFSET)
|
|
39
44
|
};
|
|
40
45
|
};
|
|
41
|
-
|
|
46
|
+
/** The sketch with `identity` added; a missing sketch is the empty one. */
|
|
47
|
+
const addDistinct = (summary, identity) => {
|
|
42
48
|
const registers = decode(summary);
|
|
43
|
-
const { index, rank } =
|
|
49
|
+
const { index, rank } = distinctRegister(identity);
|
|
44
50
|
registers[index] = Math.max(registers[index], rank);
|
|
45
51
|
return encode(registers);
|
|
46
52
|
};
|
|
47
|
-
|
|
53
|
+
/** The union of sketches, register by register. */
|
|
54
|
+
const mergeDistinct = (summaries) => {
|
|
48
55
|
const merged = new Uint8Array(REGISTER_COUNT);
|
|
49
56
|
for (const summary of summaries) {
|
|
50
57
|
const registers = decode(summary);
|
|
@@ -52,7 +59,8 @@ const mergeInsightsDistinct = (summaries) => {
|
|
|
52
59
|
}
|
|
53
60
|
return encode(merged);
|
|
54
61
|
};
|
|
55
|
-
|
|
62
|
+
/** The estimated number of distinct values a sketch holds. */
|
|
63
|
+
const countDistinct = (summary) => {
|
|
56
64
|
const registers = decode(summary);
|
|
57
65
|
let inverseSum = 0;
|
|
58
66
|
let empty = 0;
|
|
@@ -60,13 +68,11 @@ const countInsightsDistinct = (summary) => {
|
|
|
60
68
|
inverseSum += 2 ** -register;
|
|
61
69
|
if (register === 0) empty += 1;
|
|
62
70
|
}
|
|
63
|
-
const
|
|
71
|
+
const raw = .7213 / 1.0010537109375 * REGISTER_COUNT ** 2 / inverseSum;
|
|
72
|
+
const estimate = raw <= 2.5 * REGISTER_COUNT && empty > 0 ? REGISTER_COUNT * Math.log(REGISTER_COUNT / empty) : raw;
|
|
64
73
|
return Math.max(0, Math.round(estimate));
|
|
65
74
|
};
|
|
66
|
-
|
|
75
|
+
/** A sketch that holds no value. */
|
|
76
|
+
const emptyDistinct = () => encode(new Uint8Array(REGISTER_COUNT));
|
|
67
77
|
//#endregion
|
|
68
|
-
|
|
69
|
-
exports.countInsightsDistinct = countInsightsDistinct;
|
|
70
|
-
exports.emptyInsightsDistinct = emptyInsightsDistinct;
|
|
71
|
-
exports.getInsightsDistinctRegister = getInsightsDistinctRegister;
|
|
72
|
-
exports.mergeInsightsDistinct = mergeInsightsDistinct;
|
|
78
|
+
export { addDistinct, countDistinct, distinctRegister, emptyDistinct, mergeDistinct };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const require_adapter = require("./adapter.cjs");
|
|
2
|
+
const require_values = require("./values.cjs");
|
|
3
|
+
//#region src/database/memoryAdapter.ts
|
|
4
|
+
const keyId = (key) => JSON.stringify(key);
|
|
5
|
+
const numberOf = (value) => typeof value === "number" ? value : 0;
|
|
6
|
+
const violatesUnique = (table, rows, id, row) => table.indexes.some((index) => {
|
|
7
|
+
if (!index.unique) return false;
|
|
8
|
+
const entries = require_values.indexEntries(table, index, row);
|
|
9
|
+
return entries.length > 0 && [...rows].some(([otherId, other]) => otherId !== id && require_values.indexEntries(table, index, other).some((entry) => entries.some((candidate) => require_values.compareTuples(entry, candidate) === 0)));
|
|
10
|
+
});
|
|
11
|
+
const putRow = (table, rows, id, row) => {
|
|
12
|
+
if (violatesUnique(table, rows, id, row)) return false;
|
|
13
|
+
rows.set(id, row);
|
|
14
|
+
return true;
|
|
15
|
+
};
|
|
16
|
+
/** Applies one op to a draft; false means its guard or a constraint failed. */
|
|
17
|
+
const applyOp = (rows, op) => {
|
|
18
|
+
const id = keyId(op.type === "insert" ? require_adapter.rowKey(op.table, op.row) : op.key);
|
|
19
|
+
const current = rows.get(id);
|
|
20
|
+
const version = numberOf(current?.["_v"]);
|
|
21
|
+
switch (op.type) {
|
|
22
|
+
case "insert": return current === void 0 && putRow(op.table, rows, id, structuredClone(op.row));
|
|
23
|
+
case "patch": return current !== void 0 && version === op.guard.v && putRow(op.table, rows, id, {
|
|
24
|
+
...current,
|
|
25
|
+
...structuredClone(op.set),
|
|
26
|
+
["_v"]: version + 1
|
|
27
|
+
});
|
|
28
|
+
case "delete": return current !== void 0 && version === op.guard.v && rows.delete(id);
|
|
29
|
+
case "increment": {
|
|
30
|
+
const base = current ?? op.init;
|
|
31
|
+
if (base === void 0 || op.guard !== void 0 && (current === void 0 || version !== op.guard.v)) return false;
|
|
32
|
+
const next = structuredClone(base);
|
|
33
|
+
for (const [column, delta] of Object.entries(op.by)) next[column] = numberOf(base[column]) + delta;
|
|
34
|
+
next["_v"] = numberOf(base["_v"]) + 1;
|
|
35
|
+
return putRow(op.table, rows, id, next);
|
|
36
|
+
}
|
|
37
|
+
case "check": return current !== void 0 && version === op.guard.v;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The reference adapter: every read is a consistent snapshot and every write
|
|
42
|
+
* applies to a copy that replaces the state only when all ops succeed.
|
|
43
|
+
*/
|
|
44
|
+
const createMemoryAdapter = (options = {}) => {
|
|
45
|
+
const prefix = options.tablePrefix ?? "";
|
|
46
|
+
let state = /* @__PURE__ */ new Map();
|
|
47
|
+
const rowsOf = (table) => state.get(prefix + table.name) ?? /* @__PURE__ */ new Map();
|
|
48
|
+
return {
|
|
49
|
+
id: "memory",
|
|
50
|
+
fits: (ops) => ops.length <= (options.maxOps ?? Number.POSITIVE_INFINITY),
|
|
51
|
+
async get(table, keys) {
|
|
52
|
+
const rows = rowsOf(table);
|
|
53
|
+
return keys.map((key) => {
|
|
54
|
+
const row = rows.get(keyId(key));
|
|
55
|
+
return row === void 0 ? null : structuredClone(row);
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
async query(table, request) {
|
|
59
|
+
const index = require_adapter.findPhysicalIndex(table, request.index);
|
|
60
|
+
const direction = request.order === "asc" ? 1 : -1;
|
|
61
|
+
const ordered = [...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)));
|
|
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));
|
|
70
|
+
},
|
|
71
|
+
async write(ops) {
|
|
72
|
+
const draft = new Map(state);
|
|
73
|
+
const copied = /* @__PURE__ */ new Set();
|
|
74
|
+
for (const [position, op] of ops.entries()) {
|
|
75
|
+
const name = prefix + op.table.name;
|
|
76
|
+
if (!copied.has(name)) {
|
|
77
|
+
draft.set(name, new Map(draft.get(name)));
|
|
78
|
+
copied.add(name);
|
|
79
|
+
}
|
|
80
|
+
if (!applyOp(draft.get(name), op)) return {
|
|
81
|
+
ok: false,
|
|
82
|
+
failedOp: position
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
state = draft;
|
|
86
|
+
return { ok: true };
|
|
87
|
+
},
|
|
88
|
+
async prune(table, before, limit) {
|
|
89
|
+
const column = table.retention?.column ?? "";
|
|
90
|
+
/** Oldest first; the key breaks a tie. */
|
|
91
|
+
const order = (row) => [row[column], ...require_adapter.rowKey(table, row)];
|
|
92
|
+
const doomed = [...rowsOf(table)].filter(([, row]) => {
|
|
93
|
+
const time = row[column];
|
|
94
|
+
return typeof time === "number" && time <= before;
|
|
95
|
+
}).sort(([, left], [, right]) => require_values.compareTuples(order(left), order(right))).slice(0, limit);
|
|
96
|
+
const rows = new Map(rowsOf(table));
|
|
97
|
+
for (const [id] of doomed) rows.delete(id);
|
|
98
|
+
state = new Map(state).set(prefix + table.name, rows);
|
|
99
|
+
return doomed.length;
|
|
100
|
+
},
|
|
101
|
+
migrations: { async apply(tables) {
|
|
102
|
+
const next = new Map(state);
|
|
103
|
+
for (const table of tables) {
|
|
104
|
+
const name = prefix + table.name;
|
|
105
|
+
next.set(name, next.get(name) ?? /* @__PURE__ */ new Map());
|
|
106
|
+
}
|
|
107
|
+
state = next;
|
|
108
|
+
} },
|
|
109
|
+
async dispose() {
|
|
110
|
+
state = /* @__PURE__ */ new Map();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
//#endregion
|
|
115
|
+
exports.createMemoryAdapter = createMemoryAdapter;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DatabaseAdapter } from "./adapter.cjs";
|
|
2
|
+
//#region src/database/memoryAdapter.d.ts
|
|
3
|
+
interface MemoryAdapterOptions {
|
|
4
|
+
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
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The reference adapter: every read is a consistent snapshot and every write
|
|
12
|
+
* applies to a copy that replaces the state only when all ops succeed.
|
|
13
|
+
*/
|
|
14
|
+
declare const createMemoryAdapter: (options?: MemoryAdapterOptions) => DatabaseAdapter;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { MemoryAdapterOptions, createMemoryAdapter };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DatabaseAdapter } from "./adapter.mjs";
|
|
2
|
+
//#region src/database/memoryAdapter.d.ts
|
|
3
|
+
interface MemoryAdapterOptions {
|
|
4
|
+
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
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The reference adapter: every read is a consistent snapshot and every write
|
|
12
|
+
* applies to a copy that replaces the state only when all ops succeed.
|
|
13
|
+
*/
|
|
14
|
+
declare const createMemoryAdapter: (options?: MemoryAdapterOptions) => DatabaseAdapter;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { MemoryAdapterOptions, createMemoryAdapter };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { findPhysicalIndex, rowKey } from "./adapter.mjs";
|
|
2
|
+
import { compareTuples, indexEntries, indexOrderTuple, matchesQuery } from "./values.mjs";
|
|
3
|
+
//#region src/database/memoryAdapter.ts
|
|
4
|
+
const keyId = (key) => JSON.stringify(key);
|
|
5
|
+
const numberOf = (value) => typeof value === "number" ? value : 0;
|
|
6
|
+
const violatesUnique = (table, rows, id, row) => table.indexes.some((index) => {
|
|
7
|
+
if (!index.unique) return false;
|
|
8
|
+
const entries = indexEntries(table, index, row);
|
|
9
|
+
return entries.length > 0 && [...rows].some(([otherId, other]) => otherId !== id && indexEntries(table, index, other).some((entry) => entries.some((candidate) => compareTuples(entry, candidate) === 0)));
|
|
10
|
+
});
|
|
11
|
+
const putRow = (table, rows, id, row) => {
|
|
12
|
+
if (violatesUnique(table, rows, id, row)) return false;
|
|
13
|
+
rows.set(id, row);
|
|
14
|
+
return true;
|
|
15
|
+
};
|
|
16
|
+
/** Applies one op to a draft; false means its guard or a constraint failed. */
|
|
17
|
+
const applyOp = (rows, op) => {
|
|
18
|
+
const id = keyId(op.type === "insert" ? rowKey(op.table, op.row) : op.key);
|
|
19
|
+
const current = rows.get(id);
|
|
20
|
+
const version = numberOf(current?.["_v"]);
|
|
21
|
+
switch (op.type) {
|
|
22
|
+
case "insert": return current === void 0 && putRow(op.table, rows, id, structuredClone(op.row));
|
|
23
|
+
case "patch": return current !== void 0 && version === op.guard.v && putRow(op.table, rows, id, {
|
|
24
|
+
...current,
|
|
25
|
+
...structuredClone(op.set),
|
|
26
|
+
["_v"]: version + 1
|
|
27
|
+
});
|
|
28
|
+
case "delete": return current !== void 0 && version === op.guard.v && rows.delete(id);
|
|
29
|
+
case "increment": {
|
|
30
|
+
const base = current ?? op.init;
|
|
31
|
+
if (base === void 0 || op.guard !== void 0 && (current === void 0 || version !== op.guard.v)) return false;
|
|
32
|
+
const next = structuredClone(base);
|
|
33
|
+
for (const [column, delta] of Object.entries(op.by)) next[column] = numberOf(base[column]) + delta;
|
|
34
|
+
next["_v"] = numberOf(base["_v"]) + 1;
|
|
35
|
+
return putRow(op.table, rows, id, next);
|
|
36
|
+
}
|
|
37
|
+
case "check": return current !== void 0 && version === op.guard.v;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The reference adapter: every read is a consistent snapshot and every write
|
|
42
|
+
* applies to a copy that replaces the state only when all ops succeed.
|
|
43
|
+
*/
|
|
44
|
+
const createMemoryAdapter = (options = {}) => {
|
|
45
|
+
const prefix = options.tablePrefix ?? "";
|
|
46
|
+
let state = /* @__PURE__ */ new Map();
|
|
47
|
+
const rowsOf = (table) => state.get(prefix + table.name) ?? /* @__PURE__ */ new Map();
|
|
48
|
+
return {
|
|
49
|
+
id: "memory",
|
|
50
|
+
fits: (ops) => ops.length <= (options.maxOps ?? Number.POSITIVE_INFINITY),
|
|
51
|
+
async get(table, keys) {
|
|
52
|
+
const rows = rowsOf(table);
|
|
53
|
+
return keys.map((key) => {
|
|
54
|
+
const row = rows.get(keyId(key));
|
|
55
|
+
return row === void 0 ? null : structuredClone(row);
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
async query(table, request) {
|
|
59
|
+
const index = findPhysicalIndex(table, request.index);
|
|
60
|
+
const direction = request.order === "asc" ? 1 : -1;
|
|
61
|
+
const ordered = [...rowsOf(table).values()].filter((row) => matchesQuery(table, index, request, row)).sort((left, right) => direction * compareTuples(indexOrderTuple(table, index, left), indexOrderTuple(table, index, right)));
|
|
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));
|
|
70
|
+
},
|
|
71
|
+
async write(ops) {
|
|
72
|
+
const draft = new Map(state);
|
|
73
|
+
const copied = /* @__PURE__ */ new Set();
|
|
74
|
+
for (const [position, op] of ops.entries()) {
|
|
75
|
+
const name = prefix + op.table.name;
|
|
76
|
+
if (!copied.has(name)) {
|
|
77
|
+
draft.set(name, new Map(draft.get(name)));
|
|
78
|
+
copied.add(name);
|
|
79
|
+
}
|
|
80
|
+
if (!applyOp(draft.get(name), op)) return {
|
|
81
|
+
ok: false,
|
|
82
|
+
failedOp: position
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
state = draft;
|
|
86
|
+
return { ok: true };
|
|
87
|
+
},
|
|
88
|
+
async prune(table, before, limit) {
|
|
89
|
+
const column = table.retention?.column ?? "";
|
|
90
|
+
/** Oldest first; the key breaks a tie. */
|
|
91
|
+
const order = (row) => [row[column], ...rowKey(table, row)];
|
|
92
|
+
const doomed = [...rowsOf(table)].filter(([, row]) => {
|
|
93
|
+
const time = row[column];
|
|
94
|
+
return typeof time === "number" && time <= before;
|
|
95
|
+
}).sort(([, left], [, right]) => compareTuples(order(left), order(right))).slice(0, limit);
|
|
96
|
+
const rows = new Map(rowsOf(table));
|
|
97
|
+
for (const [id] of doomed) rows.delete(id);
|
|
98
|
+
state = new Map(state).set(prefix + table.name, rows);
|
|
99
|
+
return doomed.length;
|
|
100
|
+
},
|
|
101
|
+
migrations: { async apply(tables) {
|
|
102
|
+
const next = new Map(state);
|
|
103
|
+
for (const table of tables) {
|
|
104
|
+
const name = prefix + table.name;
|
|
105
|
+
next.set(name, next.get(name) ?? /* @__PURE__ */ new Map());
|
|
106
|
+
}
|
|
107
|
+
state = next;
|
|
108
|
+
} },
|
|
109
|
+
async dispose() {
|
|
110
|
+
state = /* @__PURE__ */ new Map();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
//#endregion
|
|
115
|
+
export { createMemoryAdapter };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const require_adapter = require("./adapter.cjs");
|
|
2
|
+
//#region src/database/values.ts
|
|
3
|
+
var DatabaseValueError = class extends Error {
|
|
4
|
+
name = "DatabaseValueError";
|
|
5
|
+
};
|
|
6
|
+
/** Compares strings in UTF-8 byte order, which is Unicode code point order. */
|
|
7
|
+
const compareUtf8 = (left, right) => {
|
|
8
|
+
let leftIndex = 0;
|
|
9
|
+
let rightIndex = 0;
|
|
10
|
+
while (leftIndex < left.length && rightIndex < right.length) {
|
|
11
|
+
const leftPoint = left.codePointAt(leftIndex);
|
|
12
|
+
const rightPoint = right.codePointAt(rightIndex);
|
|
13
|
+
if (leftPoint !== rightPoint) return leftPoint < rightPoint ? -1 : 1;
|
|
14
|
+
leftIndex += leftPoint > 65535 ? 2 : 1;
|
|
15
|
+
rightIndex += rightPoint > 65535 ? 2 : 1;
|
|
16
|
+
}
|
|
17
|
+
const leftDone = leftIndex >= left.length;
|
|
18
|
+
return leftDone === rightIndex >= right.length ? 0 : leftDone ? -1 : 1;
|
|
19
|
+
};
|
|
20
|
+
/** Orders strings by UTF-8 bytes, numbers numerically, and false before true. */
|
|
21
|
+
const compareKeyValues = (left, right) => {
|
|
22
|
+
if (typeof left === "string" && typeof right === "string") return compareUtf8(left, right);
|
|
23
|
+
if (typeof left === "number" && typeof right === "number") return left < right ? -1 : left > right ? 1 : 0;
|
|
24
|
+
if (typeof left === "boolean" && typeof right === "boolean") return Number(left) - Number(right);
|
|
25
|
+
throw new DatabaseValueError(`Cannot compare ${typeof left} with ${typeof right}.`);
|
|
26
|
+
};
|
|
27
|
+
/** Compares tuples element by element; a shorter equal prefix sorts first. */
|
|
28
|
+
const compareTuples = (left, right) => {
|
|
29
|
+
for (let index = 0; index < Math.min(left.length, right.length); index += 1) {
|
|
30
|
+
const result = compareKeyValues(left[index], right[index]);
|
|
31
|
+
if (result !== 0) return result;
|
|
32
|
+
}
|
|
33
|
+
return left.length - right.length;
|
|
34
|
+
};
|
|
35
|
+
/** JSON text with object keys sorted, so a value compares equal after a store reorders map keys. */
|
|
36
|
+
const canonicalJson = (value) => JSON.stringify(value ?? null, (_, inner) => inner !== null && typeof inner === "object" && !Array.isArray(inner) ? Object.fromEntries(Object.entries(inner).sort(([left], [right]) => compareUtf8(left, right))) : inner);
|
|
37
|
+
const isKeyValue = (value) => typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
|
|
38
|
+
/** Every `eq` tuple a row is indexed under; none when an index field is null. */
|
|
39
|
+
const indexEntries = (table, index, row) => {
|
|
40
|
+
if (index.sort.some((column) => !isKeyValue(row[column]))) return [];
|
|
41
|
+
let entries = [[]];
|
|
42
|
+
for (const column of index.eq) {
|
|
43
|
+
const value = row[column];
|
|
44
|
+
const values = require_adapter.findPhysicalColumn(table, column).multi ? Array.isArray(value) ? value.filter(isKeyValue) : [] : isKeyValue(value) ? [value] : [];
|
|
45
|
+
if (values.length === 0) return [];
|
|
46
|
+
entries = entries.flatMap((entry) => [...new Set(values)].map((item) => [...entry, item]));
|
|
47
|
+
}
|
|
48
|
+
return entries;
|
|
49
|
+
};
|
|
50
|
+
/** The row's position in an index read, following `indexOrderColumns`. */
|
|
51
|
+
const indexOrderTuple = (table, index, row) => require_adapter.indexOrderColumns(table, index).map((column) => {
|
|
52
|
+
const value = row[column];
|
|
53
|
+
if (!isKeyValue(value)) throw new DatabaseValueError(`${table.name}.${column} is not orderable for index ${index.name}.`);
|
|
54
|
+
return value;
|
|
55
|
+
});
|
|
56
|
+
const satisfiesBound = (tuple, bound, side) => {
|
|
57
|
+
if (bound === void 0) return true;
|
|
58
|
+
const result = compareTuples(tuple.slice(0, bound.values.length), bound.values);
|
|
59
|
+
if (result === 0) return bound.inclusive;
|
|
60
|
+
return side === "lower" ? result > 0 : result < 0;
|
|
61
|
+
};
|
|
62
|
+
/** Whether a stored row belongs to the result range of a query. */
|
|
63
|
+
const matchesQuery = (table, index, request, row) => indexEntries(table, index, row).some((entry) => compareTuples(entry, request.eq) === 0) && satisfiesBound(indexOrderTuple(table, index, row), request.lower, "lower") && satisfiesBound(indexOrderTuple(table, index, row), request.upper, "upper");
|
|
64
|
+
const INTEGER_TEXT = /^-?\d+$/u;
|
|
65
|
+
const NUMBER_TEXT = /^-?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/iu;
|
|
66
|
+
const toNumber = (raw, integer) => {
|
|
67
|
+
const text = typeof raw === "bigint" ? raw.toString() : typeof raw === "string" ? raw : typeof raw === "object" && raw !== null && "toString" in raw ? String(raw) : void 0;
|
|
68
|
+
const value = typeof raw === "number" ? raw : text !== void 0 && (integer ? INTEGER_TEXT : NUMBER_TEXT).test(text) ? Number(text) : NaN;
|
|
69
|
+
if (integer ? !Number.isSafeInteger(value) : !Number.isFinite(value)) throw new DatabaseValueError(`Expected a ${integer ? "safe integer" : "finite number"}.`);
|
|
70
|
+
return value;
|
|
71
|
+
};
|
|
72
|
+
const toBoolean = (raw) => {
|
|
73
|
+
if (raw === true || raw === 1 || raw === 1n || raw === "1" || raw === "t") return true;
|
|
74
|
+
if (raw === false || raw === 0 || raw === 0n || raw === "0" || raw === "f") return false;
|
|
75
|
+
if (raw === "true" || raw === "false") return raw === "true";
|
|
76
|
+
throw new DatabaseValueError("Expected a boolean.");
|
|
77
|
+
};
|
|
78
|
+
const toScalar = (column, raw) => {
|
|
79
|
+
switch (column.type) {
|
|
80
|
+
case "string":
|
|
81
|
+
if (typeof raw !== "string") throw new DatabaseValueError(`Expected a string in ${column.name}.`);
|
|
82
|
+
return raw;
|
|
83
|
+
case "integer":
|
|
84
|
+
case "number": return toNumber(raw, column.type === "integer");
|
|
85
|
+
case "boolean": return toBoolean(raw);
|
|
86
|
+
case "json": return raw;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
/** Converts a backend value (int8 text, BigInt, Decimal, 0/1, JSON text) to a stored value. */
|
|
90
|
+
const normalizeStoredValue = (column, raw, options) => {
|
|
91
|
+
if (raw === null || raw === void 0) return null;
|
|
92
|
+
const parsed = options.jsonText && (column.multi || column.type === "json") && typeof raw === "string" ? JSON.parse(raw) : raw;
|
|
93
|
+
if (!column.multi) return toScalar(column, parsed);
|
|
94
|
+
if (!Array.isArray(parsed) || parsed.length > 16) throw new DatabaseValueError(`Expected at most 16 values in ${column.name}.`);
|
|
95
|
+
return parsed.map((item) => toScalar(column, item));
|
|
96
|
+
};
|
|
97
|
+
/** Converts every column of a backend row; unknown backend columns are dropped. */
|
|
98
|
+
const normalizeStoredRow = (table, raw, options) => Object.fromEntries(table.columns.map((column) => [column.name, normalizeStoredValue(column, raw[column.name], options)]));
|
|
99
|
+
//#endregion
|
|
100
|
+
exports.DatabaseValueError = DatabaseValueError;
|
|
101
|
+
exports.canonicalJson = canonicalJson;
|
|
102
|
+
exports.compareKeyValues = compareKeyValues;
|
|
103
|
+
exports.compareTuples = compareTuples;
|
|
104
|
+
exports.compareUtf8 = compareUtf8;
|
|
105
|
+
exports.indexEntries = indexEntries;
|
|
106
|
+
exports.indexOrderTuple = indexOrderTuple;
|
|
107
|
+
exports.isKeyValue = isKeyValue;
|
|
108
|
+
exports.matchesQuery = matchesQuery;
|
|
109
|
+
exports.normalizeStoredRow = normalizeStoredRow;
|
|
110
|
+
exports.normalizeStoredValue = normalizeStoredValue;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DatabaseKey, DatabaseKeyValue, DatabaseValue, PhysicalColumn, PhysicalIndex, PhysicalTable, QueryRequest, StoredRow } from "./adapter.cjs";
|
|
2
|
+
//#region src/database/values.d.ts
|
|
3
|
+
declare class DatabaseValueError extends Error {
|
|
4
|
+
readonly name = "DatabaseValueError";
|
|
5
|
+
}
|
|
6
|
+
/** Compares strings in UTF-8 byte order, which is Unicode code point order. */
|
|
7
|
+
declare const compareUtf8: (left: string, right: string) => number;
|
|
8
|
+
/** Orders strings by UTF-8 bytes, numbers numerically, and false before true. */
|
|
9
|
+
declare const compareKeyValues: (left: DatabaseKeyValue, right: DatabaseKeyValue) => number;
|
|
10
|
+
/** Compares tuples element by element; a shorter equal prefix sorts first. */
|
|
11
|
+
declare const compareTuples: (left: readonly DatabaseKeyValue[], right: readonly DatabaseKeyValue[]) => number;
|
|
12
|
+
/** JSON text with object keys sorted, so a value compares equal after a store reorders map keys. */
|
|
13
|
+
declare const canonicalJson: (value: DatabaseValue | undefined) => string;
|
|
14
|
+
declare const isKeyValue: (value: unknown) => value is DatabaseKeyValue;
|
|
15
|
+
/** Every `eq` tuple a row is indexed under; none when an index field is null. */
|
|
16
|
+
declare const indexEntries: (table: PhysicalTable, index: PhysicalIndex, row: StoredRow) => readonly DatabaseKey[];
|
|
17
|
+
/** The row's position in an index read, following `indexOrderColumns`. */
|
|
18
|
+
declare const indexOrderTuple: (table: PhysicalTable, index: PhysicalIndex, row: StoredRow) => DatabaseKey;
|
|
19
|
+
/** Whether a stored row belongs to the result range of a query. */
|
|
20
|
+
declare const matchesQuery: (table: PhysicalTable, index: PhysicalIndex, request: QueryRequest, row: StoredRow) => boolean;
|
|
21
|
+
interface NormalizeOptions {
|
|
22
|
+
/** The backend returns JSON and multi-valued columns as text. */
|
|
23
|
+
readonly jsonText: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** Converts a backend value (int8 text, BigInt, Decimal, 0/1, JSON text) to a stored value. */
|
|
26
|
+
declare const normalizeStoredValue: (column: PhysicalColumn, raw: unknown, options: NormalizeOptions) => DatabaseValue;
|
|
27
|
+
/** Converts every column of a backend row; unknown backend columns are dropped. */
|
|
28
|
+
declare const normalizeStoredRow: (table: PhysicalTable, raw: Readonly<Record<string, unknown>>, options: NormalizeOptions) => StoredRow;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { DatabaseValueError, NormalizeOptions, canonicalJson, compareKeyValues, compareTuples, compareUtf8, indexEntries, indexOrderTuple, isKeyValue, matchesQuery, normalizeStoredRow, normalizeStoredValue };
|