@sfxcode/nuxt-ui-mongocamp 1.3.5 → 1.5.0
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 +1 -1
- package/dist/module.d.mts +8 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -0
- package/dist/runtime/components/MongocampAggregationBuilder.d.vue.ts +1 -0
- package/dist/runtime/components/MongocampAggregationBuilder.vue +331 -5
- package/dist/runtime/components/MongocampAggregationBuilder.vue.d.ts +1 -0
- package/dist/runtime/components/MongocampAggregationChart.d.vue.ts +6 -0
- package/dist/runtime/components/MongocampAggregationChart.vue +93 -0
- package/dist/runtime/components/MongocampAggregationChart.vue.d.ts +6 -0
- package/dist/runtime/components/MongocampAggregationStagePreview.d.vue.ts +10 -0
- package/dist/runtime/components/MongocampAggregationStagePreview.vue +149 -0
- package/dist/runtime/components/MongocampAggregationStagePreview.vue.d.ts +10 -0
- package/dist/runtime/components/MongocampBucketFiles.vue +230 -48
- package/dist/runtime/components/MongocampBulkDelete.d.vue.ts +9 -0
- package/dist/runtime/components/MongocampBulkDelete.vue +94 -25
- package/dist/runtime/components/MongocampBulkDelete.vue.d.ts +9 -0
- package/dist/runtime/components/MongocampBulkEdit.d.vue.ts +5 -0
- package/dist/runtime/components/MongocampBulkEdit.vue +41 -3
- package/dist/runtime/components/MongocampBulkEdit.vue.d.ts +5 -0
- package/dist/runtime/components/MongocampChart.d.vue.ts +29 -0
- package/dist/runtime/components/MongocampChart.vue +129 -0
- package/dist/runtime/components/MongocampChart.vue.d.ts +29 -0
- package/dist/runtime/components/MongocampCollectionCharts.d.vue.ts +17 -0
- package/dist/runtime/components/MongocampCollectionCharts.vue +200 -0
- package/dist/runtime/components/MongocampCollectionCharts.vue.d.ts +17 -0
- package/dist/runtime/components/MongocampCollectionCopy.d.vue.ts +23 -0
- package/dist/runtime/components/MongocampCollectionCopy.vue +206 -0
- package/dist/runtime/components/MongocampCollectionCopy.vue.d.ts +23 -0
- package/dist/runtime/components/MongocampCollectionData.d.vue.ts +6 -0
- package/dist/runtime/components/MongocampCollectionData.vue +423 -138
- package/dist/runtime/components/MongocampCollectionData.vue.d.ts +6 -0
- package/dist/runtime/components/MongocampCollectionInfos.vue +106 -238
- package/dist/runtime/components/MongocampCollectionRename.d.vue.ts +22 -0
- package/dist/runtime/components/MongocampCollectionRename.vue +116 -0
- package/dist/runtime/components/MongocampCollectionRename.vue.d.ts +22 -0
- package/dist/runtime/components/MongocampCollections.vue +93 -12
- package/dist/runtime/components/MongocampCommandPalette.d.vue.ts +40 -0
- package/dist/runtime/components/MongocampCommandPalette.vue +135 -0
- package/dist/runtime/components/MongocampCommandPalette.vue.d.ts +40 -0
- package/dist/runtime/components/MongocampConfirmByName.vue +2 -2
- package/dist/runtime/components/MongocampDashboard.d.vue.ts +3 -1
- package/dist/runtime/components/MongocampDashboard.vue +39 -5
- package/dist/runtime/components/MongocampDashboard.vue.d.ts +3 -1
- package/dist/runtime/components/MongocampDatabases.vue +35 -2
- package/dist/runtime/components/MongocampDocumentCompare.d.vue.ts +20 -0
- package/dist/runtime/components/MongocampDocumentCompare.vue +146 -0
- package/dist/runtime/components/MongocampDocumentCompare.vue.d.ts +20 -0
- package/dist/runtime/components/MongocampDocumentView.d.vue.ts +22 -0
- package/dist/runtime/components/MongocampDocumentView.vue +202 -0
- package/dist/runtime/components/MongocampDocumentView.vue.d.ts +22 -0
- package/dist/runtime/components/MongocampErrorAlert.vue +12 -4
- package/dist/runtime/components/MongocampExportAll.d.vue.ts +18 -0
- package/dist/runtime/components/MongocampExportAll.vue +191 -0
- package/dist/runtime/components/MongocampExportAll.vue.d.ts +18 -0
- package/dist/runtime/components/MongocampFilePreview.d.vue.ts +16 -0
- package/dist/runtime/components/MongocampFilePreview.vue +162 -0
- package/dist/runtime/components/MongocampFilePreview.vue.d.ts +16 -0
- package/dist/runtime/components/MongocampFilterBuilder.vue +11 -0
- package/dist/runtime/components/MongocampImport.vue +171 -13
- package/dist/runtime/components/MongocampIndexes.d.vue.ts +18 -0
- package/dist/runtime/components/MongocampIndexes.vue +496 -0
- package/dist/runtime/components/MongocampIndexes.vue.d.ts +18 -0
- package/dist/runtime/components/MongocampJobs.d.vue.ts +6 -1
- package/dist/runtime/components/MongocampJobs.vue +36 -5
- package/dist/runtime/components/MongocampJobs.vue.d.ts +6 -1
- package/dist/runtime/components/MongocampPermissionMatrix.d.vue.ts +12 -0
- package/dist/runtime/components/MongocampPermissionMatrix.vue +301 -0
- package/dist/runtime/components/MongocampPermissionMatrix.vue.d.ts +12 -0
- package/dist/runtime/components/MongocampQueryBar.d.vue.ts +26 -0
- package/dist/runtime/components/MongocampQueryBar.vue +398 -0
- package/dist/runtime/components/MongocampQueryBar.vue.d.ts +26 -0
- package/dist/runtime/components/MongocampRoleGrants.vue +18 -5
- package/dist/runtime/components/MongocampRoles.vue +16 -5
- package/dist/runtime/components/MongocampServerOverview.d.vue.ts +20 -0
- package/dist/runtime/components/MongocampServerOverview.vue +420 -0
- package/dist/runtime/components/MongocampServerOverview.vue.d.ts +20 -0
- package/dist/runtime/components/MongocampSettings.d.vue.ts +28 -1
- package/dist/runtime/components/MongocampSettings.vue +83 -10
- package/dist/runtime/components/MongocampSettings.vue.d.ts +28 -1
- package/dist/runtime/components/MongocampUsers.vue +19 -6
- package/dist/runtime/composables/useMongocampAdmin.d.ts +2 -1
- package/dist/runtime/composables/useMongocampAdmin.js +6 -1
- package/dist/runtime/composables/useMongocampAdminGate.d.ts +12 -0
- package/dist/runtime/composables/useMongocampAdminGate.js +12 -0
- package/dist/runtime/composables/useMongocampAggregation.d.ts +23 -1
- package/dist/runtime/composables/useMongocampAggregation.js +41 -26
- package/dist/runtime/composables/useMongocampAutoRefresh.d.ts +26 -0
- package/dist/runtime/composables/useMongocampAutoRefresh.js +78 -0
- package/dist/runtime/composables/useMongocampBucket.d.ts +13 -1
- package/dist/runtime/composables/useMongocampBucket.js +34 -0
- package/dist/runtime/composables/useMongocampBulkByFilter.d.ts +15 -0
- package/dist/runtime/composables/useMongocampBulkByFilter.js +32 -0
- package/dist/runtime/composables/useMongocampClientApi.js +2 -1
- package/dist/runtime/composables/useMongocampCollectionCopy.d.ts +45 -0
- package/dist/runtime/composables/useMongocampCollectionCopy.js +48 -0
- package/dist/runtime/composables/useMongocampCollectionStats.d.ts +10 -0
- package/dist/runtime/composables/useMongocampCollectionStats.js +17 -0
- package/dist/runtime/composables/useMongocampError.d.ts +2 -1
- package/dist/runtime/composables/useMongocampError.js +16 -1
- package/dist/runtime/composables/useMongocampExportAll.d.ts +53 -0
- package/dist/runtime/composables/useMongocampExportAll.js +57 -0
- package/dist/runtime/composables/useMongocampExtendedJson.d.ts +7 -2
- package/dist/runtime/composables/useMongocampExtendedJson.js +44 -11
- package/dist/runtime/composables/useMongocampFetchAll.d.ts +25 -0
- package/dist/runtime/composables/useMongocampFetchAll.js +27 -0
- package/dist/runtime/composables/useMongocampFieldTypes.d.ts +24 -0
- package/dist/runtime/composables/useMongocampFieldTypes.js +44 -0
- package/dist/runtime/composables/useMongocampImport.d.ts +4 -0
- package/dist/runtime/composables/useMongocampImport.js +5 -2
- package/dist/runtime/composables/useMongocampIndex.d.ts +6 -1
- package/dist/runtime/composables/useMongocampIndex.js +7 -1
- package/dist/runtime/composables/useMongocampPermissions.d.ts +17 -0
- package/dist/runtime/composables/useMongocampPermissions.js +43 -0
- package/dist/runtime/composables/useMongocampPersistedState.d.ts +15 -0
- package/dist/runtime/composables/useMongocampPersistedState.js +21 -0
- package/dist/runtime/composables/useMongocampProxyApi.js +2 -1
- package/dist/runtime/composables/useMongocampQuery.d.ts +50 -2
- package/dist/runtime/composables/useMongocampQuery.js +72 -1
- package/dist/runtime/composables/useMongocampSavedPipelines.d.ts +25 -0
- package/dist/runtime/composables/useMongocampSavedPipelines.js +23 -0
- package/dist/runtime/composables/useMongocampSavedQueries.d.ts +37 -0
- package/dist/runtime/composables/useMongocampSavedQueries.js +50 -0
- package/dist/runtime/composables/useMongocampServerOverview.d.ts +35 -0
- package/dist/runtime/composables/useMongocampServerOverview.js +51 -0
- package/dist/runtime/composables/useMongocampSession.d.ts +2 -0
- package/dist/runtime/composables/useMongocampSession.js +16 -2
- package/dist/runtime/composables/useMongocampSystem.d.ts +7 -2
- package/dist/runtime/composables/useMongocampSystem.js +6 -2
- package/dist/runtime/locales/de.json +509 -25
- package/dist/runtime/locales/en.json +509 -25
- package/dist/runtime/plugin.js +6 -3
- package/dist/runtime/utils/aggregationErrors.d.ts +4 -0
- package/dist/runtime/utils/aggregationErrors.js +7 -0
- package/dist/runtime/utils/aggregationPipeline.d.ts +5 -4
- package/dist/runtime/utils/aggregationPipeline.js +23 -9
- package/dist/runtime/utils/aggregationPreview.d.ts +13 -0
- package/dist/runtime/utils/aggregationPreview.js +10 -0
- package/dist/runtime/utils/aggregationResult.d.ts +5 -0
- package/dist/runtime/utils/aggregationResult.js +4 -0
- package/dist/runtime/utils/aggregationTemplates.d.ts +34 -0
- package/dist/runtime/utils/aggregationTemplates.js +47 -0
- package/dist/runtime/utils/analyzeSamples.d.ts +1 -0
- package/dist/runtime/utils/analyzeSamples.js +1 -1
- package/dist/runtime/utils/autoRefresh.d.ts +14 -0
- package/dist/runtime/utils/autoRefresh.js +15 -0
- package/dist/runtime/utils/batchInsert.d.ts +6 -0
- package/dist/runtime/utils/batchInsert.js +31 -3
- package/dist/runtime/utils/bigIntJson.d.ts +15 -0
- package/dist/runtime/utils/bigIntJson.js +55 -0
- package/dist/runtime/utils/bsonTypes.d.ts +10 -0
- package/dist/runtime/utils/bsonTypes.js +42 -0
- package/dist/runtime/utils/bulkDelete.d.ts +9 -1
- package/dist/runtime/utils/bulkDelete.js +15 -3
- package/dist/runtime/utils/bulkEdit.d.ts +6 -0
- package/dist/runtime/utils/bulkEdit.js +7 -0
- package/dist/runtime/utils/chartData.d.ts +11 -0
- package/dist/runtime/utils/chartData.js +9 -0
- package/dist/runtime/utils/collectionChart.d.ts +36 -0
- package/dist/runtime/utils/collectionChart.js +73 -0
- package/dist/runtime/utils/collectionCopy.d.ts +24 -0
- package/dist/runtime/utils/collectionCopy.js +37 -0
- package/dist/runtime/utils/collectionStats.d.ts +31 -0
- package/dist/runtime/utils/collectionStats.js +42 -0
- package/dist/runtime/utils/commandPalette.d.ts +48 -0
- package/dist/runtime/utils/commandPalette.js +39 -0
- package/dist/runtime/utils/configRows.d.ts +7 -0
- package/dist/runtime/utils/configRows.js +4 -0
- package/dist/runtime/utils/documentCompare.d.ts +31 -0
- package/dist/runtime/utils/documentCompare.js +71 -0
- package/dist/runtime/utils/documentDiff.d.ts +19 -0
- package/dist/runtime/utils/documentDiff.js +71 -0
- package/dist/runtime/utils/documentPaths.d.ts +41 -0
- package/dist/runtime/utils/documentPaths.js +139 -0
- package/dist/runtime/utils/documentTree.d.ts +40 -0
- package/dist/runtime/utils/documentTree.js +102 -0
- package/dist/runtime/utils/exportDocuments.d.ts +33 -0
- package/dist/runtime/utils/exportDocuments.js +53 -0
- package/dist/runtime/utils/exportRows.d.ts +2 -0
- package/dist/runtime/utils/exportRows.js +6 -3
- package/dist/runtime/utils/fieldDetails.d.ts +51 -0
- package/dist/runtime/utils/fieldDetails.js +113 -0
- package/dist/runtime/utils/filePreview.d.ts +27 -0
- package/dist/runtime/utils/filePreview.js +50 -0
- package/dist/runtime/utils/filterBuilder.d.ts +6 -1
- package/dist/runtime/utils/filterBuilder.js +6 -5
- package/dist/runtime/utils/formatBytes.d.ts +2 -0
- package/dist/runtime/utils/formatBytes.js +11 -0
- package/dist/runtime/utils/grantMatching.d.ts +8 -0
- package/dist/runtime/utils/grantMatching.js +15 -0
- package/dist/runtime/utils/importRows.js +12 -2
- package/dist/runtime/utils/indexForm.d.ts +83 -0
- package/dist/runtime/utils/indexForm.js +74 -0
- package/dist/runtime/utils/luceneExpression.d.ts +15 -0
- package/dist/runtime/utils/luceneExpression.js +74 -0
- package/dist/runtime/utils/pagedFetch.d.ts +46 -0
- package/dist/runtime/utils/pagedFetch.js +58 -0
- package/dist/runtime/utils/paginationHeaders.d.ts +4 -0
- package/dist/runtime/utils/paginationHeaders.js +4 -0
- package/dist/runtime/utils/permissionDenied.d.ts +10 -0
- package/dist/runtime/utils/permissionDenied.js +23 -0
- package/dist/runtime/utils/permissionMatrix.d.ts +55 -0
- package/dist/runtime/utils/permissionMatrix.js +85 -0
- package/dist/runtime/utils/persistedState.d.ts +25 -0
- package/dist/runtime/utils/persistedState.js +47 -0
- package/dist/runtime/utils/queryState.d.ts +45 -0
- package/dist/runtime/utils/queryState.js +75 -0
- package/dist/runtime/utils/requestLimits.d.ts +2 -0
- package/dist/runtime/utils/requestLimits.js +4 -0
- package/dist/runtime/utils/resultChart.d.ts +32 -0
- package/dist/runtime/utils/resultChart.js +59 -0
- package/dist/runtime/utils/savedPipelines.d.ts +22 -0
- package/dist/runtime/utils/savedPipelines.js +46 -0
- package/dist/runtime/utils/savedQueries.d.ts +60 -0
- package/dist/runtime/utils/savedQueries.js +143 -0
- package/dist/runtime/utils/schemaAnalysis.d.ts +16 -0
- package/dist/runtime/utils/schemaAnalysis.js +57 -0
- package/dist/runtime/utils/serverError.d.ts +19 -0
- package/dist/runtime/utils/serverError.js +19 -0
- package/dist/runtime/utils/serverOverview.d.ts +47 -0
- package/dist/runtime/utils/serverOverview.js +40 -0
- package/dist/runtime/utils/sessionGuard.d.ts +3 -0
- package/dist/runtime/utils/sessionGuard.js +18 -1
- package/package.json +15 -14
|
@@ -29,7 +29,12 @@ export async function runBulkUpdate(ids, updateMany, options = {}) {
|
|
|
29
29
|
const result = { updated: [], failed: [] };
|
|
30
30
|
const addressable = ids.filter((id) => isObjectIdHex(id));
|
|
31
31
|
const others = ids.filter((id) => !isObjectIdHex(id));
|
|
32
|
+
let done = 0;
|
|
32
33
|
for (let start = 0; start < addressable.length; start += chunkSize) {
|
|
34
|
+
if (options.signal?.aborted) {
|
|
35
|
+
result.notAttempted = addressable.slice(start);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
33
38
|
const chunk = addressable.slice(start, start + chunkSize);
|
|
34
39
|
try {
|
|
35
40
|
const response = await updateMany(chunk);
|
|
@@ -39,6 +44,8 @@ export async function runBulkUpdate(ids, updateMany, options = {}) {
|
|
|
39
44
|
} catch (error) {
|
|
40
45
|
result.failed.push({ ids: chunk, error });
|
|
41
46
|
}
|
|
47
|
+
done += chunk.length;
|
|
48
|
+
options.onProgress?.(done, ids.length);
|
|
42
49
|
}
|
|
43
50
|
for (const id of others) result.failed.push({ ids: [id], error: new BulkUnaddressableId(id) });
|
|
44
51
|
return result;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface LabelledValue {
|
|
2
|
+
label: string;
|
|
3
|
+
value: number;
|
|
4
|
+
}
|
|
5
|
+
/** Shortens a label for an axis (`very-long-name` → `very-long-na…`); the full text stays in the data table. */
|
|
6
|
+
export declare function shortenLabel(label: string, max?: number): string;
|
|
7
|
+
/**
|
|
8
|
+
* The `n` biggest items plus one summed "other" item for the rest (only when something is left over).
|
|
9
|
+
* Keeps the input order of the top items by value, biggest first.
|
|
10
|
+
*/
|
|
11
|
+
export declare function topNWithOther(items: LabelledValue[], n: number, otherLabel: string): LabelledValue[];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function shortenLabel(label, max = 24) {
|
|
2
|
+
return label.length > max ? `${label.slice(0, Math.max(1, max - 1))}\u2026` : label;
|
|
3
|
+
}
|
|
4
|
+
export function topNWithOther(items, n, otherLabel) {
|
|
5
|
+
const sorted = [...items].sort((a, b) => b.value - a.value);
|
|
6
|
+
if (sorted.length <= n) return sorted;
|
|
7
|
+
const rest = sorted.slice(n).reduce((sum, item) => sum + item.value, 0);
|
|
8
|
+
return [...sorted.slice(0, n), { label: otherLabel, value: rest }];
|
|
9
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { RequestStage } from './aggregationPipeline.js';
|
|
2
|
+
import type { DateUnit } from './aggregationTemplates.js';
|
|
3
|
+
import type { SampleAnalysis, ValueKind } from './analyzeSamples.js';
|
|
4
|
+
export type ChartKind = 'values' | 'dates' | 'numbers';
|
|
5
|
+
export interface ChartPoint {
|
|
6
|
+
label: string;
|
|
7
|
+
count: number;
|
|
8
|
+
}
|
|
9
|
+
export declare const DEFAULT_TOP = 10;
|
|
10
|
+
export declare const DEFAULT_BUCKETS = 10;
|
|
11
|
+
/** Periods of a date histogram that are drawn; more are cut (the first ones are kept) and the chart says so. */
|
|
12
|
+
export declare const MAX_PERIODS = 100;
|
|
13
|
+
/** Up to this many bars a date histogram is a bar chart, beyond it an area chart reads better. */
|
|
14
|
+
export declare const MAX_BAR_PERIODS = 36;
|
|
15
|
+
/** The chart a field of this (dominant) value kind gets, or `undefined` when none makes sense (objects, arrays, only nulls). */
|
|
16
|
+
export declare function chartKindFor(kind: ValueKind | undefined): ChartKind | undefined;
|
|
17
|
+
/** The most frequent value kind of a field in a sample analysis, ignoring `null`; `undefined` for an unknown field. */
|
|
18
|
+
export declare function dominantKind(analysis: SampleAnalysis, field: string): ValueKind | undefined;
|
|
19
|
+
export interface ChartRequest {
|
|
20
|
+
pipeline: RequestStage[];
|
|
21
|
+
/** How many rows the single page holds. */
|
|
22
|
+
rowsPerPage: number;
|
|
23
|
+
}
|
|
24
|
+
export interface ChartOptions {
|
|
25
|
+
top?: number;
|
|
26
|
+
buckets?: number;
|
|
27
|
+
unit?: DateUnit;
|
|
28
|
+
}
|
|
29
|
+
/** The request for a chart, or `undefined` when the field name cannot be used in a pipeline. */
|
|
30
|
+
export declare function chartRequest(kind: ChartKind, field: string, options?: ChartOptions): ChartRequest | undefined;
|
|
31
|
+
/** Rows of the aggregation as chart points. `noneLabel` names the group of documents without a value. */
|
|
32
|
+
export declare function rowsToPoints(kind: ChartKind, rows: Record<string, unknown>[], noneLabel: string): ChartPoint[];
|
|
33
|
+
/** Which UChart type draws it. */
|
|
34
|
+
export declare function chartTypeFor(kind: ChartKind, points: number): 'bar' | 'area';
|
|
35
|
+
/** The highest point (the first one on a tie), for the text description of a chart. */
|
|
36
|
+
export declare function highestPoint(points: ChartPoint[]): ChartPoint | undefined;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { DATE_FORMATS, isTemplateField } from "./aggregationTemplates.js";
|
|
2
|
+
export const DEFAULT_TOP = 10;
|
|
3
|
+
export const DEFAULT_BUCKETS = 10;
|
|
4
|
+
export const MAX_PERIODS = 100;
|
|
5
|
+
export const MAX_BAR_PERIODS = 36;
|
|
6
|
+
export function chartKindFor(kind) {
|
|
7
|
+
switch (kind) {
|
|
8
|
+
case "string":
|
|
9
|
+
case "boolean":
|
|
10
|
+
return "values";
|
|
11
|
+
case "date":
|
|
12
|
+
return "dates";
|
|
13
|
+
case "number":
|
|
14
|
+
return "numbers";
|
|
15
|
+
default:
|
|
16
|
+
return void 0;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function dominantKind(analysis, field) {
|
|
20
|
+
const stat = analysis.fields.find((f) => f.path === field);
|
|
21
|
+
return stat?.types.find((t) => t.type !== "null")?.type;
|
|
22
|
+
}
|
|
23
|
+
export function chartRequest(kind, field, options = {}) {
|
|
24
|
+
if (!isTemplateField(field)) return void 0;
|
|
25
|
+
const name = field.trim();
|
|
26
|
+
const path = `$${name}`;
|
|
27
|
+
if (kind === "values") {
|
|
28
|
+
return {
|
|
29
|
+
pipeline: [{ stage: "$group", value: { _id: path, count: { $sum: 1 } } }, { stage: "$sort", value: { count: -1, _id: 1 } }],
|
|
30
|
+
rowsPerPage: options.top ?? DEFAULT_TOP
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (kind === "dates") {
|
|
34
|
+
return {
|
|
35
|
+
pipeline: [
|
|
36
|
+
{ stage: "$match", value: { [name]: { $type: "date" } } },
|
|
37
|
+
{ stage: "$group", value: { _id: { $dateToString: { format: DATE_FORMATS[options.unit ?? "month"], date: path } }, count: { $sum: 1 } } },
|
|
38
|
+
{ stage: "$sort", value: { _id: 1 } }
|
|
39
|
+
],
|
|
40
|
+
rowsPerPage: MAX_PERIODS
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
pipeline: [
|
|
45
|
+
{ stage: "$match", value: { [name]: { $type: "number" } } },
|
|
46
|
+
{ stage: "$bucketAuto", value: { groupBy: path, buckets: options.buckets ?? DEFAULT_BUCKETS } }
|
|
47
|
+
],
|
|
48
|
+
rowsPerPage: options.buckets ?? DEFAULT_BUCKETS
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function text(value) {
|
|
52
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
53
|
+
}
|
|
54
|
+
function countOf(row) {
|
|
55
|
+
const n = Number(row.count);
|
|
56
|
+
return Number.isFinite(n) ? n : 0;
|
|
57
|
+
}
|
|
58
|
+
export function rowsToPoints(kind, rows, noneLabel) {
|
|
59
|
+
return rows.map((row) => {
|
|
60
|
+
const id = row._id;
|
|
61
|
+
if (kind === "numbers") {
|
|
62
|
+
const range = typeof id === "object" && id !== null ? id : {};
|
|
63
|
+
return { label: `${text(range.min)}\u2013${text(range.max)}`, count: countOf(row) };
|
|
64
|
+
}
|
|
65
|
+
return { label: id === null || id === void 0 ? noneLabel : text(id), count: countOf(row) };
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export function chartTypeFor(kind, points) {
|
|
69
|
+
return kind === "dates" && points > MAX_BAR_PERIODS ? "area" : "bar";
|
|
70
|
+
}
|
|
71
|
+
export function highestPoint(points) {
|
|
72
|
+
return points.reduce((best, p) => !best || p.count > best.count ? p : best, void 0);
|
|
73
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const MAX_COLLECTION_NAME_LENGTH = 100;
|
|
2
|
+
export type TargetNameProblem = 'empty' | 'too-long' | 'invalid-characters' | 'protected' | 'same-as-source' | 'exists';
|
|
3
|
+
/**
|
|
4
|
+
* What is wrong with a name for the new collection, or `undefined` when it is fine. `existing` is every collection the
|
|
5
|
+
* server lists; a name equal to one of them ignoring case counts as existing (grants match case-insensitively, so two
|
|
6
|
+
* names that differ only in case would be confusing even though MongoDB keeps them apart).
|
|
7
|
+
*/
|
|
8
|
+
export declare function validateTargetName(name: string, source: string, existing: readonly string[]): TargetNameProblem | undefined;
|
|
9
|
+
/** A collection that may be copied: not MongoCamp's own, not a GridFS half (those need their other half). */
|
|
10
|
+
export declare function canCopyCollection(name: string): boolean;
|
|
11
|
+
/** `20261104_153045` — local date and time, sortable, safe in a collection name. */
|
|
12
|
+
export declare function backupStamp(now: Date): string;
|
|
13
|
+
/**
|
|
14
|
+
* The name of a quick backup of `source`: `<source>_backup_<yyyymmdd>_<hhmmss>`. Characters a new name may not contain are
|
|
15
|
+
* replaced by `_`, the source part is shortened to stay within the length limit, and when the name already exists (two
|
|
16
|
+
* backups in the same second, or one made earlier) `_2`, `_3`, … is appended. The result always passes `validateTargetName`
|
|
17
|
+
* for an ordinary source; for a source that cannot be copied (`canCopyCollection`) there is no backup name.
|
|
18
|
+
*/
|
|
19
|
+
export declare function backupName(source: string, existing: readonly string[], now?: Date): string;
|
|
20
|
+
/** Thrown for a source that cannot be copied at all (MongoCamp's own collections, GridFS halves, `system.*`). */
|
|
21
|
+
export declare class CollectionNotCopyableError extends Error {
|
|
22
|
+
collection: string;
|
|
23
|
+
constructor(collection: string);
|
|
24
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isProtectedCollection } from "./collectionProtection.js";
|
|
2
|
+
export const MAX_COLLECTION_NAME_LENGTH = 100;
|
|
3
|
+
const ALLOWED_NAME = /^\w[\w.-]*$/;
|
|
4
|
+
export function validateTargetName(name, source, existing) {
|
|
5
|
+
const target = name.trim();
|
|
6
|
+
if (target === "") return "empty";
|
|
7
|
+
if (target.length > MAX_COLLECTION_NAME_LENGTH) return "too-long";
|
|
8
|
+
if (!ALLOWED_NAME.test(target)) return "invalid-characters";
|
|
9
|
+
if (isProtectedCollection(target) || target.startsWith("system.")) return "protected";
|
|
10
|
+
if (target.toLowerCase() === source.toLowerCase()) return "same-as-source";
|
|
11
|
+
if (existing.some((e) => e.toLowerCase() === target.toLowerCase())) return "exists";
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
export function canCopyCollection(name) {
|
|
15
|
+
return !isProtectedCollection(name) && !name.startsWith("system.");
|
|
16
|
+
}
|
|
17
|
+
const pad = (n, width = 2) => String(n).padStart(width, "0");
|
|
18
|
+
export function backupStamp(now) {
|
|
19
|
+
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}_${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
20
|
+
}
|
|
21
|
+
export function backupName(source, existing, now = /* @__PURE__ */ new Date()) {
|
|
22
|
+
const taken = new Set(existing.map((e) => e.toLowerCase()));
|
|
23
|
+
const cleaned = source.replace(/[^\w.-]/g, "_").replace(/^[.-]+/, "") || "collection";
|
|
24
|
+
const stamp = backupStamp(now);
|
|
25
|
+
for (let attempt = 1; ; attempt++) {
|
|
26
|
+
const suffix = `_backup_${stamp}${attempt > 1 ? `_${attempt}` : ""}`;
|
|
27
|
+
const name = `${cleaned.slice(0, MAX_COLLECTION_NAME_LENGTH - suffix.length)}${suffix}`;
|
|
28
|
+
if (!taken.has(name.toLowerCase())) return name;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class CollectionNotCopyableError extends Error {
|
|
32
|
+
constructor(collection) {
|
|
33
|
+
super(`Collection "${collection}" cannot be copied`);
|
|
34
|
+
this.collection = collection;
|
|
35
|
+
this.name = "CollectionNotCopyableError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { CollectionStatus } from '@sfxcode/nuxt-mongocamp-server';
|
|
2
|
+
export interface CollectionStorage {
|
|
3
|
+
collectionType: string;
|
|
4
|
+
/** Number of documents. */
|
|
5
|
+
count: number;
|
|
6
|
+
/** Uncompressed size of the documents, in bytes. */
|
|
7
|
+
size: number;
|
|
8
|
+
/** Size on disk (compressed), in bytes. */
|
|
9
|
+
storageSize: number;
|
|
10
|
+
avgObjSize: number;
|
|
11
|
+
indexCount: number;
|
|
12
|
+
/** Bytes per index name. */
|
|
13
|
+
indexSizes: Record<string, number>;
|
|
14
|
+
totalIndexSize: number;
|
|
15
|
+
}
|
|
16
|
+
export interface IndexUsage {
|
|
17
|
+
name: string;
|
|
18
|
+
/** The indexed fields, e.g. `{ name: 1 }`. */
|
|
19
|
+
key: Record<string, unknown>;
|
|
20
|
+
/** Operations that used the index since `since` (counted per server start). */
|
|
21
|
+
ops: number;
|
|
22
|
+
/** ISO time the counter started; `null` when the server did not say. */
|
|
23
|
+
since: string | null;
|
|
24
|
+
/** No operation has used it since `since` — a candidate to drop (never the `_id_` index). */
|
|
25
|
+
unused: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** A non-negative finite number, else 0 (a missing or malformed number is not worth failing a card for). */
|
|
28
|
+
export declare function toCount(value: unknown): number;
|
|
29
|
+
export declare function mapStorage(status: Partial<CollectionStatus>): CollectionStorage;
|
|
30
|
+
/** Rows of a `$indexStats` aggregation (already unwrapped, `host`/`ns` stripped) as typed usage; rows without a name are skipped. */
|
|
31
|
+
export declare function mapIndexUsage(rows: Array<Record<string, unknown>>): IndexUsage[];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function toCount(value) {
|
|
2
|
+
const n = typeof value === "string" && /^\d+$/.test(value) ? Number(value) : value;
|
|
3
|
+
return typeof n === "number" && Number.isFinite(n) && n >= 0 ? n : 0;
|
|
4
|
+
}
|
|
5
|
+
export function mapStorage(status) {
|
|
6
|
+
const indexSizes = {};
|
|
7
|
+
for (const [name, size] of Object.entries(status.indexSizes ?? {})) indexSizes[name] = toCount(size);
|
|
8
|
+
return {
|
|
9
|
+
collectionType: typeof status.collectionType === "string" ? status.collectionType : "",
|
|
10
|
+
count: toCount(status.count),
|
|
11
|
+
size: toCount(status.size),
|
|
12
|
+
storageSize: toCount(status.storageSize),
|
|
13
|
+
avgObjSize: toCount(status.avgObjSize),
|
|
14
|
+
indexCount: toCount(status.nindexes),
|
|
15
|
+
indexSizes,
|
|
16
|
+
totalIndexSize: toCount(status.totalIndexSize)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function isRecord(value) {
|
|
20
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
21
|
+
}
|
|
22
|
+
function isoOrNull(value) {
|
|
23
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? null : value.toISOString();
|
|
24
|
+
if (typeof value === "string" && !Number.isNaN(Date.parse(value))) return value;
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
export function mapIndexUsage(rows) {
|
|
28
|
+
const out = [];
|
|
29
|
+
for (const row of rows) {
|
|
30
|
+
if (typeof row.name !== "string" || row.name === "") continue;
|
|
31
|
+
const accesses = isRecord(row.accesses) ? row.accesses : {};
|
|
32
|
+
const ops = toCount(accesses.ops);
|
|
33
|
+
out.push({
|
|
34
|
+
name: row.name,
|
|
35
|
+
key: isRecord(row.key) ? row.key : {},
|
|
36
|
+
ops,
|
|
37
|
+
since: isoOrNull(accesses.since),
|
|
38
|
+
unused: ops === 0
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type PaletteKind = 'collection' | 'bucket' | 'user' | 'role' | 'job' | 'action';
|
|
2
|
+
export interface PaletteEntry {
|
|
3
|
+
/** Unique, e.g. `collection:orders` */
|
|
4
|
+
id: string;
|
|
5
|
+
kind: PaletteKind;
|
|
6
|
+
label: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
icon: string;
|
|
9
|
+
/** Route to open; absent for actions without a page */
|
|
10
|
+
to?: string;
|
|
11
|
+
/** Set for actions that are not navigation (`refresh`, `logout`) */
|
|
12
|
+
action?: 'refresh' | 'logout';
|
|
13
|
+
}
|
|
14
|
+
export interface PalettePaths {
|
|
15
|
+
/** `<collectionPath>/<name>/data` */
|
|
16
|
+
collectionPath: string;
|
|
17
|
+
/** `<bucketPath>/<bucket>` */
|
|
18
|
+
bucketPath: string;
|
|
19
|
+
usersPath: string;
|
|
20
|
+
/** `<rolePath>/<name>` (the grants page) */
|
|
21
|
+
rolePath: string;
|
|
22
|
+
jobsPath: string;
|
|
23
|
+
settingsPath: string;
|
|
24
|
+
accountPath: string;
|
|
25
|
+
}
|
|
26
|
+
export interface PaletteData {
|
|
27
|
+
collections?: string[];
|
|
28
|
+
users?: string[];
|
|
29
|
+
roles?: string[];
|
|
30
|
+
jobs?: {
|
|
31
|
+
name: string;
|
|
32
|
+
group: string;
|
|
33
|
+
}[];
|
|
34
|
+
}
|
|
35
|
+
/** A cache entry is reusable while younger than the TTL; a clock that went backwards counts as stale. */
|
|
36
|
+
export declare function isFresh(loadedAt: number | undefined, now: number, ttlMs: number): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Everything the palette can jump to. `canRead` hides collections the user may not read (UI-level, fail-open);
|
|
39
|
+
* `mc_*` collections and GridFS halves never appear as collections — a `<name>.files` becomes a bucket entry.
|
|
40
|
+
*/
|
|
41
|
+
export declare function buildEntries(data: PaletteData, paths: PalettePaths, options?: {
|
|
42
|
+
canRead?: (collection: string) => boolean;
|
|
43
|
+
}): PaletteEntry[];
|
|
44
|
+
/** The fixed actions; settings is for administrators only. */
|
|
45
|
+
export declare function buildActions(paths: PalettePaths, options: {
|
|
46
|
+
isAdmin: boolean;
|
|
47
|
+
canLogout?: boolean;
|
|
48
|
+
}): PaletteEntry[];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isBucketMemberCollection } from "./collectionProtection.js";
|
|
2
|
+
const join = (base, ...parts) => [base.replace(/\/+$/, ""), ...parts.map(encodeURIComponent)].join("/");
|
|
3
|
+
export function isFresh(loadedAt, now, ttlMs) {
|
|
4
|
+
return loadedAt !== void 0 && now >= loadedAt && now - loadedAt < ttlMs;
|
|
5
|
+
}
|
|
6
|
+
export function buildEntries(data, paths, options = {}) {
|
|
7
|
+
const canRead = options.canRead ?? (() => true);
|
|
8
|
+
const entries = [];
|
|
9
|
+
for (const name of data.collections ?? []) {
|
|
10
|
+
if (name.startsWith("mc_")) continue;
|
|
11
|
+
if (name.endsWith(".files")) {
|
|
12
|
+
if (!canRead(name)) continue;
|
|
13
|
+
const bucket = name.slice(0, -".files".length);
|
|
14
|
+
entries.push({ id: `bucket:${bucket}`, kind: "bucket", label: bucket, icon: "i-lucide-folder-archive", to: join(paths.bucketPath, bucket) });
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (isBucketMemberCollection(name) || !canRead(name)) continue;
|
|
18
|
+
entries.push({ id: `collection:${name}`, kind: "collection", label: name, icon: "i-lucide-database", to: join(paths.collectionPath, name, "data") });
|
|
19
|
+
}
|
|
20
|
+
for (const user of data.users ?? []) {
|
|
21
|
+
entries.push({ id: `user:${user}`, kind: "user", label: user, icon: "i-lucide-user", to: paths.usersPath });
|
|
22
|
+
}
|
|
23
|
+
for (const role of data.roles ?? []) {
|
|
24
|
+
entries.push({ id: `role:${role}`, kind: "role", label: role, icon: "i-lucide-shield", to: join(paths.rolePath, role) });
|
|
25
|
+
}
|
|
26
|
+
for (const job of data.jobs ?? []) {
|
|
27
|
+
entries.push({ id: `job:${job.group}/${job.name}`, kind: "job", label: job.name, description: job.group, icon: "i-lucide-clock", to: paths.jobsPath });
|
|
28
|
+
}
|
|
29
|
+
return entries;
|
|
30
|
+
}
|
|
31
|
+
export function buildActions(paths, options) {
|
|
32
|
+
const actions = [
|
|
33
|
+
{ id: "action:account", kind: "action", label: "account", icon: "i-lucide-user-cog", to: paths.accountPath }
|
|
34
|
+
];
|
|
35
|
+
if (options.isAdmin) actions.push({ id: "action:settings", kind: "action", label: "settings", icon: "i-lucide-settings", to: paths.settingsPath });
|
|
36
|
+
actions.push({ id: "action:refresh", kind: "action", label: "refresh", icon: "i-lucide-refresh-cw", action: "refresh" });
|
|
37
|
+
if (options.canLogout ?? true) actions.push({ id: "action:logout", kind: "action", label: "logout", icon: "i-lucide-log-out", action: "logout" });
|
|
38
|
+
return actions;
|
|
39
|
+
}
|
|
@@ -9,3 +9,10 @@ export interface ConfigRow {
|
|
|
9
9
|
}
|
|
10
10
|
export declare function isSecretConfigKey(key: string): boolean;
|
|
11
11
|
export declare function buildConfigRows(configurations: unknown): ConfigRow[];
|
|
12
|
+
export declare const GRANT_STATUS_KEY = "AUTH_GRANT_DENIED_STATUS";
|
|
13
|
+
/**
|
|
14
|
+
* Whether the server still answers a missing grant with `401` (its default; `403` is planned for the next major version): a
|
|
15
|
+
* generated client clears the session on a 401, which this module works around by turning that 401 into a 403 itself. `false`
|
|
16
|
+
* when the setting is `403`, absent (an older server) or unreadable.
|
|
17
|
+
*/
|
|
18
|
+
export declare function grantStatusHint(rows: readonly ConfigRow[]): boolean;
|
|
@@ -51,3 +51,7 @@ export function buildConfigRows(configurations) {
|
|
|
51
51
|
}
|
|
52
52
|
return rows.sort((a, b) => a.key.localeCompare(b.key));
|
|
53
53
|
}
|
|
54
|
+
export const GRANT_STATUS_KEY = "AUTH_GRANT_DENIED_STATUS";
|
|
55
|
+
export function grantStatusHint(rows) {
|
|
56
|
+
return rows.find((row) => row.key === GRANT_STATUS_KEY)?.value.trim() === "401";
|
|
57
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { PathValueType } from './documentPaths.js';
|
|
2
|
+
export type CompareStatus = 'same' | 'changed' | 'added' | 'removed';
|
|
3
|
+
export interface CompareRow {
|
|
4
|
+
path: string;
|
|
5
|
+
status: CompareStatus;
|
|
6
|
+
/** Plain (unwrapped) values; absent on the side that lacks the path */
|
|
7
|
+
left?: unknown;
|
|
8
|
+
right?: unknown;
|
|
9
|
+
leftType?: PathValueType;
|
|
10
|
+
rightType?: PathValueType;
|
|
11
|
+
/** Same text on both sides but a different BSON type (`1` vs `"1"`, long vs int) */
|
|
12
|
+
typeDiffers?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface CompareResult {
|
|
15
|
+
rows: CompareRow[];
|
|
16
|
+
counts: Record<CompareStatus, number>;
|
|
17
|
+
/** One of the documents was larger than the path limit */
|
|
18
|
+
truncated: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** A leaf as a plain value: wrappers unwrapped, Date → ISO text, BigInt → digits. */
|
|
21
|
+
export declare function plainValue(value: unknown): unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Path-level comparison of two documents. Both are flattened (arrays by index, an extended-JSON wrapper is one leaf),
|
|
24
|
+
* so `{ $oid }`/`{ $date }`/`{ $numberLong }` compare as the plain values the table shows. Rows follow the left
|
|
25
|
+
* document's order, paths only the right one has come after. A key's order inside an object is not a difference.
|
|
26
|
+
*/
|
|
27
|
+
export declare function compareDocuments(left: unknown, right: unknown, options?: {
|
|
28
|
+
maxEntries?: number;
|
|
29
|
+
}): CompareResult;
|
|
30
|
+
/** Short text of a leaf for a table cell. */
|
|
31
|
+
export declare function compareText(value: unknown): string;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { unwrapExtendedJson } from "../composables/useMongocampExtendedJson.js";
|
|
2
|
+
import { flattenDocument } from "./documentPaths.js";
|
|
3
|
+
const FULL_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
4
|
+
export function plainValue(value) {
|
|
5
|
+
if (value instanceof Date) return value.toISOString();
|
|
6
|
+
if (typeof value === "bigint") return value.toString();
|
|
7
|
+
return unwrapExtendedJson(value);
|
|
8
|
+
}
|
|
9
|
+
const WRAPPED_AS_TEXT = /* @__PURE__ */ new Set(["objectId", "date", "long", "decimal"]);
|
|
10
|
+
const NUMERIC = /* @__PURE__ */ new Set(["number", "int", "long", "double", "decimal"]);
|
|
11
|
+
const EXACT_NUMERIC = /* @__PURE__ */ new Set(["int", "long", "double", "decimal"]);
|
|
12
|
+
function typesAgree(a, b) {
|
|
13
|
+
if (a === b) return true;
|
|
14
|
+
if (NUMERIC.has(a) && NUMERIC.has(b)) return !(EXACT_NUMERIC.has(a) && EXACT_NUMERIC.has(b));
|
|
15
|
+
return a === "string" && WRAPPED_AS_TEXT.has(b) || b === "string" && WRAPPED_AS_TEXT.has(a);
|
|
16
|
+
}
|
|
17
|
+
function leafEqual(a, b) {
|
|
18
|
+
if (a === b) return true;
|
|
19
|
+
if (typeof a === "string" && typeof b === "string") {
|
|
20
|
+
return FULL_TIMESTAMP.test(a) && FULL_TIMESTAMP.test(b) && Date.parse(a) === Date.parse(b);
|
|
21
|
+
}
|
|
22
|
+
if (Array.isArray(a) && Array.isArray(b)) return a.length === 0 && b.length === 0;
|
|
23
|
+
if (typeof a === "object" && typeof b === "object" && a && b && !Array.isArray(a) && !Array.isArray(b)) {
|
|
24
|
+
return Object.keys(a).length === 0 && Object.keys(b).length === 0;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
function withoutEmptyRoot(result) {
|
|
29
|
+
return { ...result, entries: result.entries.filter((e) => e.path !== "" || e.type !== "object" && e.type !== "array") };
|
|
30
|
+
}
|
|
31
|
+
export function compareDocuments(left, right, options = {}) {
|
|
32
|
+
const a = withoutEmptyRoot(flattenDocument(left, { maxEntries: options.maxEntries }));
|
|
33
|
+
const b = withoutEmptyRoot(flattenDocument(right, { maxEntries: options.maxEntries }));
|
|
34
|
+
const rightByPath = new Map(b.entries.map((e) => [e.path, e]));
|
|
35
|
+
const seen = /* @__PURE__ */ new Set();
|
|
36
|
+
const rows = [];
|
|
37
|
+
for (const entry of a.entries) {
|
|
38
|
+
seen.add(entry.path);
|
|
39
|
+
const other = rightByPath.get(entry.path);
|
|
40
|
+
const l = plainValue(entry.value);
|
|
41
|
+
if (!other) {
|
|
42
|
+
rows.push({ path: entry.path, status: "removed", left: l, leftType: entry.type });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const r = plainValue(other.value);
|
|
46
|
+
const equal = leafEqual(l, r);
|
|
47
|
+
const typeDiffers = !typesAgree(entry.type, other.type) && String(l) === String(r);
|
|
48
|
+
rows.push({
|
|
49
|
+
path: entry.path,
|
|
50
|
+
status: equal && !typeDiffers ? "same" : "changed",
|
|
51
|
+
left: l,
|
|
52
|
+
right: r,
|
|
53
|
+
leftType: entry.type,
|
|
54
|
+
rightType: other.type,
|
|
55
|
+
typeDiffers
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
for (const entry of b.entries) {
|
|
59
|
+
if (seen.has(entry.path)) continue;
|
|
60
|
+
rows.push({ path: entry.path, status: "added", right: plainValue(entry.value), rightType: entry.type });
|
|
61
|
+
}
|
|
62
|
+
const counts = { same: 0, changed: 0, added: 0, removed: 0 };
|
|
63
|
+
for (const row of rows) counts[row.status]++;
|
|
64
|
+
return { rows, counts, truncated: a.truncated || b.truncated };
|
|
65
|
+
}
|
|
66
|
+
export function compareText(value) {
|
|
67
|
+
if (value === null) return "null";
|
|
68
|
+
if (typeof value === "string") return value;
|
|
69
|
+
if (typeof value === "object") return JSON.stringify(value);
|
|
70
|
+
return String(value);
|
|
71
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface DocumentChanges {
|
|
2
|
+
/** dotted path → new value (already in the wire shape the caller built, e.g. `{ $date }` wrappers kept) */
|
|
3
|
+
set: Record<string, unknown>;
|
|
4
|
+
/** dotted paths to remove */
|
|
5
|
+
unset: string[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The minimal changes that turn `original` into `edited` (both whole documents, `_id` ignored): changed leaves as
|
|
9
|
+
* dotted keys, whole values for arrays and anything that is not a plain object on both sides, removed fields as
|
|
10
|
+
* unsets. Returns `null` when the change cannot be expressed as a partial update (a top-level field name that
|
|
11
|
+
* contains a dot or starts with `$`) — the caller then falls back to a full replace.
|
|
12
|
+
*/
|
|
13
|
+
export declare function diffDocument(original: Record<string, unknown>, edited: Record<string, unknown>): DocumentChanges | null;
|
|
14
|
+
export declare function hasChanges(changes: DocumentChanges): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* The request body for `updateDocumentPartial`. Plain fields and `$unset` may be mixed (verified). Never returns
|
|
17
|
+
* an empty body — the server answers an empty partial update with a 500 — so callers check `hasChanges` first.
|
|
18
|
+
*/
|
|
19
|
+
export declare function buildPartialBody(changes: DocumentChanges): Record<string, unknown>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { unwrapExtendedJson } from "../composables/useMongocampExtendedJson.js";
|
|
2
|
+
function isPlainObject(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Date);
|
|
4
|
+
}
|
|
5
|
+
function isWrapper(value) {
|
|
6
|
+
const keys = Object.keys(value);
|
|
7
|
+
return keys.length === 1 && keys[0].startsWith("$");
|
|
8
|
+
}
|
|
9
|
+
function isUnsafeKey(key) {
|
|
10
|
+
return key === "" || key.includes(".") || key.startsWith("$");
|
|
11
|
+
}
|
|
12
|
+
function normalize(value) {
|
|
13
|
+
if (value instanceof Date) return value.toISOString();
|
|
14
|
+
return unwrapExtendedJson(value);
|
|
15
|
+
}
|
|
16
|
+
const FULL_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
17
|
+
function sameInstant(a, b) {
|
|
18
|
+
return FULL_TIMESTAMP.test(a) && FULL_TIMESTAMP.test(b) && Date.parse(a) === Date.parse(b);
|
|
19
|
+
}
|
|
20
|
+
function deepEqual(a, b) {
|
|
21
|
+
if (a === b) return true;
|
|
22
|
+
if (typeof a === "string" && typeof b === "string") return sameInstant(a, b);
|
|
23
|
+
if (Array.isArray(a) && Array.isArray(b)) return a.length === b.length && a.every((item, i) => deepEqual(item, b[i]));
|
|
24
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
25
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
26
|
+
return [...keys].every((key) => deepEqual(a[key], b[key]));
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
function canDescend(original, edited) {
|
|
31
|
+
if (!isPlainObject(original) || !isPlainObject(edited)) return false;
|
|
32
|
+
if (isWrapper(original) || isWrapper(edited)) return false;
|
|
33
|
+
return ![...Object.keys(original), ...Object.keys(edited)].some(isUnsafeKey);
|
|
34
|
+
}
|
|
35
|
+
function walk(original, edited, prefix, changes) {
|
|
36
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(original), ...Object.keys(edited)])) {
|
|
37
|
+
if (prefix === "" && key === "_id") continue;
|
|
38
|
+
if (isUnsafeKey(key)) return false;
|
|
39
|
+
const path = prefix + key;
|
|
40
|
+
const hadIt = key in original;
|
|
41
|
+
const next = edited[key];
|
|
42
|
+
if (next === void 0) {
|
|
43
|
+
if (hadIt) changes.unset.push(path);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (!hadIt) {
|
|
47
|
+
changes.set[path] = next;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const before = original[key];
|
|
51
|
+
if (deepEqual(normalize(before), normalize(next))) continue;
|
|
52
|
+
if (canDescend(before, next)) {
|
|
53
|
+
if (!walk(before, next, `${path}.`, changes)) return false;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
changes.set[path] = next;
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
export function diffDocument(original, edited) {
|
|
61
|
+
const changes = { set: {}, unset: [] };
|
|
62
|
+
return walk(original, edited, "", changes) ? changes : null;
|
|
63
|
+
}
|
|
64
|
+
export function hasChanges(changes) {
|
|
65
|
+
return Object.keys(changes.set).length > 0 || changes.unset.length > 0;
|
|
66
|
+
}
|
|
67
|
+
export function buildPartialBody(changes) {
|
|
68
|
+
const body = { ...changes.set };
|
|
69
|
+
if (changes.unset.length > 0) body.$unset = Object.fromEntries(changes.unset.map((path) => [path, ""]));
|
|
70
|
+
return body;
|
|
71
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type PathValueType = 'null' | 'string' | 'boolean' | 'number' | 'int' | 'long' | 'double' | 'decimal' | 'objectId' | 'date' | 'array' | 'object';
|
|
2
|
+
export interface FlatEntry {
|
|
3
|
+
path: string;
|
|
4
|
+
type: PathValueType;
|
|
5
|
+
/** The leaf as stored: a wrapper stays one value (`{ $oid }` is not descended into); empty arrays/objects are leaves. */
|
|
6
|
+
value: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface FlattenOptions {
|
|
9
|
+
/** Deeper structures are returned as one `object`/`array` leaf (default 32). */
|
|
10
|
+
maxDepth?: number;
|
|
11
|
+
/** Stops after this many entries (default 10 000); `truncated` tells. */
|
|
12
|
+
maxEntries?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface FlattenResult {
|
|
15
|
+
entries: FlatEntry[];
|
|
16
|
+
truncated: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* What a value is, with extended-JSON wrappers recognised as their BSON type. Read a document with `extendedJson=canonical`
|
|
20
|
+
* (server 1.8.1) to get the types: `_id` and nested ids are `{ $oid }`, dates `{ $date }`, numbers `{ $numberInt }` /
|
|
21
|
+
* `{ $numberLong }` / `{ $numberDouble }` / `{ $numberDecimal }`, date-looking text `{ $string }`. A document read as plain
|
|
22
|
+
* JSON has no wrappers (ids and dates are strings) and is typed as what JSON says — the module no longer guesses.
|
|
23
|
+
*/
|
|
24
|
+
export declare function typeOf(value: unknown): PathValueType;
|
|
25
|
+
/** Splits a path at its unescaped dots; `''` is the empty path (no segments). */
|
|
26
|
+
export declare function splitPath(path: string): string[];
|
|
27
|
+
export declare function joinPath(segments: Array<string | number>): string;
|
|
28
|
+
/**
|
|
29
|
+
* Every leaf of `doc` as `{ path, type, value }`, depth-first in key order. Arrays are walked by index, a wrapper is one
|
|
30
|
+
* leaf, an empty array/object is a leaf. The document is never changed.
|
|
31
|
+
*/
|
|
32
|
+
export declare function flattenDocument(doc: unknown, options?: FlattenOptions): FlattenResult;
|
|
33
|
+
/** The value at `path`, or `undefined` when any part of it is missing. Never reads inherited properties. */
|
|
34
|
+
export declare function getPath(doc: unknown, path: string): unknown;
|
|
35
|
+
export declare function hasPath(doc: unknown, path: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* A copy of `doc` with `value` at `path`; only the objects/arrays along the path are copied, the rest is shared. Missing
|
|
38
|
+
* intermediate objects are created, an array may be appended to at index `length`. Throws for a path through
|
|
39
|
+
* `__proto__`/`constructor`/`prototype`, a non-index segment in an array or an index beyond its end.
|
|
40
|
+
*/
|
|
41
|
+
export declare function setPath<T extends Record<string, unknown>>(doc: T, path: string, value: unknown): T;
|