@webexatech/plugin-collection-transfer 0.0.0-stage → 0.1.3-nocobase.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.
Files changed (212) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/LICENSE +201 -0
  3. package/README.md +158 -3
  4. package/client-v2.d.ts +2 -0
  5. package/client-v2.js +1 -0
  6. package/client.d.ts +2 -0
  7. package/client.js +1 -0
  8. package/dist/client/949.72b7b97eaf5473f5.js +10 -0
  9. package/dist/client/SettingsPage.d.ts +3 -0
  10. package/dist/client/index.d.ts +1 -0
  11. package/dist/client/index.js +10 -0
  12. package/dist/client/plugin.d.ts +5 -0
  13. package/dist/client-v2/907.b7c707602a2b4e6c.js +10 -0
  14. package/dist/client-v2/CollectionTransferSettings.d.ts +22 -0
  15. package/dist/client-v2/SettingsPage.d.ts +4 -0
  16. package/dist/client-v2/index.d.ts +1 -0
  17. package/dist/client-v2/index.js +10 -0
  18. package/dist/client-v2/locale.d.ts +7 -0
  19. package/dist/client-v2/plugin.d.ts +7 -0
  20. package/dist/externalVersion.js +24 -0
  21. package/dist/index.d.ts +2 -0
  22. package/dist/index.js +48 -0
  23. package/dist/locale/en-US.json +70 -0
  24. package/dist/locale/zh-CN.json +70 -0
  25. package/dist/node_modules/archiver/LICENSE +22 -0
  26. package/dist/node_modules/archiver/index.js +68 -0
  27. package/dist/node_modules/archiver/lib/core.js +974 -0
  28. package/dist/node_modules/archiver/lib/error.js +40 -0
  29. package/dist/node_modules/archiver/lib/plugins/json.js +110 -0
  30. package/dist/node_modules/archiver/lib/plugins/tar.js +167 -0
  31. package/dist/node_modules/archiver/lib/plugins/zip.js +120 -0
  32. package/dist/node_modules/archiver/node_modules/async/all.js +119 -0
  33. package/dist/node_modules/archiver/node_modules/async/allLimit.js +46 -0
  34. package/dist/node_modules/archiver/node_modules/async/allSeries.js +45 -0
  35. package/dist/node_modules/archiver/node_modules/async/any.js +122 -0
  36. package/dist/node_modules/archiver/node_modules/async/anyLimit.js +47 -0
  37. package/dist/node_modules/archiver/node_modules/async/anySeries.js +46 -0
  38. package/dist/node_modules/archiver/node_modules/async/apply.js +11 -0
  39. package/dist/node_modules/archiver/node_modules/async/applyEach.js +57 -0
  40. package/dist/node_modules/archiver/node_modules/async/applyEachSeries.js +37 -0
  41. package/dist/node_modules/archiver/node_modules/async/asyncify.js +118 -0
  42. package/dist/node_modules/archiver/node_modules/async/auto.js +333 -0
  43. package/dist/node_modules/archiver/node_modules/async/autoInject.js +182 -0
  44. package/dist/node_modules/archiver/node_modules/async/bower.json +17 -0
  45. package/dist/node_modules/archiver/node_modules/async/cargo.js +63 -0
  46. package/dist/node_modules/archiver/node_modules/async/cargoQueue.js +71 -0
  47. package/dist/node_modules/archiver/node_modules/async/compose.js +55 -0
  48. package/dist/node_modules/archiver/node_modules/async/concat.js +115 -0
  49. package/dist/node_modules/archiver/node_modules/async/concatLimit.js +60 -0
  50. package/dist/node_modules/archiver/node_modules/async/concatSeries.js +41 -0
  51. package/dist/node_modules/archiver/node_modules/async/constant.js +14 -0
  52. package/dist/node_modules/archiver/node_modules/async/detect.js +96 -0
  53. package/dist/node_modules/archiver/node_modules/async/detectLimit.js +48 -0
  54. package/dist/node_modules/archiver/node_modules/async/detectSeries.js +47 -0
  55. package/dist/node_modules/archiver/node_modules/async/dir.js +43 -0
  56. package/dist/node_modules/archiver/node_modules/async/dist/async.js +6062 -0
  57. package/dist/node_modules/archiver/node_modules/async/dist/async.min.js +1 -0
  58. package/dist/node_modules/archiver/node_modules/async/doDuring.js +68 -0
  59. package/dist/node_modules/archiver/node_modules/async/doUntil.js +46 -0
  60. package/dist/node_modules/archiver/node_modules/async/doWhilst.js +68 -0
  61. package/dist/node_modules/archiver/node_modules/async/during.js +78 -0
  62. package/dist/node_modules/archiver/node_modules/async/each.js +129 -0
  63. package/dist/node_modules/archiver/node_modules/async/eachLimit.js +50 -0
  64. package/dist/node_modules/archiver/node_modules/async/eachOf.js +185 -0
  65. package/dist/node_modules/archiver/node_modules/async/eachOfLimit.js +47 -0
  66. package/dist/node_modules/archiver/node_modules/async/eachOfSeries.js +39 -0
  67. package/dist/node_modules/archiver/node_modules/async/eachSeries.js +44 -0
  68. package/dist/node_modules/archiver/node_modules/async/ensureAsync.js +67 -0
  69. package/dist/node_modules/archiver/node_modules/async/every.js +119 -0
  70. package/dist/node_modules/archiver/node_modules/async/everyLimit.js +46 -0
  71. package/dist/node_modules/archiver/node_modules/async/everySeries.js +45 -0
  72. package/dist/node_modules/archiver/node_modules/async/filter.js +93 -0
  73. package/dist/node_modules/archiver/node_modules/async/filterLimit.js +45 -0
  74. package/dist/node_modules/archiver/node_modules/async/filterSeries.js +43 -0
  75. package/dist/node_modules/archiver/node_modules/async/find.js +96 -0
  76. package/dist/node_modules/archiver/node_modules/async/findLimit.js +48 -0
  77. package/dist/node_modules/archiver/node_modules/async/findSeries.js +47 -0
  78. package/dist/node_modules/archiver/node_modules/async/flatMap.js +115 -0
  79. package/dist/node_modules/archiver/node_modules/async/flatMapLimit.js +60 -0
  80. package/dist/node_modules/archiver/node_modules/async/flatMapSeries.js +41 -0
  81. package/dist/node_modules/archiver/node_modules/async/foldl.js +153 -0
  82. package/dist/node_modules/archiver/node_modules/async/foldr.js +41 -0
  83. package/dist/node_modules/archiver/node_modules/async/forEach.js +129 -0
  84. package/dist/node_modules/archiver/node_modules/async/forEachLimit.js +50 -0
  85. package/dist/node_modules/archiver/node_modules/async/forEachOf.js +185 -0
  86. package/dist/node_modules/archiver/node_modules/async/forEachOfLimit.js +47 -0
  87. package/dist/node_modules/archiver/node_modules/async/forEachOfSeries.js +39 -0
  88. package/dist/node_modules/archiver/node_modules/async/forEachSeries.js +44 -0
  89. package/dist/node_modules/archiver/node_modules/async/forever.js +68 -0
  90. package/dist/node_modules/archiver/node_modules/async/groupBy.js +108 -0
  91. package/dist/node_modules/archiver/node_modules/async/groupByLimit.js +71 -0
  92. package/dist/node_modules/archiver/node_modules/async/groupBySeries.js +36 -0
  93. package/dist/node_modules/archiver/node_modules/async/index.js +588 -0
  94. package/dist/node_modules/archiver/node_modules/async/inject.js +153 -0
  95. package/dist/node_modules/archiver/node_modules/async/internal/DoublyLinkedList.js +92 -0
  96. package/dist/node_modules/archiver/node_modules/async/internal/Heap.js +120 -0
  97. package/dist/node_modules/archiver/node_modules/async/internal/applyEach.js +29 -0
  98. package/dist/node_modules/archiver/node_modules/async/internal/asyncEachOfLimit.js +75 -0
  99. package/dist/node_modules/archiver/node_modules/async/internal/awaitify.js +28 -0
  100. package/dist/node_modules/archiver/node_modules/async/internal/breakLoop.js +10 -0
  101. package/dist/node_modules/archiver/node_modules/async/internal/consoleFunc.js +31 -0
  102. package/dist/node_modules/archiver/node_modules/async/internal/createTester.js +40 -0
  103. package/dist/node_modules/archiver/node_modules/async/internal/eachOfLimit.js +90 -0
  104. package/dist/node_modules/archiver/node_modules/async/internal/filter.js +55 -0
  105. package/dist/node_modules/archiver/node_modules/async/internal/getIterator.js +11 -0
  106. package/dist/node_modules/archiver/node_modules/async/internal/initialParams.js +14 -0
  107. package/dist/node_modules/archiver/node_modules/async/internal/isArrayLike.js +10 -0
  108. package/dist/node_modules/archiver/node_modules/async/internal/iterator.js +57 -0
  109. package/dist/node_modules/archiver/node_modules/async/internal/map.js +30 -0
  110. package/dist/node_modules/archiver/node_modules/async/internal/once.js +17 -0
  111. package/dist/node_modules/archiver/node_modules/async/internal/onlyOnce.js +15 -0
  112. package/dist/node_modules/archiver/node_modules/async/internal/parallel.js +34 -0
  113. package/dist/node_modules/archiver/node_modules/async/internal/promiseCallback.js +23 -0
  114. package/dist/node_modules/archiver/node_modules/async/internal/queue.js +294 -0
  115. package/dist/node_modules/archiver/node_modules/async/internal/range.js +14 -0
  116. package/dist/node_modules/archiver/node_modules/async/internal/reject.js +26 -0
  117. package/dist/node_modules/archiver/node_modules/async/internal/setImmediate.js +34 -0
  118. package/dist/node_modules/archiver/node_modules/async/internal/withoutIndex.js +10 -0
  119. package/dist/node_modules/archiver/node_modules/async/internal/wrapAsync.js +34 -0
  120. package/dist/node_modules/archiver/node_modules/async/log.js +41 -0
  121. package/dist/node_modules/archiver/node_modules/async/map.js +142 -0
  122. package/dist/node_modules/archiver/node_modules/async/mapLimit.js +45 -0
  123. package/dist/node_modules/archiver/node_modules/async/mapSeries.js +44 -0
  124. package/dist/node_modules/archiver/node_modules/async/mapValues.js +152 -0
  125. package/dist/node_modules/archiver/node_modules/async/mapValuesLimit.js +61 -0
  126. package/dist/node_modules/archiver/node_modules/async/mapValuesSeries.js +37 -0
  127. package/dist/node_modules/archiver/node_modules/async/memoize.js +91 -0
  128. package/dist/node_modules/archiver/node_modules/async/nextTick.js +52 -0
  129. package/dist/node_modules/archiver/node_modules/async/package.json +75 -0
  130. package/dist/node_modules/archiver/node_modules/async/parallel.js +180 -0
  131. package/dist/node_modules/archiver/node_modules/async/parallelLimit.js +41 -0
  132. package/dist/node_modules/archiver/node_modules/async/priorityQueue.js +60 -0
  133. package/dist/node_modules/archiver/node_modules/async/queue.js +24 -0
  134. package/dist/node_modules/archiver/node_modules/async/race.js +67 -0
  135. package/dist/node_modules/archiver/node_modules/async/reduce.js +153 -0
  136. package/dist/node_modules/archiver/node_modules/async/reduceRight.js +41 -0
  137. package/dist/node_modules/archiver/node_modules/async/reflect.js +78 -0
  138. package/dist/node_modules/archiver/node_modules/async/reflectAll.js +93 -0
  139. package/dist/node_modules/archiver/node_modules/async/reject.js +87 -0
  140. package/dist/node_modules/archiver/node_modules/async/rejectLimit.js +45 -0
  141. package/dist/node_modules/archiver/node_modules/async/rejectSeries.js +43 -0
  142. package/dist/node_modules/archiver/node_modules/async/retry.js +159 -0
  143. package/dist/node_modules/archiver/node_modules/async/retryable.js +77 -0
  144. package/dist/node_modules/archiver/node_modules/async/select.js +93 -0
  145. package/dist/node_modules/archiver/node_modules/async/selectLimit.js +45 -0
  146. package/dist/node_modules/archiver/node_modules/async/selectSeries.js +43 -0
  147. package/dist/node_modules/archiver/node_modules/async/seq.js +79 -0
  148. package/dist/node_modules/archiver/node_modules/async/series.js +186 -0
  149. package/dist/node_modules/archiver/node_modules/async/setImmediate.js +45 -0
  150. package/dist/node_modules/archiver/node_modules/async/some.js +122 -0
  151. package/dist/node_modules/archiver/node_modules/async/someLimit.js +47 -0
  152. package/dist/node_modules/archiver/node_modules/async/someSeries.js +46 -0
  153. package/dist/node_modules/archiver/node_modules/async/sortBy.js +190 -0
  154. package/dist/node_modules/archiver/node_modules/async/timeout.js +89 -0
  155. package/dist/node_modules/archiver/node_modules/async/times.js +50 -0
  156. package/dist/node_modules/archiver/node_modules/async/timesLimit.js +43 -0
  157. package/dist/node_modules/archiver/node_modules/async/timesSeries.js +32 -0
  158. package/dist/node_modules/archiver/node_modules/async/transform.js +173 -0
  159. package/dist/node_modules/archiver/node_modules/async/tryEach.js +78 -0
  160. package/dist/node_modules/archiver/node_modules/async/unmemoize.js +25 -0
  161. package/dist/node_modules/archiver/node_modules/async/until.js +61 -0
  162. package/dist/node_modules/archiver/node_modules/async/waterfall.js +105 -0
  163. package/dist/node_modules/archiver/node_modules/async/whilst.js +78 -0
  164. package/dist/node_modules/archiver/node_modules/async/wrapSync.js +118 -0
  165. package/dist/node_modules/archiver/node_modules/readable-stream/errors-browser.js +127 -0
  166. package/dist/node_modules/archiver/node_modules/readable-stream/errors.js +116 -0
  167. package/dist/node_modules/archiver/node_modules/readable-stream/experimentalWarning.js +17 -0
  168. package/dist/node_modules/archiver/node_modules/readable-stream/lib/_stream_duplex.js +126 -0
  169. package/dist/node_modules/archiver/node_modules/readable-stream/lib/_stream_passthrough.js +37 -0
  170. package/dist/node_modules/archiver/node_modules/readable-stream/lib/_stream_readable.js +1027 -0
  171. package/dist/node_modules/archiver/node_modules/readable-stream/lib/_stream_transform.js +190 -0
  172. package/dist/node_modules/archiver/node_modules/readable-stream/lib/_stream_writable.js +641 -0
  173. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/async_iterator.js +180 -0
  174. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/buffer_list.js +183 -0
  175. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/destroy.js +96 -0
  176. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/end-of-stream.js +86 -0
  177. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/from-browser.js +3 -0
  178. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/from.js +52 -0
  179. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/pipeline.js +86 -0
  180. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/state.js +22 -0
  181. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/stream-browser.js +1 -0
  182. package/dist/node_modules/archiver/node_modules/readable-stream/lib/internal/streams/stream.js +1 -0
  183. package/dist/node_modules/archiver/node_modules/readable-stream/package.json +68 -0
  184. package/dist/node_modules/archiver/node_modules/readable-stream/readable-browser.js +9 -0
  185. package/dist/node_modules/archiver/node_modules/readable-stream/readable.js +16 -0
  186. package/dist/node_modules/archiver/node_modules/tar-stream/extract.js +257 -0
  187. package/dist/node_modules/archiver/node_modules/tar-stream/headers.js +295 -0
  188. package/dist/node_modules/archiver/node_modules/tar-stream/index.js +2 -0
  189. package/dist/node_modules/archiver/node_modules/tar-stream/pack.js +255 -0
  190. package/dist/node_modules/archiver/node_modules/tar-stream/package.json +58 -0
  191. package/dist/node_modules/archiver/node_modules/tar-stream/sandbox.js +11 -0
  192. package/dist/node_modules/archiver/package.json +1 -0
  193. package/dist/node_modules/yauzl/LICENSE +21 -0
  194. package/dist/node_modules/yauzl/index.js +1 -0
  195. package/dist/node_modules/yauzl/package.json +1 -0
  196. package/dist/server/exporter.d.ts +42 -0
  197. package/dist/server/exporter.js +311 -0
  198. package/dist/server/field-value-writer.d.ts +9 -0
  199. package/dist/server/field-value-writer.js +107 -0
  200. package/dist/server/importer.d.ts +138 -0
  201. package/dist/server/importer.js +842 -0
  202. package/dist/server/index.d.ts +1 -0
  203. package/dist/server/index.js +42 -0
  204. package/dist/server/package-format.d.ts +50 -0
  205. package/dist/server/package-format.js +115 -0
  206. package/dist/server/plugin.d.ts +6 -0
  207. package/dist/server/plugin.js +61 -0
  208. package/dist/server/resourcer.d.ts +16 -0
  209. package/dist/server/resourcer.js +187 -0
  210. package/package.json +54 -4
  211. package/server.d.ts +2 -0
  212. package/server.js +1 -0
