@adminforth/import-export 1.1.0 → 1.2.1
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 +7 -0
- package/build.log +2 -2
- package/custom/ImportCsv.vue +80 -17
- package/dist/custom/ImportCsv.vue +80 -17
- package/dist/index.js +92 -21
- package/index.ts +111 -26
- package/package.json +2 -1
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# AdminForth ImportExport Plugin
|
|
2
|
+
|
|
3
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="License: MIT" /> <img src="https://woodpecker.devforth.io/api/badges/3848/status.svg" alt="Build Status" /> <a href="https://www.npmjs.com/package/@adminforth/import-export"> <img src="https://img.shields.io/npm/dt/@adminforth/import-export" alt="npm downloads" /></a> <a href="https://www.npmjs.com/package/@adminforth/import-export"><img src="https://img.shields.io/npm/v/@adminforth/import-export" alt="npm version" /></a> <a href="https://www.npmjs.com/package/@adminforth/import-export">
|
|
4
|
+
|
|
5
|
+
Allows to add import/export to csv options to an adminforth table.
|
|
6
|
+
|
|
7
|
+
## For usage, see [AdminForth ImportExport Documentation](https://adminforth.dev/docs/tutorial/Plugins/import-export/)
|
package/build.log
CHANGED
|
@@ -10,5 +10,5 @@ custom/package-lock.json
|
|
|
10
10
|
custom/package.json
|
|
11
11
|
custom/tsconfig.json
|
|
12
12
|
|
|
13
|
-
sent
|
|
14
|
-
total size is
|
|
13
|
+
sent 12,488 bytes received 115 bytes 25,206.00 bytes/sec
|
|
14
|
+
total size is 12,074 speedup is 0.96
|
package/custom/ImportCsv.vue
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
<div @click="importCsv" class="cursor-pointer flex gap-2 items-center">
|
|
3
|
+
{{$t('Import from CSV')}}
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
<svg v-if="inProgress"
|
|
6
|
+
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>
|
|
7
|
+
</div>
|
|
8
|
+
<Dialog ref="confirmDialog"
|
|
9
|
+
:header="t('Import Confirmation')"
|
|
10
|
+
:buttons="[
|
|
11
|
+
{ label: t('Import & Replace'), onclick: (dialog) => { confirmImport(dialog) } },
|
|
12
|
+
{ label: t('Import New Only'), onclick: (dialog) => { confirmImportNewOnly(dialog) } },
|
|
13
|
+
{ label: t('Cancel'), onclick: (dialog) => dialog.hide() }
|
|
14
|
+
]"
|
|
15
|
+
>
|
|
16
|
+
<div v-if="importStats">
|
|
17
|
+
<p>{{ $t('Are you sure you want to continue?') }}</p>
|
|
18
|
+
<p class="mt-2">{{ $t('You are importing {count} records:', { count: importStats.total }) }}</p>
|
|
19
|
+
<ul class="list-disc ml-6 mt-2">
|
|
20
|
+
<li>{{ $t('{count} existing records will be replaced (old records will be lost)', { count: importStats.existingCount }) }}</li>
|
|
21
|
+
<li>{{ $t('{count} new records will be added', { count: importStats.newCount }) }}</li>
|
|
22
|
+
</ul>
|
|
23
|
+
</div>
|
|
24
|
+
</Dialog>
|
|
9
25
|
</template>
|
|
10
26
|
|
|
11
27
|
<script setup lang="ts">
|
|
@@ -13,32 +29,54 @@ import { ref, Ref } from 'vue';
|
|
|
13
29
|
import { callAdminForthApi } from '@/utils';
|
|
14
30
|
import adminforth from '@/adminforth';
|
|
15
31
|
import Papa from 'papaparse';
|
|
32
|
+
import { Dialog } from '@/afcl';
|
|
33
|
+
import { useI18n } from 'vue-i18n';
|
|
16
34
|
|
|
17
|
-
const
|
|
35
|
+
const { t } = useI18n();
|
|
18
36
|
|
|
37
|
+
const inProgress: Ref<boolean> = ref(false);
|
|
38
|
+
const confirmDialog = ref(null);
|
|
39
|
+
const importStats = ref(null);
|
|
40
|
+
const pendingData = ref(null);
|
|
19
41
|
const props = defineProps({
|
|
20
42
|
meta: Object,
|
|
21
43
|
record: Object,
|
|
22
44
|
});
|
|
23
45
|
|
|
24
|
-
async function
|
|
25
|
-
|
|
46
|
+
async function confirmImport(dialog) {
|
|
47
|
+
dialog.hide();
|
|
48
|
+
await postData(pendingData.value);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function checkRecords(data: Record<string, string[]>) {
|
|
52
|
+
const resp = await callAdminForthApi({
|
|
53
|
+
path: `/plugin/${props.meta.pluginInstanceId}/check-records`,
|
|
54
|
+
method: 'POST',
|
|
55
|
+
body: { data }
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return resp;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function confirmImportNewOnly(dialog) {
|
|
62
|
+
dialog.hide();
|
|
63
|
+
await postDataNewOnly(pendingData.value);
|
|
64
|
+
}
|
|
26
65
|
|
|
66
|
+
async function postData(data: Record<string, string[]>, skipDuplicates: boolean = false) {
|
|
27
67
|
const resp = await callAdminForthApi({
|
|
28
68
|
path: `/plugin/${props.meta.pluginInstanceId}/import-csv`,
|
|
29
69
|
method: 'POST',
|
|
30
|
-
body: {
|
|
31
|
-
data
|
|
32
|
-
}
|
|
70
|
+
body: { data }
|
|
33
71
|
});
|
|
34
72
|
|
|
35
73
|
inProgress.value = false;
|
|
36
74
|
|
|
37
|
-
if (resp.importedCount > 0) {
|
|
75
|
+
if (resp.importedCount > 0 || resp.updatedCount > 0) {
|
|
38
76
|
adminforth.list.refresh();
|
|
39
77
|
}
|
|
40
78
|
adminforth.alert({
|
|
41
|
-
message: `Imported
|
|
79
|
+
message: `Imported ${resp.importedCount || 0} records. Updated ${resp.updatedCount || 0} records. ${resp.errors?.length ? `Errors: ${resp.errors.join(', ')}` : ''}`,
|
|
42
80
|
variant: resp.errors?.length ? (
|
|
43
81
|
resp.importedCount ? 'warning' : 'danger'
|
|
44
82
|
) : 'success'
|
|
@@ -47,8 +85,26 @@ async function postData(data: Record<string, string[]>) {
|
|
|
47
85
|
adminforth.list.closeThreeDotsDropdown();
|
|
48
86
|
}
|
|
49
87
|
|
|
88
|
+
async function postDataNewOnly(data: Record<string, string[]>) {
|
|
89
|
+
const resp = await callAdminForthApi({
|
|
90
|
+
path: `/plugin/${props.meta.pluginInstanceId}/import-csv-new-only`,
|
|
91
|
+
method: 'POST',
|
|
92
|
+
body: { data }
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
inProgress.value = false;
|
|
96
|
+
if (resp.importedCount > 0) {
|
|
97
|
+
adminforth.list.refresh();
|
|
98
|
+
}
|
|
99
|
+
adminforth.alert({
|
|
100
|
+
message: `Imported ${resp.importedCount || 0} records. ${resp.errors?.length ? `Errors: ${resp.errors.join(', ')}` : ''}`,
|
|
101
|
+
variant: resp.errors?.length ? 'warning' : 'success'
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
adminforth.list.closeThreeDotsDropdown();
|
|
105
|
+
}
|
|
106
|
+
|
|
50
107
|
async function importCsv() {
|
|
51
|
-
// create file input and open it (with csv type)
|
|
52
108
|
inProgress.value = false;
|
|
53
109
|
const fileInput = document.createElement('input');
|
|
54
110
|
|
|
@@ -63,7 +119,6 @@ async function importCsv() {
|
|
|
63
119
|
inProgress.value = false;
|
|
64
120
|
return;
|
|
65
121
|
}
|
|
66
|
-
|
|
67
122
|
const reader = new FileReader();
|
|
68
123
|
reader.onload = async (e) => {
|
|
69
124
|
try {
|
|
@@ -72,6 +127,7 @@ async function importCsv() {
|
|
|
72
127
|
Papa.parse(text, {
|
|
73
128
|
header: true,
|
|
74
129
|
skipEmptyLines: true,
|
|
130
|
+
|
|
75
131
|
complete: async (results) => {
|
|
76
132
|
if (results.errors.length > 0) {
|
|
77
133
|
throw new Error(`CSV parsing errors: ${results.errors.map(e => e.message).join(', ')}`);
|
|
@@ -86,7 +142,14 @@ async function importCsv() {
|
|
|
86
142
|
data[column] = rows.map(row => row[column]);
|
|
87
143
|
});
|
|
88
144
|
|
|
89
|
-
|
|
145
|
+
// Store data for later use
|
|
146
|
+
pendingData.value = data;
|
|
147
|
+
|
|
148
|
+
// Check records and show confirmation
|
|
149
|
+
const stats = await checkRecords(data);
|
|
150
|
+
importStats.value = stats;
|
|
151
|
+
confirmDialog.value?.open();
|
|
152
|
+
inProgress.value = false;
|
|
90
153
|
},
|
|
91
154
|
error: (error) => {
|
|
92
155
|
throw new Error(`Failed to parse CSV: ${error.message}`);
|
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
<div @click="importCsv" class="cursor-pointer flex gap-2 items-center">
|
|
3
|
+
{{$t('Import from CSV')}}
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
<svg v-if="inProgress"
|
|
6
|
+
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>
|
|
7
|
+
</div>
|
|
8
|
+
<Dialog ref="confirmDialog"
|
|
9
|
+
:header="t('Import Confirmation')"
|
|
10
|
+
:buttons="[
|
|
11
|
+
{ label: t('Import & Replace'), onclick: (dialog) => { confirmImport(dialog) } },
|
|
12
|
+
{ label: t('Import New Only'), onclick: (dialog) => { confirmImportNewOnly(dialog) } },
|
|
13
|
+
{ label: t('Cancel'), onclick: (dialog) => dialog.hide() }
|
|
14
|
+
]"
|
|
15
|
+
>
|
|
16
|
+
<div v-if="importStats">
|
|
17
|
+
<p>{{ $t('Are you sure you want to continue?') }}</p>
|
|
18
|
+
<p class="mt-2">{{ $t('You are importing {count} records:', { count: importStats.total }) }}</p>
|
|
19
|
+
<ul class="list-disc ml-6 mt-2">
|
|
20
|
+
<li>{{ $t('{count} existing records will be replaced (old records will be lost)', { count: importStats.existingCount }) }}</li>
|
|
21
|
+
<li>{{ $t('{count} new records will be added', { count: importStats.newCount }) }}</li>
|
|
22
|
+
</ul>
|
|
23
|
+
</div>
|
|
24
|
+
</Dialog>
|
|
9
25
|
</template>
|
|
10
26
|
|
|
11
27
|
<script setup lang="ts">
|
|
@@ -13,32 +29,54 @@ import { ref, Ref } from 'vue';
|
|
|
13
29
|
import { callAdminForthApi } from '@/utils';
|
|
14
30
|
import adminforth from '@/adminforth';
|
|
15
31
|
import Papa from 'papaparse';
|
|
32
|
+
import { Dialog } from '@/afcl';
|
|
33
|
+
import { useI18n } from 'vue-i18n';
|
|
16
34
|
|
|
17
|
-
const
|
|
35
|
+
const { t } = useI18n();
|
|
18
36
|
|
|
37
|
+
const inProgress: Ref<boolean> = ref(false);
|
|
38
|
+
const confirmDialog = ref(null);
|
|
39
|
+
const importStats = ref(null);
|
|
40
|
+
const pendingData = ref(null);
|
|
19
41
|
const props = defineProps({
|
|
20
42
|
meta: Object,
|
|
21
43
|
record: Object,
|
|
22
44
|
});
|
|
23
45
|
|
|
24
|
-
async function
|
|
25
|
-
|
|
46
|
+
async function confirmImport(dialog) {
|
|
47
|
+
dialog.hide();
|
|
48
|
+
await postData(pendingData.value);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function checkRecords(data: Record<string, string[]>) {
|
|
52
|
+
const resp = await callAdminForthApi({
|
|
53
|
+
path: `/plugin/${props.meta.pluginInstanceId}/check-records`,
|
|
54
|
+
method: 'POST',
|
|
55
|
+
body: { data }
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return resp;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function confirmImportNewOnly(dialog) {
|
|
62
|
+
dialog.hide();
|
|
63
|
+
await postDataNewOnly(pendingData.value);
|
|
64
|
+
}
|
|
26
65
|
|
|
66
|
+
async function postData(data: Record<string, string[]>, skipDuplicates: boolean = false) {
|
|
27
67
|
const resp = await callAdminForthApi({
|
|
28
68
|
path: `/plugin/${props.meta.pluginInstanceId}/import-csv`,
|
|
29
69
|
method: 'POST',
|
|
30
|
-
body: {
|
|
31
|
-
data
|
|
32
|
-
}
|
|
70
|
+
body: { data }
|
|
33
71
|
});
|
|
34
72
|
|
|
35
73
|
inProgress.value = false;
|
|
36
74
|
|
|
37
|
-
if (resp.importedCount > 0) {
|
|
75
|
+
if (resp.importedCount > 0 || resp.updatedCount > 0) {
|
|
38
76
|
adminforth.list.refresh();
|
|
39
77
|
}
|
|
40
78
|
adminforth.alert({
|
|
41
|
-
message: `Imported
|
|
79
|
+
message: `Imported ${resp.importedCount || 0} records. Updated ${resp.updatedCount || 0} records. ${resp.errors?.length ? `Errors: ${resp.errors.join(', ')}` : ''}`,
|
|
42
80
|
variant: resp.errors?.length ? (
|
|
43
81
|
resp.importedCount ? 'warning' : 'danger'
|
|
44
82
|
) : 'success'
|
|
@@ -47,8 +85,26 @@ async function postData(data: Record<string, string[]>) {
|
|
|
47
85
|
adminforth.list.closeThreeDotsDropdown();
|
|
48
86
|
}
|
|
49
87
|
|
|
88
|
+
async function postDataNewOnly(data: Record<string, string[]>) {
|
|
89
|
+
const resp = await callAdminForthApi({
|
|
90
|
+
path: `/plugin/${props.meta.pluginInstanceId}/import-csv-new-only`,
|
|
91
|
+
method: 'POST',
|
|
92
|
+
body: { data }
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
inProgress.value = false;
|
|
96
|
+
if (resp.importedCount > 0) {
|
|
97
|
+
adminforth.list.refresh();
|
|
98
|
+
}
|
|
99
|
+
adminforth.alert({
|
|
100
|
+
message: `Imported ${resp.importedCount || 0} records. ${resp.errors?.length ? `Errors: ${resp.errors.join(', ')}` : ''}`,
|
|
101
|
+
variant: resp.errors?.length ? 'warning' : 'success'
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
adminforth.list.closeThreeDotsDropdown();
|
|
105
|
+
}
|
|
106
|
+
|
|
50
107
|
async function importCsv() {
|
|
51
|
-
// create file input and open it (with csv type)
|
|
52
108
|
inProgress.value = false;
|
|
53
109
|
const fileInput = document.createElement('input');
|
|
54
110
|
|
|
@@ -63,7 +119,6 @@ async function importCsv() {
|
|
|
63
119
|
inProgress.value = false;
|
|
64
120
|
return;
|
|
65
121
|
}
|
|
66
|
-
|
|
67
122
|
const reader = new FileReader();
|
|
68
123
|
reader.onload = async (e) => {
|
|
69
124
|
try {
|
|
@@ -72,6 +127,7 @@ async function importCsv() {
|
|
|
72
127
|
Papa.parse(text, {
|
|
73
128
|
header: true,
|
|
74
129
|
skipEmptyLines: true,
|
|
130
|
+
|
|
75
131
|
complete: async (results) => {
|
|
76
132
|
if (results.errors.length > 0) {
|
|
77
133
|
throw new Error(`CSV parsing errors: ${results.errors.map(e => e.message).join(', ')}`);
|
|
@@ -86,7 +142,14 @@ async function importCsv() {
|
|
|
86
142
|
data[column] = rows.map(row => row[column]);
|
|
87
143
|
});
|
|
88
144
|
|
|
89
|
-
|
|
145
|
+
// Store data for later use
|
|
146
|
+
pendingData.value = data;
|
|
147
|
+
|
|
148
|
+
// Check records and show confirmation
|
|
149
|
+
const stats = await checkRecords(data);
|
|
150
|
+
importStats.value = stats;
|
|
151
|
+
confirmDialog.value?.open();
|
|
152
|
+
inProgress.value = false;
|
|
90
153
|
},
|
|
91
154
|
error: (error) => {
|
|
92
155
|
throw new Error(`Failed to parse CSV: ${error.message}`);
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { AdminForthPlugin, suggestIfTypo,
|
|
10
|
+
import { AdminForthPlugin, suggestIfTypo, Filters } from "adminforth";
|
|
11
11
|
export default class ImportExport extends AdminForthPlugin {
|
|
12
12
|
constructor(options) {
|
|
13
13
|
super(options, import.meta.url);
|
|
@@ -56,28 +56,11 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
56
56
|
noAuth: true,
|
|
57
57
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body }) {
|
|
58
58
|
const { filters, sort } = body;
|
|
59
|
-
for (const filter of (filters || [])) {
|
|
60
|
-
if (!Object.values(AdminForthFilterOperators).includes(filter.operator)) {
|
|
61
|
-
throw new Error(`Operator '${filter.operator}' is not allowed`);
|
|
62
|
-
}
|
|
63
|
-
if (!this.resourceConfig.columns.some((col) => col.name === filter.field)) {
|
|
64
|
-
throw new Error(`Field '${filter.field}' is not in resource '${this.resourceConfig.resourceId}'. Available fields: ${this.resourceConfig.columns.map((col) => col.name).join(', ')}`);
|
|
65
|
-
}
|
|
66
|
-
if (filter.operator === AdminForthFilterOperators.IN || filter.operator === AdminForthFilterOperators.NIN) {
|
|
67
|
-
if (!Array.isArray(filter.value)) {
|
|
68
|
-
throw new Error(`Value for operator '${filter.operator}' should be an array`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
if (filter.operator === AdminForthFilterOperators.IN && filter.value.length === 0) {
|
|
72
|
-
// nonsense
|
|
73
|
-
return { data: [], total: 0 };
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
59
|
const data = yield this.adminforth.connectors[this.resourceConfig.dataSource].getData({
|
|
77
60
|
resource: this.resourceConfig,
|
|
78
61
|
limit: 1e6,
|
|
79
62
|
offset: 0,
|
|
80
|
-
filters,
|
|
63
|
+
filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(filters),
|
|
81
64
|
sort,
|
|
82
65
|
getTotals: true,
|
|
83
66
|
});
|
|
@@ -107,8 +90,6 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
107
90
|
noAuth: true,
|
|
108
91
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body }) {
|
|
109
92
|
const { data } = body;
|
|
110
|
-
// data is in format {[columnName]: [value1, value2, value3...], [columnName2]: [value1, value2, value3...]}
|
|
111
|
-
// we need to convert it to [{columnName: value1, columnName2: value1}, {columnName: value2, columnName2: value2}...]
|
|
112
93
|
const rows = [];
|
|
113
94
|
const columns = Object.keys(data);
|
|
114
95
|
// check column names are valid
|
|
@@ -122,6 +103,59 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
122
103
|
if (errors.length > 0) {
|
|
123
104
|
return { ok: false, errors };
|
|
124
105
|
}
|
|
106
|
+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
107
|
+
const columnValues = Object.values(data);
|
|
108
|
+
for (let i = 0; i < columnValues[0].length; i++) {
|
|
109
|
+
const row = {};
|
|
110
|
+
for (let j = 0; j < columns.length; j++) {
|
|
111
|
+
row[columns[j]] = columnValues[j][i];
|
|
112
|
+
}
|
|
113
|
+
rows.push(row);
|
|
114
|
+
}
|
|
115
|
+
let importedCount = 0;
|
|
116
|
+
let updatedCount = 0;
|
|
117
|
+
yield Promise.all(rows.map((row) => __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
try {
|
|
119
|
+
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
|
|
120
|
+
const existingRecord = yield this.adminforth.resource(this.resourceConfig.resourceId)
|
|
121
|
+
.list([Filters.EQ(primaryKeyColumn.name, row[primaryKeyColumn.name])]);
|
|
122
|
+
if (existingRecord.length > 0) {
|
|
123
|
+
yield this.adminforth.resource(this.resourceConfig.resourceId)
|
|
124
|
+
.update(row[primaryKeyColumn.name], row);
|
|
125
|
+
updatedCount++;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
yield this.adminforth.resource(this.resourceConfig.resourceId).create(row);
|
|
130
|
+
importedCount++;
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
errors.push(e.message);
|
|
134
|
+
}
|
|
135
|
+
})));
|
|
136
|
+
return { ok: true, importedCount, updatedCount, errors };
|
|
137
|
+
})
|
|
138
|
+
});
|
|
139
|
+
server.endpoint({
|
|
140
|
+
method: 'POST',
|
|
141
|
+
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
142
|
+
noAuth: true,
|
|
143
|
+
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body }) {
|
|
144
|
+
const { data } = body;
|
|
145
|
+
const rows = [];
|
|
146
|
+
const columns = Object.keys(data);
|
|
147
|
+
// check column names are valid
|
|
148
|
+
const errors = [];
|
|
149
|
+
columns.forEach((col) => {
|
|
150
|
+
if (!this.resourceConfig.columns.some((c) => c.name === col)) {
|
|
151
|
+
const similar = suggestIfTypo(this.resourceConfig.columns.map((c) => c.name), col);
|
|
152
|
+
errors.push(`Column '${col}' defined in CSV not found in resource '${this.resourceConfig.resourceId}'. ${similar ? `If you mean '${similar}', rename it in CSV` : 'If column is in database but not in resource configuration, add it with showIn:[]'}`);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
if (errors.length > 0) {
|
|
156
|
+
return { ok: false, errors };
|
|
157
|
+
}
|
|
158
|
+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
125
159
|
const columnValues = Object.values(data);
|
|
126
160
|
for (let i = 0; i < columnValues[0].length; i++) {
|
|
127
161
|
const row = {};
|
|
@@ -133,6 +167,13 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
133
167
|
let importedCount = 0;
|
|
134
168
|
yield Promise.all(rows.map((row) => __awaiter(this, void 0, void 0, function* () {
|
|
135
169
|
try {
|
|
170
|
+
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
|
|
171
|
+
const existingRecord = yield this.adminforth.resource(this.resourceConfig.resourceId)
|
|
172
|
+
.list([Filters.EQ(primaryKeyColumn.name, row[primaryKeyColumn.name])]);
|
|
173
|
+
if (existingRecord.length > 0) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
136
177
|
yield this.adminforth.resource(this.resourceConfig.resourceId).create(row);
|
|
137
178
|
importedCount++;
|
|
138
179
|
}
|
|
@@ -143,5 +184,35 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
143
184
|
return { ok: true, importedCount, errors };
|
|
144
185
|
})
|
|
145
186
|
});
|
|
187
|
+
server.endpoint({
|
|
188
|
+
method: 'POST',
|
|
189
|
+
path: `/plugin/${this.pluginInstanceId}/check-records`,
|
|
190
|
+
noAuth: true,
|
|
191
|
+
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body }) {
|
|
192
|
+
const { data } = body;
|
|
193
|
+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
194
|
+
const rows = [];
|
|
195
|
+
const columns = Object.keys(data);
|
|
196
|
+
const columnValues = Object.values(data);
|
|
197
|
+
for (let i = 0; i < columnValues[0].length; i++) {
|
|
198
|
+
const row = {};
|
|
199
|
+
for (let j = 0; j < columns.length; j++) {
|
|
200
|
+
row[columns[j]] = columnValues[j][i];
|
|
201
|
+
}
|
|
202
|
+
rows.push(row);
|
|
203
|
+
}
|
|
204
|
+
const primaryKeys = rows
|
|
205
|
+
.map(row => row[primaryKeyColumn.name])
|
|
206
|
+
.filter(key => key !== undefined && key !== null && key !== '');
|
|
207
|
+
const records = yield this.adminforth.resource(this.resourceConfig.resourceId).list([]);
|
|
208
|
+
const existingRecords = records.filter((record) => primaryKeys.includes(record[primaryKeyColumn.name]));
|
|
209
|
+
return {
|
|
210
|
+
ok: true,
|
|
211
|
+
total: rows.length,
|
|
212
|
+
existingCount: existingRecords.length,
|
|
213
|
+
newCount: rows.length - existingRecords.length
|
|
214
|
+
};
|
|
215
|
+
})
|
|
216
|
+
});
|
|
146
217
|
}
|
|
147
218
|
}
|
package/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators } from "adminforth";
|
|
1
|
+
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters } from "adminforth";
|
|
2
2
|
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource } from "adminforth";
|
|
3
3
|
import type { PluginOptions } from './types.js';
|
|
4
4
|
|
|
@@ -57,29 +57,11 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
57
57
|
handler: async ({ body }) => {
|
|
58
58
|
const { filters, sort } = body;
|
|
59
59
|
|
|
60
|
-
for (const filter of (filters || [])) {
|
|
61
|
-
if (!Object.values(AdminForthFilterOperators).includes(filter.operator)) {
|
|
62
|
-
throw new Error(`Operator '${filter.operator}' is not allowed`);
|
|
63
|
-
}
|
|
64
|
-
if (!this.resourceConfig.columns.some((col) => col.name === filter.field)) {
|
|
65
|
-
throw new Error(`Field '${filter.field}' is not in resource '${this.resourceConfig.resourceId}'. Available fields: ${this.resourceConfig.columns.map((col) => col.name).join(', ')}`);
|
|
66
|
-
}
|
|
67
|
-
if (filter.operator === AdminForthFilterOperators.IN || filter.operator === AdminForthFilterOperators.NIN) {
|
|
68
|
-
if (!Array.isArray(filter.value)) {
|
|
69
|
-
throw new Error(`Value for operator '${filter.operator}' should be an array`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (filter.operator === AdminForthFilterOperators.IN && filter.value.length === 0) {
|
|
73
|
-
// nonsense
|
|
74
|
-
return { data: [], total: 0 };
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
60
|
const data = await this.adminforth.connectors[this.resourceConfig.dataSource].getData({
|
|
79
61
|
resource: this.resourceConfig,
|
|
80
62
|
limit: 1e6,
|
|
81
63
|
offset: 0,
|
|
82
|
-
filters,
|
|
64
|
+
filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(filters),
|
|
83
65
|
sort,
|
|
84
66
|
getTotals: true,
|
|
85
67
|
});
|
|
@@ -114,8 +96,6 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
114
96
|
noAuth: true,
|
|
115
97
|
handler: async ({ body }) => {
|
|
116
98
|
const { data } = body;
|
|
117
|
-
// data is in format {[columnName]: [value1, value2, value3...], [columnName2]: [value1, value2, value3...]}
|
|
118
|
-
// we need to convert it to [{columnName: value1, columnName2: value1}, {columnName: value2, columnName2: value2}...]
|
|
119
99
|
const rows = [];
|
|
120
100
|
const columns = Object.keys(data);
|
|
121
101
|
|
|
@@ -133,6 +113,8 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
133
113
|
return { ok: false, errors };
|
|
134
114
|
}
|
|
135
115
|
|
|
116
|
+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
117
|
+
|
|
136
118
|
const columnValues: any[] = Object.values(data);
|
|
137
119
|
for (let i = 0; i < columnValues[0].length; i++) {
|
|
138
120
|
const row = {};
|
|
@@ -143,21 +125,124 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
143
125
|
}
|
|
144
126
|
|
|
145
127
|
let importedCount = 0;
|
|
128
|
+
let updatedCount = 0;
|
|
129
|
+
|
|
146
130
|
await Promise.all(rows.map(async (row) => {
|
|
147
131
|
try {
|
|
148
|
-
|
|
149
|
-
|
|
132
|
+
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
|
|
133
|
+
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
|
|
134
|
+
.list([Filters.EQ(primaryKeyColumn.name, row[primaryKeyColumn.name])]);
|
|
135
|
+
|
|
136
|
+
if (existingRecord.length > 0) {
|
|
137
|
+
await this.adminforth.resource(this.resourceConfig.resourceId)
|
|
138
|
+
.update(row[primaryKeyColumn.name], row);
|
|
139
|
+
updatedCount++;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
await this.adminforth.resource(this.resourceConfig.resourceId).create(row);
|
|
144
|
+
importedCount++;
|
|
150
145
|
} catch (e) {
|
|
151
|
-
|
|
146
|
+
errors.push(e.message);
|
|
152
147
|
}
|
|
153
148
|
}));
|
|
154
149
|
|
|
150
|
+
return { ok: true, importedCount, updatedCount, errors };
|
|
151
|
+
}
|
|
152
|
+
});
|
|
155
153
|
|
|
156
|
-
|
|
154
|
+
server.endpoint({
|
|
155
|
+
method: 'POST',
|
|
156
|
+
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
157
|
+
noAuth: true,
|
|
158
|
+
handler: async ({ body }) => {
|
|
159
|
+
const { data } = body;
|
|
160
|
+
const rows = [];
|
|
161
|
+
const columns = Object.keys(data);
|
|
157
162
|
|
|
163
|
+
// check column names are valid
|
|
164
|
+
const errors: string[] = [];
|
|
165
|
+
columns.forEach((col) => {
|
|
166
|
+
if (!this.resourceConfig.columns.some((c) => c.name === col)) {
|
|
167
|
+
const similar = suggestIfTypo(this.resourceConfig.columns.map((c) => c.name), col);
|
|
168
|
+
errors.push(`Column '${col}' defined in CSV not found in resource '${this.resourceConfig.resourceId}'. ${
|
|
169
|
+
similar ? `If you mean '${similar}', rename it in CSV` : 'If column is in database but not in resource configuration, add it with showIn:[]'}`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
if (errors.length > 0) {
|
|
174
|
+
return { ok: false, errors };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
178
|
+
const columnValues: any[] = Object.values(data);
|
|
179
|
+
for (let i = 0; i < columnValues[0].length; i++) {
|
|
180
|
+
const row = {};
|
|
181
|
+
for (let j = 0; j < columns.length; j++) {
|
|
182
|
+
row[columns[j]] = columnValues[j][i];
|
|
183
|
+
}
|
|
184
|
+
rows.push(row);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let importedCount = 0;
|
|
188
|
+
|
|
189
|
+
await Promise.all(rows.map(async (row) => {
|
|
190
|
+
try {
|
|
191
|
+
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
|
|
192
|
+
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
|
|
193
|
+
.list([Filters.EQ(primaryKeyColumn.name, row[primaryKeyColumn.name])]);
|
|
194
|
+
|
|
195
|
+
if (existingRecord.length > 0) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
await this.adminforth.resource(this.resourceConfig.resourceId).create(row);
|
|
200
|
+
importedCount++;
|
|
201
|
+
} catch (e) {
|
|
202
|
+
errors.push(e.message);
|
|
203
|
+
}
|
|
204
|
+
}));
|
|
205
|
+
|
|
206
|
+
return { ok: true, importedCount, errors };
|
|
158
207
|
}
|
|
159
208
|
});
|
|
160
209
|
|
|
210
|
+
server.endpoint({
|
|
211
|
+
method: 'POST',
|
|
212
|
+
path: `/plugin/${this.pluginInstanceId}/check-records`,
|
|
213
|
+
noAuth: true,
|
|
214
|
+
handler: async ({ body }) => {
|
|
215
|
+
const { data } = body;
|
|
216
|
+
|
|
217
|
+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
218
|
+
|
|
219
|
+
const rows = [];
|
|
220
|
+
const columns = Object.keys(data);
|
|
221
|
+
const columnValues = Object.values(data);
|
|
222
|
+
for (let i = 0; i < (columnValues[0] as any[]).length; i++) {
|
|
223
|
+
const row = {};
|
|
224
|
+
for (let j = 0; j < columns.length; j++) {
|
|
225
|
+
row[columns[j]] = columnValues[j][i];
|
|
226
|
+
}
|
|
227
|
+
rows.push(row);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const primaryKeys = rows
|
|
231
|
+
.map(row => row[primaryKeyColumn.name])
|
|
232
|
+
.filter(key => key !== undefined && key !== null && key !== '');
|
|
233
|
+
|
|
234
|
+
const records = await this.adminforth.resource(this.resourceConfig.resourceId).list([])
|
|
235
|
+
|
|
236
|
+
const existingRecords = records.filter((record) => primaryKeys.includes(record[primaryKeyColumn.name]));
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
ok: true,
|
|
240
|
+
total: rows.length,
|
|
241
|
+
existingCount: existingRecords.length,
|
|
242
|
+
newCount: rows.length - existingRecords.length
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
});
|
|
161
246
|
}
|
|
162
247
|
|
|
163
248
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adminforth/import-export",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"homepage": "https://adminforth.dev/docs/tutorial/Plugins/import-export/",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"build": "tsc && rsync -av --exclude 'node_modules' custom dist/",
|
|
9
10
|
"prepare": "npm link adminforth"
|