@nocobase/plugin-action-import 1.0.0-alpha.9 → 1.0.1-alpha.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.
Files changed (45) hide show
  1. package/dist/client/ImportActionInitializer.d.ts +1 -6
  2. package/dist/client/index.js +6 -6
  3. package/dist/client/useImportAction.d.ts +1 -0
  4. package/dist/externalVersion.js +9 -6
  5. package/dist/locale/en-US.json +3 -3
  6. package/dist/locale/es-ES.json +2 -2
  7. package/dist/locale/ko_KR.json +3 -3
  8. package/dist/locale/pt-BR.json +2 -2
  9. package/dist/locale/zh-CN.json +3 -3
  10. package/dist/node_modules/xlsx/bin/xlsx.njs +8 -0
  11. package/dist/node_modules/xlsx/bower.json +1 -1
  12. package/dist/node_modules/xlsx/dist/cpexcel.d.ts +39 -0
  13. package/dist/node_modules/xlsx/dist/xlsx.core.min.js +16 -17
  14. package/dist/node_modules/xlsx/dist/xlsx.extendscript.js +11207 -15096
  15. package/dist/node_modules/xlsx/dist/xlsx.full.min.js +23 -23
  16. package/dist/node_modules/xlsx/dist/xlsx.mini.min.js +9 -8
  17. package/dist/node_modules/xlsx/dist/xlsx.zahl.js +4 -0
  18. package/dist/node_modules/xlsx/dist/zahl.d.ts +4 -0
  19. package/dist/node_modules/xlsx/package.json +1 -1
  20. package/dist/node_modules/xlsx/types/index.d.ts +220 -40
  21. package/dist/node_modules/xlsx/xlsx.js +5 -4
  22. package/dist/node_modules/xlsx/xlsxworker.js +1 -2
  23. package/dist/server/actions/{downloadXlsxTemplate.js → download-xlsx-template.js} +13 -17
  24. package/dist/server/actions/import-xlsx.js +89 -0
  25. package/dist/server/actions/index.d.ts +2 -2
  26. package/dist/server/actions/index.js +4 -4
  27. package/dist/server/index.d.ts +1 -2
  28. package/dist/server/index.js +0 -6
  29. package/dist/server/services/template-creator.d.ts +24 -0
  30. package/dist/server/services/template-creator.js +70 -0
  31. package/dist/server/services/xlsx-importer.d.ts +37 -0
  32. package/dist/server/services/xlsx-importer.js +185 -0
  33. package/package.json +3 -3
  34. package/dist/node_modules/node-xlsx/lib/helpers.js +0 -142
  35. package/dist/node_modules/node-xlsx/lib/index.js +0 -6
  36. package/dist/node_modules/node-xlsx/lib/workbook.js +0 -16
  37. package/dist/node_modules/node-xlsx/package.json +0 -1
  38. package/dist/node_modules/xlsx/dist/jszip.js +0 -9000
  39. package/dist/node_modules/xlsx/dist/xlsx.js +0 -22693
  40. package/dist/node_modules/xlsx/dist/xlsx.min.js +0 -15
  41. package/dist/node_modules/xlsx/jszip.js +0 -9000
  42. package/dist/node_modules/xlsx/xlsx.mini.js +0 -10709
  43. package/dist/server/actions/importXlsx.js +0 -194
  44. /package/dist/server/actions/{downloadXlsxTemplate.d.ts → download-xlsx-template.d.ts} +0 -0
  45. /package/dist/server/actions/{importXlsx.d.ts → import-xlsx.d.ts} +0 -0
