@hot-updater/plugin-core 1.0.0-rc.14 → 1.0.0-rc.16
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/bundleEventRow.cjs +42 -0
- package/dist/bundleEventRow.mjs +41 -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/database/adapter.cjs +37 -0
- package/dist/database/adapter.d.cts +150 -0
- package/dist/database/adapter.d.mts +150 -0
- package/dist/database/adapter.mjs +30 -0
- package/dist/database/memoryAdapter.cjs +102 -0
- package/dist/database/memoryAdapter.d.cts +16 -0
- package/dist/database/memoryAdapter.d.mts +16 -0
- package/dist/database/memoryAdapter.mjs +102 -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 +129 -0
- package/dist/database/verifyAdapter.d.cts +29 -0
- package/dist/database/verifyAdapter.d.mts +29 -0
- package/dist/database/verifyAdapter.mjs +128 -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/databaseMetadata.cjs +2 -2
- package/dist/databaseMetadata.mjs +1 -1
- package/dist/databaseRows.cjs +0 -6
- package/dist/databaseRows.mjs +0 -6
- package/dist/index.cjs +14 -41
- package/dist/index.d.cts +15 -19
- package/dist/index.d.mts +15 -19
- package/dist/index.mjs +10 -16
- package/dist/insightsContract.cjs +65 -17
- package/dist/insightsContract.d.cts +6 -1
- package/dist/insightsContract.d.mts +6 -1
- package/dist/insightsContract.mjs +52 -5
- package/dist/insightsDistinctSummary.cjs +73 -0
- package/dist/insightsDistinctSummary.d.cts +12 -0
- package/dist/insightsDistinctSummary.d.mts +12 -0
- package/dist/insightsDistinctSummary.mjs +69 -0
- package/dist/insightsOverview.cjs +139 -0
- package/dist/insightsOverview.d.cts +42 -0
- package/dist/insightsOverview.d.mts +42 -0
- package/dist/insightsOverview.mjs +134 -0
- package/dist/internal.cjs +44 -8
- package/dist/internal.d.cts +12 -9
- package/dist/internal.d.mts +12 -9
- package/dist/internal.mjs +9 -4
- 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 +33 -0
- package/dist/types/databaseConfig.d.mts +33 -0
- package/dist/types/databaseConfig.mjs +5 -0
- package/dist/types/databaseOperations.d.cts +5 -172
- package/dist/types/databaseOperations.d.mts +5 -172
- package/dist/types/databaseRows.d.cts +3 -6
- package/dist/types/databaseRows.d.mts +3 -6
- package/dist/types/index.cjs +2 -0
- package/dist/types/index.d.cts +9 -86
- package/dist/types/index.d.mts +9 -86
- package/dist/types/index.mjs +2 -1
- package/dist/types/insights.d.cts +172 -0
- package/dist/types/insights.d.mts +172 -0
- package/dist/types/internal.d.cts +4 -7
- package/dist/types/internal.d.mts +4 -7
- package/dist/types/models.d.cts +24 -0
- package/dist/types/models.d.mts +24 -0
- package/dist/types/public.d.cts +4 -2
- package/dist/types/public.d.mts +4 -2
- package/package.json +2 -2
- 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 -873
- package/dist/createDatabasePlugin.d.cts +0 -28
- package/dist/createDatabasePlugin.d.mts +0 -28
- package/dist/createDatabasePlugin.mjs +0 -869
- 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 -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 -293
- package/dist/databasePluginCrudValidationFields.d.cts +0 -1
- package/dist/databasePluginCrudValidationFields.d.mts +0 -1
- package/dist/databasePluginCrudValidationFields.mjs +0 -281
- 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/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/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 -88
- package/dist/types/databaseFields.d.cts +0 -12
- package/dist/types/databaseFields.d.mts +0 -12
- package/dist/types/databaseFields.mjs +0 -88
- package/dist/types/databasePlugin.d.cts +0 -304
- package/dist/types/databasePlugin.d.mts +0 -304
- package/dist/types/databaseQuery.d.cts +0 -46
- package/dist/types/databaseQuery.d.mts +0 -46
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
//#region src/database/adapter.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The storage adapter contract. An adapter implements `get`, `query`, and
|
|
4
|
+
* `write` with its backend's native features and knows nothing about Hot
|
|
5
|
+
* Updater's domain or plugins. Adapter authors import it from the public
|
|
6
|
+
* `@hot-updater/server/database` subpath.
|
|
7
|
+
*/
|
|
8
|
+
type DatabaseJson = null | boolean | number | string | readonly DatabaseJson[] | {
|
|
9
|
+
readonly [key: string]: DatabaseJson;
|
|
10
|
+
};
|
|
11
|
+
/** A stored value: a string, number, boolean, null, or JSON document. */
|
|
12
|
+
type DatabaseValue = DatabaseJson;
|
|
13
|
+
/** A value allowed in keys and indexes. */
|
|
14
|
+
type DatabaseKeyValue = string | number | boolean;
|
|
15
|
+
/** Primary-key values in the table's key-column order. */
|
|
16
|
+
type DatabaseKey = readonly DatabaseKeyValue[];
|
|
17
|
+
type StoredRow = {
|
|
18
|
+
readonly [column: string]: DatabaseValue;
|
|
19
|
+
};
|
|
20
|
+
/** `integer` holds safe integers; `number` holds any finite number. */
|
|
21
|
+
type PhysicalColumnType = "string" | "integer" | "number" | "boolean" | "json";
|
|
22
|
+
interface PhysicalColumn {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly type: PhysicalColumnType;
|
|
25
|
+
readonly nullable: boolean;
|
|
26
|
+
/** Strings only: the maximum length in UTF-16 code units; unbounded text when absent. */
|
|
27
|
+
readonly maxLength?: number;
|
|
28
|
+
/** Strings only: ASCII text, stored in single bytes where a backend limits index size (MySQL). */
|
|
29
|
+
readonly ascii?: true;
|
|
30
|
+
/** A whole number the backend stores when a writer omits the column; the engine's own columns default to 0. */
|
|
31
|
+
readonly default?: number;
|
|
32
|
+
/** A multi-valued field: an array of 1–16 values of `type`, indexable in `eq` only. */
|
|
33
|
+
readonly multi?: true;
|
|
34
|
+
}
|
|
35
|
+
interface PhysicalIndex {
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly eq: readonly string[];
|
|
38
|
+
readonly sort: readonly string[];
|
|
39
|
+
/** The `eq` values identify at most one row. */
|
|
40
|
+
readonly unique?: true;
|
|
41
|
+
}
|
|
42
|
+
interface PhysicalTable {
|
|
43
|
+
/** The logical name; each adapter applies its own table prefix. */
|
|
44
|
+
readonly name: string;
|
|
45
|
+
readonly columns: readonly PhysicalColumn[];
|
|
46
|
+
/** Primary-key columns, in order. */
|
|
47
|
+
readonly key: readonly string[];
|
|
48
|
+
readonly indexes: readonly PhysicalIndex[];
|
|
49
|
+
}
|
|
50
|
+
interface QueryBound {
|
|
51
|
+
/** A prefix of the index's order columns (see `indexOrderColumns`). */
|
|
52
|
+
readonly values: readonly DatabaseKeyValue[];
|
|
53
|
+
readonly inclusive: boolean;
|
|
54
|
+
}
|
|
55
|
+
interface QueryRequest {
|
|
56
|
+
readonly index: string;
|
|
57
|
+
/** Values for the index's `eq` columns, in order. */
|
|
58
|
+
readonly eq: readonly DatabaseKeyValue[];
|
|
59
|
+
readonly lower?: QueryBound;
|
|
60
|
+
readonly upper?: QueryBound;
|
|
61
|
+
readonly order: "asc" | "desc";
|
|
62
|
+
/** 1–500 rows. */
|
|
63
|
+
readonly limit: number;
|
|
64
|
+
}
|
|
65
|
+
/** The row's `_v` at read time. */
|
|
66
|
+
interface WriteGuard {
|
|
67
|
+
readonly v: number;
|
|
68
|
+
}
|
|
69
|
+
/** At most one op per (table, key); guards see the pre-write state. */
|
|
70
|
+
type WriteOp = {
|
|
71
|
+
readonly type: "insert";
|
|
72
|
+
readonly table: PhysicalTable;
|
|
73
|
+
/** The complete row, `_v` included; the key must not exist. */
|
|
74
|
+
readonly row: StoredRow;
|
|
75
|
+
} | {
|
|
76
|
+
readonly type: "patch";
|
|
77
|
+
readonly table: PhysicalTable;
|
|
78
|
+
readonly key: DatabaseKey;
|
|
79
|
+
/** Named non-key columns only; the adapter sets `_v` to `guard.v + 1`. */
|
|
80
|
+
readonly set: StoredRow;
|
|
81
|
+
readonly guard: WriteGuard;
|
|
82
|
+
readonly previous: StoredRow;
|
|
83
|
+
} | {
|
|
84
|
+
readonly type: "delete";
|
|
85
|
+
readonly table: PhysicalTable;
|
|
86
|
+
readonly key: DatabaseKey;
|
|
87
|
+
readonly guard: WriteGuard;
|
|
88
|
+
readonly previous: StoredRow;
|
|
89
|
+
} | {
|
|
90
|
+
readonly type: "increment";
|
|
91
|
+
readonly table: PhysicalTable;
|
|
92
|
+
readonly key: DatabaseKey;
|
|
93
|
+
readonly by: Readonly<Record<string, number>>;
|
|
94
|
+
/** The row to create, before `by` applies, when the key does not exist. */
|
|
95
|
+
readonly init?: StoredRow;
|
|
96
|
+
readonly guard?: WriteGuard;
|
|
97
|
+
} | {
|
|
98
|
+
readonly type: "check";
|
|
99
|
+
readonly table: PhysicalTable;
|
|
100
|
+
readonly key: DatabaseKey;
|
|
101
|
+
readonly guard: WriteGuard;
|
|
102
|
+
};
|
|
103
|
+
type WriteResult = {
|
|
104
|
+
readonly ok: true;
|
|
105
|
+
} |
|
|
106
|
+
/** A guard failed, unique violations and missing rows included. */
|
|
107
|
+
{
|
|
108
|
+
readonly ok: false;
|
|
109
|
+
readonly failedOp: number;
|
|
110
|
+
} |
|
|
111
|
+
/** Transient: deadlock, serialization, SQLITE_BUSY, TransactionConflict, ABORTED. */
|
|
112
|
+
{
|
|
113
|
+
readonly ok: false;
|
|
114
|
+
readonly retry: true;
|
|
115
|
+
};
|
|
116
|
+
interface DatabaseAdapter {
|
|
117
|
+
readonly id: string;
|
|
118
|
+
/** True when the ops commit atomically in one write. */
|
|
119
|
+
fits(ops: readonly WriteOp[]): boolean;
|
|
120
|
+
/** Strongly consistent batch point read; results follow the order of keys. */
|
|
121
|
+
get(table: PhysicalTable, keys: readonly DatabaseKey[]): Promise<readonly (StoredRow | null)[]>;
|
|
122
|
+
/**
|
|
123
|
+
* Index range read; loops native pages until `limit` rows or the range ends, never a short page.
|
|
124
|
+
* A row read from a key-value index copy may lack `_v`; the engine reads it whole with `get` where it guards.
|
|
125
|
+
*/
|
|
126
|
+
query(table: PhysicalTable, request: QueryRequest): Promise<readonly StoredRow[]>;
|
|
127
|
+
write(ops: readonly WriteOp[]): Promise<WriteResult>;
|
|
128
|
+
/** Called only by `@hot-updater/server/db` tooling. */
|
|
129
|
+
readonly migrations?: {
|
|
130
|
+
apply(tables: readonly PhysicalTable[]): Promise<void>;
|
|
131
|
+
};
|
|
132
|
+
dispose?(): Promise<void>;
|
|
133
|
+
}
|
|
134
|
+
declare const DATABASE_VERSION_COLUMN = "_v";
|
|
135
|
+
declare const DATABASE_MAX_QUERY_LIMIT = 500;
|
|
136
|
+
declare const DATABASE_MAX_MULTI_VALUES = 16;
|
|
137
|
+
declare class DatabaseSchemaError extends Error {
|
|
138
|
+
readonly name = "DatabaseSchemaError";
|
|
139
|
+
}
|
|
140
|
+
declare const findPhysicalColumn: (table: PhysicalTable, name: string) => PhysicalColumn;
|
|
141
|
+
declare const findPhysicalIndex: (table: PhysicalTable, name: string) => PhysicalIndex;
|
|
142
|
+
/**
|
|
143
|
+
* The columns that order an index read and close its cursors: the sort
|
|
144
|
+
* columns, then the key columns that neither `eq` nor `sort` fixes.
|
|
145
|
+
*/
|
|
146
|
+
declare const indexOrderColumns: (table: PhysicalTable, index: PhysicalIndex) => readonly string[];
|
|
147
|
+
/** The primary key of a row, in key-column order. */
|
|
148
|
+
declare const rowKey: (table: PhysicalTable, row: StoredRow) => DatabaseKey;
|
|
149
|
+
//#endregion
|
|
150
|
+
export { DATABASE_MAX_MULTI_VALUES, DATABASE_MAX_QUERY_LIMIT, DATABASE_VERSION_COLUMN, DatabaseAdapter, DatabaseJson, DatabaseKey, DatabaseKeyValue, DatabaseSchemaError, DatabaseValue, PhysicalColumn, PhysicalColumnType, PhysicalIndex, PhysicalTable, QueryBound, QueryRequest, StoredRow, WriteGuard, WriteOp, WriteResult, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
/** The primary key of a row, in key-column order. */
|
|
24
|
+
const rowKey = (table, row) => table.key.map((column) => {
|
|
25
|
+
const value = row[column];
|
|
26
|
+
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.`);
|
|
27
|
+
return value;
|
|
28
|
+
});
|
|
29
|
+
//#endregion
|
|
30
|
+
export { DATABASE_MAX_MULTI_VALUES, DATABASE_MAX_QUERY_LIMIT, DATABASE_VERSION_COLUMN, DatabaseSchemaError, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
migrations: { async apply(tables) {
|
|
89
|
+
const next = new Map(state);
|
|
90
|
+
for (const table of tables) {
|
|
91
|
+
const name = prefix + table.name;
|
|
92
|
+
next.set(name, next.get(name) ?? /* @__PURE__ */ new Map());
|
|
93
|
+
}
|
|
94
|
+
state = next;
|
|
95
|
+
} },
|
|
96
|
+
async dispose() {
|
|
97
|
+
state = /* @__PURE__ */ new Map();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
102
|
+
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,102 @@
|
|
|
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
|
+
migrations: { async apply(tables) {
|
|
89
|
+
const next = new Map(state);
|
|
90
|
+
for (const table of tables) {
|
|
91
|
+
const name = prefix + table.name;
|
|
92
|
+
next.set(name, next.get(name) ?? /* @__PURE__ */ new Map());
|
|
93
|
+
}
|
|
94
|
+
state = next;
|
|
95
|
+
} },
|
|
96
|
+
async dispose() {
|
|
97
|
+
state = /* @__PURE__ */ new Map();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
102
|
+
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 };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DatabaseKey, DatabaseKeyValue, DatabaseValue, PhysicalColumn, PhysicalIndex, PhysicalTable, QueryRequest, StoredRow } from "./adapter.mjs";
|
|
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 };
|