@sfxcode/nuxt-ui-mongocamp 1.3.0 → 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/components/MongocampRoleGrants.vue +25 -0
- package/dist/runtime/components/MongocampRoles.vue +74 -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 +147 -2
- package/dist/runtime/locales/en.json +147 -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 +2 -2
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
export const FORBIDDEN_OPERATORS = [
|
|
2
|
+
"$out",
|
|
3
|
+
"$merge",
|
|
4
|
+
"$function",
|
|
5
|
+
"$accumulator",
|
|
6
|
+
"$where",
|
|
7
|
+
"$lookup",
|
|
8
|
+
"$unionWith",
|
|
9
|
+
"$graphLookup"
|
|
10
|
+
];
|
|
11
|
+
export const VISUAL_STAGE_TYPES = ["$match", "$group", "$sort", "$project", "$limit"];
|
|
12
|
+
const MAX_TEXT_LENGTH = 2e5;
|
|
13
|
+
const MAX_STAGES = 50;
|
|
14
|
+
const MAX_DEPTH = 64;
|
|
15
|
+
const MAX_FINDINGS = 20;
|
|
16
|
+
const FORBIDDEN = new Set(FORBIDDEN_OPERATORS);
|
|
17
|
+
function isPlainObject(value) {
|
|
18
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
function scan(value, path, depth, stageIndex, found) {
|
|
21
|
+
if (found.length >= MAX_FINDINGS) return;
|
|
22
|
+
if (depth > MAX_DEPTH) {
|
|
23
|
+
found.push({ stageIndex, operator: "too-deep", path });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
value.forEach((item, i) => scan(item, `${path}[${i}]`, depth + 1, stageIndex, found));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (!isPlainObject(value)) return;
|
|
31
|
+
for (const [key, inner] of Object.entries(value)) {
|
|
32
|
+
const keyPath = `${path}.${key}`;
|
|
33
|
+
if (FORBIDDEN.has(key)) found.push({ stageIndex, operator: key, path: keyPath });
|
|
34
|
+
scan(inner, keyPath, depth + 1, stageIndex, found);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function findForbidden(pipeline) {
|
|
38
|
+
const found = [];
|
|
39
|
+
pipeline.forEach((stage, stageIndex) => {
|
|
40
|
+
if (FORBIDDEN.has(stage.stage)) found.push({ stageIndex, operator: stage.stage, path: stage.stage });
|
|
41
|
+
scan(stage.value, stage.stage, 0, stageIndex, found);
|
|
42
|
+
});
|
|
43
|
+
return found;
|
|
44
|
+
}
|
|
45
|
+
function isEmptyValue(value) {
|
|
46
|
+
return value === void 0 || value === null || value === "" || isPlainObject(value) && Object.keys(value).length === 0;
|
|
47
|
+
}
|
|
48
|
+
function normaliseValue(type, value) {
|
|
49
|
+
if (type === "$limit" && typeof value === "string" && /^\d+$/.test(value.trim())) return Number(value.trim());
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
export function buildPipeline(stages) {
|
|
53
|
+
const pipeline = [];
|
|
54
|
+
for (const { type, value } of stages) {
|
|
55
|
+
const normalised = normaliseValue(type, value);
|
|
56
|
+
if (!isEmptyValue(normalised)) pipeline.push({ stage: type, value: normalised });
|
|
57
|
+
}
|
|
58
|
+
return pipeline;
|
|
59
|
+
}
|
|
60
|
+
export function validateStages(stages) {
|
|
61
|
+
const issues = [];
|
|
62
|
+
stages.forEach(({ type, value: raw }, stageIndex) => {
|
|
63
|
+
const value = normaliseValue(type, raw);
|
|
64
|
+
if (isEmptyValue(value)) return;
|
|
65
|
+
if (type === "$limit") {
|
|
66
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1) issues.push({ stageIndex, code: "invalid-limit" });
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (!isPlainObject(value)) {
|
|
70
|
+
issues.push({ stageIndex, code: "not-object" });
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (type === "$group" && !("_id" in value)) issues.push({ stageIndex, code: "group-needs-id" });
|
|
74
|
+
if (type === "$sort" && !Object.values(value).every((direction) => direction === 1 || direction === -1)) issues.push({ stageIndex, code: "invalid-sort" });
|
|
75
|
+
});
|
|
76
|
+
return issues;
|
|
77
|
+
}
|
|
78
|
+
export function parsePipeline(text) {
|
|
79
|
+
if (text.length > MAX_TEXT_LENGTH) return { ok: false, error: { code: "too-large" } };
|
|
80
|
+
let data;
|
|
81
|
+
try {
|
|
82
|
+
data = JSON.parse(text);
|
|
83
|
+
} catch {
|
|
84
|
+
return { ok: false, error: { code: "invalid-json" } };
|
|
85
|
+
}
|
|
86
|
+
if (!Array.isArray(data)) return { ok: false, error: { code: "not-array" } };
|
|
87
|
+
if (data.length > MAX_STAGES) return { ok: false, error: { code: "too-many-stages" } };
|
|
88
|
+
const pipeline = [];
|
|
89
|
+
for (const [stageIndex, item] of data.entries()) {
|
|
90
|
+
const keys = isPlainObject(item) ? Object.keys(item) : [];
|
|
91
|
+
if (isPlainObject(item) && keys.length === 1 && keys[0].startsWith("$")) {
|
|
92
|
+
pipeline.push({ stage: keys[0], value: item[keys[0]] });
|
|
93
|
+
} else if (isPlainObject(item) && keys.length === 2 && keys.includes("stage") && keys.includes("value") && typeof item.stage === "string" && item.stage.startsWith("$")) {
|
|
94
|
+
pipeline.push({ stage: item.stage, value: item.value });
|
|
95
|
+
} else {
|
|
96
|
+
return { ok: false, error: { code: "stage-shape", stageIndex } };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const forbidden = findForbidden(pipeline)[0];
|
|
100
|
+
if (forbidden) {
|
|
101
|
+
return {
|
|
102
|
+
ok: false,
|
|
103
|
+
error: { code: forbidden.operator === "too-deep" ? "too-deep" : "forbidden-operator", stageIndex: forbidden.stageIndex, operator: forbidden.operator, path: forbidden.path }
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
return { ok: true, pipeline };
|
|
107
|
+
}
|
|
108
|
+
export function pipelineToText(pipeline) {
|
|
109
|
+
return JSON.stringify(pipeline.map((stage) => ({ [stage.stage]: stage.value })), null, 2);
|
|
110
|
+
}
|
|
111
|
+
export function toVisualStages(pipeline, makeId = (index) => `stage-${index}`) {
|
|
112
|
+
const stages = [];
|
|
113
|
+
for (const [index, { stage, value }] of pipeline.entries()) {
|
|
114
|
+
if (!VISUAL_STAGE_TYPES.includes(stage)) return null;
|
|
115
|
+
stages.push({ id: makeId(index), type: stage, value });
|
|
116
|
+
}
|
|
117
|
+
return stages;
|
|
118
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const MAX_COLUMNS = 100;
|
|
2
|
+
const MAX_ROWS_FOR_COLUMNS = 200;
|
|
3
|
+
const MAX_CELL_LENGTH = 80;
|
|
4
|
+
export function resultColumns(rows) {
|
|
5
|
+
const keys = /* @__PURE__ */ new Set();
|
|
6
|
+
for (const row of rows.slice(0, MAX_ROWS_FOR_COLUMNS)) {
|
|
7
|
+
for (const key of Object.keys(row)) {
|
|
8
|
+
keys.add(key);
|
|
9
|
+
if (keys.size >= MAX_COLUMNS) break;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return keys.has("_id") ? ["_id", ...[...keys].filter((key) => key !== "_id")] : [...keys];
|
|
13
|
+
}
|
|
14
|
+
export function formatCell(value) {
|
|
15
|
+
const full = value === void 0 ? "" : typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
|
|
16
|
+
return { text: full.length > MAX_CELL_LENGTH ? `${full.slice(0, MAX_CELL_LENGTH - 1)}\u2026` : full, full };
|
|
17
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type ValueKind = 'string' | 'number' | 'boolean' | 'date' | 'object' | 'array' | 'null';
|
|
2
|
+
export interface FieldStatistic {
|
|
3
|
+
path: string;
|
|
4
|
+
present: number;
|
|
5
|
+
share: number;
|
|
6
|
+
types: {
|
|
7
|
+
type: ValueKind;
|
|
8
|
+
count: number;
|
|
9
|
+
}[];
|
|
10
|
+
topValues?: {
|
|
11
|
+
value: string;
|
|
12
|
+
count: number;
|
|
13
|
+
}[];
|
|
14
|
+
}
|
|
15
|
+
export interface SampleAnalysis {
|
|
16
|
+
sampled: number;
|
|
17
|
+
fields: FieldStatistic[];
|
|
18
|
+
truncated: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface AnalyzeOptions {
|
|
21
|
+
maxDepth?: number;
|
|
22
|
+
maxFields?: number;
|
|
23
|
+
topValues?: number;
|
|
24
|
+
maxDistinctForTop?: number;
|
|
25
|
+
}
|
|
26
|
+
export declare function kindOf(value: unknown): ValueKind;
|
|
27
|
+
export declare function analyzeSamples(rows: unknown[], options?: AnalyzeOptions): SampleAnalysis;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { isIsoDateTimeString } from "./isoDate.js";
|
|
2
|
+
const DEFAULTS = { maxDepth: 3, maxFields: 200, topValues: 5, maxDistinctForTop: 20 };
|
|
3
|
+
const MAX_VALUE_LENGTH = 40;
|
|
4
|
+
function isPlainObject(value) {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
export function kindOf(value) {
|
|
8
|
+
if (value === null || value === void 0) return "null";
|
|
9
|
+
if (Array.isArray(value)) return "array";
|
|
10
|
+
if (typeof value === "string") return isIsoDateTimeString(value) ? "date" : "string";
|
|
11
|
+
if (typeof value === "number" || typeof value === "bigint") return "number";
|
|
12
|
+
if (typeof value === "boolean") return "boolean";
|
|
13
|
+
return "object";
|
|
14
|
+
}
|
|
15
|
+
function shorten(text) {
|
|
16
|
+
return text.length > MAX_VALUE_LENGTH ? `${text.slice(0, MAX_VALUE_LENGTH - 1)}\u2026` : text;
|
|
17
|
+
}
|
|
18
|
+
function comparePaths(a, b) {
|
|
19
|
+
if (a === "_id") return b === "_id" ? 0 : -1;
|
|
20
|
+
if (b === "_id") return 1;
|
|
21
|
+
const left = a.split(".");
|
|
22
|
+
const right = b.split(".");
|
|
23
|
+
for (let i = 0; i < Math.min(left.length, right.length); i++) {
|
|
24
|
+
const order = left[i].localeCompare(right[i]);
|
|
25
|
+
if (order !== 0) return order;
|
|
26
|
+
}
|
|
27
|
+
return left.length - right.length;
|
|
28
|
+
}
|
|
29
|
+
export function analyzeSamples(rows, options = {}) {
|
|
30
|
+
const { maxDepth, maxFields, topValues, maxDistinctForTop } = { ...DEFAULTS, ...options };
|
|
31
|
+
const stats = /* @__PURE__ */ new Map();
|
|
32
|
+
let sampled = 0;
|
|
33
|
+
function visit(value, path, depth, seen) {
|
|
34
|
+
const kind = kindOf(value);
|
|
35
|
+
let entry = seen.get(path);
|
|
36
|
+
if (!entry) {
|
|
37
|
+
entry = /* @__PURE__ */ new Map();
|
|
38
|
+
seen.set(path, entry);
|
|
39
|
+
}
|
|
40
|
+
const values = entry.get(kind) ?? /* @__PURE__ */ new Set();
|
|
41
|
+
entry.set(kind, values);
|
|
42
|
+
if (kind === "string" || kind === "number" || kind === "boolean" || kind === "date") values.add(String(value));
|
|
43
|
+
if (depth >= maxDepth) return;
|
|
44
|
+
if (isPlainObject(value)) {
|
|
45
|
+
for (const [key, inner] of Object.entries(value)) visit(inner, `${path}.${key}`, depth + 1, seen);
|
|
46
|
+
} else if (Array.isArray(value)) {
|
|
47
|
+
for (const item of value) if (isPlainObject(item)) {
|
|
48
|
+
for (const [key, inner] of Object.entries(item)) visit(inner, `${path}[].${key}`, depth + 1, seen);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (const row of rows) {
|
|
53
|
+
if (!isPlainObject(row)) continue;
|
|
54
|
+
sampled++;
|
|
55
|
+
const seen = /* @__PURE__ */ new Map();
|
|
56
|
+
for (const [key, value] of Object.entries(row)) visit(value, key, 1, seen);
|
|
57
|
+
for (const [path, kinds] of seen) {
|
|
58
|
+
let acc = stats.get(path);
|
|
59
|
+
if (!acc) {
|
|
60
|
+
acc = { present: 0, types: /* @__PURE__ */ new Map(), values: /* @__PURE__ */ new Map(), highCardinality: false };
|
|
61
|
+
stats.set(path, acc);
|
|
62
|
+
}
|
|
63
|
+
acc.present++;
|
|
64
|
+
for (const [kind, values] of kinds) {
|
|
65
|
+
acc.types.set(kind, (acc.types.get(kind) ?? 0) + 1);
|
|
66
|
+
for (const text of values) {
|
|
67
|
+
if (acc.highCardinality) break;
|
|
68
|
+
acc.values.set(text, (acc.values.get(text) ?? 0) + 1);
|
|
69
|
+
if (acc.values.size > maxDistinctForTop) {
|
|
70
|
+
acc.highCardinality = true;
|
|
71
|
+
acc.values.clear();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const paths = [...stats.keys()].sort(comparePaths);
|
|
78
|
+
const fields = paths.slice(0, maxFields).map((path) => {
|
|
79
|
+
const acc = stats.get(path);
|
|
80
|
+
const field = {
|
|
81
|
+
path,
|
|
82
|
+
present: acc.present,
|
|
83
|
+
share: sampled === 0 ? 0 : acc.present / sampled,
|
|
84
|
+
types: [...acc.types].map(([type, count]) => ({ type, count })).sort((a, b) => b.count - a.count || a.type.localeCompare(b.type))
|
|
85
|
+
};
|
|
86
|
+
if (!acc.highCardinality && acc.values.size > 0) {
|
|
87
|
+
field.topValues = [...acc.values].map(([value, count]) => ({ value: shorten(value), count })).sort((a, b) => b.count - a.count || a.value.localeCompare(b.value)).slice(0, topValues);
|
|
88
|
+
}
|
|
89
|
+
return field;
|
|
90
|
+
});
|
|
91
|
+
return { sampled, fields, truncated: paths.length > maxFields };
|
|
92
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface BatchInsertFailure {
|
|
2
|
+
start: number;
|
|
3
|
+
count: number;
|
|
4
|
+
error: unknown;
|
|
5
|
+
mayBePartial: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface BatchInsertResult {
|
|
8
|
+
inserted: number;
|
|
9
|
+
failedBatches: BatchInsertFailure[];
|
|
10
|
+
skipped: number;
|
|
11
|
+
aborted: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface BatchInsertOptions {
|
|
14
|
+
batchSize?: number;
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
onProgress?: (progress: {
|
|
17
|
+
done: number;
|
|
18
|
+
total: number;
|
|
19
|
+
}) => void;
|
|
20
|
+
isAtomicFailure?: (error: unknown) => boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare function runBatchInsert<T>(rows: T[], insert: (batch: T[]) => Promise<unknown>, options?: BatchInsertOptions): Promise<BatchInsertResult>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const DEFAULT_BATCH_SIZE = 1e3;
|
|
2
|
+
const MAX_BATCH_SIZE = 1e4;
|
|
3
|
+
export async function runBatchInsert(rows, insert, options = {}) {
|
|
4
|
+
const batchSize = Math.min(Math.max(Math.floor(options.batchSize ?? DEFAULT_BATCH_SIZE) || DEFAULT_BATCH_SIZE, 1), MAX_BATCH_SIZE);
|
|
5
|
+
const result = { inserted: 0, failedBatches: [], skipped: 0, aborted: false };
|
|
6
|
+
let done = 0;
|
|
7
|
+
for (let start = 0; start < rows.length; start += batchSize) {
|
|
8
|
+
if (options.signal?.aborted) {
|
|
9
|
+
result.aborted = true;
|
|
10
|
+
result.skipped = rows.length - start;
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
const batch = rows.slice(start, start + batchSize);
|
|
14
|
+
try {
|
|
15
|
+
await insert(batch);
|
|
16
|
+
result.inserted += batch.length;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
result.failedBatches.push({ start, count: batch.length, error, mayBePartial: !(options.isAtomicFailure?.(error) ?? false) });
|
|
19
|
+
}
|
|
20
|
+
done += batch.length;
|
|
21
|
+
options.onProgress?.({ done, total: rows.length });
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
@@ -5,4 +5,9 @@ export interface BulkDeleteResult<T> {
|
|
|
5
5
|
error: unknown;
|
|
6
6
|
}[];
|
|
7
7
|
}
|
|
8
|
-
export
|
|
8
|
+
export interface BulkRemoveMany<T> {
|
|
9
|
+
run: (items: T[]) => Promise<number | undefined>;
|
|
10
|
+
canHandle?: (item: T) => boolean;
|
|
11
|
+
chunkSize?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function runBulkDelete<T>(items: T[], remove: (item: T) => Promise<unknown>, concurrency?: number, removeMany?: BulkRemoveMany<T>): Promise<BulkDeleteResult<T>>;
|
|
@@ -1,9 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
const DEFAULT_CHUNK_SIZE = 100;
|
|
2
|
+
export async function runBulkDelete(items, remove, concurrency = 5, removeMany) {
|
|
2
3
|
const outcomes = Array.from({ length: items.length });
|
|
4
|
+
const individually = [];
|
|
5
|
+
if (removeMany) {
|
|
6
|
+
const chunkSize = Math.max(1, Math.floor(removeMany.chunkSize ?? DEFAULT_CHUNK_SIZE) || DEFAULT_CHUNK_SIZE);
|
|
7
|
+
const batched = [];
|
|
8
|
+
items.forEach((item, index) => {
|
|
9
|
+
if (removeMany.canHandle?.(item) ?? true) batched.push(index);
|
|
10
|
+
else individually.push(index);
|
|
11
|
+
});
|
|
12
|
+
for (let start = 0; start < batched.length; start += chunkSize) {
|
|
13
|
+
const indexes = batched.slice(start, start + chunkSize);
|
|
14
|
+
try {
|
|
15
|
+
const deletedCount = await removeMany.run(indexes.map((i) => items[i]));
|
|
16
|
+
if (typeof deletedCount === "number" && deletedCount >= indexes.length) {
|
|
17
|
+
for (const i of indexes) outcomes[i] = { ok: true };
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
} catch {
|
|
21
|
+
}
|
|
22
|
+
individually.push(...indexes);
|
|
23
|
+
}
|
|
24
|
+
} else {
|
|
25
|
+
individually.push(...items.keys());
|
|
26
|
+
}
|
|
3
27
|
let next = 0;
|
|
4
28
|
async function worker() {
|
|
5
|
-
while (next <
|
|
6
|
-
const index = next
|
|
29
|
+
while (next < individually.length) {
|
|
30
|
+
const index = individually[next++];
|
|
7
31
|
try {
|
|
8
32
|
await remove(items[index]);
|
|
9
33
|
outcomes[index] = { ok: true };
|
|
@@ -12,7 +36,7 @@ export async function runBulkDelete(items, remove, concurrency = 5) {
|
|
|
12
36
|
}
|
|
13
37
|
}
|
|
14
38
|
}
|
|
15
|
-
await Promise.all(Array.from({ length: Math.min(concurrency,
|
|
39
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, individually.length) }, worker));
|
|
16
40
|
const result = { deleted: [], failed: [] };
|
|
17
41
|
items.forEach((item, index) => {
|
|
18
42
|
const outcome = outcomes[index];
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare class BulkMatchShortfall extends Error {
|
|
2
|
+
matched: number;
|
|
3
|
+
expected: number;
|
|
4
|
+
constructor(matched: number, expected: number);
|
|
5
|
+
}
|
|
6
|
+
export declare class BulkUnaddressableId extends Error {
|
|
7
|
+
id: string;
|
|
8
|
+
constructor(id: string);
|
|
9
|
+
}
|
|
10
|
+
export interface BulkUpdateFailure {
|
|
11
|
+
ids: string[];
|
|
12
|
+
error: unknown;
|
|
13
|
+
}
|
|
14
|
+
export interface BulkUpdateResult {
|
|
15
|
+
updated: string[];
|
|
16
|
+
failed: BulkUpdateFailure[];
|
|
17
|
+
}
|
|
18
|
+
export interface BulkUpdateOptions {
|
|
19
|
+
chunkSize?: number;
|
|
20
|
+
}
|
|
21
|
+
export declare function isEditableField(field: string): boolean;
|
|
22
|
+
export declare function buildIdFilter(ids: string[]): {
|
|
23
|
+
$or: {
|
|
24
|
+
_id: string;
|
|
25
|
+
}[];
|
|
26
|
+
};
|
|
27
|
+
export declare function runBulkUpdate(ids: string[], updateMany: (ids: string[]) => Promise<unknown>, options?: BulkUpdateOptions): Promise<BulkUpdateResult>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isObjectIdHex } from "./objectId.js";
|
|
2
|
+
export class BulkMatchShortfall extends Error {
|
|
3
|
+
constructor(matched, expected) {
|
|
4
|
+
super(`Only ${matched} of ${expected} documents were found`);
|
|
5
|
+
this.matched = matched;
|
|
6
|
+
this.expected = expected;
|
|
7
|
+
this.name = "BulkMatchShortfall";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export class BulkUnaddressableId extends Error {
|
|
11
|
+
constructor(id) {
|
|
12
|
+
super(`Document id "${id}" cannot be used in a bulk filter`);
|
|
13
|
+
this.id = id;
|
|
14
|
+
this.name = "BulkUnaddressableId";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const DEFAULT_CHUNK_SIZE = 100;
|
|
18
|
+
export function isEditableField(field) {
|
|
19
|
+
return field !== "" && field !== "_id" && !field.startsWith("$") && !field.includes(".");
|
|
20
|
+
}
|
|
21
|
+
export function buildIdFilter(ids) {
|
|
22
|
+
if (ids.length === 0) throw new Error("buildIdFilter needs at least one id");
|
|
23
|
+
const bad = ids.find((id) => !isObjectIdHex(id));
|
|
24
|
+
if (bad !== void 0) throw new BulkUnaddressableId(bad);
|
|
25
|
+
return { $or: ids.map((id) => ({ _id: id })) };
|
|
26
|
+
}
|
|
27
|
+
export async function runBulkUpdate(ids, updateMany, options = {}) {
|
|
28
|
+
const chunkSize = Math.max(1, Math.floor(options.chunkSize ?? DEFAULT_CHUNK_SIZE) || DEFAULT_CHUNK_SIZE);
|
|
29
|
+
const result = { updated: [], failed: [] };
|
|
30
|
+
const addressable = ids.filter((id) => isObjectIdHex(id));
|
|
31
|
+
const others = ids.filter((id) => !isObjectIdHex(id));
|
|
32
|
+
for (let start = 0; start < addressable.length; start += chunkSize) {
|
|
33
|
+
const chunk = addressable.slice(start, start + chunkSize);
|
|
34
|
+
try {
|
|
35
|
+
const response = await updateMany(chunk);
|
|
36
|
+
const matched = response?.matchedCount;
|
|
37
|
+
if (typeof matched === "number" && matched < chunk.length) result.failed.push({ ids: chunk, error: new BulkMatchShortfall(matched, chunk.length) });
|
|
38
|
+
else result.updated.push(...chunk);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
result.failed.push({ ids: chunk, error });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const id of others) result.failed.push({ ids: [id], error: new BulkUnaddressableId(id) });
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FilterFieldType, FilterOperator } from './filterBuilder.js';
|
|
2
|
+
export interface SuggestionOption {
|
|
3
|
+
value: string;
|
|
4
|
+
label: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function isSuggestable(value: string, operator: FilterOperator): boolean;
|
|
7
|
+
export declare function wantsSuggestions(type: FilterFieldType, operator: FilterOperator): boolean;
|
|
8
|
+
export declare function suggestionOptions(values: string[], operator: FilterOperator, text: string): SuggestionOption[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const SUGGESTION_OPERATORS = ["equals", "notEquals", "oneOf"];
|
|
2
|
+
const UNFILTERABLE = /[:*?()\\]/;
|
|
3
|
+
export function isSuggestable(value, operator) {
|
|
4
|
+
if (value.trim() === "" || UNFILTERABLE.test(value)) return false;
|
|
5
|
+
return operator !== "oneOf" || !value.includes(",");
|
|
6
|
+
}
|
|
7
|
+
export function wantsSuggestions(type, operator) {
|
|
8
|
+
return type === "string" && SUGGESTION_OPERATORS.includes(operator);
|
|
9
|
+
}
|
|
10
|
+
export function suggestionOptions(values, operator, text) {
|
|
11
|
+
const unique = [...new Set(values)].filter((value) => isSuggestable(value, operator));
|
|
12
|
+
if (operator !== "oneOf") return unique.map((value) => ({ value, label: value }));
|
|
13
|
+
const entered = text.split(",").slice(0, -1).map((item) => item.trim()).filter((item) => item.length > 0);
|
|
14
|
+
return unique.filter((value) => !entered.includes(value)).map((value) => ({ value: [...entered, value].join(", "), label: value }));
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CoerceOptions, ImportColumnType, ImportError, ImportLimits } from './importRows.js';
|
|
2
|
+
export type ImportFormat = 'json' | 'csv';
|
|
3
|
+
export interface PrepareImportInput {
|
|
4
|
+
format: ImportFormat;
|
|
5
|
+
text: string;
|
|
6
|
+
delimiter?: string;
|
|
7
|
+
columnTypes?: Record<string, ImportColumnType>;
|
|
8
|
+
emptyCells?: CoerceOptions['emptyCells'];
|
|
9
|
+
limits?: ImportLimits;
|
|
10
|
+
}
|
|
11
|
+
export interface PreparedImport {
|
|
12
|
+
rows: Record<string, unknown>[];
|
|
13
|
+
sourceIndexes: number[];
|
|
14
|
+
columns: string[];
|
|
15
|
+
columnTypes: Record<string, ImportColumnType>;
|
|
16
|
+
errors: ImportError[];
|
|
17
|
+
truncated: boolean;
|
|
18
|
+
sourceRowCount: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function detectImportFormat(fileName: string, text: string): ImportFormat;
|
|
21
|
+
export declare function prepareImport(input: PrepareImportInput): PreparedImport;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { unwrapExtendedJson } from "../composables/useMongocampExtendedJson.js";
|
|
2
|
+
import { DEFAULT_IMPORT_LIMITS, coerceRow, inferColumnTypes, parseCsvRows, parseJsonRows, validateRows } from "./importRows.js";
|
|
3
|
+
const PREVIEW_KEY_ROWS = 100;
|
|
4
|
+
export function detectImportFormat(fileName, text) {
|
|
5
|
+
const lower = fileName.toLowerCase();
|
|
6
|
+
if (lower.endsWith(".json")) return "json";
|
|
7
|
+
if (lower.endsWith(".csv") || lower.endsWith(".tsv")) return "csv";
|
|
8
|
+
const first = text.replace(/^\uFEFF/, "").trimStart()[0];
|
|
9
|
+
return first === "[" || first === "{" ? "json" : "csv";
|
|
10
|
+
}
|
|
11
|
+
function collectKeys(rows) {
|
|
12
|
+
const keys = /* @__PURE__ */ new Set();
|
|
13
|
+
for (const row of rows.slice(0, PREVIEW_KEY_ROWS)) for (const key of Object.keys(row)) keys.add(key);
|
|
14
|
+
return [...keys];
|
|
15
|
+
}
|
|
16
|
+
export function prepareImport(input) {
|
|
17
|
+
const limits = input.limits ?? DEFAULT_IMPORT_LIMITS;
|
|
18
|
+
const parsed = input.format === "json" ? parseJsonRows(input.text, limits) : parseCsvRows(input.text, input.delimiter, limits);
|
|
19
|
+
const errors = [...parsed.errors];
|
|
20
|
+
const parsedSources = parsed.sourceIndexes ?? parsed.rows.map((_, i) => i);
|
|
21
|
+
const columns = parsed.columns ?? collectKeys(parsed.rows);
|
|
22
|
+
const sourceRowCount = parsed.rows.length + parsed.errors.filter((e) => e.rowIndex >= 0).length;
|
|
23
|
+
let columnTypes = {};
|
|
24
|
+
let candidates = [];
|
|
25
|
+
let candidateSources = [];
|
|
26
|
+
if (input.format === "csv") {
|
|
27
|
+
columnTypes = input.columnTypes ?? inferColumnTypes(parsed.rows, columns);
|
|
28
|
+
parsed.rows.forEach((row, i) => {
|
|
29
|
+
const { row: typed, errors: cellErrors } = coerceRow(row, columnTypes, { emptyCells: input.emptyCells });
|
|
30
|
+
if (cellErrors.length > 0) {
|
|
31
|
+
for (const e of cellErrors) errors.push({ rowIndex: parsedSources[i], ...e });
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
candidates.push(typed);
|
|
35
|
+
candidateSources.push(parsedSources[i]);
|
|
36
|
+
});
|
|
37
|
+
} else {
|
|
38
|
+
candidates = parsed.rows.map((row) => unwrapExtendedJson(row));
|
|
39
|
+
candidateSources = parsedSources;
|
|
40
|
+
}
|
|
41
|
+
const validated = validateRows(candidates, limits);
|
|
42
|
+
for (const e of validated.errors) errors.push(e.rowIndex >= 0 ? { ...e, rowIndex: candidateSources[e.rowIndex] } : e);
|
|
43
|
+
return {
|
|
44
|
+
rows: validated.rows,
|
|
45
|
+
sourceIndexes: (validated.sourceIndexes ?? []).map((i) => candidateSources[i]),
|
|
46
|
+
columns,
|
|
47
|
+
columnTypes,
|
|
48
|
+
errors,
|
|
49
|
+
truncated: validated.truncated,
|
|
50
|
+
sourceRowCount
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export type ImportColumnType = 'string' | 'number' | 'boolean' | 'date-time' | 'json';
|
|
2
|
+
export type ImportErrorCode = 'file-too-large' | 'invalid-json' | 'not-array' | 'not-object' | 'invalid-delimiter' | 'invalid-csv' | 'invalid-header' | 'field-count' | 'too-many-rows' | 'empty-row' | 'forbidden-key' | 'too-deep' | 'invalid-number' | 'unsafe-integer' | 'invalid-id' | 'invalid-boolean' | 'invalid-date' | 'invalid-json-value';
|
|
3
|
+
export interface ImportError {
|
|
4
|
+
rowIndex: number;
|
|
5
|
+
column?: string;
|
|
6
|
+
code: ImportErrorCode;
|
|
7
|
+
message: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ImportLimits {
|
|
10
|
+
maxRows: number;
|
|
11
|
+
maxBytes: number;
|
|
12
|
+
}
|
|
13
|
+
export interface ImportReport<T = Record<string, unknown>> {
|
|
14
|
+
rows: T[];
|
|
15
|
+
columns?: string[];
|
|
16
|
+
errors: ImportError[];
|
|
17
|
+
truncated: boolean;
|
|
18
|
+
sourceIndexes?: number[];
|
|
19
|
+
}
|
|
20
|
+
export interface CoerceOptions {
|
|
21
|
+
emptyCells?: 'omit' | 'empty-string';
|
|
22
|
+
}
|
|
23
|
+
export interface CoerceResult {
|
|
24
|
+
row: Record<string, unknown>;
|
|
25
|
+
errors: {
|
|
26
|
+
column: string;
|
|
27
|
+
code: ImportErrorCode;
|
|
28
|
+
message: string;
|
|
29
|
+
}[];
|
|
30
|
+
}
|
|
31
|
+
export declare const DEFAULT_IMPORT_LIMITS: ImportLimits;
|
|
32
|
+
export declare function parseJsonRows(text: string, limits?: ImportLimits): ImportReport;
|
|
33
|
+
export declare function parseCsvRows(text: string, delimiter?: string, limits?: ImportLimits): ImportReport<Record<string, string>>;
|
|
34
|
+
export declare function validateRows<T extends Record<string, unknown>>(rows: T[], limits?: ImportLimits): ImportReport<T>;
|
|
35
|
+
export declare function coerceRow(row: Record<string, unknown>, columnTypes: Record<string, ImportColumnType>, options?: CoerceOptions): CoerceResult;
|
|
36
|
+
export declare function inferColumnTypes(rows: Record<string, unknown>[], columns: string[]): Record<string, ImportColumnType>;
|