@sfxcode/nuxt-ui-mongocamp 1.3.1 → 1.3.2
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.json +1 -1
- package/dist/runtime/components/MongocampAggregationBuilder.d.vue.ts +6 -0
- package/dist/runtime/components/MongocampAggregationBuilder.vue +390 -0
- package/dist/runtime/components/MongocampAggregationBuilder.vue.d.ts +6 -0
- package/dist/runtime/components/MongocampBulkDelete.d.vue.ts +11 -1
- package/dist/runtime/components/MongocampBulkDelete.vue +1 -0
- package/dist/runtime/components/MongocampBulkDelete.vue.d.ts +11 -1
- package/dist/runtime/components/MongocampBulkEdit.d.vue.ts +29 -0
- package/dist/runtime/components/MongocampBulkEdit.vue +138 -0
- package/dist/runtime/components/MongocampBulkEdit.vue.d.ts +29 -0
- package/dist/runtime/components/MongocampCollectionData.vue +100 -1
- package/dist/runtime/components/MongocampCollectionInfos.vue +162 -0
- package/dist/runtime/components/MongocampFilterBuilder.d.vue.ts +1 -0
- package/dist/runtime/components/MongocampFilterBuilder.vue +33 -2
- package/dist/runtime/components/MongocampFilterBuilder.vue.d.ts +1 -0
- package/dist/runtime/components/MongocampImport.d.vue.ts +10 -0
- package/dist/runtime/components/MongocampImport.vue +479 -0
- package/dist/runtime/components/MongocampImport.vue.d.ts +10 -0
- package/dist/runtime/composables/useMongocampAggregation.d.ts +28 -0
- package/dist/runtime/composables/useMongocampAggregation.js +99 -0
- package/dist/runtime/composables/useMongocampBulkDelete.d.ts +1 -1
- package/dist/runtime/composables/useMongocampImport.d.ts +14 -0
- package/dist/runtime/composables/useMongocampImport.js +25 -0
- package/dist/runtime/locales/de.json +140 -2
- package/dist/runtime/locales/en.json +140 -2
- package/dist/runtime/utils/aggregationErrors.d.ts +10 -0
- package/dist/runtime/utils/aggregationErrors.js +17 -0
- package/dist/runtime/utils/aggregationPipeline.d.ts +42 -0
- package/dist/runtime/utils/aggregationPipeline.js +118 -0
- package/dist/runtime/utils/aggregationResult.d.ts +5 -0
- package/dist/runtime/utils/aggregationResult.js +17 -0
- package/dist/runtime/utils/analyzeSamples.d.ts +27 -0
- package/dist/runtime/utils/analyzeSamples.js +92 -0
- package/dist/runtime/utils/batchInsert.d.ts +22 -0
- package/dist/runtime/utils/batchInsert.js +24 -0
- package/dist/runtime/utils/bulkDelete.d.ts +6 -1
- package/dist/runtime/utils/bulkDelete.js +28 -4
- package/dist/runtime/utils/bulkEdit.d.ts +27 -0
- package/dist/runtime/utils/bulkEdit.js +45 -0
- package/dist/runtime/utils/filterSuggestions.d.ts +8 -0
- package/dist/runtime/utils/filterSuggestions.js +15 -0
- package/dist/runtime/utils/importPipeline.d.ts +21 -0
- package/dist/runtime/utils/importPipeline.js +52 -0
- package/dist/runtime/utils/importRows.d.ts +36 -0
- package/dist/runtime/utils/importRows.js +228 -0
- package/dist/runtime/utils/objectId.d.ts +1 -0
- package/dist/runtime/utils/objectId.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { computed, ref, watch } from "vue";
|
|
3
|
+
import { useI18n, useMongocampError } from "#imports";
|
|
4
|
+
import { useMongocampImport } from "../composables/useMongocampImport";
|
|
5
|
+
import { DEFAULT_IMPORT_LIMITS } from "../utils/importRows";
|
|
6
|
+
import { detectImportFormat, prepareImport } from "../utils/importPipeline";
|
|
7
|
+
const props = defineProps({
|
|
8
|
+
collectionName: { type: String, required: true }
|
|
9
|
+
});
|
|
10
|
+
const emit = defineEmits(["imported"]);
|
|
11
|
+
const PREVIEW_ROWS = 5;
|
|
12
|
+
const ERROR_LIMIT = 10;
|
|
13
|
+
const { t } = useI18n();
|
|
14
|
+
const { describeError } = useMongocampError();
|
|
15
|
+
const { importRows } = useMongocampImport();
|
|
16
|
+
const isOpen = ref(false);
|
|
17
|
+
const step = ref("select");
|
|
18
|
+
const fileInputRef = ref(null);
|
|
19
|
+
const readError = ref("");
|
|
20
|
+
const fileName = ref("");
|
|
21
|
+
const fileText = ref("");
|
|
22
|
+
const format = ref("json");
|
|
23
|
+
const delimiter = ref(",");
|
|
24
|
+
const userColumnTypes = ref(null);
|
|
25
|
+
const emptyAsText = ref(false);
|
|
26
|
+
const stampMetadata = ref(true);
|
|
27
|
+
const progress = ref({ done: 0, total: 0 });
|
|
28
|
+
const attemptRows = ref([]);
|
|
29
|
+
const attemptSources = ref([]);
|
|
30
|
+
const failures = ref([]);
|
|
31
|
+
const totalInserted = ref(0);
|
|
32
|
+
const skipped = ref(0);
|
|
33
|
+
let controller = null;
|
|
34
|
+
const prepared = computed(() => {
|
|
35
|
+
if (!fileText.value) return null;
|
|
36
|
+
return prepareImport({
|
|
37
|
+
format: format.value,
|
|
38
|
+
text: fileText.value,
|
|
39
|
+
delimiter: delimiter.value,
|
|
40
|
+
columnTypes: format.value === "csv" ? userColumnTypes.value ?? void 0 : void 0,
|
|
41
|
+
emptyCells: emptyAsText.value ? "empty-string" : "omit"
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
const columnTypes = computed(() => userColumnTypes.value ?? prepared.value?.columnTypes ?? {});
|
|
45
|
+
const formatItems = computed(() => [
|
|
46
|
+
{ label: t("nuxtUiMongocamp.import.formatJson"), value: "json" },
|
|
47
|
+
{ label: t("nuxtUiMongocamp.import.formatCsv"), value: "csv" }
|
|
48
|
+
]);
|
|
49
|
+
const delimiterItems = computed(() => [
|
|
50
|
+
{ label: t("nuxtUiMongocamp.import.delimiterComma"), value: "," },
|
|
51
|
+
{ label: t("nuxtUiMongocamp.import.delimiterSemicolon"), value: ";" },
|
|
52
|
+
{ label: t("nuxtUiMongocamp.import.delimiterTab"), value: " " }
|
|
53
|
+
]);
|
|
54
|
+
const typeItems = computed(() => [
|
|
55
|
+
{ label: t("nuxtUiMongocamp.import.typeString"), value: "string" },
|
|
56
|
+
{ label: t("nuxtUiMongocamp.import.typeNumber"), value: "number" },
|
|
57
|
+
{ label: t("nuxtUiMongocamp.import.typeBoolean"), value: "boolean" },
|
|
58
|
+
{ label: t("nuxtUiMongocamp.import.typeDate"), value: "date-time" },
|
|
59
|
+
{ label: t("nuxtUiMongocamp.import.typeJson"), value: "json" }
|
|
60
|
+
]);
|
|
61
|
+
const readyCount = computed(() => prepared.value?.rows.length ?? 0);
|
|
62
|
+
const rowErrors = computed(() => (prepared.value?.errors ?? []).filter((e) => e.rowIndex >= 0 && e.code !== "too-many-rows"));
|
|
63
|
+
const fileErrors = computed(() => (prepared.value?.errors ?? []).filter((e) => e.rowIndex < 0 && e.code !== "too-many-rows"));
|
|
64
|
+
const visibleErrors = computed(() => [...rowErrors.value].sort((a, b) => a.rowIndex - b.rowIndex).slice(0, ERROR_LIMIT));
|
|
65
|
+
const hiddenErrorCount = computed(() => Math.max(0, rowErrors.value.length - ERROR_LIMIT));
|
|
66
|
+
const previewColumns = computed(() => (prepared.value?.columns ?? []).map((column) => ({
|
|
67
|
+
id: column,
|
|
68
|
+
header: column,
|
|
69
|
+
accessorFn: (row) => row[column]
|
|
70
|
+
})));
|
|
71
|
+
const previewRows = computed(() => (prepared.value?.rows ?? []).slice(0, PREVIEW_ROWS).map((row) => {
|
|
72
|
+
const shown = {};
|
|
73
|
+
for (const [key, value] of Object.entries(row)) shown[key] = typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
|
|
74
|
+
return shown;
|
|
75
|
+
}));
|
|
76
|
+
function errorText(error) {
|
|
77
|
+
const message = t(`nuxtUiMongocamp.import.error.${error.code}`);
|
|
78
|
+
if (error.rowIndex < 0) return message;
|
|
79
|
+
const where = error.column ? t("nuxtUiMongocamp.import.errorRowColumn", { row: error.rowIndex + 1, column: error.column }) : t("nuxtUiMongocamp.import.errorRow", { row: error.rowIndex + 1 });
|
|
80
|
+
return `${where}: ${message}`;
|
|
81
|
+
}
|
|
82
|
+
function resetState() {
|
|
83
|
+
controller?.abort();
|
|
84
|
+
controller = null;
|
|
85
|
+
step.value = "select";
|
|
86
|
+
readError.value = "";
|
|
87
|
+
fileName.value = "";
|
|
88
|
+
fileText.value = "";
|
|
89
|
+
format.value = "json";
|
|
90
|
+
delimiter.value = ",";
|
|
91
|
+
userColumnTypes.value = null;
|
|
92
|
+
emptyAsText.value = false;
|
|
93
|
+
stampMetadata.value = true;
|
|
94
|
+
progress.value = { done: 0, total: 0 };
|
|
95
|
+
attemptRows.value = [];
|
|
96
|
+
attemptSources.value = [];
|
|
97
|
+
failures.value = [];
|
|
98
|
+
totalInserted.value = 0;
|
|
99
|
+
skipped.value = 0;
|
|
100
|
+
}
|
|
101
|
+
function openModal() {
|
|
102
|
+
isOpen.value = true;
|
|
103
|
+
}
|
|
104
|
+
watch(isOpen, (open) => {
|
|
105
|
+
if (!open) resetState();
|
|
106
|
+
});
|
|
107
|
+
function openFilePicker() {
|
|
108
|
+
fileInputRef.value?.click();
|
|
109
|
+
}
|
|
110
|
+
async function handleFileSelected(event) {
|
|
111
|
+
const input = event.target;
|
|
112
|
+
const file = input.files?.[0];
|
|
113
|
+
input.value = "";
|
|
114
|
+
if (!file) return;
|
|
115
|
+
readError.value = "";
|
|
116
|
+
try {
|
|
117
|
+
const text = await file.text();
|
|
118
|
+
fileName.value = file.name;
|
|
119
|
+
fileText.value = text;
|
|
120
|
+
format.value = detectImportFormat(file.name, text);
|
|
121
|
+
delimiter.value = file.name.toLowerCase().endsWith(".tsv") ? " " : ",";
|
|
122
|
+
userColumnTypes.value = null;
|
|
123
|
+
step.value = "preview";
|
|
124
|
+
} catch {
|
|
125
|
+
readError.value = t("nuxtUiMongocamp.import.readError");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function backToSelect() {
|
|
129
|
+
fileName.value = "";
|
|
130
|
+
fileText.value = "";
|
|
131
|
+
userColumnTypes.value = null;
|
|
132
|
+
step.value = "select";
|
|
133
|
+
}
|
|
134
|
+
function onFormatChange(value) {
|
|
135
|
+
format.value = value;
|
|
136
|
+
userColumnTypes.value = null;
|
|
137
|
+
}
|
|
138
|
+
function onDelimiterChange(value) {
|
|
139
|
+
delimiter.value = value;
|
|
140
|
+
userColumnTypes.value = null;
|
|
141
|
+
}
|
|
142
|
+
function setColumnType(column, type) {
|
|
143
|
+
userColumnTypes.value = { ...columnTypes.value, [column]: type };
|
|
144
|
+
}
|
|
145
|
+
async function failureReason(error) {
|
|
146
|
+
const response = error?.response;
|
|
147
|
+
if (response) {
|
|
148
|
+
try {
|
|
149
|
+
const body = await response.clone().json();
|
|
150
|
+
if (typeof body?.msg === "string" && body.msg) return body.msg;
|
|
151
|
+
} catch {
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return describeError(error).message;
|
|
155
|
+
}
|
|
156
|
+
async function runImport(rows, sources) {
|
|
157
|
+
attemptRows.value = rows;
|
|
158
|
+
attemptSources.value = sources;
|
|
159
|
+
progress.value = { done: 0, total: rows.length };
|
|
160
|
+
failures.value = [];
|
|
161
|
+
skipped.value = 0;
|
|
162
|
+
step.value = "importing";
|
|
163
|
+
const run = new AbortController();
|
|
164
|
+
controller = run;
|
|
165
|
+
const result = await importRows(props.collectionName, rows, {
|
|
166
|
+
signal: run.signal,
|
|
167
|
+
stampMetadata: stampMetadata.value,
|
|
168
|
+
onProgress: (p) => {
|
|
169
|
+
progress.value = p;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
if (controller === run) controller = null;
|
|
173
|
+
if (!isOpen.value) return;
|
|
174
|
+
totalInserted.value += result.inserted;
|
|
175
|
+
skipped.value = result.skipped;
|
|
176
|
+
failures.value = await Promise.all(result.failedBatches.map(async (batch) => ({
|
|
177
|
+
start: batch.start,
|
|
178
|
+
count: batch.count,
|
|
179
|
+
from: sources[batch.start] + 1,
|
|
180
|
+
to: sources[batch.start + batch.count - 1] + 1,
|
|
181
|
+
reason: await failureReason(batch.error),
|
|
182
|
+
mayBePartial: batch.mayBePartial
|
|
183
|
+
})));
|
|
184
|
+
step.value = "done";
|
|
185
|
+
if (result.inserted > 0) emit("imported", result.inserted);
|
|
186
|
+
}
|
|
187
|
+
async function startImport() {
|
|
188
|
+
if (!prepared.value || prepared.value.rows.length === 0) return;
|
|
189
|
+
await runImport(prepared.value.rows, prepared.value.sourceIndexes);
|
|
190
|
+
}
|
|
191
|
+
async function retryFailed() {
|
|
192
|
+
const rows = [];
|
|
193
|
+
const sources = [];
|
|
194
|
+
for (const range of failures.value) {
|
|
195
|
+
rows.push(...attemptRows.value.slice(range.start, range.start + range.count));
|
|
196
|
+
sources.push(...attemptSources.value.slice(range.start, range.start + range.count));
|
|
197
|
+
}
|
|
198
|
+
if (rows.length > 0) await runImport(rows, sources);
|
|
199
|
+
}
|
|
200
|
+
function stopImport() {
|
|
201
|
+
controller?.abort();
|
|
202
|
+
}
|
|
203
|
+
const anyMayBePartial = computed(() => failures.value.some((f) => f.mayBePartial));
|
|
204
|
+
const progressPercent = computed(() => progress.value.total === 0 ? 0 : Math.round(progress.value.done / progress.value.total * 100));
|
|
205
|
+
const limitsText = computed(() => ({ maxRows: DEFAULT_IMPORT_LIMITS.maxRows, maxMb: DEFAULT_IMPORT_LIMITS.maxBytes / (1024 * 1024) }));
|
|
206
|
+
</script>
|
|
207
|
+
|
|
208
|
+
<template>
|
|
209
|
+
<UTooltip :text="t('nuxtUiMongocamp.import.trigger')">
|
|
210
|
+
<UButton
|
|
211
|
+
icon="i-lucide-file-up"
|
|
212
|
+
color="neutral"
|
|
213
|
+
variant="ghost"
|
|
214
|
+
size="sm"
|
|
215
|
+
:aria-label="t('nuxtUiMongocamp.import.trigger')"
|
|
216
|
+
@click="openModal"
|
|
217
|
+
/>
|
|
218
|
+
</UTooltip>
|
|
219
|
+
|
|
220
|
+
<UModal
|
|
221
|
+
v-model:open="isOpen"
|
|
222
|
+
:title="t('nuxtUiMongocamp.import.title')"
|
|
223
|
+
:dismissible="step !== 'importing'"
|
|
224
|
+
:ui="{ footer: 'justify-end', content: 'sm:max-w-3xl' }"
|
|
225
|
+
>
|
|
226
|
+
<template #body>
|
|
227
|
+
<div
|
|
228
|
+
v-if="step === 'select'"
|
|
229
|
+
class="flex flex-col items-start gap-3"
|
|
230
|
+
>
|
|
231
|
+
<p class="font-medium">
|
|
232
|
+
{{ t("nuxtUiMongocamp.import.chooseFile") }}
|
|
233
|
+
</p>
|
|
234
|
+
<p class="text-sm text-dimmed">
|
|
235
|
+
{{ t("nuxtUiMongocamp.import.hint", limitsText) }}
|
|
236
|
+
</p>
|
|
237
|
+
<input
|
|
238
|
+
ref="fileInputRef"
|
|
239
|
+
type="file"
|
|
240
|
+
class="hidden"
|
|
241
|
+
accept=".json,.csv,.tsv,.txt,application/json,text/csv"
|
|
242
|
+
data-testid="import-file-input"
|
|
243
|
+
@change="handleFileSelected"
|
|
244
|
+
>
|
|
245
|
+
<UButton
|
|
246
|
+
icon="i-lucide-folder-open"
|
|
247
|
+
:label="t('nuxtUiMongocamp.import.chooseButton')"
|
|
248
|
+
@click="openFilePicker"
|
|
249
|
+
/>
|
|
250
|
+
<UAlert
|
|
251
|
+
v-if="readError"
|
|
252
|
+
color="error"
|
|
253
|
+
variant="subtle"
|
|
254
|
+
icon="i-lucide-circle-alert"
|
|
255
|
+
:description="readError"
|
|
256
|
+
/>
|
|
257
|
+
</div>
|
|
258
|
+
|
|
259
|
+
<div
|
|
260
|
+
v-else-if="step === 'preview' && prepared"
|
|
261
|
+
class="flex flex-col gap-4"
|
|
262
|
+
>
|
|
263
|
+
<div class="flex flex-wrap items-end gap-3">
|
|
264
|
+
<span class="text-sm font-medium break-all">{{ fileName }}</span>
|
|
265
|
+
<USelect
|
|
266
|
+
:model-value="format"
|
|
267
|
+
:items="formatItems"
|
|
268
|
+
size="sm"
|
|
269
|
+
class="w-28"
|
|
270
|
+
:aria-label="t('nuxtUiMongocamp.import.format')"
|
|
271
|
+
@update:model-value="onFormatChange"
|
|
272
|
+
/>
|
|
273
|
+
<USelect
|
|
274
|
+
v-if="format === 'csv'"
|
|
275
|
+
:model-value="delimiter"
|
|
276
|
+
:items="delimiterItems"
|
|
277
|
+
size="sm"
|
|
278
|
+
class="w-40"
|
|
279
|
+
:aria-label="t('nuxtUiMongocamp.import.delimiter')"
|
|
280
|
+
@update:model-value="onDelimiterChange"
|
|
281
|
+
/>
|
|
282
|
+
</div>
|
|
283
|
+
|
|
284
|
+
<UAlert
|
|
285
|
+
v-for="error in fileErrors"
|
|
286
|
+
:key="error.code"
|
|
287
|
+
color="error"
|
|
288
|
+
variant="subtle"
|
|
289
|
+
icon="i-lucide-circle-alert"
|
|
290
|
+
:description="errorText(error)"
|
|
291
|
+
/>
|
|
292
|
+
|
|
293
|
+
<template v-if="fileErrors.length === 0">
|
|
294
|
+
<p class="text-sm font-medium">
|
|
295
|
+
{{ t("nuxtUiMongocamp.import.summary", { ready: readyCount, total: prepared.sourceRowCount }) }}
|
|
296
|
+
</p>
|
|
297
|
+
<UAlert
|
|
298
|
+
v-if="prepared.truncated"
|
|
299
|
+
color="warning"
|
|
300
|
+
variant="subtle"
|
|
301
|
+
icon="i-lucide-triangle-alert"
|
|
302
|
+
:description="t('nuxtUiMongocamp.import.truncated', { maxRows: limitsText.maxRows })"
|
|
303
|
+
/>
|
|
304
|
+
|
|
305
|
+
<div
|
|
306
|
+
v-if="format === 'csv' && prepared.columns.length > 0"
|
|
307
|
+
class="flex flex-col gap-2"
|
|
308
|
+
>
|
|
309
|
+
<p class="text-sm font-medium">
|
|
310
|
+
{{ t("nuxtUiMongocamp.import.columnTypes") }}
|
|
311
|
+
</p>
|
|
312
|
+
<div class="grid grid-cols-1 gap-2 sm:grid-cols-2">
|
|
313
|
+
<div
|
|
314
|
+
v-for="column in prepared.columns"
|
|
315
|
+
:key="column"
|
|
316
|
+
class="flex items-center gap-2"
|
|
317
|
+
>
|
|
318
|
+
<span class="w-32 truncate text-sm">{{ column }}</span>
|
|
319
|
+
<USelect
|
|
320
|
+
:model-value="columnTypes[column]"
|
|
321
|
+
:items="typeItems"
|
|
322
|
+
size="sm"
|
|
323
|
+
class="w-32"
|
|
324
|
+
:aria-label="column"
|
|
325
|
+
@update:model-value="setColumnType(column, $event)"
|
|
326
|
+
/>
|
|
327
|
+
</div>
|
|
328
|
+
</div>
|
|
329
|
+
<USwitch
|
|
330
|
+
v-model="emptyAsText"
|
|
331
|
+
:label="t('nuxtUiMongocamp.import.emptyAsText')"
|
|
332
|
+
/>
|
|
333
|
+
</div>
|
|
334
|
+
|
|
335
|
+
<UAlert
|
|
336
|
+
v-if="rowErrors.length > 0"
|
|
337
|
+
color="warning"
|
|
338
|
+
variant="subtle"
|
|
339
|
+
icon="i-lucide-triangle-alert"
|
|
340
|
+
:title="t('nuxtUiMongocamp.import.problems', { count: rowErrors.length }, rowErrors.length)"
|
|
341
|
+
>
|
|
342
|
+
<template #description>
|
|
343
|
+
<ul class="list-disc pl-5 text-sm">
|
|
344
|
+
<li
|
|
345
|
+
v-for="error in visibleErrors"
|
|
346
|
+
:key="`${error.rowIndex}-${error.column ?? ''}-${error.code}`"
|
|
347
|
+
>
|
|
348
|
+
{{ errorText(error) }}
|
|
349
|
+
</li>
|
|
350
|
+
</ul>
|
|
351
|
+
<p
|
|
352
|
+
v-if="hiddenErrorCount > 0"
|
|
353
|
+
class="mt-1 text-sm"
|
|
354
|
+
>
|
|
355
|
+
{{ t("nuxtUiMongocamp.import.moreErrors", { count: hiddenErrorCount }) }}
|
|
356
|
+
</p>
|
|
357
|
+
</template>
|
|
358
|
+
</UAlert>
|
|
359
|
+
|
|
360
|
+
<div v-if="previewRows.length > 0">
|
|
361
|
+
<p class="mb-1 text-sm font-medium">
|
|
362
|
+
{{ t("nuxtUiMongocamp.import.previewTitle", { count: previewRows.length }) }}
|
|
363
|
+
</p>
|
|
364
|
+
<UTable
|
|
365
|
+
:data="previewRows"
|
|
366
|
+
:columns="previewColumns"
|
|
367
|
+
class="max-h-64"
|
|
368
|
+
/>
|
|
369
|
+
</div>
|
|
370
|
+
|
|
371
|
+
<USwitch
|
|
372
|
+
v-model="stampMetadata"
|
|
373
|
+
:label="t('nuxtUiMongocamp.import.stampMetadata')"
|
|
374
|
+
/>
|
|
375
|
+
</template>
|
|
376
|
+
</div>
|
|
377
|
+
|
|
378
|
+
<div
|
|
379
|
+
v-else-if="step === 'importing'"
|
|
380
|
+
class="flex flex-col gap-3"
|
|
381
|
+
>
|
|
382
|
+
<p class="text-sm">
|
|
383
|
+
{{ t("nuxtUiMongocamp.import.importing", progress) }}
|
|
384
|
+
</p>
|
|
385
|
+
<UProgress
|
|
386
|
+
:model-value="progressPercent"
|
|
387
|
+
:aria-label="t('nuxtUiMongocamp.import.importing', progress)"
|
|
388
|
+
/>
|
|
389
|
+
</div>
|
|
390
|
+
|
|
391
|
+
<div
|
|
392
|
+
v-else-if="step === 'done'"
|
|
393
|
+
class="flex flex-col gap-3"
|
|
394
|
+
>
|
|
395
|
+
<UAlert
|
|
396
|
+
:color="totalInserted > 0 ? 'success' : 'neutral'"
|
|
397
|
+
variant="subtle"
|
|
398
|
+
:icon="totalInserted > 0 ? 'i-lucide-circle-check' : 'i-lucide-info'"
|
|
399
|
+
:title="t('nuxtUiMongocamp.import.result', { count: totalInserted }, totalInserted)"
|
|
400
|
+
/>
|
|
401
|
+
<UAlert
|
|
402
|
+
v-if="skipped > 0"
|
|
403
|
+
color="warning"
|
|
404
|
+
variant="subtle"
|
|
405
|
+
icon="i-lucide-triangle-alert"
|
|
406
|
+
:description="t('nuxtUiMongocamp.import.aborted', { count: skipped })"
|
|
407
|
+
/>
|
|
408
|
+
<UAlert
|
|
409
|
+
v-if="failures.length > 0"
|
|
410
|
+
color="error"
|
|
411
|
+
variant="subtle"
|
|
412
|
+
icon="i-lucide-circle-alert"
|
|
413
|
+
>
|
|
414
|
+
<template #description>
|
|
415
|
+
<ul class="list-disc pl-5 text-sm">
|
|
416
|
+
<li
|
|
417
|
+
v-for="failure in failures"
|
|
418
|
+
:key="failure.start"
|
|
419
|
+
>
|
|
420
|
+
{{ t("nuxtUiMongocamp.import.failedBatch", { from: failure.from, to: failure.to, reason: failure.reason }) }}
|
|
421
|
+
</li>
|
|
422
|
+
</ul>
|
|
423
|
+
<p
|
|
424
|
+
v-if="anyMayBePartial"
|
|
425
|
+
class="mt-2 text-sm"
|
|
426
|
+
>
|
|
427
|
+
{{ t("nuxtUiMongocamp.import.maybePartial") }}
|
|
428
|
+
</p>
|
|
429
|
+
</template>
|
|
430
|
+
</UAlert>
|
|
431
|
+
</div>
|
|
432
|
+
</template>
|
|
433
|
+
|
|
434
|
+
<template #footer>
|
|
435
|
+
<template v-if="step === 'preview'">
|
|
436
|
+
<UButton
|
|
437
|
+
:label="t('nuxtUiMongocamp.import.back')"
|
|
438
|
+
color="neutral"
|
|
439
|
+
variant="ghost"
|
|
440
|
+
@click="backToSelect"
|
|
441
|
+
/>
|
|
442
|
+
<UButton
|
|
443
|
+
:label="t('nuxtUiMongocamp.import.importButton', { count: readyCount }, readyCount)"
|
|
444
|
+
:disabled="readyCount === 0"
|
|
445
|
+
@click="startImport"
|
|
446
|
+
/>
|
|
447
|
+
</template>
|
|
448
|
+
<template v-else-if="step === 'importing'">
|
|
449
|
+
<UButton
|
|
450
|
+
:label="t('nuxtUiMongocamp.import.stop')"
|
|
451
|
+
color="neutral"
|
|
452
|
+
variant="subtle"
|
|
453
|
+
@click="stopImport"
|
|
454
|
+
/>
|
|
455
|
+
</template>
|
|
456
|
+
<template v-else-if="step === 'done'">
|
|
457
|
+
<UButton
|
|
458
|
+
v-if="failures.length > 0"
|
|
459
|
+
:label="t('nuxtUiMongocamp.import.retryFailed')"
|
|
460
|
+
color="neutral"
|
|
461
|
+
variant="subtle"
|
|
462
|
+
@click="retryFailed"
|
|
463
|
+
/>
|
|
464
|
+
<UButton
|
|
465
|
+
:label="t('nuxtUiMongocamp.import.close')"
|
|
466
|
+
@click="isOpen = false"
|
|
467
|
+
/>
|
|
468
|
+
</template>
|
|
469
|
+
<template v-else>
|
|
470
|
+
<UButton
|
|
471
|
+
:label="t('nuxtUiMongocamp.common.cancel')"
|
|
472
|
+
color="neutral"
|
|
473
|
+
variant="ghost"
|
|
474
|
+
@click="isOpen = false"
|
|
475
|
+
/>
|
|
476
|
+
</template>
|
|
477
|
+
</template>
|
|
478
|
+
</UModal>
|
|
479
|
+
</template>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type __VLS_Props = {
|
|
2
|
+
collectionName: string;
|
|
3
|
+
};
|
|
4
|
+
declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
5
|
+
imported: (count: number) => any;
|
|
6
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
|
|
7
|
+
onImported?: ((count: number) => any) | undefined;
|
|
8
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
9
|
+
declare const _default: typeof __VLS_export;
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RequestStage } from '../utils/aggregationPipeline.js';
|
|
2
|
+
export interface AggregationPaging {
|
|
3
|
+
page?: number;
|
|
4
|
+
rowsPerPage?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface AggregationResult {
|
|
7
|
+
rows: Record<string, unknown>[];
|
|
8
|
+
total: number;
|
|
9
|
+
pages: number;
|
|
10
|
+
page: number;
|
|
11
|
+
}
|
|
12
|
+
export type DistinctValue = string | number | boolean;
|
|
13
|
+
export interface DistinctResult {
|
|
14
|
+
values: DistinctValue[];
|
|
15
|
+
total: number;
|
|
16
|
+
truncated: boolean;
|
|
17
|
+
}
|
|
18
|
+
export type AggregationErrorKind = 'forbidden' | 'retryable' | 'mongo' | 'network' | 'unauthorized' | 'forbidden-access' | 'other';
|
|
19
|
+
export interface DescribedAggregationError {
|
|
20
|
+
kind: AggregationErrorKind;
|
|
21
|
+
message: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function useMongocampAggregation(): {
|
|
24
|
+
runAggregation: (collectionName: string, pipeline: RequestStage[], paging?: AggregationPaging) => Promise<AggregationResult>;
|
|
25
|
+
listDistinct: (collectionName: string, field: string, limit?: number) => Promise<DistinctResult>;
|
|
26
|
+
sampleDocuments: (collectionName: string, size?: number) => Promise<AggregationResult>;
|
|
27
|
+
describeAggregationError: (error: unknown) => Promise<DescribedAggregationError>;
|
|
28
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { useMongocampClientApi, useI18n } from "#imports";
|
|
2
|
+
import { useMongocampError } from "./useMongocampError.js";
|
|
3
|
+
import { findForbidden } from "../utils/aggregationPipeline.js";
|
|
4
|
+
import { ForbiddenPipelineError, parseServerError } from "../utils/aggregationErrors.js";
|
|
5
|
+
const AGGREGATE_TIMEOUT_MS = 3e4;
|
|
6
|
+
const REQUEST_TIMEOUT_MS = 15e3;
|
|
7
|
+
const DEFAULT_SAMPLE_SIZE = 500;
|
|
8
|
+
const MAX_SAMPLE_SIZE = 1e3;
|
|
9
|
+
const DEFAULT_DISTINCT_LIMIT = 100;
|
|
10
|
+
function withTimeout(ms) {
|
|
11
|
+
return { signal: AbortSignal.timeout(ms) };
|
|
12
|
+
}
|
|
13
|
+
function isPlainObject(value) {
|
|
14
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
function headerNumber(headers, name) {
|
|
17
|
+
const parsed = Number.parseInt(headers?.get(name) ?? "", 10);
|
|
18
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
19
|
+
}
|
|
20
|
+
async function responseMessage(error) {
|
|
21
|
+
const response = error?.response;
|
|
22
|
+
if (!response) return void 0;
|
|
23
|
+
try {
|
|
24
|
+
const body = await response.clone().json();
|
|
25
|
+
return typeof body?.msg === "string" ? body.msg : void 0;
|
|
26
|
+
} catch {
|
|
27
|
+
return void 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function isEmptyResultError(error) {
|
|
31
|
+
const status = error?.response?.status;
|
|
32
|
+
if (status !== 500) return false;
|
|
33
|
+
const message = await responseMessage(error);
|
|
34
|
+
return message !== void 0 && parseServerError(message).kind === "empty-result";
|
|
35
|
+
}
|
|
36
|
+
export function useMongocampAggregation() {
|
|
37
|
+
const { collectionApi } = useMongocampClientApi();
|
|
38
|
+
const { describeError } = useMongocampError();
|
|
39
|
+
const { t } = useI18n();
|
|
40
|
+
async function runAggregation(collectionName, pipeline, paging = {}) {
|
|
41
|
+
const findings = findForbidden(pipeline);
|
|
42
|
+
if (findings.length > 0) throw new ForbiddenPipelineError(findings);
|
|
43
|
+
const page = paging.page ?? 1;
|
|
44
|
+
let response;
|
|
45
|
+
try {
|
|
46
|
+
response = await collectionApi.aggregateRaw({
|
|
47
|
+
collectionName,
|
|
48
|
+
// the generated type says string values; the server takes any JSON. allowDiskUse is required.
|
|
49
|
+
mongoAggregateRequest: { pipeline, allowDiskUse: false },
|
|
50
|
+
rowsPerPage: paging.rowsPerPage,
|
|
51
|
+
page
|
|
52
|
+
}, withTimeout(AGGREGATE_TIMEOUT_MS));
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (await isEmptyResultError(error)) return { rows: [], total: 0, pages: 0, page };
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
const value = await response.value();
|
|
58
|
+
const rows = Array.isArray(value) ? value.filter(isPlainObject) : [];
|
|
59
|
+
const headers = response.raw?.headers;
|
|
60
|
+
return {
|
|
61
|
+
rows,
|
|
62
|
+
total: headerNumber(headers, "x-pagination-count-rows") ?? rows.length,
|
|
63
|
+
pages: headerNumber(headers, "x-pagination-count-pages") ?? 1,
|
|
64
|
+
page: headerNumber(headers, "x-pagination-current-page") ?? page
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async function listDistinct(collectionName, field, limit = DEFAULT_DISTINCT_LIMIT) {
|
|
68
|
+
const response = await collectionApi.distinctRaw({ collectionName, field, rowsPerPage: limit, page: 1 }, withTimeout(REQUEST_TIMEOUT_MS));
|
|
69
|
+
const value = await response.value();
|
|
70
|
+
const values = (Array.isArray(value) ? value : []).filter((v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean");
|
|
71
|
+
const total = headerNumber(response.raw?.headers, "x-pagination-count-rows") ?? values.length;
|
|
72
|
+
return { values, total, truncated: total > values.length };
|
|
73
|
+
}
|
|
74
|
+
async function sampleDocuments(collectionName, size = DEFAULT_SAMPLE_SIZE) {
|
|
75
|
+
const clamped = Math.min(Math.max(Math.floor(size) || DEFAULT_SAMPLE_SIZE, 1), MAX_SAMPLE_SIZE);
|
|
76
|
+
return runAggregation(collectionName, [{ stage: "$sample", value: { size: clamped } }], { page: 1, rowsPerPage: clamped });
|
|
77
|
+
}
|
|
78
|
+
async function describeAggregationError(error) {
|
|
79
|
+
if (error instanceof ForbiddenPipelineError) {
|
|
80
|
+
return { kind: "forbidden", message: t("nuxtUiMongocamp.aggregation.errors.forbidden", { operator: error.findings[0]?.operator ?? "" }) };
|
|
81
|
+
}
|
|
82
|
+
const response = error?.response;
|
|
83
|
+
const status = response?.status;
|
|
84
|
+
if (status === 405) return { kind: "retryable", message: t("nuxtUiMongocamp.aggregation.errors.retryable") };
|
|
85
|
+
if (response && status !== void 0 && status >= 400 && status !== 401 && status !== 403) {
|
|
86
|
+
try {
|
|
87
|
+
const body = await response.clone().json();
|
|
88
|
+
if (typeof body?.msg === "string" && body.msg) {
|
|
89
|
+
const info = parseServerError(body.msg);
|
|
90
|
+
if (info.kind !== "empty-result" && info.message) return { kind: info.kind, message: info.message };
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const described = describeError(error);
|
|
96
|
+
return { kind: described.kind === "forbidden" ? "forbidden-access" : described.kind, message: described.message };
|
|
97
|
+
}
|
|
98
|
+
return { runAggregation, listDistinct, sampleDocuments, describeAggregationError };
|
|
99
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { runBulkDelete } from '../utils/bulkDelete.js';
|
|
2
|
-
export type { BulkDeleteResult } from '../utils/bulkDelete.js';
|
|
2
|
+
export type { BulkDeleteResult, BulkRemoveMany } from '../utils/bulkDelete.js';
|
|
3
3
|
export declare function useMongocampBulkDelete(): {
|
|
4
4
|
runBulkDelete: typeof runBulkDelete;
|
|
5
5
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BatchInsertResult } from '../utils/batchInsert.js';
|
|
2
|
+
export type { BatchInsertResult } from '../utils/batchInsert.js';
|
|
3
|
+
export interface ImportRowsOptions {
|
|
4
|
+
batchSize?: number;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
onProgress?: (progress: {
|
|
7
|
+
done: number;
|
|
8
|
+
total: number;
|
|
9
|
+
}) => void;
|
|
10
|
+
stampMetadata?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function useMongocampImport(): {
|
|
13
|
+
importRows: (collectionName: string, rows: Record<string, unknown>[], options?: ImportRowsOptions) => Promise<BatchInsertResult>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useMongocampClientApi } from "#imports";
|
|
2
|
+
import useMongocampDocument from "./useMongocampDocument.js";
|
|
3
|
+
import { runBatchInsert } from "../utils/batchInsert.js";
|
|
4
|
+
const BATCH_TIMEOUT_MS = 6e4;
|
|
5
|
+
function isRejectedBeforeWrite(error) {
|
|
6
|
+
const status = error?.response?.status;
|
|
7
|
+
return status === 400;
|
|
8
|
+
}
|
|
9
|
+
export function useMongocampImport() {
|
|
10
|
+
const { documentApi } = useMongocampClientApi();
|
|
11
|
+
const { ensureMetaData } = useMongocampDocument();
|
|
12
|
+
async function importRows(collectionName, rows, options = {}) {
|
|
13
|
+
const prepared = options.stampMetadata ? rows.map((row) => ensureMetaData({ ...row })) : rows;
|
|
14
|
+
return runBatchInsert(
|
|
15
|
+
prepared,
|
|
16
|
+
(batch) => {
|
|
17
|
+
const timeout = AbortSignal.timeout(BATCH_TIMEOUT_MS);
|
|
18
|
+
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
19
|
+
return documentApi.insertMany({ collectionName, requestBody: batch }, { signal });
|
|
20
|
+
},
|
|
21
|
+
{ batchSize: options.batchSize, signal: options.signal, onProgress: options.onProgress, isAtomicFailure: isRejectedBeforeWrite }
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
return { importRows };
|
|
25
|
+
}
|