@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
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useMongocampClientApi } from "#imports";
|
|
2
|
+
import { mapIndexUsage, mapStorage } from "../utils/collectionStats.js";
|
|
3
|
+
import { useMongocampAggregation } from "./useMongocampAggregation.js";
|
|
4
|
+
const REQUEST_TIMEOUT_MS = 15e3;
|
|
5
|
+
export function useMongocampCollectionStats() {
|
|
6
|
+
const { collectionApi } = useMongocampClientApi();
|
|
7
|
+
const { runAggregation } = useMongocampAggregation();
|
|
8
|
+
async function getStorage(collection) {
|
|
9
|
+
const status = await collectionApi.getCollectionInformation({ collectionName: collection }, { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) });
|
|
10
|
+
return mapStorage(status);
|
|
11
|
+
}
|
|
12
|
+
async function getIndexUsage(collection) {
|
|
13
|
+
const { rows } = await runAggregation(collection, [{ stage: "$indexStats", value: {} }], { rowsPerPage: 1e3 }, { allowStatsStages: true });
|
|
14
|
+
return mapIndexUsage(rows);
|
|
15
|
+
}
|
|
16
|
+
return { getStorage, getIndexUsage };
|
|
17
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export type MongocampErrorKind = 'network' | 'unauthorized' | 'forbidden' | 'other';
|
|
1
|
+
export type MongocampErrorKind = 'network' | 'unauthorized' | 'forbidden' | 'notAllowed' | 'invalid' | 'other';
|
|
2
2
|
export interface DescribedError {
|
|
3
3
|
kind: MongocampErrorKind;
|
|
4
4
|
message: string;
|
|
5
5
|
}
|
|
6
6
|
export declare function useMongocampError(): {
|
|
7
7
|
describeError: (error: unknown, fallbackKey?: string) => DescribedError;
|
|
8
|
+
describeServerError: (error: unknown, fallbackKey?: string) => Promise<DescribedError>;
|
|
8
9
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { useI18n } from "#imports";
|
|
2
|
+
import { notAllowedOperator, OPERATOR_NOT_ALLOWED_CODE, readServerError } from "../utils/serverError.js";
|
|
2
3
|
export function useMongocampError() {
|
|
3
4
|
const { t } = useI18n();
|
|
4
5
|
function describeError(error, fallbackKey) {
|
|
@@ -16,5 +17,19 @@ export function useMongocampError() {
|
|
|
16
17
|
const message = error instanceof Error && error.message ? error.message : t(fallbackKey ?? "nuxtUiMongocamp.errors.generic");
|
|
17
18
|
return { kind: "other", message };
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
+
async function describeServerError(error, fallbackKey) {
|
|
21
|
+
const base = describeError(error, fallbackKey);
|
|
22
|
+
if (base.kind === "network" || base.kind === "unauthorized") return base;
|
|
23
|
+
const server = await readServerError(error);
|
|
24
|
+
if (server.code === OPERATOR_NOT_ALLOWED_CODE) {
|
|
25
|
+
const operator = notAllowedOperator(server.message);
|
|
26
|
+
return { kind: "notAllowed", message: operator ? t("nuxtUiMongocamp.errors.notAllowedOperator", { operator }) : t("nuxtUiMongocamp.errors.notAllowed") };
|
|
27
|
+
}
|
|
28
|
+
if (base.kind === "forbidden") return base;
|
|
29
|
+
if (server.message && server.status !== void 0 && server.status >= 400) {
|
|
30
|
+
return { kind: server.status === 400 ? "invalid" : "other", message: server.message };
|
|
31
|
+
}
|
|
32
|
+
return base;
|
|
33
|
+
}
|
|
34
|
+
return { describeError, describeServerError };
|
|
20
35
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ExportFormat } from '../utils/exportDocuments.js';
|
|
2
|
+
export interface ExportCollectionOptions {
|
|
3
|
+
collection: string;
|
|
4
|
+
format: ExportFormat;
|
|
5
|
+
/** Lucene filter: export only the documents that match (the current filter of a table). It travels in the URL: over ~1 800 encoded characters it throws `FilterTooLongError`. */
|
|
6
|
+
filter?: string;
|
|
7
|
+
sort?: string[];
|
|
8
|
+
/** Limit the fields; for a CSV they are also its columns, in this order (default: the fields of the first 1 000 documents). */
|
|
9
|
+
projection?: string[];
|
|
10
|
+
/** CSV columns in this order — the same as `projection`, kept for the old name. */
|
|
11
|
+
columns?: string[];
|
|
12
|
+
/** Stop after this many documents (`limit`); default: all. */
|
|
13
|
+
maxRows?: number;
|
|
14
|
+
/** How many documents the caller expects (the count of the filter): a file with fewer is reported as `incomplete`. */
|
|
15
|
+
expected?: number;
|
|
16
|
+
/** After every chunk of the stream. */
|
|
17
|
+
onProgress?: (progress: ExportProgress) => void;
|
|
18
|
+
/** Aborting rejects with an `AbortError`; nothing is downloaded. */
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
/** File name; default `<collection>-<yyyymmdd>-<hhmm>.<format>`. */
|
|
21
|
+
filename?: string;
|
|
22
|
+
/** Hand the file to the browser as a download (default `true`). `false` only returns the blob. */
|
|
23
|
+
download?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface ExportProgress {
|
|
26
|
+
/** Bytes received so far. */
|
|
27
|
+
bytes: number;
|
|
28
|
+
/** Lines received so far (NDJSON: documents; CSV: documents + the header, unless a value holds a line break). */
|
|
29
|
+
lines: number;
|
|
30
|
+
}
|
|
31
|
+
export interface ExportResult {
|
|
32
|
+
blob: Blob;
|
|
33
|
+
filename: string;
|
|
34
|
+
mimeType: string;
|
|
35
|
+
format: ExportFormat;
|
|
36
|
+
bytes: number;
|
|
37
|
+
/** Documents in the file: exact for NDJSON (one per line); for JSON and CSV the expected count when the file is complete. */
|
|
38
|
+
documents?: number;
|
|
39
|
+
/**
|
|
40
|
+
* The file is certainly **cut short**: the server ends a stream that fails midway with a normal `200`, so the file is checked
|
|
41
|
+
* — JSON must end with `]`, NDJSON and CSV must hold at least the expected number of documents. Tell the user.
|
|
42
|
+
*/
|
|
43
|
+
incomplete: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Exports a whole collection, or every document matching a filter, as JSON, NDJSON or CSV — not just the rows on screen.
|
|
47
|
+
* The server streams the file (`GET …/documents/export`, server 1.8.1): there is no document cap and the browser never parses
|
|
48
|
+
* a document. Values are written as plain JSON, so an integer beyond 2^53 keeps every digit as the text the server wrote.
|
|
49
|
+
* The file ends up in a `Blob` (the browser may keep it on disk) and is handed to the browser as a download.
|
|
50
|
+
*/
|
|
51
|
+
export declare function useMongocampExportAll(): {
|
|
52
|
+
exportCollection: (options: ExportCollectionOptions) => Promise<ExportResult>;
|
|
53
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useMongocampClientApi } from "#imports";
|
|
2
|
+
import { downloadBlob } from "../utils/exportRows.js";
|
|
3
|
+
import { EXPORT_MIME_TYPES, exportFilename } from "../utils/exportDocuments.js";
|
|
4
|
+
import { FilterTooLongError } from "../utils/aggregationErrors.js";
|
|
5
|
+
import { isFilterTooLong, MAX_ENCODED_FILTER_LENGTH } from "../utils/requestLimits.js";
|
|
6
|
+
function countingStream(onProgress) {
|
|
7
|
+
const state = { bytes: 0, newlines: 0, last: 0 };
|
|
8
|
+
const stream = new TransformStream({
|
|
9
|
+
transform(chunk, controller) {
|
|
10
|
+
state.bytes += chunk.length;
|
|
11
|
+
for (const byte of chunk) if (byte === 10) state.newlines++;
|
|
12
|
+
if (chunk.length > 0) state.last = chunk[chunk.length - 1];
|
|
13
|
+
onProgress?.({ bytes: state.bytes, lines: state.newlines + (state.last !== 10 && state.bytes > 0 ? 1 : 0) });
|
|
14
|
+
controller.enqueue(chunk);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return { stream, state };
|
|
18
|
+
}
|
|
19
|
+
export function useMongocampExportAll() {
|
|
20
|
+
const { documentApi } = useMongocampClientApi();
|
|
21
|
+
async function exportCollection(options) {
|
|
22
|
+
const { collection, format, download = true } = options;
|
|
23
|
+
if (isFilterTooLong(options.filter)) throw new FilterTooLongError(MAX_ENCODED_FILTER_LENGTH);
|
|
24
|
+
const projection = options.projection?.length ? options.projection : options.columns?.length ? options.columns : void 0;
|
|
25
|
+
const response = await documentApi.exportDocumentsRaw({
|
|
26
|
+
collectionName: collection,
|
|
27
|
+
format,
|
|
28
|
+
filter: options.filter?.trim() ? options.filter : void 0,
|
|
29
|
+
sort: options.sort?.length ? options.sort : void 0,
|
|
30
|
+
projection,
|
|
31
|
+
limit: options.maxRows
|
|
32
|
+
}, options.signal ? { signal: options.signal } : void 0);
|
|
33
|
+
const mimeType = EXPORT_MIME_TYPES[format];
|
|
34
|
+
const { stream, state } = countingStream(options.onProgress);
|
|
35
|
+
const body = response.raw.body;
|
|
36
|
+
const blob = body ? await new Response(body.pipeThrough(stream), { headers: { "content-type": mimeType } }).blob() : await response.raw.blob();
|
|
37
|
+
const bytes = body ? state.bytes : blob.size;
|
|
38
|
+
const lines = body ? state.newlines + (state.last !== 10 && state.bytes > 0 ? 1 : 0) : 0;
|
|
39
|
+
const wanted = options.expected === void 0 ? void 0 : Math.min(options.expected, options.maxRows ?? Infinity);
|
|
40
|
+
let incomplete;
|
|
41
|
+
let documents;
|
|
42
|
+
if (format === "json") {
|
|
43
|
+
incomplete = !(await blob.slice(Math.max(0, bytes - 8)).text()).trimEnd().endsWith("]");
|
|
44
|
+
if (!incomplete) documents = wanted;
|
|
45
|
+
} else if (format === "ndjson") {
|
|
46
|
+
documents = lines;
|
|
47
|
+
incomplete = wanted !== void 0 && lines < wanted;
|
|
48
|
+
} else {
|
|
49
|
+
incomplete = wanted !== void 0 && lines - 1 < wanted;
|
|
50
|
+
if (!incomplete) documents = wanted;
|
|
51
|
+
}
|
|
52
|
+
const filename = options.filename ?? exportFilename(collection, format);
|
|
53
|
+
if (download) downloadBlob(filename, blob);
|
|
54
|
+
return { blob, filename, mimeType, format, bytes, documents, incomplete };
|
|
55
|
+
}
|
|
56
|
+
return { exportCollection };
|
|
57
|
+
}
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
import type { TypeHints } from '../utils/bsonTypes.js';
|
|
2
|
+
export interface UnwrapOptions {
|
|
3
|
+
/** Keep a `$numberLong` outside the safe-integer range as its exact digit string (default `true`). `false` gives the rounded number, e.g. for type inference. */
|
|
4
|
+
exactLongs?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function unwrapExtendedJson(value: unknown, options?: UnwrapOptions): unknown;
|
|
7
|
+
export declare function wrapExtendedJson(formData: Record<string, unknown>, originalDoc?: Record<string, unknown>, hints?: TypeHints): Record<string, unknown>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isIsoDateTimeString } from "../utils/isoDate.js";
|
|
2
|
+
import { wireValue } from "../utils/bsonTypes.js";
|
|
2
3
|
function isPlainObject(value) {
|
|
3
4
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
5
|
}
|
|
@@ -9,28 +10,60 @@ function unwrapDateTime(raw) {
|
|
|
9
10
|
if (isPlainObject(inner) && typeof inner.$numberLong === "string") return new Date(Number(inner.$numberLong)).toISOString();
|
|
10
11
|
return raw;
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
+
const INTEGER_TEXT = /^-?\d+$/;
|
|
14
|
+
function unwrapNumber(raw, exactLongs) {
|
|
13
15
|
for (const key of NUMBER_WRAPPER_KEYS) {
|
|
14
16
|
const wrapped = raw[key];
|
|
15
17
|
if (typeof wrapped === "string" || typeof wrapped === "number") {
|
|
16
18
|
const parsed = Number(wrapped);
|
|
17
|
-
if (
|
|
19
|
+
if (Number.isNaN(parsed)) continue;
|
|
20
|
+
if (key === "$numberLong" && exactLongs && typeof wrapped === "string" && INTEGER_TEXT.test(wrapped) && !Number.isSafeInteger(parsed)) return wrapped;
|
|
21
|
+
return parsed;
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
24
|
return void 0;
|
|
21
25
|
}
|
|
22
|
-
export function unwrapExtendedJson(value) {
|
|
23
|
-
if (Array.isArray(value)) return value.map(unwrapExtendedJson);
|
|
26
|
+
export function unwrapExtendedJson(value, options = {}) {
|
|
27
|
+
if (Array.isArray(value)) return value.map((item) => unwrapExtendedJson(item, options));
|
|
24
28
|
if (!isPlainObject(value)) return value;
|
|
25
29
|
if (typeof value.$oid === "string") return value.$oid;
|
|
30
|
+
if (typeof value.$string === "string" && Object.keys(value).length === 1) return value.$string;
|
|
26
31
|
if ("$date" in value) return unwrapDateTime(value);
|
|
27
|
-
const number = unwrapNumber(value);
|
|
32
|
+
const number = unwrapNumber(value, options.exactLongs ?? true);
|
|
28
33
|
if (number !== void 0) return number;
|
|
29
34
|
const result = {};
|
|
30
|
-
for (const [key, v] of Object.entries(value)) result[key] = unwrapExtendedJson(v);
|
|
35
|
+
for (const [key, v] of Object.entries(value)) result[key] = unwrapExtendedJson(v, options);
|
|
31
36
|
return result;
|
|
32
37
|
}
|
|
33
|
-
|
|
38
|
+
const INT32_MIN = -(2 ** 31);
|
|
39
|
+
const INT32_MAX = 2 ** 31 - 1;
|
|
40
|
+
function rewrapNumber(value, original) {
|
|
41
|
+
if (!Number.isFinite(value)) return value;
|
|
42
|
+
if ("$numberInt" in original && Number.isInteger(value) && value >= INT32_MIN && value <= INT32_MAX) return { $numberInt: String(value) };
|
|
43
|
+
if ("$numberLong" in original && Number.isSafeInteger(value)) return { $numberLong: String(value) };
|
|
44
|
+
if ("$numberDouble" in original) return { $numberDouble: String(value) };
|
|
45
|
+
if ("$numberDecimal" in original) return { $numberDecimal: String(value) };
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
function isWrapper(value) {
|
|
49
|
+
if (!isPlainObject(value)) return false;
|
|
50
|
+
const keys = Object.keys(value);
|
|
51
|
+
return keys.length === 1 && keys[0].startsWith("$");
|
|
52
|
+
}
|
|
53
|
+
const joinKey = (path, key) => path === "" ? key : `${path}.${key}`;
|
|
54
|
+
function rewrapValue(value, originalValue, path, hints) {
|
|
55
|
+
const hint = hints?.[path];
|
|
56
|
+
if (hint !== void 0 && !isWrapper(originalValue) && value !== null && typeof value !== "object") return wireValue(hint, value);
|
|
57
|
+
if (typeof value === "string" && INTEGER_TEXT.test(value) && isPlainObject(originalValue) && typeof originalValue.$numberLong === "string") {
|
|
58
|
+
return { $numberLong: value };
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === "number" && isPlainObject(originalValue)) {
|
|
61
|
+
const wrapped = rewrapNumber(value, originalValue);
|
|
62
|
+
if (wrapped !== value) return wrapped;
|
|
63
|
+
}
|
|
64
|
+
if (typeof value === "string" && isPlainObject(originalValue) && typeof originalValue.$string === "string") {
|
|
65
|
+
return { $string: value };
|
|
66
|
+
}
|
|
34
67
|
if (typeof value === "string" && isIsoDateTimeString(value)) {
|
|
35
68
|
return { $date: value };
|
|
36
69
|
}
|
|
@@ -39,20 +72,20 @@ function rewrapValue(value, originalValue) {
|
|
|
39
72
|
}
|
|
40
73
|
if (Array.isArray(value)) {
|
|
41
74
|
const originalArray = Array.isArray(originalValue) ? originalValue : [];
|
|
42
|
-
return value.map((item, index) => rewrapValue(item, originalArray[index]));
|
|
75
|
+
return value.map((item, index) => rewrapValue(item, originalArray[index], `${path}[]`, hints));
|
|
43
76
|
}
|
|
44
77
|
if (isPlainObject(value)) {
|
|
45
78
|
const originalObject = isPlainObject(originalValue) ? originalValue : void 0;
|
|
46
79
|
const nested = {};
|
|
47
|
-
for (const [key, v] of Object.entries(value)) nested[key] = rewrapValue(v, originalObject?.[key]);
|
|
80
|
+
for (const [key, v] of Object.entries(value)) nested[key] = rewrapValue(v, originalObject?.[key], joinKey(path, key), hints);
|
|
48
81
|
return nested;
|
|
49
82
|
}
|
|
50
83
|
return value;
|
|
51
84
|
}
|
|
52
|
-
export function wrapExtendedJson(formData, originalDoc) {
|
|
85
|
+
export function wrapExtendedJson(formData, originalDoc, hints) {
|
|
53
86
|
const result = originalDoc ? { ...originalDoc } : {};
|
|
54
87
|
for (const [key, value] of Object.entries(formData)) {
|
|
55
|
-
result[key] = rewrapValue(value, originalDoc?.[key]);
|
|
88
|
+
result[key] = rewrapValue(value, originalDoc?.[key], key, hints);
|
|
56
89
|
}
|
|
57
90
|
delete result._id;
|
|
58
91
|
return result;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FetchAllOptions, FetchAllResult } from '../utils/pagedFetch.js';
|
|
2
|
+
export interface FetchAllDocumentsOptions<T> extends FetchAllOptions<T> {
|
|
3
|
+
/** Lucene filter, as for the document list. */
|
|
4
|
+
filter?: string;
|
|
5
|
+
/** Sort keys as the document list takes them (`['name', '-age']`). Without one the server's natural order is used. */
|
|
6
|
+
sort?: string[];
|
|
7
|
+
/** Fields to return; `['_id']` is the cheap way to collect ids. */
|
|
8
|
+
projection?: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface FetchAllFilesOptions<T> extends FetchAllOptions<T> {
|
|
11
|
+
filter?: string;
|
|
12
|
+
sort?: string;
|
|
13
|
+
projection?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Reads every document of a collection (or of a Lucene filter) page by page: `fetchAllPages` over the document list. The
|
|
17
|
+
* rows are what the list returns — read from the response text, so integers beyond 2^53 are `{ $numberLong }` wrappers
|
|
18
|
+
* (run them through `unwrapExtendedJson` for display) — and the total comes from the pagination header.
|
|
19
|
+
* Capped at 10 000 rows by default (`maxRows` raises it); see `utils/pagedFetch.ts` for stopping, progress, abort and
|
|
20
|
+
* the effect of documents changing during the read. Every page has its own 60 s timeout.
|
|
21
|
+
*/
|
|
22
|
+
export declare function useMongocampFetchAll(): {
|
|
23
|
+
fetchAllDocuments: <T = Record<string, unknown>>(collectionName: string, options?: FetchAllDocumentsOptions<T>) => Promise<FetchAllResult<T>>;
|
|
24
|
+
fetchAllFiles: <T = Record<string, unknown>>(bucketName: string, options?: FetchAllFilesOptions<T>) => Promise<FetchAllResult<T>>;
|
|
25
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useMongocampClientApi } from "#imports";
|
|
2
|
+
import { readJsonKeepingLongs } from "../utils/bigIntJson.js";
|
|
3
|
+
import { fetchAllPages } from "../utils/pagedFetch.js";
|
|
4
|
+
import { headerNumber } from "../utils/paginationHeaders.js";
|
|
5
|
+
const PAGE_TIMEOUT_MS = 6e4;
|
|
6
|
+
function requestInit(signal) {
|
|
7
|
+
const timeout = AbortSignal.timeout(PAGE_TIMEOUT_MS);
|
|
8
|
+
return { signal: signal && typeof AbortSignal.any === "function" ? AbortSignal.any([signal, timeout]) : timeout };
|
|
9
|
+
}
|
|
10
|
+
export function useMongocampFetchAll() {
|
|
11
|
+
const { documentApi, fileApi } = useMongocampClientApi();
|
|
12
|
+
async function fetchAllDocuments(collectionName, options = {}) {
|
|
13
|
+
const { filter, sort, projection, ...paging } = options;
|
|
14
|
+
return fetchAllPages(async (page, pageSize) => {
|
|
15
|
+
const response = await documentApi.searchDocumentsRaw({ collectionName, searchRequest: { filter, sort, projection }, page, rowsPerPage: pageSize }, requestInit(options.signal));
|
|
16
|
+
return { rows: await readJsonKeepingLongs(response), total: headerNumber(response.raw?.headers, "x-pagination-count-rows") };
|
|
17
|
+
}, paging);
|
|
18
|
+
}
|
|
19
|
+
async function fetchAllFiles(bucketName, options = {}) {
|
|
20
|
+
const { filter, sort, projection, ...paging } = options;
|
|
21
|
+
return fetchAllPages(async (page, pageSize) => {
|
|
22
|
+
const response = await fileApi.listFilesRaw({ bucketName, filter, sort, projection, page, rowsPerPage: pageSize }, requestInit(options.signal));
|
|
23
|
+
return { rows: await readJsonKeepingLongs(response), total: headerNumber(response.raw?.headers, "x-pagination-count-rows") };
|
|
24
|
+
}, paging);
|
|
25
|
+
}
|
|
26
|
+
return { fetchAllDocuments, fetchAllFiles };
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FieldTypeInfo } from '../utils/fieldDetails.js';
|
|
2
|
+
import type { TypeHints } from '../utils/bsonTypes.js';
|
|
3
|
+
import type { FilterFieldType } from '../utils/filterBuilder.js';
|
|
4
|
+
export type FieldTypesStatus = 'idle' | 'loading' | 'ready' | 'failed';
|
|
5
|
+
/**
|
|
6
|
+
* The fields of a collection with the types of their values, from the server's own sample (`GET …/fields/detail`, server
|
|
7
|
+
* 1.8.1) — one request per collection, shared by every component that asks (a `useState` cache), instead of guessing from the
|
|
8
|
+
* rows of the page on screen. Components call `load()` (also after a reload that may have changed the data: `load(true)`) and
|
|
9
|
+
* read `fields` / `typeOf` / `filterFields` / `hints`; while nothing is loaded, or when the request failed, they are empty and
|
|
10
|
+
* the caller keeps its own fallback. Never throws.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useMongocampFieldTypes(collection: string | (() => string)): {
|
|
13
|
+
load: (force?: boolean) => Promise<void>;
|
|
14
|
+
invalidate: (target?: string) => void;
|
|
15
|
+
status: () => FieldTypesStatus;
|
|
16
|
+
fields: () => FieldTypeInfo[];
|
|
17
|
+
selectable: () => FieldTypeInfo[];
|
|
18
|
+
typeOf: (path: string) => FieldTypeInfo | undefined;
|
|
19
|
+
filterFields: () => {
|
|
20
|
+
key: string;
|
|
21
|
+
type: FilterFieldType;
|
|
22
|
+
}[];
|
|
23
|
+
hints: () => TypeHints;
|
|
24
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useMongocampClientApi, useState } from "#imports";
|
|
2
|
+
import { filterTypeOf, toFieldTypeInfos, toTypeHints } from "../utils/fieldDetails.js";
|
|
3
|
+
const REQUEST_TIMEOUT_MS = 15e3;
|
|
4
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
5
|
+
export function useMongocampFieldTypes(collection) {
|
|
6
|
+
const { collectionApi } = useMongocampClientApi();
|
|
7
|
+
const cache = useState("mongocamp-field-types", () => ({}));
|
|
8
|
+
const name = () => typeof collection === "function" ? collection() : collection;
|
|
9
|
+
const entry = () => cache.value[name()] ?? { status: "idle", fields: [] };
|
|
10
|
+
async function load(force = false) {
|
|
11
|
+
const key = name();
|
|
12
|
+
if (!key) return;
|
|
13
|
+
if (!force && (cache.value[key]?.status === "ready" || inFlight.has(key))) return inFlight.get(key);
|
|
14
|
+
const run = (async () => {
|
|
15
|
+
cache.value = { ...cache.value, [key]: { status: "loading", fields: cache.value[key]?.fields ?? [] } };
|
|
16
|
+
try {
|
|
17
|
+
const details = await collectionApi.getFieldDetails({ collectionName: key }, { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) });
|
|
18
|
+
cache.value = { ...cache.value, [key]: { status: "ready", fields: toFieldTypeInfos(details) } };
|
|
19
|
+
} catch {
|
|
20
|
+
cache.value = { ...cache.value, [key]: { status: "failed", fields: [] } };
|
|
21
|
+
} finally {
|
|
22
|
+
inFlight.delete(key);
|
|
23
|
+
}
|
|
24
|
+
})();
|
|
25
|
+
inFlight.set(key, run);
|
|
26
|
+
return run;
|
|
27
|
+
}
|
|
28
|
+
function invalidate(target = name()) {
|
|
29
|
+
const { [target]: _dropped, ...rest } = cache.value;
|
|
30
|
+
cache.value = rest;
|
|
31
|
+
}
|
|
32
|
+
const status = () => entry().status;
|
|
33
|
+
const fields = () => entry().fields;
|
|
34
|
+
const selectable = () => entry().fields.filter((f) => !f.element);
|
|
35
|
+
const typeOf = (path) => entry().fields.find((f) => f.path === path);
|
|
36
|
+
function filterFields() {
|
|
37
|
+
return selectable().flatMap((f) => {
|
|
38
|
+
const type = filterTypeOf(f);
|
|
39
|
+
return type ? [{ key: f.path, type }] : [];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const hints = () => toTypeHints(entry().fields);
|
|
43
|
+
return { load, invalidate, status, fields, selectable, typeOf, filterFields, hints };
|
|
44
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { BatchInsertResult } from '../utils/batchInsert.js';
|
|
2
|
+
import type { ImportResponse } from '@sfxcode/nuxt-mongocamp-server';
|
|
2
3
|
export type { BatchInsertResult } from '../utils/batchInsert.js';
|
|
3
4
|
export interface ImportRowsOptions {
|
|
4
5
|
batchSize?: number;
|
|
@@ -11,4 +12,7 @@ export interface ImportRowsOptions {
|
|
|
11
12
|
}
|
|
12
13
|
export declare function useMongocampImport(): {
|
|
13
14
|
importRows: (collectionName: string, rows: Record<string, unknown>[], options?: ImportRowsOptions) => Promise<BatchInsertResult>;
|
|
15
|
+
importNdjson: (collectionName: string, file: Blob, options?: {
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}) => Promise<ImportResponse>;
|
|
14
18
|
};
|
|
@@ -16,10 +16,13 @@ export function useMongocampImport() {
|
|
|
16
16
|
(batch) => {
|
|
17
17
|
const timeout = AbortSignal.timeout(BATCH_TIMEOUT_MS);
|
|
18
18
|
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
19
|
-
return documentApi.insertMany({ collectionName, requestBody: batch }, { signal });
|
|
19
|
+
return documentApi.insertMany({ collectionName, requestBody: batch, ordered: false }, { signal });
|
|
20
20
|
},
|
|
21
21
|
{ batchSize: options.batchSize, signal: options.signal, onProgress: options.onProgress, isAtomicFailure: isRejectedBeforeWrite }
|
|
22
22
|
);
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
async function importNdjson(collectionName, file, options = {}) {
|
|
25
|
+
return documentApi.importDocuments({ collectionName, body: file }, options.signal ? { signal: options.signal } : void 0);
|
|
26
|
+
}
|
|
27
|
+
return { importRows, importNdjson };
|
|
25
28
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import type { IndexCreateResponse, IndexDropResponse, IndexOptionsRequest, MongoIndex } from '@sfxcode/nuxt-mongocamp-server';
|
|
1
|
+
import type { IndexCreateResponse, IndexDropResponse, IndexInfo, IndexOptionsRequest, MongoIndex } from '@sfxcode/nuxt-mongocamp-server';
|
|
2
2
|
export declare function useMongocampIndex(): {
|
|
3
3
|
listIndexes: (collectionName: string) => Promise<MongoIndex[]>;
|
|
4
|
+
listIndexDetails: (collectionName: string) => Promise<IndexInfo[]>;
|
|
4
5
|
createIndex: (collectionName: string, keys: Record<string, number>, options?: IndexOptionsRequest) => Promise<IndexCreateResponse>;
|
|
6
|
+
createOrderedIndex: (collectionName: string, orderedKeys: Array<{
|
|
7
|
+
field: string;
|
|
8
|
+
direction: string;
|
|
9
|
+
}>, options?: IndexOptionsRequest) => Promise<IndexCreateResponse>;
|
|
5
10
|
createIndexForField: (collectionName: string, fieldName: string, sortAscending?: boolean) => Promise<IndexCreateResponse>;
|
|
6
11
|
createUniqueIndex: (collectionName: string, fieldName: string, sortAscending?: boolean) => Promise<IndexCreateResponse>;
|
|
7
12
|
createTextIndex: (collectionName: string, fieldName: string) => Promise<IndexCreateResponse>;
|
|
@@ -8,9 +8,15 @@ export function useMongocampIndex() {
|
|
|
8
8
|
async function listIndexes(collectionName) {
|
|
9
9
|
return indexApi.listIndices({ collectionName });
|
|
10
10
|
}
|
|
11
|
+
async function listIndexDetails(collectionName) {
|
|
12
|
+
return indexApi.indexDetails({ collectionName }, withTimeout());
|
|
13
|
+
}
|
|
11
14
|
async function createIndex(collectionName, keys, options) {
|
|
12
15
|
return indexApi.createIndex({ collectionName, indexCreateRequest: { keys, indexOptionsRequest: options } });
|
|
13
16
|
}
|
|
17
|
+
async function createOrderedIndex(collectionName, orderedKeys, options) {
|
|
18
|
+
return indexApi.createIndex({ collectionName, indexCreateRequest: { orderedKeys, indexOptionsRequest: options } });
|
|
19
|
+
}
|
|
14
20
|
async function createIndexForField(collectionName, fieldName, sortAscending = true) {
|
|
15
21
|
return indexApi.createIndexForField({ collectionName, fieldName, sortAscending }, withTimeout());
|
|
16
22
|
}
|
|
@@ -26,5 +32,5 @@ export function useMongocampIndex() {
|
|
|
26
32
|
async function deleteIndex(collectionName, indexName) {
|
|
27
33
|
return indexApi.deleteIndex({ collectionName, indexName });
|
|
28
34
|
}
|
|
29
|
-
return { listIndexes, createIndex, createIndexForField, createUniqueIndex, createTextIndex, createExpiringIndex, deleteIndex };
|
|
35
|
+
return { listIndexes, listIndexDetails, createIndex, createOrderedIndex, createIndexForField, createUniqueIndex, createTextIndex, createExpiringIndex, deleteIndex };
|
|
30
36
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the signed-in user may do with a collection — so a component can hide an action the server would refuse instead of
|
|
3
|
+
* letting the click fail. **UI-level only**: the server stays the authority.
|
|
4
|
+
*
|
|
5
|
+
* `load()` asks the server (`GET /auth/permissions`, server 1.7.0), which resolves roles, grants and the signed-in
|
|
6
|
+
* user's rights itself — and also works in server proxy mode, where there is no profile. `canRead` / `canWrite` /
|
|
7
|
+
* `canAdminister` stay synchronous: they answer from what was loaded, and until then from the grants of the session profile
|
|
8
|
+
* (admin, proxy mode, a profile without a grant list and GridFS bucket members answer "yes" — fail-open). A failing
|
|
9
|
+
* `load()` changes nothing. Administrators are never asked: everything is allowed.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useMongocampPermissions(): {
|
|
12
|
+
canRead: (collectionName: string) => boolean;
|
|
13
|
+
canWrite: (collectionName: string) => boolean;
|
|
14
|
+
canAdminister: (collectionName: string) => boolean;
|
|
15
|
+
readableCollections: (collectionNames: string[]) => string[];
|
|
16
|
+
load: (collectionName?: string) => Promise<void>;
|
|
17
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useRuntimeConfig } from "#app";
|
|
2
|
+
import { useMongocampClientApi, useMongocampStorage, useState } from "#imports";
|
|
3
|
+
import { permissionFromGrants } from "../utils/grantMatching.js";
|
|
4
|
+
const REQUEST_TIMEOUT_MS = 15e3;
|
|
5
|
+
export function useMongocampPermissions() {
|
|
6
|
+
const config = useRuntimeConfig();
|
|
7
|
+
const state = useMongocampStorage();
|
|
8
|
+
const cache = useState("mongocamp-permissions", () => ({ user: "", byName: {} }));
|
|
9
|
+
const isProxyMode = config.public.nuxtUiMongocampOptions?.useServerProxy ?? false;
|
|
10
|
+
const currentUser = () => state.value?.profile?.user ?? "";
|
|
11
|
+
const key = (name) => name.trim().toLowerCase();
|
|
12
|
+
function fromServer(collectionName) {
|
|
13
|
+
if (cache.value.user !== currentUser()) return void 0;
|
|
14
|
+
return cache.value.byName[key(collectionName)];
|
|
15
|
+
}
|
|
16
|
+
function allowed(collectionName, permission) {
|
|
17
|
+
const answered = fromServer(collectionName);
|
|
18
|
+
if (answered) return answered[permission];
|
|
19
|
+
if (isProxyMode) return true;
|
|
20
|
+
const profile = state.value?.profile;
|
|
21
|
+
if (!profile || profile.isAdmin) return true;
|
|
22
|
+
return permissionFromGrants(profile.grants, collectionName, permission) ?? true;
|
|
23
|
+
}
|
|
24
|
+
const canRead = (collectionName) => allowed(collectionName, "read");
|
|
25
|
+
const canWrite = (collectionName) => allowed(collectionName, "write");
|
|
26
|
+
const canAdminister = (collectionName) => allowed(collectionName, "administrate");
|
|
27
|
+
function readableCollections(collectionNames) {
|
|
28
|
+
return collectionNames.filter(canRead);
|
|
29
|
+
}
|
|
30
|
+
async function load(collectionName) {
|
|
31
|
+
if (state.value?.profile?.isAdmin) return;
|
|
32
|
+
try {
|
|
33
|
+
const { authApi } = useMongocampClientApi();
|
|
34
|
+
const rows = await authApi.userPermissions(collectionName ? { collection: collectionName } : {}, { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) });
|
|
35
|
+
const user = currentUser();
|
|
36
|
+
const byName = cache.value.user === user ? { ...cache.value.byName } : {};
|
|
37
|
+
for (const row of rows) byName[key(row.name)] = { read: row.read, write: row.write, administrate: row.administrate };
|
|
38
|
+
cache.value = { user, byName };
|
|
39
|
+
} catch {
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { canRead, canWrite, canAdminister, readableCollections, load };
|
|
43
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter, Ref } from 'vue';
|
|
2
|
+
import type { PersistOptions, StorageLike } from '../utils/persistedState.js';
|
|
3
|
+
export interface PersistedStateOptions<T> extends PersistOptions<T> {
|
|
4
|
+
/** Defaults to `localStorage` in the browser; `null` turns it off; nothing is stored on the server or when it is unreachable. */
|
|
5
|
+
storage?: StorageLike | null;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A ref whose value is remembered in `localStorage` under `nuxt-ui-mongocamp:<key>` (a versioned `{ v, data }` envelope).
|
|
9
|
+
* `key` may be a getter: when it changes the stored value for the new key is loaded (or the defaults). Stored values are
|
|
10
|
+
* merged over the defaults, so a field added later gets its default. Anything unreadable falls back to the defaults.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useMongocampPersistedState<T extends Record<string, unknown>>(key: MaybeRefOrGetter<string>, defaults: T, options?: PersistedStateOptions<T>): {
|
|
13
|
+
state: Ref<T>;
|
|
14
|
+
reset: () => void;
|
|
15
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ref, toValue, watch } from "vue";
|
|
2
|
+
import { browserStorage, clearPersisted, PERSISTED_STATE_PREFIX, readPersisted, writePersisted } from "../utils/persistedState.js";
|
|
3
|
+
export function useMongocampPersistedState(key, defaults, options = {}) {
|
|
4
|
+
const storage = options.storage === null ? void 0 : options.storage ?? (import.meta.client ? browserStorage() : void 0);
|
|
5
|
+
const fullKey = () => PERSISTED_STATE_PREFIX + toValue(key);
|
|
6
|
+
const load = () => ({ ...defaults, ...readPersisted(storage, fullKey(), options) });
|
|
7
|
+
const state = ref(load());
|
|
8
|
+
let loadedKey = fullKey();
|
|
9
|
+
watch(() => fullKey(), (next) => {
|
|
10
|
+
loadedKey = next;
|
|
11
|
+
state.value = load();
|
|
12
|
+
});
|
|
13
|
+
watch(state, (value) => {
|
|
14
|
+
writePersisted(storage, loadedKey, value, options.version, options.maxBytes);
|
|
15
|
+
}, { deep: true });
|
|
16
|
+
function reset() {
|
|
17
|
+
clearPersisted(storage, loadedKey);
|
|
18
|
+
state.value = { ...defaults };
|
|
19
|
+
}
|
|
20
|
+
return { state, reset };
|
|
21
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { useRuntimeConfig } from "#app";
|
|
2
2
|
import { Configuration } from "@sfxcode/nuxt-mongocamp-server/api";
|
|
3
3
|
import { createProxyMongocampApis } from "../utils/createProxyMongocampApis.js";
|
|
4
|
+
import { permissionDeniedMiddleware } from "../utils/permissionDenied.js";
|
|
4
5
|
export function useMongocampProxyApi() {
|
|
5
6
|
const config = useRuntimeConfig();
|
|
6
7
|
const serverProxyPath = config.public.nuxtUiMongocampOptions.serverProxyPath ?? "/api/_mongocamp";
|
|
7
|
-
const configuration = new Configuration({ basePath: serverProxyPath });
|
|
8
|
+
const configuration = new Configuration({ basePath: serverProxyPath, middleware: [permissionDeniedMiddleware] });
|
|
8
9
|
return createProxyMongocampApis(configuration);
|
|
9
10
|
}
|