@adminforth/import-export 1.6.2 → 1.8.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/build.log +2 -2
- package/custom/ExportCsv.vue +107 -16
- package/dist/custom/ExportCsv.vue +107 -16
- package/dist/index.d.ts +56 -1
- package/dist/index.js +221 -159
- package/index.ts +265 -176
- package/package.json +1 -1
package/build.log
CHANGED
|
@@ -11,5 +11,5 @@ custom/package.json
|
|
|
11
11
|
custom/pnpm-lock.yaml
|
|
12
12
|
custom/tsconfig.json
|
|
13
13
|
|
|
14
|
-
sent
|
|
15
|
-
total size is
|
|
14
|
+
sent 21,367 bytes received 134 bytes 43,002.00 bytes/sec
|
|
15
|
+
total size is 20,878 speedup is 0.97
|
package/custom/ExportCsv.vue
CHANGED
|
@@ -1,33 +1,127 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div class="
|
|
3
|
-
|
|
2
|
+
<div ref="rootRef" class="relative" @click.stop>
|
|
3
|
+
<div class="cursor-pointer flex gap-2 items-center justify-between -mx-4 -my-2 px-4 py-2" @click="toggle">
|
|
4
|
+
<span class="flex gap-2 items-center">
|
|
5
|
+
{{ $t('Export to CSV') }}
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
<svg v-if="inProgress"
|
|
8
|
+
aria-hidden="true" class="w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
|
|
9
|
+
</span>
|
|
10
|
+
<svg class="w-2 h-2 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
|
|
11
|
+
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 1 1 5l4 4"/>
|
|
12
|
+
</svg>
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
<div v-if="open"
|
|
16
|
+
class="absolute right-full top-0 mr-5 z-40 w-max bg-lightThreeDotsMenuBodyBackground dark:bg-darkThreeDotsMenuBodyBackground rounded-lg shadow border border-gray-100 dark:border-gray-600">
|
|
17
|
+
<ul class="py-2 text-sm text-lightThreeDotsMenuBodyText dark:text-darkThreeDotsMenuBodyText">
|
|
18
|
+
<li v-for="option in options" :key="option.select">
|
|
19
|
+
<div
|
|
20
|
+
class="px-4 py-1.5 text-sm whitespace-nowrap"
|
|
21
|
+
:class="isDisabled(option)
|
|
22
|
+
? 'opacity-50 cursor-not-allowed'
|
|
23
|
+
: 'cursor-pointer hover:text-lightThreeDotsMenuBodyTextHover hover:bg-lightThreeDotsMenuBodyBackgroundHover dark:hover:bg-darkThreeDotsMenuBodyBackgroundHover dark:hover:text-darkThreeDotsMenuBodyTextHover'"
|
|
24
|
+
@click="!isDisabled(option) && run(option.select)"
|
|
25
|
+
>
|
|
26
|
+
{{ option.label() }}
|
|
27
|
+
</div>
|
|
28
|
+
</li>
|
|
29
|
+
</ul>
|
|
30
|
+
</div>
|
|
7
31
|
</div>
|
|
8
32
|
</template>
|
|
9
33
|
|
|
10
34
|
<script setup lang="ts">
|
|
11
|
-
import { ref } from 'vue';
|
|
35
|
+
import { ref, onMounted, onUnmounted } from 'vue';
|
|
12
36
|
import { useCoreStore } from '@/stores/core';
|
|
13
37
|
import { callAdminForthApi } from '@/utils';
|
|
14
38
|
import { useFiltersStore } from '@/stores/filters';
|
|
15
39
|
import adminforth from '@/adminforth';
|
|
16
40
|
import Papa from 'papaparse';
|
|
41
|
+
import { useI18n } from 'vue-i18n';
|
|
17
42
|
|
|
43
|
+
const { t } = useI18n();
|
|
18
44
|
const filtersStore = useFiltersStore();
|
|
19
45
|
const coreStore = useCoreStore();
|
|
20
46
|
const inProgress = ref(false);
|
|
47
|
+
const open = ref(false);
|
|
48
|
+
const rootRef = ref<HTMLElement | null>(null);
|
|
49
|
+
const allCount = ref<number | null>(null);
|
|
50
|
+
const filteredCount = ref<number | null>(null);
|
|
21
51
|
|
|
22
52
|
defineExpose({
|
|
23
|
-
click,
|
|
53
|
+
click: () => { toggle(); },
|
|
24
54
|
});
|
|
25
55
|
|
|
26
56
|
const props = defineProps({
|
|
27
57
|
meta: Object,
|
|
28
58
|
record: Object,
|
|
59
|
+
checkboxes: Array,
|
|
29
60
|
});
|
|
30
61
|
|
|
62
|
+
function formatCount(count: number) {
|
|
63
|
+
if (count < 1000) return `${count}`;
|
|
64
|
+
if (count < 1_000_000) return `${Math.floor(count / 1000)}k+`;
|
|
65
|
+
return `${Math.floor(count / 1_000_000)}M+`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function withCount(label: string, count: number | null) {
|
|
69
|
+
return count === null ? `${label} (…)` : `${label} (${formatCount(count)})`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const options = [
|
|
73
|
+
{ select: 'all', label: () => withCount(t('All'), allCount.value) },
|
|
74
|
+
{ select: 'filtered', label: () => withCount(t('Filtered'), filteredCount.value) },
|
|
75
|
+
{ select: 'selected', label: () => `${t('Selected')} (${props.checkboxes?.length || 0})` },
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
function isDisabled(option: { select: string }) {
|
|
79
|
+
return option.select === 'selected' && !(props.checkboxes?.length);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function handleClickOutside(e: MouseEvent) {
|
|
83
|
+
if (open.value && rootRef.value && !rootRef.value.contains(e.target as Node)) {
|
|
84
|
+
open.value = false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
onMounted(() => document.addEventListener('mousedown', handleClickOutside));
|
|
89
|
+
onUnmounted(() => document.removeEventListener('mousedown', handleClickOutside));
|
|
90
|
+
|
|
91
|
+
function toggle() {
|
|
92
|
+
open.value = !open.value;
|
|
93
|
+
if (open.value) {
|
|
94
|
+
fetchCounts();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function fetchCounts() {
|
|
99
|
+
const resourceId = coreStore.resource?.resourceId;
|
|
100
|
+
if (!resourceId) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const countOnly = (filters: any) => callAdminForthApi({
|
|
104
|
+
path: '/get_resource_data',
|
|
105
|
+
method: 'POST',
|
|
106
|
+
body: { source: 'list', resourceId, limit: 1, offset: 0, filters, sort: [] },
|
|
107
|
+
});
|
|
108
|
+
try {
|
|
109
|
+
const [allResp, filteredResp] = await Promise.all([
|
|
110
|
+
countOnly([]),
|
|
111
|
+
countOnly(filtersStore.getFilters()),
|
|
112
|
+
]);
|
|
113
|
+
allCount.value = allResp?.total ?? null;
|
|
114
|
+
filteredCount.value = filteredResp?.total ?? null;
|
|
115
|
+
} catch (e) {
|
|
116
|
+
// leave counts as null (renders "…") on failure
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function run(select: string) {
|
|
121
|
+
open.value = false;
|
|
122
|
+
exportCsv(select);
|
|
123
|
+
}
|
|
124
|
+
|
|
31
125
|
function downloadFile(data: string, filename: string) {
|
|
32
126
|
const blob = new Blob([data], { type: 'text/csv' });
|
|
33
127
|
const url = window.URL.createObjectURL(blob);
|
|
@@ -38,15 +132,16 @@ function downloadFile(data: string, filename: string) {
|
|
|
38
132
|
window.URL.revokeObjectURL(url);
|
|
39
133
|
}
|
|
40
134
|
|
|
41
|
-
async function exportCsv() {
|
|
135
|
+
async function exportCsv(select: string) {
|
|
42
136
|
inProgress.value = true;
|
|
43
137
|
try {
|
|
44
138
|
const resp = await callAdminForthApi({
|
|
45
|
-
path: `/plugin/${props.meta
|
|
139
|
+
path: `/plugin/${props.meta?.pluginInstanceId}/export-csv`,
|
|
46
140
|
method: 'POST',
|
|
47
141
|
body: {
|
|
48
|
-
filters:
|
|
142
|
+
filters: select === 'filtered' ? filtersStore.getFilters() : [],
|
|
49
143
|
sort: filtersStore.getSort(),
|
|
144
|
+
selectedIds: select === 'selected' ? props.checkboxes : undefined,
|
|
50
145
|
}
|
|
51
146
|
});
|
|
52
147
|
|
|
@@ -55,14 +150,14 @@ async function exportCsv() {
|
|
|
55
150
|
}
|
|
56
151
|
|
|
57
152
|
// Generate properly formatted CSV
|
|
58
|
-
const csvContent = '
|
|
153
|
+
const csvContent = '' + Papa.unparse(resp.data, {
|
|
59
154
|
quotes: resp.columnsToForceQuote, // Force quotes only certain columns (!!!not all this breaks BI/Excel tasks)
|
|
60
155
|
quoteChar: '"',
|
|
61
156
|
escapeChar: '"',
|
|
62
157
|
});
|
|
63
158
|
|
|
64
159
|
// Download the file
|
|
65
|
-
const filename = `export-${coreStore.resource
|
|
160
|
+
const filename = `export-${coreStore.resource?.resourceId}-${new Date().toISOString()}.csv`;
|
|
66
161
|
downloadFile(csvContent, filename);
|
|
67
162
|
|
|
68
163
|
adminforth.alert({
|
|
@@ -78,8 +173,4 @@ async function exportCsv() {
|
|
|
78
173
|
adminforth.list.closeThreeDotsDropdown();
|
|
79
174
|
}
|
|
80
175
|
}
|
|
81
|
-
|
|
82
|
-
function click() {
|
|
83
|
-
exportCsv();
|
|
84
|
-
}
|
|
85
|
-
</script>
|
|
176
|
+
</script>
|
|
@@ -1,33 +1,127 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div class="
|
|
3
|
-
|
|
2
|
+
<div ref="rootRef" class="relative" @click.stop>
|
|
3
|
+
<div class="cursor-pointer flex gap-2 items-center justify-between -mx-4 -my-2 px-4 py-2" @click="toggle">
|
|
4
|
+
<span class="flex gap-2 items-center">
|
|
5
|
+
{{ $t('Export to CSV') }}
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
<svg v-if="inProgress"
|
|
8
|
+
aria-hidden="true" class="w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
|
|
9
|
+
</span>
|
|
10
|
+
<svg class="w-2 h-2 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
|
|
11
|
+
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 1 1 5l4 4"/>
|
|
12
|
+
</svg>
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
<div v-if="open"
|
|
16
|
+
class="absolute right-full top-0 mr-5 z-40 w-max bg-lightThreeDotsMenuBodyBackground dark:bg-darkThreeDotsMenuBodyBackground rounded-lg shadow border border-gray-100 dark:border-gray-600">
|
|
17
|
+
<ul class="py-2 text-sm text-lightThreeDotsMenuBodyText dark:text-darkThreeDotsMenuBodyText">
|
|
18
|
+
<li v-for="option in options" :key="option.select">
|
|
19
|
+
<div
|
|
20
|
+
class="px-4 py-1.5 text-sm whitespace-nowrap"
|
|
21
|
+
:class="isDisabled(option)
|
|
22
|
+
? 'opacity-50 cursor-not-allowed'
|
|
23
|
+
: 'cursor-pointer hover:text-lightThreeDotsMenuBodyTextHover hover:bg-lightThreeDotsMenuBodyBackgroundHover dark:hover:bg-darkThreeDotsMenuBodyBackgroundHover dark:hover:text-darkThreeDotsMenuBodyTextHover'"
|
|
24
|
+
@click="!isDisabled(option) && run(option.select)"
|
|
25
|
+
>
|
|
26
|
+
{{ option.label() }}
|
|
27
|
+
</div>
|
|
28
|
+
</li>
|
|
29
|
+
</ul>
|
|
30
|
+
</div>
|
|
7
31
|
</div>
|
|
8
32
|
</template>
|
|
9
33
|
|
|
10
34
|
<script setup lang="ts">
|
|
11
|
-
import { ref } from 'vue';
|
|
35
|
+
import { ref, onMounted, onUnmounted } from 'vue';
|
|
12
36
|
import { useCoreStore } from '@/stores/core';
|
|
13
37
|
import { callAdminForthApi } from '@/utils';
|
|
14
38
|
import { useFiltersStore } from '@/stores/filters';
|
|
15
39
|
import adminforth from '@/adminforth';
|
|
16
40
|
import Papa from 'papaparse';
|
|
41
|
+
import { useI18n } from 'vue-i18n';
|
|
17
42
|
|
|
43
|
+
const { t } = useI18n();
|
|
18
44
|
const filtersStore = useFiltersStore();
|
|
19
45
|
const coreStore = useCoreStore();
|
|
20
46
|
const inProgress = ref(false);
|
|
47
|
+
const open = ref(false);
|
|
48
|
+
const rootRef = ref<HTMLElement | null>(null);
|
|
49
|
+
const allCount = ref<number | null>(null);
|
|
50
|
+
const filteredCount = ref<number | null>(null);
|
|
21
51
|
|
|
22
52
|
defineExpose({
|
|
23
|
-
click,
|
|
53
|
+
click: () => { toggle(); },
|
|
24
54
|
});
|
|
25
55
|
|
|
26
56
|
const props = defineProps({
|
|
27
57
|
meta: Object,
|
|
28
58
|
record: Object,
|
|
59
|
+
checkboxes: Array,
|
|
29
60
|
});
|
|
30
61
|
|
|
62
|
+
function formatCount(count: number) {
|
|
63
|
+
if (count < 1000) return `${count}`;
|
|
64
|
+
if (count < 1_000_000) return `${Math.floor(count / 1000)}k+`;
|
|
65
|
+
return `${Math.floor(count / 1_000_000)}M+`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function withCount(label: string, count: number | null) {
|
|
69
|
+
return count === null ? `${label} (…)` : `${label} (${formatCount(count)})`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const options = [
|
|
73
|
+
{ select: 'all', label: () => withCount(t('All'), allCount.value) },
|
|
74
|
+
{ select: 'filtered', label: () => withCount(t('Filtered'), filteredCount.value) },
|
|
75
|
+
{ select: 'selected', label: () => `${t('Selected')} (${props.checkboxes?.length || 0})` },
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
function isDisabled(option: { select: string }) {
|
|
79
|
+
return option.select === 'selected' && !(props.checkboxes?.length);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function handleClickOutside(e: MouseEvent) {
|
|
83
|
+
if (open.value && rootRef.value && !rootRef.value.contains(e.target as Node)) {
|
|
84
|
+
open.value = false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
onMounted(() => document.addEventListener('mousedown', handleClickOutside));
|
|
89
|
+
onUnmounted(() => document.removeEventListener('mousedown', handleClickOutside));
|
|
90
|
+
|
|
91
|
+
function toggle() {
|
|
92
|
+
open.value = !open.value;
|
|
93
|
+
if (open.value) {
|
|
94
|
+
fetchCounts();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function fetchCounts() {
|
|
99
|
+
const resourceId = coreStore.resource?.resourceId;
|
|
100
|
+
if (!resourceId) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const countOnly = (filters: any) => callAdminForthApi({
|
|
104
|
+
path: '/get_resource_data',
|
|
105
|
+
method: 'POST',
|
|
106
|
+
body: { source: 'list', resourceId, limit: 1, offset: 0, filters, sort: [] },
|
|
107
|
+
});
|
|
108
|
+
try {
|
|
109
|
+
const [allResp, filteredResp] = await Promise.all([
|
|
110
|
+
countOnly([]),
|
|
111
|
+
countOnly(filtersStore.getFilters()),
|
|
112
|
+
]);
|
|
113
|
+
allCount.value = allResp?.total ?? null;
|
|
114
|
+
filteredCount.value = filteredResp?.total ?? null;
|
|
115
|
+
} catch (e) {
|
|
116
|
+
// leave counts as null (renders "…") on failure
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function run(select: string) {
|
|
121
|
+
open.value = false;
|
|
122
|
+
exportCsv(select);
|
|
123
|
+
}
|
|
124
|
+
|
|
31
125
|
function downloadFile(data: string, filename: string) {
|
|
32
126
|
const blob = new Blob([data], { type: 'text/csv' });
|
|
33
127
|
const url = window.URL.createObjectURL(blob);
|
|
@@ -38,15 +132,16 @@ function downloadFile(data: string, filename: string) {
|
|
|
38
132
|
window.URL.revokeObjectURL(url);
|
|
39
133
|
}
|
|
40
134
|
|
|
41
|
-
async function exportCsv() {
|
|
135
|
+
async function exportCsv(select: string) {
|
|
42
136
|
inProgress.value = true;
|
|
43
137
|
try {
|
|
44
138
|
const resp = await callAdminForthApi({
|
|
45
|
-
path: `/plugin/${props.meta
|
|
139
|
+
path: `/plugin/${props.meta?.pluginInstanceId}/export-csv`,
|
|
46
140
|
method: 'POST',
|
|
47
141
|
body: {
|
|
48
|
-
filters:
|
|
142
|
+
filters: select === 'filtered' ? filtersStore.getFilters() : [],
|
|
49
143
|
sort: filtersStore.getSort(),
|
|
144
|
+
selectedIds: select === 'selected' ? props.checkboxes : undefined,
|
|
50
145
|
}
|
|
51
146
|
});
|
|
52
147
|
|
|
@@ -55,14 +150,14 @@ async function exportCsv() {
|
|
|
55
150
|
}
|
|
56
151
|
|
|
57
152
|
// Generate properly formatted CSV
|
|
58
|
-
const csvContent = '
|
|
153
|
+
const csvContent = '' + Papa.unparse(resp.data, {
|
|
59
154
|
quotes: resp.columnsToForceQuote, // Force quotes only certain columns (!!!not all this breaks BI/Excel tasks)
|
|
60
155
|
quoteChar: '"',
|
|
61
156
|
escapeChar: '"',
|
|
62
157
|
});
|
|
63
158
|
|
|
64
159
|
// Download the file
|
|
65
|
-
const filename = `export-${coreStore.resource
|
|
160
|
+
const filename = `export-${coreStore.resource?.resourceId}-${new Date().toISOString()}.csv`;
|
|
66
161
|
downloadFile(csvContent, filename);
|
|
67
162
|
|
|
68
163
|
adminforth.alert({
|
|
@@ -78,8 +173,4 @@ async function exportCsv() {
|
|
|
78
173
|
adminforth.list.closeThreeDotsDropdown();
|
|
79
174
|
}
|
|
80
175
|
}
|
|
81
|
-
|
|
82
|
-
function click() {
|
|
83
|
-
exportCsv();
|
|
84
|
-
}
|
|
85
|
-
</script>
|
|
176
|
+
</script>
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AdminForthPlugin } from "adminforth";
|
|
2
|
-
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthResource } from "adminforth";
|
|
2
|
+
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthResource, AdminUser, HttpExtra, IAdminForthHttpResponse } from "adminforth";
|
|
3
3
|
import type { PluginOptions } from './types.js';
|
|
4
4
|
export default class ImportExport extends AdminForthPlugin {
|
|
5
5
|
options: PluginOptions;
|
|
@@ -15,6 +15,61 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
15
15
|
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource): void;
|
|
16
16
|
instanceUniqueRepresentation(pluginOptions: any): string;
|
|
17
17
|
setupEndpoints(server: IHttpServer): void;
|
|
18
|
+
/**
|
|
19
|
+
* Export resource records as CSV-ready data.
|
|
20
|
+
* Can be called programmatically, e.g. `this.exportCsv(filters, sort)`.
|
|
21
|
+
*/
|
|
22
|
+
exportCsv(filters: any, sort: any, options?: {
|
|
23
|
+
adminUser?: AdminUser;
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
}): Promise<{
|
|
26
|
+
ok: true;
|
|
27
|
+
data: {
|
|
28
|
+
fields: string[];
|
|
29
|
+
data: unknown[][];
|
|
30
|
+
};
|
|
31
|
+
columnsToForceQuote: boolean[];
|
|
32
|
+
exportedCount: number;
|
|
33
|
+
}>;
|
|
34
|
+
/**
|
|
35
|
+
* Import records from column-oriented data, creating new records and updating
|
|
36
|
+
* existing ones (matched by primary key).
|
|
37
|
+
* Can be called programmatically, e.g. `this.importCsv(data, { adminUser })`.
|
|
38
|
+
*/
|
|
39
|
+
importCsv(data: Record<string, unknown[]>, options?: {
|
|
40
|
+
adminUser?: AdminUser;
|
|
41
|
+
headers?: Record<string, string>;
|
|
42
|
+
extra?: HttpExtra;
|
|
43
|
+
response?: IAdminForthHttpResponse;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
ok: boolean;
|
|
46
|
+
importedCount?: number;
|
|
47
|
+
updatedCount?: number;
|
|
48
|
+
errors: string[];
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* Import only records that do not already exist (matched by primary key).
|
|
52
|
+
* Can be called programmatically, e.g. `this.importCsvNewOnly(data, { adminUser })`.
|
|
53
|
+
*/
|
|
54
|
+
importCsvNewOnly(data: Record<string, unknown[]>, options?: {
|
|
55
|
+
adminUser?: AdminUser;
|
|
56
|
+
headers?: Record<string, string>;
|
|
57
|
+
extra?: HttpExtra;
|
|
58
|
+
}): Promise<{
|
|
59
|
+
ok: boolean;
|
|
60
|
+
importedCount?: number;
|
|
61
|
+
errors: string[];
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* Check how many of the given records already exist (matched by primary key).
|
|
65
|
+
* Can be called programmatically, e.g. `this.checkRecords(data)`.
|
|
66
|
+
*/
|
|
67
|
+
checkRecords(data: Record<string, unknown[]>): Promise<{
|
|
68
|
+
ok: true;
|
|
69
|
+
total: number;
|
|
70
|
+
existingCount: number;
|
|
71
|
+
newCount: number;
|
|
72
|
+
}>;
|
|
18
73
|
private getColumnNames;
|
|
19
74
|
private validateColumns;
|
|
20
75
|
private buildRowsFromData;
|