@@ -1,194 +0,0 @@
1
- /**
2
- * This file is part of the NocoBase (R) project.
3
- * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
- * Authors: NocoBase Team.
5
- *
6
- * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
- * For more information, please refer to: https://www.nocobase.com/agreement.
8
- */
9
-
10
- var __create = Object.create;
11
- var __defProp = Object.defineProperty;
12
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
- var __getOwnPropNames = Object.getOwnPropertyNames;
14
- var __getProtoOf = Object.getPrototypeOf;
15
- var __hasOwnProp = Object.prototype.hasOwnProperty;
16
- var __export = (target, all) => {
17
- for (var name in all)
18
- __defProp(target, name, { get: all[name], enumerable: true });
19
- };
20
- var __copyProps = (to, from, except, desc) => {
21
- if (from && typeof from === "object" || typeof from === "function") {
22
- for (let key of __getOwnPropNames(from))
23
- if (!__hasOwnProp.call(to, key) && key !== except)
24
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
- }
26
- return to;
27
- };
28
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
- // If the importer is in node compatibility mode or this is not an ESM
30
- // file that has been converted to a CommonJS file using a Babel-
31
- // compatible transform (i.e. "__esModule" has not been set), then set
32
- // "default" to the CommonJS "module.exports" for node compatibility.
33
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
- mod
35
- ));
36
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
- var importXlsx_exports = {};
38
- __export(importXlsx_exports, {
39
- importXlsx: () => importXlsx
40
- });
41
- module.exports = __toCommonJS(importXlsx_exports);
42
- var import_utils = require("@nocobase/utils");
43
- var import_node_xlsx = __toESM(require("node-xlsx"));
44
- var import_xlsx = __toESM(require("xlsx"));
45
- var import__ = require("../../");
46
- const IMPORT_LIMIT_COUNT = 200;
47
- class Importer {
48
- repository;
49
- collection;
50
- columns;
51
- items = [];
52
- headerRow;
53
- context;
54
- constructor(ctx) {
55
- const { resourceName, resourceOf } = ctx.action;
56
- this.context = ctx;
57
- this.repository = ctx.db.getRepository(resourceName, resourceOf);
58
- this.collection = this.repository.collection;
59
- this.parseXlsx();
60
- }
61
- getRows() {
62
- const workbook = import_xlsx.default.read(this.context.file.buffer, {
63
- type: "buffer",
64
- sheetRows: IMPORT_LIMIT_COUNT
65
- // cellDates: true,
66
- // raw: false,
67
- });
68
- const r = workbook.Sheets[workbook.SheetNames[0]];
69
- const rows = import_xlsx.default.utils.sheet_to_json(r, { header: 1, defval: null, raw: false });
70
- return rows;
71
- }
72
- parseXlsx() {
73
- const rows = this.getRows();
74
- let columns = this.context.request.body.columns;
75
- if (typeof columns === "string") {
76
- columns = JSON.parse(columns);
77
- }
78
- this.columns = columns.map((column) => {
79
- return {
80
- ...column,
81
- field: this.collection.fields.get(column.dataIndex[0])
82
- };
83
- }).filter((col) => col.field);
84
- const str = this.columns.map((column) => column.defaultTitle).join("||");
85
- for (const row of rows) {
86
- if (this.hasHeaderRow()) {
87
- if (row && row.join("").trim()) {
88
- this.items.push(row);
89
- }
90
- }
91
- if (str === row.filter((r) => r).join("||")) {
92
- this.headerRow = row;
93
- }
94
- }
95
- }
96
- getFieldByIndex(index) {
97
- return this.columns[index].field;
98
- }
99
- async getItems() {
100
- const items = [];
101
- for (const row of this.items) {
102
- const values = {};
103
- const errors = [];
104
- for (let index = 0; index < row.length; index++) {
105
- if (!this.columns[index]) {
106
- continue;
107
- }
108
- const column = this.columns[index];
109
- const { field, defaultTitle } = column;
110
- let value = row[index];
111
- if (value === void 0 || value === null) {
112
- continue;
113
- }
114
- const parser = this.context.db.buildFieldValueParser(field, { ...this.context, column });
115
- await parser.setValue(typeof value === "string" ? value.trim() : value);
116
- value = parser.getValue();
117
- if (parser.errors.length > 0) {
118
- errors.push(`${defaultTitle}: ${parser.errors.join(";")}`);
119
- }
120
- if (value === void 0) {
121
- continue;
122
- }
123
- values[field.name] = value;
124
- }
125
- items.push({
126
- row,
127
- values,
128
- errors
129
- });
130
- }
131
- return items;
132
- }
133
- hasSortField() {
134
- return !!this.collection.options.sortable;
135
- }
136
- async run() {
137
- return await this.context.db.sequelize.transaction(async (transaction) => {
138
- let sort = 0;
139
- if (this.hasSortField()) {
140
- sort = await this.repository.model.max("sort", { transaction });
141
- }
142
- const result = [[], []];
143
- for (const { row, values, errors } of await this.getItems()) {
144
- if (errors.length > 0) {
145
- row.push(errors.join(";"));
146
- result[1].push(row);
147
- continue;
148
- }
149
- if (this.hasSortField()) {
150
- values["sort"] = ++sort;
151
- }
152
- try {
153
- const instance = await this.repository.create({
154
- values,
155
- transaction,
156
- logging: false,
157
- context: this.context
158
- });
159
- result[0].push(instance);
160
- } catch (error) {
161
- this.context.log.error(error, row);
162
- row.push(error.message);
163
- result[1].push(row);
164
- }
165
- }
166
- return result;
167
- });
168
- }
169
- hasHeaderRow() {
170
- return !!this.headerRow;
171
- }
172
- }
173
- async function importXlsx(ctx, next) {
174
- const importer = new Importer(ctx);
175
- if (!importer.hasHeaderRow()) {
176
- ctx.throw(400, ctx.t("Imported template does not match, please download again.", { ns: import__.namespace }));
177
- }
178
- const [success, failure] = await importer.run();
179
- ctx.body = {
180
- rows: import_node_xlsx.default.build([
181
- {
182
- name: `${(0, import_utils.uid)()}.xlsx`,
183
- data: [importer.headerRow].concat(failure)
184
- }
185
- ]),
186
- successCount: success.length,
187
- failureCount: failure.length
188
- };
189
- await next();
190
- }
191
- // Annotate the CommonJS export names for ESM import in node:
192
- 0 && (module.exports = {
193
- importXlsx
194
- });