@sfxcode/nuxt-ui-mongocamp 1.2.7 → 1.3.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.json +1 -1
- package/dist/runtime/components/MongocampAccount.vue +10 -0
- package/dist/runtime/components/MongocampBucketFiles.vue +18 -3
- package/dist/runtime/components/MongocampBulkDelete.d.vue.ts +16 -0
- package/dist/runtime/components/MongocampBulkDelete.vue +96 -0
- package/dist/runtime/components/MongocampBulkDelete.vue.d.ts +16 -0
- package/dist/runtime/components/MongocampCollectionData.vue +258 -11
- package/dist/runtime/components/MongocampCollectionInfos.vue +11 -5
- package/dist/runtime/components/MongocampCollections.vue +18 -4
- package/dist/runtime/components/MongocampDashboard.d.vue.ts +25 -0
- package/dist/runtime/components/MongocampDashboard.vue +111 -0
- package/dist/runtime/components/MongocampDashboard.vue.d.ts +25 -0
- package/dist/runtime/components/MongocampDatabases.vue +16 -24
- package/dist/runtime/components/MongocampEmptyState.d.vue.ts +18 -0
- package/dist/runtime/components/MongocampEmptyState.vue +28 -0
- package/dist/runtime/components/MongocampEmptyState.vue.d.ts +18 -0
- package/dist/runtime/components/MongocampErrorAlert.d.vue.ts +15 -0
- package/dist/runtime/components/MongocampErrorAlert.vue +53 -0
- package/dist/runtime/components/MongocampErrorAlert.vue.d.ts +15 -0
- package/dist/runtime/components/MongocampFilterBuilder.d.vue.ts +20 -0
- package/dist/runtime/components/MongocampFilterBuilder.vue +201 -0
- package/dist/runtime/components/MongocampFilterBuilder.vue.d.ts +20 -0
- package/dist/runtime/components/MongocampJobs.vue +44 -8
- package/dist/runtime/components/MongocampLogin.vue +3 -2
- package/dist/runtime/components/MongocampRoleGrants.vue +70 -38
- package/dist/runtime/components/MongocampRoles.vue +79 -8
- package/dist/runtime/components/MongocampUsers.vue +108 -16
- package/dist/runtime/composables/useMongocampBulkDelete.d.ts +5 -0
- package/dist/runtime/composables/useMongocampBulkDelete.js +4 -0
- package/dist/runtime/composables/useMongocampError.d.ts +8 -0
- package/dist/runtime/composables/useMongocampError.js +20 -0
- package/dist/runtime/composables/useMongocampExport.d.ts +5 -0
- package/dist/runtime/composables/useMongocampExport.js +4 -0
- package/dist/runtime/composables/useMongocampExtendedJson.js +1 -4
- package/dist/runtime/composables/useMongocampFilterBuilder.d.ts +12 -0
- package/dist/runtime/composables/useMongocampFilterBuilder.js +13 -0
- package/dist/runtime/composables/useMongocampQuery.d.ts +31 -24
- package/dist/runtime/composables/useMongocampQuery.js +60 -16
- package/dist/runtime/locales/de.json +97 -7
- package/dist/runtime/locales/en.json +97 -7
- package/dist/runtime/utils/bulkDelete.d.ts +8 -0
- package/dist/runtime/utils/bulkDelete.js +23 -0
- package/dist/runtime/utils/createSelectColumn.d.ts +7 -0
- package/dist/runtime/utils/createSelectColumn.js +19 -0
- package/dist/runtime/utils/escapeFieldName.d.ts +5 -0
- package/dist/runtime/utils/escapeFieldName.js +3 -0
- package/dist/runtime/utils/exportRows.d.ts +2 -0
- package/dist/runtime/utils/exportRows.js +18 -0
- package/dist/runtime/utils/filterBuilder.d.ts +31 -0
- package/dist/runtime/utils/filterBuilder.js +120 -0
- package/dist/runtime/utils/isoDate.d.ts +1 -0
- package/dist/runtime/utils/isoDate.js +4 -0
- package/package.json +15 -15
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
<script setup>
|
|
2
|
-
import { h, reactive, ref, resolveComponent } from "vue";
|
|
3
|
-
import { useI18n } from "#imports";
|
|
2
|
+
import { computed, h, reactive, ref, resolveComponent } from "vue";
|
|
3
|
+
import { useI18n, useMongocampError } from "#imports";
|
|
4
4
|
import useMongocampAdmin from "../composables/useMongocampAdmin";
|
|
5
|
+
import { runBulkDelete } from "../utils/bulkDelete";
|
|
6
|
+
import { createSelectColumn } from "../utils/createSelectColumn";
|
|
5
7
|
const props = defineProps({
|
|
6
8
|
grantsPath: { type: String, required: false }
|
|
7
9
|
});
|
|
@@ -40,10 +42,36 @@ const editRoleSchema = reactive([
|
|
|
40
42
|
label: t("nuxtUiMongocamp.roles.adminRole")
|
|
41
43
|
}
|
|
42
44
|
]);
|
|
45
|
+
const loadError = ref(null);
|
|
46
|
+
const { describeError } = useMongocampError();
|
|
47
|
+
const rowSelection = ref({});
|
|
48
|
+
const selectedRoleNames = computed(() => Object.keys(rowSelection.value).filter((name) => rowSelection.value[name]));
|
|
49
|
+
const bulkDeleting = ref(false);
|
|
50
|
+
const bulkError = ref("");
|
|
51
|
+
async function handleBulkDelete() {
|
|
52
|
+
bulkDeleting.value = true;
|
|
53
|
+
bulkError.value = "";
|
|
54
|
+
const names = [...selectedRoleNames.value];
|
|
55
|
+
try {
|
|
56
|
+
const { failed } = await runBulkDelete(names, (name) => deleteRole(name));
|
|
57
|
+
await fetchRoles();
|
|
58
|
+
if (failed.length > 0) {
|
|
59
|
+
rowSelection.value = Object.fromEntries(failed.map((f) => [f.item, true]));
|
|
60
|
+
bulkError.value = t("nuxtUiMongocamp.bulk.partialFailure", { failed: failed.length, total: names.length, reason: describeError(failed[0].error).message });
|
|
61
|
+
}
|
|
62
|
+
} finally {
|
|
63
|
+
bulkDeleting.value = false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
43
66
|
async function fetchRoles() {
|
|
44
67
|
loading.value = true;
|
|
68
|
+
loadError.value = null;
|
|
69
|
+
rowSelection.value = {};
|
|
45
70
|
try {
|
|
46
71
|
roles.value = await listRoles(filterText.value);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
loadError.value = e;
|
|
74
|
+
roles.value = [];
|
|
47
75
|
} finally {
|
|
48
76
|
loading.value = false;
|
|
49
77
|
}
|
|
@@ -78,18 +106,30 @@ async function handleEditRole() {
|
|
|
78
106
|
errorMessage.value = e instanceof Error ? e.message : t("nuxtUiMongocamp.roles.errorEditRole");
|
|
79
107
|
}
|
|
80
108
|
}
|
|
109
|
+
const deleteError = ref("");
|
|
110
|
+
const deleting = ref(false);
|
|
81
111
|
function confirmDelete(roleName) {
|
|
82
112
|
roleToDelete.value = roleName;
|
|
113
|
+
deleteError.value = "";
|
|
83
114
|
isDeleteModalOpen.value = true;
|
|
84
115
|
}
|
|
85
116
|
async function handleDeleteRole() {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
117
|
+
deleting.value = true;
|
|
118
|
+
deleteError.value = "";
|
|
119
|
+
try {
|
|
120
|
+
await deleteRole(roleToDelete.value);
|
|
121
|
+
isDeleteModalOpen.value = false;
|
|
122
|
+
await fetchRoles();
|
|
123
|
+
} catch (e) {
|
|
124
|
+
deleteError.value = describeError(e, "nuxtUiMongocamp.roles.errorDelete").message;
|
|
125
|
+
} finally {
|
|
126
|
+
deleting.value = false;
|
|
127
|
+
}
|
|
89
128
|
}
|
|
90
129
|
const UBadge = resolveComponent("UBadge");
|
|
91
130
|
const UButton = resolveComponent("UButton");
|
|
92
131
|
const UTooltip = resolveComponent("UTooltip");
|
|
132
|
+
const UCheckbox = resolveComponent("UCheckbox");
|
|
93
133
|
function withTooltip(text, node) {
|
|
94
134
|
return h(UTooltip, { text }, { default: () => node });
|
|
95
135
|
}
|
|
@@ -104,7 +144,8 @@ function sortHeader(column, label) {
|
|
|
104
144
|
onClick: () => column.toggleSorting(isSorted === "asc")
|
|
105
145
|
});
|
|
106
146
|
}
|
|
107
|
-
const columns = [
|
|
147
|
+
const columns = computed(() => [
|
|
148
|
+
createSelectColumn(UCheckbox, { selectAll: t("nuxtUiMongocamp.bulk.selectAll"), selectRow: t("nuxtUiMongocamp.bulk.selectRow") }),
|
|
108
149
|
{
|
|
109
150
|
accessorKey: "name",
|
|
110
151
|
header: ({ column }) => sortHeader(column, t("nuxtUiMongocamp.roles.columnRole"))
|
|
@@ -156,7 +197,7 @@ const columns = [
|
|
|
156
197
|
}))
|
|
157
198
|
])
|
|
158
199
|
}
|
|
159
|
-
];
|
|
200
|
+
]);
|
|
160
201
|
fetchRoles();
|
|
161
202
|
</script>
|
|
162
203
|
|
|
@@ -193,12 +234,32 @@ fetchRoles();
|
|
|
193
234
|
</div>
|
|
194
235
|
</div>
|
|
195
236
|
|
|
237
|
+
<MongocampErrorAlert
|
|
238
|
+
:error="loadError"
|
|
239
|
+
@retry="fetchRoles"
|
|
240
|
+
/>
|
|
241
|
+
|
|
242
|
+
<MongocampBulkDelete
|
|
243
|
+
:selected="selectedRoleNames"
|
|
244
|
+
:deleting="bulkDeleting"
|
|
245
|
+
:error="bulkError"
|
|
246
|
+
@open="bulkError = ''"
|
|
247
|
+
@clear="rowSelection = {}"
|
|
248
|
+
@confirm="handleBulkDelete"
|
|
249
|
+
/>
|
|
250
|
+
|
|
196
251
|
<UTable
|
|
197
252
|
v-model:sorting="sorting"
|
|
253
|
+
v-model:row-selection="rowSelection"
|
|
254
|
+
:get-row-id="(row) => row.name"
|
|
198
255
|
:data="roles"
|
|
199
256
|
:columns="columns"
|
|
200
257
|
:loading="loading"
|
|
201
|
-
|
|
258
|
+
>
|
|
259
|
+
<template #empty>
|
|
260
|
+
<MongocampEmptyState :title="t('nuxtUiMongocamp.empty.roles')" />
|
|
261
|
+
</template>
|
|
262
|
+
</UTable>
|
|
202
263
|
|
|
203
264
|
<!-- Add Role Modal -->
|
|
204
265
|
<UModal
|
|
@@ -252,17 +313,27 @@ fetchRoles();
|
|
|
252
313
|
>
|
|
253
314
|
<template #body>
|
|
254
315
|
<p>{{ t("nuxtUiMongocamp.roles.confirmDelete", { roleName: roleToDelete }) }}</p>
|
|
316
|
+
<UAlert
|
|
317
|
+
v-if="deleteError"
|
|
318
|
+
class="mt-3"
|
|
319
|
+
color="error"
|
|
320
|
+
variant="subtle"
|
|
321
|
+
icon="i-lucide-circle-alert"
|
|
322
|
+
:description="deleteError"
|
|
323
|
+
/>
|
|
255
324
|
</template>
|
|
256
325
|
<template #footer>
|
|
257
326
|
<UButton
|
|
258
327
|
:label="t('nuxtUiMongocamp.common.cancel')"
|
|
259
328
|
color="neutral"
|
|
260
329
|
variant="ghost"
|
|
330
|
+
:disabled="deleting"
|
|
261
331
|
@click="isDeleteModalOpen = false"
|
|
262
332
|
/>
|
|
263
333
|
<UButton
|
|
264
334
|
:label="t('nuxtUiMongocamp.common.delete')"
|
|
265
335
|
color="error"
|
|
336
|
+
:loading="deleting"
|
|
266
337
|
@click="handleDeleteRole"
|
|
267
338
|
/>
|
|
268
339
|
</template>
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
<script setup>
|
|
2
|
-
import { h, reactive, ref, resolveComponent } from "vue";
|
|
3
|
-
import { useI18n } from "#imports";
|
|
2
|
+
import { computed, h, reactive, ref, resolveComponent } from "vue";
|
|
3
|
+
import { useI18n, useMongocampError, useMongocampStorage } from "#imports";
|
|
4
4
|
import useMongocampAdmin from "../composables/useMongocampAdmin";
|
|
5
|
+
import { runBulkDelete } from "../utils/bulkDelete";
|
|
6
|
+
import { createSelectColumn } from "../utils/createSelectColumn";
|
|
5
7
|
const { t } = useI18n();
|
|
6
8
|
const { listUsers, addUser, deleteUser, updateUserRoles, updateUserPassword, listRoles } = useMongocampAdmin();
|
|
7
9
|
const users = ref([]);
|
|
@@ -75,10 +77,44 @@ const editUserSchema = reactive([
|
|
|
75
77
|
transferRightHeaderText: t("nuxtUiMongocamp.users.assigned")
|
|
76
78
|
}
|
|
77
79
|
]);
|
|
80
|
+
const loadError = ref(null);
|
|
81
|
+
const { describeError } = useMongocampError();
|
|
82
|
+
const storage = useMongocampStorage();
|
|
83
|
+
const rowSelection = ref({});
|
|
84
|
+
const selectedUserIds = computed(() => Object.keys(rowSelection.value).filter((id) => rowSelection.value[id]));
|
|
85
|
+
const bulkDeleting = ref(false);
|
|
86
|
+
const bulkError = ref("");
|
|
87
|
+
function isSelf(userId) {
|
|
88
|
+
const me = storage.value?.profile.user;
|
|
89
|
+
return !!me && userId === me;
|
|
90
|
+
}
|
|
91
|
+
function canSelectUser(row) {
|
|
92
|
+
return !isSelf(row.original.user);
|
|
93
|
+
}
|
|
94
|
+
async function handleBulkDelete() {
|
|
95
|
+
bulkDeleting.value = true;
|
|
96
|
+
bulkError.value = "";
|
|
97
|
+
const ids = [...selectedUserIds.value];
|
|
98
|
+
try {
|
|
99
|
+
const { failed } = await runBulkDelete(ids, (id) => deleteUser(id));
|
|
100
|
+
await fetchUsers();
|
|
101
|
+
if (failed.length > 0) {
|
|
102
|
+
rowSelection.value = Object.fromEntries(failed.map((f) => [f.item, true]));
|
|
103
|
+
bulkError.value = t("nuxtUiMongocamp.bulk.partialFailure", { failed: failed.length, total: ids.length, reason: describeError(failed[0].error).message });
|
|
104
|
+
}
|
|
105
|
+
} finally {
|
|
106
|
+
bulkDeleting.value = false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
78
109
|
async function fetchUsers() {
|
|
79
110
|
loading.value = true;
|
|
111
|
+
loadError.value = null;
|
|
112
|
+
rowSelection.value = {};
|
|
80
113
|
try {
|
|
81
114
|
users.value = await listUsers(filterText.value);
|
|
115
|
+
} catch (e) {
|
|
116
|
+
loadError.value = e;
|
|
117
|
+
users.value = [];
|
|
82
118
|
} finally {
|
|
83
119
|
loading.value = false;
|
|
84
120
|
}
|
|
@@ -127,18 +163,30 @@ async function handleEditUser() {
|
|
|
127
163
|
errorMessage.value = e instanceof Error ? e.message : t("nuxtUiMongocamp.users.errorEditUser");
|
|
128
164
|
}
|
|
129
165
|
}
|
|
166
|
+
const deleteError = ref("");
|
|
167
|
+
const deleting = ref(false);
|
|
130
168
|
function confirmDelete(userId) {
|
|
131
169
|
userToDelete.value = userId;
|
|
170
|
+
deleteError.value = "";
|
|
132
171
|
isDeleteModalOpen.value = true;
|
|
133
172
|
}
|
|
134
173
|
async function handleDeleteUser() {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
174
|
+
deleting.value = true;
|
|
175
|
+
deleteError.value = "";
|
|
176
|
+
try {
|
|
177
|
+
await deleteUser(userToDelete.value);
|
|
178
|
+
isDeleteModalOpen.value = false;
|
|
179
|
+
await fetchUsers();
|
|
180
|
+
} catch (e) {
|
|
181
|
+
deleteError.value = describeError(e, "nuxtUiMongocamp.users.errorDelete").message;
|
|
182
|
+
} finally {
|
|
183
|
+
deleting.value = false;
|
|
184
|
+
}
|
|
138
185
|
}
|
|
139
186
|
const UBadge = resolveComponent("UBadge");
|
|
140
187
|
const UButton = resolveComponent("UButton");
|
|
141
188
|
const UTooltip = resolveComponent("UTooltip");
|
|
189
|
+
const UCheckbox = resolveComponent("UCheckbox");
|
|
142
190
|
function withTooltip(text, node) {
|
|
143
191
|
return h(UTooltip, { text }, { default: () => node });
|
|
144
192
|
}
|
|
@@ -153,7 +201,27 @@ function sortHeader(column, label) {
|
|
|
153
201
|
onClick: () => column.toggleSorting(isSorted === "asc")
|
|
154
202
|
});
|
|
155
203
|
}
|
|
156
|
-
|
|
204
|
+
function deleteButton(userId) {
|
|
205
|
+
const button = (disabled) => h(UButton, {
|
|
206
|
+
"icon": "i-lucide-trash-2",
|
|
207
|
+
"color": "error",
|
|
208
|
+
"variant": "ghost",
|
|
209
|
+
"size": "sm",
|
|
210
|
+
"aria-label": t("nuxtUiMongocamp.users.deleteUserTooltip"),
|
|
211
|
+
disabled,
|
|
212
|
+
"onClick": () => confirmDelete(userId)
|
|
213
|
+
});
|
|
214
|
+
if (isSelf(userId)) {
|
|
215
|
+
return h("span", { class: "inline-flex", title: t("nuxtUiMongocamp.bulk.cannotSelectSelf") }, [button(true)]);
|
|
216
|
+
}
|
|
217
|
+
return withTooltip(t("nuxtUiMongocamp.users.deleteUserTooltip"), button(false));
|
|
218
|
+
}
|
|
219
|
+
const columns = computed(() => [
|
|
220
|
+
createSelectColumn(UCheckbox, {
|
|
221
|
+
selectAll: t("nuxtUiMongocamp.bulk.selectAll"),
|
|
222
|
+
selectRow: t("nuxtUiMongocamp.bulk.selectRow"),
|
|
223
|
+
unselectable: t("nuxtUiMongocamp.bulk.cannotSelectSelf")
|
|
224
|
+
}),
|
|
157
225
|
{
|
|
158
226
|
accessorKey: "user",
|
|
159
227
|
header: ({ column }) => sortHeader(column, t("nuxtUiMongocamp.users.columnUser"))
|
|
@@ -193,17 +261,10 @@ const columns = [
|
|
|
193
261
|
"aria-label": t("nuxtUiMongocamp.users.editUserTooltip"),
|
|
194
262
|
"onClick": () => openEdit(row.original)
|
|
195
263
|
})),
|
|
196
|
-
|
|
197
|
-
"icon": "i-lucide-trash-2",
|
|
198
|
-
"color": "error",
|
|
199
|
-
"variant": "ghost",
|
|
200
|
-
"size": "sm",
|
|
201
|
-
"aria-label": t("nuxtUiMongocamp.users.deleteUserTooltip"),
|
|
202
|
-
"onClick": () => confirmDelete(row.original.user)
|
|
203
|
-
}))
|
|
264
|
+
deleteButton(row.original.user)
|
|
204
265
|
])
|
|
205
266
|
}
|
|
206
|
-
];
|
|
267
|
+
]);
|
|
207
268
|
fetchUsers();
|
|
208
269
|
fetchRoles();
|
|
209
270
|
</script>
|
|
@@ -241,12 +302,33 @@ fetchRoles();
|
|
|
241
302
|
</div>
|
|
242
303
|
</div>
|
|
243
304
|
|
|
305
|
+
<MongocampErrorAlert
|
|
306
|
+
:error="loadError"
|
|
307
|
+
@retry="fetchUsers"
|
|
308
|
+
/>
|
|
309
|
+
|
|
310
|
+
<MongocampBulkDelete
|
|
311
|
+
:selected="selectedUserIds"
|
|
312
|
+
:deleting="bulkDeleting"
|
|
313
|
+
:error="bulkError"
|
|
314
|
+
@open="bulkError = ''"
|
|
315
|
+
@clear="rowSelection = {}"
|
|
316
|
+
@confirm="handleBulkDelete"
|
|
317
|
+
/>
|
|
318
|
+
|
|
244
319
|
<UTable
|
|
245
320
|
v-model:sorting="sorting"
|
|
321
|
+
v-model:row-selection="rowSelection"
|
|
322
|
+
:get-row-id="(row) => row.user"
|
|
323
|
+
:row-selection-options="{ enableRowSelection: canSelectUser }"
|
|
246
324
|
:data="users"
|
|
247
325
|
:columns="columns"
|
|
248
326
|
:loading="loading"
|
|
249
|
-
|
|
327
|
+
>
|
|
328
|
+
<template #empty>
|
|
329
|
+
<MongocampEmptyState :title="t('nuxtUiMongocamp.empty.users')" />
|
|
330
|
+
</template>
|
|
331
|
+
</UTable>
|
|
250
332
|
|
|
251
333
|
<!-- Add User Modal -->
|
|
252
334
|
<UModal
|
|
@@ -300,17 +382,27 @@ fetchRoles();
|
|
|
300
382
|
>
|
|
301
383
|
<template #body>
|
|
302
384
|
<p>{{ t("nuxtUiMongocamp.users.confirmDelete", { userId: userToDelete }) }}</p>
|
|
385
|
+
<UAlert
|
|
386
|
+
v-if="deleteError"
|
|
387
|
+
class="mt-3"
|
|
388
|
+
color="error"
|
|
389
|
+
variant="subtle"
|
|
390
|
+
icon="i-lucide-circle-alert"
|
|
391
|
+
:description="deleteError"
|
|
392
|
+
/>
|
|
303
393
|
</template>
|
|
304
394
|
<template #footer>
|
|
305
395
|
<UButton
|
|
306
396
|
:label="t('nuxtUiMongocamp.common.cancel')"
|
|
307
397
|
color="neutral"
|
|
308
398
|
variant="ghost"
|
|
399
|
+
:disabled="deleting"
|
|
309
400
|
@click="isDeleteModalOpen = false"
|
|
310
401
|
/>
|
|
311
402
|
<UButton
|
|
312
403
|
:label="t('nuxtUiMongocamp.common.delete')"
|
|
313
404
|
color="error"
|
|
405
|
+
:loading="deleting"
|
|
314
406
|
@click="handleDeleteUser"
|
|
315
407
|
/>
|
|
316
408
|
</template>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type MongocampErrorKind = 'network' | 'unauthorized' | 'forbidden' | 'other';
|
|
2
|
+
export interface DescribedError {
|
|
3
|
+
kind: MongocampErrorKind;
|
|
4
|
+
message: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function useMongocampError(): {
|
|
7
|
+
describeError: (error: unknown, fallbackKey?: string) => DescribedError;
|
|
8
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useI18n } from "#imports";
|
|
2
|
+
export function useMongocampError() {
|
|
3
|
+
const { t } = useI18n();
|
|
4
|
+
function describeError(error, fallbackKey) {
|
|
5
|
+
const name = error?.name;
|
|
6
|
+
const status = error?.response?.status;
|
|
7
|
+
if (name === "FetchError") {
|
|
8
|
+
return { kind: "network", message: t("nuxtUiMongocamp.errors.network") };
|
|
9
|
+
}
|
|
10
|
+
if (status === 401) {
|
|
11
|
+
return { kind: "unauthorized", message: t("nuxtUiMongocamp.errors.unauthorized") };
|
|
12
|
+
}
|
|
13
|
+
if (status === 403) {
|
|
14
|
+
return { kind: "forbidden", message: t("nuxtUiMongocamp.errors.forbidden") };
|
|
15
|
+
}
|
|
16
|
+
const message = error instanceof Error && error.message ? error.message : t(fallbackKey ?? "nuxtUiMongocamp.errors.generic");
|
|
17
|
+
return { kind: "other", message };
|
|
18
|
+
}
|
|
19
|
+
return { describeError };
|
|
20
|
+
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
+
import { isIsoDateTimeString } from "../utils/isoDate.js";
|
|
1
2
|
function isPlainObject(value) {
|
|
2
3
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
4
|
}
|
|
4
5
|
const NUMBER_WRAPPER_KEYS = ["$numberInt", "$numberLong", "$numberDouble", "$numberDecimal"];
|
|
5
|
-
const ISO_DATE_TIME_REGEX = /^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
6
|
-
function isIsoDateTimeString(value) {
|
|
7
|
-
return ISO_DATE_TIME_REGEX.test(value) && !Number.isNaN(Date.parse(value));
|
|
8
|
-
}
|
|
9
6
|
function unwrapDateTime(raw) {
|
|
10
7
|
const inner = raw.$date;
|
|
11
8
|
if (typeof inner === "string" || typeof inner === "number") return new Date(inner).toISOString();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { buildCondition, buildFilterQuery, conditionValueText, countActiveConditions, operatorLabelKey, operatorsFor, toFilterFieldType, valueCountFor } from '../utils/filterBuilder.js';
|
|
2
|
+
export type { FilterCondition, FilterFieldType, FilterGroup, FilterOperator } from '../utils/filterBuilder.js';
|
|
3
|
+
export declare function useMongocampFilterBuilder(): {
|
|
4
|
+
operatorsFor: typeof operatorsFor;
|
|
5
|
+
valueCountFor: typeof valueCountFor;
|
|
6
|
+
toFilterFieldType: typeof toFilterFieldType;
|
|
7
|
+
operatorLabelKey: typeof operatorLabelKey;
|
|
8
|
+
conditionValueText: typeof conditionValueText;
|
|
9
|
+
buildCondition: typeof buildCondition;
|
|
10
|
+
buildFilterQuery: typeof buildFilterQuery;
|
|
11
|
+
countActiveConditions: typeof countActiveConditions;
|
|
12
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildCondition,
|
|
3
|
+
buildFilterQuery,
|
|
4
|
+
conditionValueText,
|
|
5
|
+
countActiveConditions,
|
|
6
|
+
operatorLabelKey,
|
|
7
|
+
operatorsFor,
|
|
8
|
+
toFilterFieldType,
|
|
9
|
+
valueCountFor
|
|
10
|
+
} from "../utils/filterBuilder.js";
|
|
11
|
+
export function useMongocampFilterBuilder() {
|
|
12
|
+
return { operatorsFor, valueCountFor, toFilterFieldType, operatorLabelKey, conditionValueText, buildCondition, buildFilterQuery, countActiveConditions };
|
|
13
|
+
}
|
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
/** Scalar value accepted by all query helpers. `null` and `undefined` produce an empty (no-op) expression. */
|
|
2
2
|
export type QueryValue = string | number | boolean | null | undefined;
|
|
3
|
+
/**
|
|
4
|
+
* What kind of values a field holds, when you know it. It only selects a shorter query for
|
|
5
|
+
* `exists`/`notExists`: `text` → `field:*`, `number` and `date` → a closed range over every possible value.
|
|
6
|
+
*/
|
|
7
|
+
export type ExistsFieldType = 'text' | 'number' | 'date';
|
|
3
8
|
export interface MongocampQueryHelper {
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Matches documents where the field has a value. Without `type` this ORs a text wildcard with a
|
|
11
|
+
* full-range number and a full-range date clause, so it works for any of those types.
|
|
12
|
+
*
|
|
13
|
+
* MongoCamp 1.5 does not understand `_exists_:field` (it matches nothing), and `field:*` only matches text,
|
|
14
|
+
* which is why this is not just a wildcard.
|
|
15
|
+
*/
|
|
16
|
+
exists: (fieldName: string, type?: ExistsFieldType) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Matches documents where the field is absent: every `exists` clause negated and ANDed (negating the
|
|
19
|
+
* whole group is silently ignored by the server).
|
|
20
|
+
*/
|
|
21
|
+
notExists: (fieldName: string, type?: ExistsFieldType) => string;
|
|
8
22
|
/** Alias for `notExists`. */
|
|
9
|
-
isEmpty: (fieldName: string) => string;
|
|
23
|
+
isEmpty: (fieldName: string, type?: ExistsFieldType) => string;
|
|
10
24
|
/** Alias for `exists`. */
|
|
11
|
-
isNotEmpty: (fieldName: string) => string;
|
|
25
|
+
isNotEmpty: (fieldName: string, type?: ExistsFieldType) => string;
|
|
12
26
|
/**
|
|
13
27
|
* `field:"value"` — exact match.
|
|
14
28
|
* Returns `''` when value is `null`, `undefined`, or an empty string.
|
|
@@ -21,12 +35,20 @@ export interface MongocampQueryHelper {
|
|
|
21
35
|
notEquals: (fieldName: string, value: QueryValue) => string;
|
|
22
36
|
/**
|
|
23
37
|
* `field:[from TO to]` — inclusive range.
|
|
24
|
-
*
|
|
38
|
+
*
|
|
39
|
+
* Pass `null` (or `'*'`) for an open end. MongoCamp 1.5 rejects `[x TO *]`, so the open end is replaced by
|
|
40
|
+
* the far bound of the other bound's type: the 64-bit integer limits for numbers, year 0001 / 9999 for
|
|
41
|
+
* dates. Both ends open means "has a value" and returns `exists(field)`. An open end next to a *text*
|
|
42
|
+
* bound can't be expressed and still becomes `*`, which the server rejects.
|
|
43
|
+
*
|
|
44
|
+
* A date without a time is widened to the whole UTC day (`T00:00:00Z` as lower, `T23:59:59.999Z` as
|
|
45
|
+
* upper bound) — the server matches nothing for a bare date. Characters that would break out of the
|
|
46
|
+
* range (brackets, quotes, whitespace…) in a bound are escaped.
|
|
25
47
|
*/
|
|
26
48
|
inRange: (fieldName: string, from: QueryValue, to: QueryValue) => string;
|
|
27
49
|
/**
|
|
28
|
-
* `-field:[from TO to]` — excluded range.
|
|
29
|
-
*
|
|
50
|
+
* `-field:[from TO to]` — excluded range. Open ends behave as in `inRange`; with both open it is
|
|
51
|
+
* `notExists(field)`.
|
|
30
52
|
*/
|
|
31
53
|
notInRange: (fieldName: string, from: QueryValue, to: QueryValue) => string;
|
|
32
54
|
/** Alias for `inRange`. */
|
|
@@ -81,19 +103,4 @@ export interface MongocampQueryHelper {
|
|
|
81
103
|
*/
|
|
82
104
|
or: (...queryParts: (string | null | undefined)[]) => string;
|
|
83
105
|
}
|
|
84
|
-
/**
|
|
85
|
-
* High-level Lucene query helpers for MongoCamp collection filters.
|
|
86
|
-
*
|
|
87
|
-
* All helpers return a Lucene expression string. A return value of `''` means "no filter"
|
|
88
|
-
* and is safe to pass directly to `and`/`or` — they filter it out automatically.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* const h = useMongocampQuery()
|
|
92
|
-
* h.and(
|
|
93
|
-
* h.or(h.like('name', 'alice'), h.like('email', 'alice')),
|
|
94
|
-
* h.inClause('role', ['admin', 'user']),
|
|
95
|
-
* h.between('createdAt', '2024-01-01', null),
|
|
96
|
-
* )
|
|
97
|
-
* // => '((name:*alice* OR email:*alice*) AND role:("admin" OR "user") AND createdAt:[2024-01-01 TO *])'
|
|
98
|
-
*/
|
|
99
106
|
export declare function useMongocampQuery(): MongocampQueryHelper;
|
|
@@ -1,32 +1,76 @@
|
|
|
1
|
+
import { escapeFieldName } from "../utils/escapeFieldName.js";
|
|
2
|
+
import { isIsoDateTimeString } from "../utils/isoDate.js";
|
|
1
3
|
import { useMongocampQueryBuilder } from "./useMongocampQueryBuilder.js";
|
|
4
|
+
const NUMBER_BOUNDS = { from: "-9223372036854775808", to: "9223372036854775807" };
|
|
5
|
+
const DATE_BOUNDS = { from: "0001-01-01T00:00:00Z", to: "9999-12-31T23:59:59.999Z" };
|
|
6
|
+
const PLAIN_NUMBER_REGEX = /^-?(?:\d+(?:\.\d+)?|\.\d+)$/;
|
|
7
|
+
const DATE_ONLY_REGEX = /^\d{4}-\d{2}-\d{2}$/;
|
|
8
|
+
function boundKind(value) {
|
|
9
|
+
if (PLAIN_NUMBER_REGEX.test(value)) return "number";
|
|
10
|
+
if (isIsoDateTimeString(value)) return "date";
|
|
11
|
+
return "other";
|
|
12
|
+
}
|
|
13
|
+
function boundText(value) {
|
|
14
|
+
if (value == null) return null;
|
|
15
|
+
const text = value.toString().trim();
|
|
16
|
+
return text === "" || text === "*" ? null : text;
|
|
17
|
+
}
|
|
18
|
+
function escapeBound(value) {
|
|
19
|
+
return value.replace(/[\s[\]{}()"\\^~!&|]/g, "\\$&");
|
|
20
|
+
}
|
|
21
|
+
function widenDate(value, edge) {
|
|
22
|
+
return DATE_ONLY_REGEX.test(value) ? `${value}${edge === "from" ? "T00:00:00Z" : "T23:59:59.999Z"}` : value;
|
|
23
|
+
}
|
|
24
|
+
function farBound(kind, edge) {
|
|
25
|
+
if (kind === "number") return NUMBER_BOUNDS[edge];
|
|
26
|
+
if (kind === "date") return DATE_BOUNDS[edge];
|
|
27
|
+
return "*";
|
|
28
|
+
}
|
|
2
29
|
export function useMongocampQuery() {
|
|
3
30
|
const { q } = useMongocampQueryBuilder();
|
|
4
|
-
function
|
|
5
|
-
return q.field(
|
|
31
|
+
function fieldTerm(fieldName, value) {
|
|
32
|
+
return q.field(escapeFieldName(fieldName), value);
|
|
33
|
+
}
|
|
34
|
+
function existsClauses(fieldName, type) {
|
|
35
|
+
const clauses = [];
|
|
36
|
+
if (!type || type === "text") clauses.push(fieldTerm(fieldName, "*"));
|
|
37
|
+
if (!type || type === "number") clauses.push(fieldTerm(fieldName, q.range(NUMBER_BOUNDS.from, NUMBER_BOUNDS.to)));
|
|
38
|
+
if (!type || type === "date") clauses.push(fieldTerm(fieldName, q.range(DATE_BOUNDS.from, DATE_BOUNDS.to)));
|
|
39
|
+
return clauses;
|
|
40
|
+
}
|
|
41
|
+
function exists(fieldName, type) {
|
|
42
|
+
const clauses = existsClauses(fieldName, type);
|
|
43
|
+
return clauses.length === 1 ? clauses[0] : `(${clauses.join(" OR ")})`;
|
|
6
44
|
}
|
|
7
|
-
function notExists(fieldName) {
|
|
8
|
-
|
|
45
|
+
function notExists(fieldName, type) {
|
|
46
|
+
const clauses = existsClauses(fieldName, type).map((clause) => `-${clause}`);
|
|
47
|
+
return clauses.length === 1 ? clauses[0] : and(...clauses);
|
|
9
48
|
}
|
|
10
|
-
function isEmpty(fieldName) {
|
|
11
|
-
return notExists(fieldName);
|
|
49
|
+
function isEmpty(fieldName, type) {
|
|
50
|
+
return notExists(fieldName, type);
|
|
12
51
|
}
|
|
13
|
-
function isNotEmpty(fieldName) {
|
|
14
|
-
return exists(fieldName);
|
|
52
|
+
function isNotEmpty(fieldName, type) {
|
|
53
|
+
return exists(fieldName, type);
|
|
15
54
|
}
|
|
16
55
|
function equals(fieldName, value) {
|
|
17
56
|
if (value == null || value.toString().length === 0) return "";
|
|
18
|
-
return
|
|
57
|
+
return fieldTerm(fieldName, q.terms(value.toString()));
|
|
19
58
|
}
|
|
20
59
|
function notEquals(fieldName, value) {
|
|
21
60
|
const expr = equals(fieldName, value);
|
|
22
61
|
return expr ? `-${expr}` : "";
|
|
23
62
|
}
|
|
24
63
|
function inRange(fieldName, from, to) {
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
64
|
+
const lower = boundText(from);
|
|
65
|
+
const upper = boundText(to);
|
|
66
|
+
if (lower === null && upper === null) return exists(fieldName);
|
|
67
|
+
const kind = boundKind(lower ?? upper);
|
|
68
|
+
const fromStr = lower === null ? farBound(kind, "from") : escapeBound(boundKind(lower) === "date" ? widenDate(lower, "from") : lower);
|
|
69
|
+
const toStr = upper === null ? farBound(kind, "to") : escapeBound(boundKind(upper) === "date" ? widenDate(upper, "to") : upper);
|
|
70
|
+
return fieldTerm(fieldName, q.range(fromStr, toStr));
|
|
28
71
|
}
|
|
29
72
|
function notInRange(fieldName, from, to) {
|
|
73
|
+
if (boundText(from) === null && boundText(to) === null) return notExists(fieldName);
|
|
30
74
|
return `-${inRange(fieldName, from, to)}`;
|
|
31
75
|
}
|
|
32
76
|
function between(fieldName, from, to) {
|
|
@@ -34,21 +78,21 @@ export function useMongocampQuery() {
|
|
|
34
78
|
}
|
|
35
79
|
function like(fieldName, value) {
|
|
36
80
|
const term = value != null && value.toString().length !== 0 ? q.phrase(`*${value}*`) : "*";
|
|
37
|
-
return
|
|
81
|
+
return fieldTerm(fieldName, term);
|
|
38
82
|
}
|
|
39
83
|
function notLike(fieldName, value) {
|
|
40
84
|
return `-${like(fieldName, value)}`;
|
|
41
85
|
}
|
|
42
86
|
function startsWith(fieldName, value) {
|
|
43
87
|
const term = value != null && value.toString().length !== 0 ? q.phrase(`${value}*`) : "*";
|
|
44
|
-
return
|
|
88
|
+
return fieldTerm(fieldName, term);
|
|
45
89
|
}
|
|
46
90
|
function notStartsWith(fieldName, value) {
|
|
47
91
|
return `-${startsWith(fieldName, value)}`;
|
|
48
92
|
}
|
|
49
93
|
function endsWith(fieldName, value) {
|
|
50
94
|
const term = value != null && value.toString().length !== 0 ? q.phrase(`*${value}`) : "*";
|
|
51
|
-
return
|
|
95
|
+
return fieldTerm(fieldName, term);
|
|
52
96
|
}
|
|
53
97
|
function notEndsWith(fieldName, value) {
|
|
54
98
|
return `-${endsWith(fieldName, value)}`;
|
|
@@ -61,7 +105,7 @@ export function useMongocampQuery() {
|
|
|
61
105
|
if (parameter.length === 0) return "";
|
|
62
106
|
const fields = parameter.map(toTerms);
|
|
63
107
|
const inner = fields.length === 1 ? fields[0] : `(${fields.join(" OR ")})`;
|
|
64
|
-
return
|
|
108
|
+
return fieldTerm(fieldName, inner);
|
|
65
109
|
}
|
|
66
110
|
function notInClause(fieldName, parameter) {
|
|
67
111
|
if (parameter.length === 0) return "";
|