@@ -0,0 +1,311 @@
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 exporter_exports = {};
38
+ __export(exporter_exports, {
39
+ CollectionExporter: () => CollectionExporter,
40
+ enabledPluginNames: () => enabledPluginNames
41
+ });
42
+ module.exports = __toCommonJS(exporter_exports);
43
+ var import_archiver = __toESM(require("archiver"));
44
+ var import_crypto = __toESM(require("crypto"));
45
+ var import_fs = __toESM(require("fs"));
46
+ var import_promises = __toESM(require("fs/promises"));
47
+ var import_lodash = __toESM(require("lodash"));
48
+ var import_os = __toESM(require("os"));
49
+ var import_path = __toESM(require("path"));
50
+ var import_sequelize = require("sequelize");
51
+ var import_promises2 = require("stream/promises");
52
+ var import_field_value_writer = require("./field-value-writer");
53
+ var import_package_format = require("./package-format");
54
+ var import_package = __toESM(require("../../package.json"));
55
+ function unsupportedReason(row) {
56
+ const template = row.template || "general";
57
+ if (!import_package_format.SUPPORTED_TEMPLATES.includes(template)) {
58
+ return `template:${template}`;
59
+ }
60
+ if (!import_lodash.default.isEmpty(row.inherits)) {
61
+ return "inherits";
62
+ }
63
+ if (!(0, import_package_format.isValidName)(row.name)) {
64
+ return "name";
65
+ }
66
+ return void 0;
67
+ }
68
+ function isUserCollection(row) {
69
+ return !row.hidden && !row.isThrough && !import_package_format.MIRRORED_ORIGINS.includes(row.from);
70
+ }
71
+ function enabledPluginNames(app) {
72
+ const names = /* @__PURE__ */ new Set();
73
+ for (const plugin of app.pm.getPlugins().values()) {
74
+ if (!plugin.enabled) continue;
75
+ const options = plugin.options;
76
+ const name = options.packageName || options.name;
77
+ if (name) names.add(name);
78
+ }
79
+ return [...names].sort();
80
+ }
81
+ class CollectionExporter {
82
+ constructor(app) {
83
+ this.app = app;
84
+ }
85
+ get db() {
86
+ return this.app.db;
87
+ }
88
+ /** Collections a user may pick for export, with a flag for the ones the PoC cannot transfer. */
89
+ async listCollections() {
90
+ const rows = await this.collectionRows();
91
+ return rows.filter(isUserCollection).map((row) => {
92
+ const reason = unsupportedReason(row);
93
+ return {
94
+ name: row.name,
95
+ title: row.title || row.name,
96
+ template: row.template || "general",
97
+ supported: !reason,
98
+ unsupportedReason: reason
99
+ };
100
+ });
101
+ }
102
+ async export(options) {
103
+ const selected = import_lodash.default.uniq(options.collections || []);
104
+ if (!selected.length) {
105
+ throw new Error("No collections selected");
106
+ }
107
+ const rowsByName = import_lodash.default.keyBy(await this.collectionRows(), (row) => row.name);
108
+ for (const name of selected) {
109
+ const row = rowsByName[name];
110
+ if (!row || !isUserCollection(row)) {
111
+ throw new Error(`Collection "${name}" is not a user collection`);
112
+ }
113
+ const reason = unsupportedReason(row);
114
+ if (reason) {
115
+ throw new Error(`Collection "${name}" cannot be transferred (${reason})`);
116
+ }
117
+ }
118
+ const { collections, through, fieldRows } = await this.resolveSelection(selected, rowsByName);
119
+ const workDir = import_path.default.resolve(import_os.default.tmpdir(), `collection-transfer-${import_crypto.default.randomUUID()}`);
120
+ await import_promises.default.mkdir(import_path.default.join(workDir, "schema"), { recursive: true });
121
+ try {
122
+ const { collectionPayloads, associationPayloads, dependencies } = this.buildPayloads(
123
+ collections,
124
+ through,
125
+ rowsByName,
126
+ fieldRows
127
+ );
128
+ const manifest = {
129
+ formatVersion: import_package_format.FORMAT_VERSION,
130
+ pluginVersion: import_package.default.version,
131
+ sourceAppVersion: this.app.getPackageVersion(),
132
+ dialect: this.db.sequelize.getDialect(),
133
+ exportedAt: (/* @__PURE__ */ new Date()).toISOString(),
134
+ includeData: !!options.includeData,
135
+ selected,
136
+ collections,
137
+ through,
138
+ dependencies,
139
+ plugins: enabledPluginNames(this.app)
140
+ };
141
+ await this.writeJson(workDir, import_package_format.COLLECTIONS_FILE, collectionPayloads);
142
+ await this.writeJson(workDir, import_package_format.ASSOCIATIONS_FILE, associationPayloads);
143
+ await this.writeJson(workDir, import_package_format.THROUGH_FILE, through);
144
+ for (const name of [...collections, ...through]) {
145
+ await this.dumpCollection(workDir, name, manifest.includeData);
146
+ }
147
+ await this.writeJson(workDir, import_package_format.MANIFEST_FILE, manifest);
148
+ const fileName = `collections-${manifest.exportedAt.replace(/[:.]/g, "-")}.zip`;
149
+ const filePath = import_path.default.resolve(import_os.default.tmpdir(), `${import_crypto.default.randomUUID()}-${fileName}`);
150
+ await this.zipDir(workDir, filePath);
151
+ return { filePath, fileName, manifest };
152
+ } finally {
153
+ await import_promises.default.rm(workDir, { recursive: true, force: true });
154
+ }
155
+ }
156
+ async collectionRows() {
157
+ const models = await this.db.getRepository("collections").find({ sort: ["sort"] });
158
+ return models.map((model) => model.get());
159
+ }
160
+ async fieldRowsOf(collectionNames) {
161
+ if (!collectionNames.length) return [];
162
+ const models = await this.db.getRepository("fields").find({
163
+ filter: { collectionName: { $in: collectionNames } },
164
+ sort: ["sort"]
165
+ });
166
+ return models.map((model) => model.get());
167
+ }
168
+ /**
169
+ * Expands the selection with the collections it cannot work without: user-defined through collections of
170
+ * belongsToMany fields become companions; auto-created through collections are listed separately because the
171
+ * belongsToMany hook recreates them on import.
172
+ */
173
+ async resolveSelection(selected, rowsByName) {
174
+ const collections = [...selected];
175
+ const through = [];
176
+ let pending = [...selected];
177
+ const fieldRows = [];
178
+ while (pending.length) {
179
+ const rows = await this.fieldRowsOf(pending);
180
+ fieldRows.push(...rows);
181
+ pending = [];
182
+ for (const field of rows) {
183
+ if (field.type !== "belongsToMany" || !field.through) continue;
184
+ const throughName = field.through;
185
+ if (collections.includes(throughName) || through.includes(throughName)) continue;
186
+ const throughRow = rowsByName[throughName];
187
+ if (!throughRow) {
188
+ throw new Error(
189
+ `Through collection "${throughName}" of field "${field.collectionName}.${field.name}" not found`
190
+ );
191
+ }
192
+ if (throughRow.autoCreate) {
193
+ through.push(throughName);
194
+ } else {
195
+ const reason = unsupportedReason(throughRow);
196
+ if (reason) {
197
+ throw new Error(`Through collection "${throughName}" cannot be transferred (${reason})`);
198
+ }
199
+ collections.push(throughName);
200
+ pending.push(throughName);
201
+ }
202
+ }
203
+ }
204
+ return { collections, through, fieldRows };
205
+ }
206
+ buildPayloads(collections, through, rowsByName, fieldRows) {
207
+ const inPackage = /* @__PURE__ */ new Set([...collections, ...through]);
208
+ const fieldsByCollection = import_lodash.default.groupBy(fieldRows, (field) => field.collectionName);
209
+ const fieldsByKey = import_lodash.default.keyBy(fieldRows, (field) => field.key);
210
+ const collectionPayloads = collections.map((name) => {
211
+ const row = rowsByName[name];
212
+ const scalarFields = (fieldsByCollection[name] || []).filter((field) => !import_package_format.ASSOCIATION_TYPES.includes(field.type) && !field.isForeignKey).map((field) => this.fieldPayload(field));
213
+ return {
214
+ ...import_lodash.default.omit(row, import_package_format.COLLECTION_BOOKKEEPING_KEYS),
215
+ name,
216
+ fields: scalarFields
217
+ };
218
+ });
219
+ const associationPayloads = [];
220
+ const handledKeys = /* @__PURE__ */ new Set();
221
+ const dependencies = /* @__PURE__ */ new Set();
222
+ for (const name of collections) {
223
+ for (const field of fieldsByCollection[name] || []) {
224
+ if (!import_package_format.ASSOCIATION_TYPES.includes(field.type) || handledKeys.has(field.key)) continue;
225
+ handledKeys.add(field.key);
226
+ const target = field.target;
227
+ if (!inPackage.has(target)) {
228
+ dependencies.add(target);
229
+ }
230
+ const payload = {
231
+ ...this.fieldPayload(field),
232
+ collectionName: name,
233
+ target
234
+ };
235
+ const reverse = field.reverseKey ? fieldsByKey[field.reverseKey] : void 0;
236
+ if (reverse && !handledKeys.has(reverse.key)) {
237
+ handledKeys.add(reverse.key);
238
+ payload.reverseField = this.fieldPayload(reverse);
239
+ }
240
+ associationPayloads.push(payload);
241
+ }
242
+ }
243
+ return { collectionPayloads, associationPayloads, dependencies: [...dependencies].sort() };
244
+ }
245
+ fieldPayload(field) {
246
+ return import_lodash.default.omit(field, import_package_format.FIELD_BOOKKEEPING_KEYS);
247
+ }
248
+ /** Adapted from plugin-backup-restore Dumper.dumpCollection: meta always, rows only when includeData. */
249
+ async dumpCollection(workDir, name, includeData) {
250
+ const collection = this.db.getCollection(name);
251
+ if (!collection) {
252
+ throw new Error(`Collection "${name}" is not loaded`);
253
+ }
254
+ const dir = import_path.default.join(workDir, (0, import_package_format.dataDir)(name));
255
+ await import_promises.default.mkdir(dir, { recursive: true });
256
+ const rawAttributes = collection.model.getAttributes();
257
+ const columnToAttribute = /* @__PURE__ */ new Map();
258
+ for (const [attributeName, attribute] of Object.entries(rawAttributes)) {
259
+ columnToAttribute.set(attribute.field || attributeName, attributeName);
260
+ }
261
+ const columns = [...columnToAttribute.keys()];
262
+ const autoIncrementAttribute = Object.entries(rawAttributes).find(([, attribute]) => attribute.autoIncrement);
263
+ let count = 0;
264
+ if (includeData && columns.length) {
265
+ const stream = import_fs.default.createWriteStream(import_path.default.join(dir, "data"));
266
+ const rows = await this.db.sequelize.query(`SELECT * FROM ${collection.quotedTableName()}`, {
267
+ type: import_sequelize.QueryTypes.SELECT
268
+ });
269
+ for (const row of rows) {
270
+ const values = columns.map((column) => {
271
+ const field = collection.getField(columnToAttribute.get(column));
272
+ return field ? import_field_value_writer.FieldValueWriter.toDumpedValue(field, row[column]) : row[column];
273
+ });
274
+ stream.write(JSON.stringify(values) + "\n", "utf8");
275
+ }
276
+ stream.end();
277
+ await (0, import_promises2.finished)(stream);
278
+ count = rows.length;
279
+ }
280
+ const meta = {
281
+ name,
282
+ columns,
283
+ count,
284
+ autoIncrementColumn: autoIncrementAttribute ? autoIncrementAttribute[1].field || autoIncrementAttribute[0] : null
285
+ };
286
+ await import_promises.default.writeFile(import_path.default.join(dir, "meta"), JSON.stringify(meta), "utf8");
287
+ }
288
+ async writeJson(workDir, relativePath, value) {
289
+ await import_promises.default.writeFile(import_path.default.join(workDir, relativePath), JSON.stringify(value, null, 2), "utf8");
290
+ }
291
+ async zipDir(dir, filePath) {
292
+ const output = import_fs.default.createWriteStream(filePath);
293
+ const archive = (0, import_archiver.default)("zip", { zlib: { level: 9 } });
294
+ const closed = new Promise((resolve, reject) => {
295
+ output.on("close", () => resolve());
296
+ archive.on("warning", (err) => {
297
+ if (err.code !== "ENOENT") reject(err);
298
+ });
299
+ archive.on("error", reject);
300
+ });
301
+ archive.pipe(output);
302
+ archive.directory(dir, false);
303
+ await archive.finalize();
304
+ await closed;
305
+ }
306
+ }
307
+ // Annotate the CommonJS export names for ESM import in node:
308
+ 0 && (module.exports = {
309
+ CollectionExporter,
310
+ enabledPluginNames
311
+ });
@@ -0,0 +1,9 @@
1
+ import { Database, Field } from '@nocobase/database';
2
+ type WriterFunc = (val: unknown, database: Database) => unknown;
3
+ export declare class FieldValueWriter {
4
+ static writers: Map<string, WriterFunc>;
5
+ static write(field: Field, val: unknown, database: Database): any;
6
+ static toDumpedValue(field: Field, val: unknown): any;
7
+ static registerWriter(types: string | string[], writer: WriterFunc): void;
8
+ }
9
+ export {};
@@ -0,0 +1,107 @@
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 field_value_writer_exports = {};
38
+ __export(field_value_writer_exports, {
39
+ FieldValueWriter: () => FieldValueWriter
40
+ });
41
+ module.exports = __toCommonJS(field_value_writer_exports);
42
+ var import_database = require("@nocobase/database");
43
+ var import_lodash = __toESM(require("lodash"));
44
+ const GEO_TYPES = ["point", "lineString", "circle", "polygon"];
45
+ class FieldValueWriter {
46
+ static writers = /* @__PURE__ */ new Map();
47
+ static write(field, val, database) {
48
+ if (val === null) return val;
49
+ if (GEO_TYPES.includes(field.type)) {
50
+ let result = import_lodash.default.isString(val) ? JSON.parse(val) : val;
51
+ const mockObj = {
52
+ setDataValue: (_name, newVal) => {
53
+ result = newVal;
54
+ }
55
+ };
56
+ field.options.set.call(mockObj, result);
57
+ return result;
58
+ }
59
+ const writer = FieldValueWriter.writers.get(field.typeToString());
60
+ return writer ? writer(val, database) : val;
61
+ }
62
+ static toDumpedValue(field, val) {
63
+ if (val === null) return val;
64
+ if (GEO_TYPES.includes(field.type)) {
65
+ return field.options.get.call({ getDataValue: () => val });
66
+ }
67
+ return val;
68
+ }
69
+ static registerWriter(types, writer) {
70
+ for (const type of import_lodash.default.castArray(types)) {
71
+ FieldValueWriter.writers.set(type, writer);
72
+ }
73
+ }
74
+ }
75
+ function isJSONObjectOrArrayString(str) {
76
+ if (typeof str !== "string") return false;
77
+ try {
78
+ const parsed = JSON.parse(str);
79
+ return typeof parsed === "object" && parsed !== null;
80
+ } catch (e) {
81
+ return false;
82
+ }
83
+ }
84
+ function formatInOffset(iso, offset) {
85
+ const match = /^([+-])(\d{2}):?(\d{2})$/.exec(offset);
86
+ const minutes = match ? (match[1] === "-" ? -1 : 1) * (Number(match[2]) * 60 + Number(match[3])) : 0;
87
+ return new Date(Date.parse(iso) + minutes * 6e4).toISOString().slice(0, 19).replace("T", " ");
88
+ }
89
+ function isIso8601(str) {
90
+ return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(str);
91
+ }
92
+ FieldValueWriter.registerWriter(
93
+ [import_database.DataTypes.JSON.toString(), import_database.DataTypes.JSONB.toString()],
94
+ (val) => isJSONObjectOrArrayString(val) ? JSON.parse(val) : val
95
+ );
96
+ FieldValueWriter.registerWriter("DatetimeNoTzTypeMySQL", (val, database) => {
97
+ const timezone = database.options.rawTimezone || "+00:00";
98
+ if (typeof val === "string" && isIso8601(val)) {
99
+ return formatInOffset(val, timezone);
100
+ }
101
+ return val;
102
+ });
103
+ FieldValueWriter.registerWriter(import_database.DataTypes.BOOLEAN.toString(), (val) => Boolean(val));
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ FieldValueWriter
107
+ });
@@ -0,0 +1,138 @@
1
+ import { Application } from '@nocobase/server';
2
+ import { Manifest } from './package-format';
3
+ export type ProblemCode = 'formatVersion' | 'dialect' | 'invalidPackage' | 'invalidName' | 'collectionExists' | 'tableExists' | 'missingDependency' | 'unknownFieldType' | 'unsupportedTemplate' | 'identifierTooLong' | 'unsafeExpression' | 'outsidePackage' | 'unresolvedConflict' | 'columnsMismatch' | 'systemCollection';
4
+ /** What to do with a collection from the package that already exists in this environment. */
5
+ export declare const CONFLICT_MODES: readonly ["skipCollectionImportData", "skipAll", "overrideSchema", "overrideAll"];
6
+ export type ConflictMode = (typeof CONFLICT_MODES)[number];
7
+ export declare const DEFAULT_CONFLICT_MODE: ConflictMode;
8
+ export interface PreviewConflict {
9
+ name: string;
10
+ title: string;
11
+ /** Association fields here that target this collection (or use it as junction table). Override removes them. */
12
+ referencedBy: {
13
+ collection: string;
14
+ field: string;
15
+ restored: boolean;
16
+ }[];
17
+ /** Package columns the existing table lacks; "Skip collection, import data" is only possible when empty. */
18
+ missingColumns: string[];
19
+ }
20
+ export interface Problem {
21
+ code: ProblemCode;
22
+ collection?: string;
23
+ field?: string;
24
+ detail?: string;
25
+ }
26
+ export interface PreviewCollection {
27
+ name: string;
28
+ title: string;
29
+ fieldCount: number;
30
+ rowCount: number;
31
+ isThrough: boolean;
32
+ }
33
+ /** One field's UI interface. Only the browser knows which interfaces the target supports, so the client checks. */
34
+ export interface PreviewInterface {
35
+ interface: string;
36
+ collection: string;
37
+ field: string;
38
+ }
39
+ export interface Preview {
40
+ token: string;
41
+ manifest: Manifest;
42
+ collections: PreviewCollection[];
43
+ problems: Problem[];
44
+ interfaces: PreviewInterface[];
45
+ missingPlugins: string[];
46
+ conflicts: PreviewConflict[];
47
+ }
48
+ export interface ImportResult {
49
+ /** Collections created (new or overridden). */
50
+ collections: string[];
51
+ overridden: string[];
52
+ skipped: string[];
53
+ through: string[];
54
+ rowCounts: Record<string, number>;
55
+ }
56
+ export interface ImportOptions {
57
+ userId?: string | number | null;
58
+ /** One mode per conflicting collection (see Preview.conflicts). */
59
+ resolutions?: Record<string, ConflictMode>;
60
+ }
61
+ export declare class ImportBlockedError extends Error {
62
+ problems: Problem[];
63
+ constructor(problems: Problem[]);
64
+ }
65
+ export declare class CollectionImporter {
66
+ private app;
67
+ constructor(app: Application);
68
+ private get db();
69
+ static uploadDir(): string;
70
+ private packageDir;
71
+ /** Extracts an uploaded zip into its own directory and returns the token that identifies it. */
72
+ receive(zipPath: string): Promise<string>;
73
+ /**
74
+ * Extracts only the entries the package format defines, streaming each one to disk while counting bytes, and stops as
75
+ * soon as an entry or the whole package passes the limits. Nothing is buffered in memory.
76
+ */
77
+ static extractArchive(zipPath: string, dir: string): Promise<void>;
78
+ discard(token: string): Promise<void>;
79
+ /** Upper bounds for an uploaded package (static so tests can lower them). */
80
+ static limits: {
81
+ maxEntries: number;
82
+ maxEntryBytes: number;
83
+ maxTotalBytes: number;
84
+ maxCompressionRatio: number;
85
+ ratioCheckBytes: number;
86
+ maxSchemaFileBytes: number;
87
+ maxRowBytes: number;
88
+ };
89
+ /**
90
+ * Reads the zip's central directory before anything is extracted and refuses archives that would expand beyond the
91
+ * limits (zip bombs). yauzl validates the declared sizes while extracting, so the declared values cannot be undercut.
92
+ */
93
+ static inspectArchive(zipPath: string): Promise<void>;
94
+ preview(token: string): Promise<Preview>;
95
+ /**
96
+ * @param options.userId the importing user. "Created by" / "Updated by" of every imported row is set to this user,
97
+ * because user ids from the source environment do not identify the same people here.
98
+ */
99
+ import(token: string, options?: ImportOptions): Promise<ImportResult>;
100
+ /** Collections of the package that already exist here, with what an override would remove. */
101
+ private conflicts;
102
+ /** A collection created in the collection manager: has a row, is not hidden, not a junction, not mirrored from code. */
103
+ private isUserCollection;
104
+ /** An auto-created junction table whose owner is a user collection with a belongsToMany field using it. */
105
+ private isOwnedJunction;
106
+ /** Junction table name → collection that owns the belongsToMany field using it. */
107
+ private throughOwners;
108
+ private load;
109
+ private check;
110
+ private interfacesOf;
111
+ private missingPlugins;
112
+ /**
113
+ * Everything an import creates (collections, fields, FK columns, through tables) must belong to the package.
114
+ * Exports only produce such packages; this refuses crafted ones that point at existing collections or tables.
115
+ */
116
+ private checkContainment;
117
+ private checkField;
118
+ private collectionRowExists;
119
+ private expectedTableName;
120
+ private existingTableNames;
121
+ /**
122
+ * afterCreateForForeignKeyField catches and only logs its own errors, so a through collection or FK field that
123
+ * failed to be created would not abort the transaction. Verify every expected object explicitly.
124
+ */
125
+ private assertHookResults;
126
+ /** Rows are appended by column name into an existing table, so every package column must exist there. */
127
+ private assertColumnsSubset;
128
+ /** The dumped rows are column arrays, so the tables created here must have exactly the source columns. */
129
+ private assertColumns;
130
+ /**
131
+ * @param intoExisting rows go into a table that existed before (skip mode): rows whose primary key already exists
132
+ * are left alone, and the returned count is the number of rows actually added.
133
+ */
134
+ private insertRows;
135
+ /** Serial PKs only: move the sequence past the imported ids. Snowflake/uid PKs have no sequence. */
136
+ private resetSequence;
137
+ private removeExpiredUploads;
138
+ }