@strapi/data-transfer 0.0.0 → 4.6.0-beta.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/dist/encryption/decrypt.js +4 -4
- package/dist/encryption/encrypt.js +4 -4
- package/dist/engine/index.d.ts +11 -10
- package/dist/engine/index.js +145 -166
- package/dist/providers/{local-file-destination-provider.d.ts → local-file-destination-provider/index.d.ts} +9 -5
- package/dist/providers/{local-file-destination-provider.js → local-file-destination-provider/index.js} +54 -82
- package/dist/providers/local-file-destination-provider/utils.d.ts +9 -0
- package/dist/providers/local-file-destination-provider/utils.js +61 -0
- package/dist/providers/{local-file-source-provider.d.ts → local-file-source-provider/index.d.ts} +18 -22
- package/dist/providers/{local-file-source-provider.js → local-file-source-provider/index.js} +91 -36
- package/dist/providers/local-strapi-destination-provider/index.d.ts +31 -0
- package/dist/providers/local-strapi-destination-provider/index.js +187 -0
- package/dist/providers/local-strapi-destination-provider/strategies/index.d.ts +1 -0
- package/dist/providers/local-strapi-destination-provider/strategies/index.js +28 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/configuration.d.ts +5 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/configuration.js +44 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/entities.d.ts +9 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/entities.js +88 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/index.d.ts +32 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/index.js +106 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/links.d.ts +3 -0
- package/dist/providers/local-strapi-destination-provider/strategies/restore/links.js +29 -0
- package/dist/providers/local-strapi-source-provider/assets.d.ts +5 -0
- package/dist/providers/local-strapi-source-provider/assets.js +31 -0
- package/dist/providers/local-strapi-source-provider/configuration.js +1 -1
- package/dist/providers/local-strapi-source-provider/entities.js +29 -9
- package/dist/providers/local-strapi-source-provider/index.d.ts +8 -7
- package/dist/providers/local-strapi-source-provider/index.js +32 -2
- package/dist/providers/local-strapi-source-provider/{links/index.d.ts → links.d.ts} +1 -1
- package/dist/providers/local-strapi-source-provider/links.js +23 -0
- package/dist/providers/shared/index.d.ts +1 -0
- package/dist/providers/shared/index.js +28 -0
- package/dist/providers/shared/strapi/entity.d.ts +19 -0
- package/dist/providers/shared/strapi/entity.js +130 -0
- package/dist/providers/shared/strapi/index.d.ts +2 -0
- package/dist/providers/shared/strapi/index.js +29 -0
- package/dist/providers/shared/strapi/link.d.ts +6 -0
- package/dist/providers/shared/strapi/link.js +201 -0
- package/dist/providers/test-utils/index.d.ts +111 -0
- package/dist/providers/test-utils/index.js +64 -0
- package/dist/strategies/index.d.ts +3 -3
- package/dist/strategies/index.js +27 -4
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +30 -0
- package/dist/utils/json.d.ts +23 -0
- package/dist/{utils.js → utils/json.js} +9 -38
- package/dist/utils/schema.d.ts +3 -0
- package/dist/utils/schema.js +22 -0
- package/dist/utils/stream.d.ts +10 -0
- package/dist/utils/stream.js +39 -0
- package/jest.config.js +3 -1
- package/package.json +5 -4
- package/dist/providers/local-strapi-destination-provider.d.ts +0 -22
- package/dist/providers/local-strapi-destination-provider.js +0 -78
- package/dist/providers/local-strapi-source-provider/links/index.js +0 -37
- package/dist/providers/local-strapi-source-provider/links/utils.d.ts +0 -27
- package/dist/providers/local-strapi-source-provider/links/utils.js +0 -155
- package/dist/utils.d.ts +0 -10
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
26
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
27
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
28
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
29
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
30
|
+
};
|
|
31
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
32
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
33
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
34
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
35
|
+
};
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
var _LocalStrapiDestinationProvider_instances, _LocalStrapiDestinationProvider_entitiesMapper, _LocalStrapiDestinationProvider_validateOptions, _LocalStrapiDestinationProvider_deleteAll;
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.createLocalStrapiDestinationProvider = exports.DEFAULT_CONFLICT_STRATEGY = exports.VALID_CONFLICT_STRATEGIES = void 0;
|
|
42
|
+
const stream_1 = require("stream");
|
|
43
|
+
const path_1 = __importDefault(require("path"));
|
|
44
|
+
const fse = __importStar(require("fs-extra"));
|
|
45
|
+
const strategies_1 = require("./strategies");
|
|
46
|
+
const utils = __importStar(require("../../utils"));
|
|
47
|
+
exports.VALID_CONFLICT_STRATEGIES = ['restore', 'merge'];
|
|
48
|
+
exports.DEFAULT_CONFLICT_STRATEGY = 'restore';
|
|
49
|
+
class LocalStrapiDestinationProvider {
|
|
50
|
+
constructor(options) {
|
|
51
|
+
_LocalStrapiDestinationProvider_instances.add(this);
|
|
52
|
+
this.name = 'destination::local-strapi';
|
|
53
|
+
this.type = 'destination';
|
|
54
|
+
_LocalStrapiDestinationProvider_entitiesMapper.set(this, void 0);
|
|
55
|
+
this.options = options;
|
|
56
|
+
__classPrivateFieldSet(this, _LocalStrapiDestinationProvider_entitiesMapper, {}, "f");
|
|
57
|
+
}
|
|
58
|
+
async bootstrap() {
|
|
59
|
+
__classPrivateFieldGet(this, _LocalStrapiDestinationProvider_instances, "m", _LocalStrapiDestinationProvider_validateOptions).call(this);
|
|
60
|
+
this.strapi = await this.options.getStrapi();
|
|
61
|
+
}
|
|
62
|
+
async close() {
|
|
63
|
+
await this.strapi?.destroy?.();
|
|
64
|
+
}
|
|
65
|
+
async beforeTransfer() {
|
|
66
|
+
if (this.options.strategy === 'restore') {
|
|
67
|
+
await __classPrivateFieldGet(this, _LocalStrapiDestinationProvider_instances, "m", _LocalStrapiDestinationProvider_deleteAll).call(this);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
getMetadata() {
|
|
71
|
+
const strapiVersion = strapi.config.get('info.strapi');
|
|
72
|
+
const createdAt = new Date().toISOString();
|
|
73
|
+
const plugins = Object.keys(strapi.plugins);
|
|
74
|
+
return {
|
|
75
|
+
createdAt,
|
|
76
|
+
strapi: {
|
|
77
|
+
version: strapiVersion,
|
|
78
|
+
plugins: plugins.map((name) => ({
|
|
79
|
+
name,
|
|
80
|
+
// TODO: Get the plugin actual version when it'll be available
|
|
81
|
+
version: strapiVersion,
|
|
82
|
+
})),
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
getSchemas() {
|
|
87
|
+
if (!this.strapi) {
|
|
88
|
+
throw new Error('Not able to get Schemas. Strapi instance not found');
|
|
89
|
+
}
|
|
90
|
+
const schemas = {
|
|
91
|
+
...this.strapi.contentTypes,
|
|
92
|
+
...this.strapi.components,
|
|
93
|
+
};
|
|
94
|
+
return utils.schema.mapSchemasValues(schemas);
|
|
95
|
+
}
|
|
96
|
+
getEntitiesStream() {
|
|
97
|
+
if (!this.strapi) {
|
|
98
|
+
throw new Error('Not able to import entities. Strapi instance not found');
|
|
99
|
+
}
|
|
100
|
+
const { strategy } = this.options;
|
|
101
|
+
const updateMappingTable = (type, oldID, newID) => {
|
|
102
|
+
if (!__classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[type]) {
|
|
103
|
+
__classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[type] = {};
|
|
104
|
+
}
|
|
105
|
+
Object.assign(__classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[type], { [oldID]: newID });
|
|
106
|
+
};
|
|
107
|
+
if (strategy === 'restore') {
|
|
108
|
+
return strategies_1.restore.createEntitiesWriteStream({
|
|
109
|
+
strapi: this.strapi,
|
|
110
|
+
updateMappingTable,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`Invalid strategy supplied: "${strategy}"`);
|
|
114
|
+
}
|
|
115
|
+
async getAssetsStream() {
|
|
116
|
+
if (!this.strapi) {
|
|
117
|
+
throw new Error('Not able to stream Assets. Strapi instance not found');
|
|
118
|
+
}
|
|
119
|
+
const assetsDirectory = path_1.default.join(this.strapi.dirs.static.public, 'uploads');
|
|
120
|
+
const backupDirectory = path_1.default.join(this.strapi.dirs.static.public, `uploads_backup_${Date.now()}`);
|
|
121
|
+
await fse.rename(assetsDirectory, backupDirectory);
|
|
122
|
+
await fse.mkdir(assetsDirectory);
|
|
123
|
+
return new stream_1.Writable({
|
|
124
|
+
objectMode: true,
|
|
125
|
+
async final(next) {
|
|
126
|
+
await fse.rm(backupDirectory, { recursive: true, force: true });
|
|
127
|
+
next();
|
|
128
|
+
},
|
|
129
|
+
async write(chunk, _encoding, callback) {
|
|
130
|
+
const entryPath = path_1.default.join(assetsDirectory, chunk.filename);
|
|
131
|
+
const writableStream = fse.createWriteStream(entryPath);
|
|
132
|
+
chunk.stream
|
|
133
|
+
.pipe(writableStream)
|
|
134
|
+
.on('close', callback)
|
|
135
|
+
.on('error', async (error) => {
|
|
136
|
+
try {
|
|
137
|
+
await fse.rm(assetsDirectory, { recursive: true, force: true });
|
|
138
|
+
await fse.rename(backupDirectory, assetsDirectory);
|
|
139
|
+
this.destroy(new Error(`There was an error during the transfer process. The original files have been restored to ${assetsDirectory}`));
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
throw new Error(`There was an error doing the rollback process. The original files are in ${backupDirectory}, but we failed to restore them to ${assetsDirectory}`);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
callback(error);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
async getConfigurationStream() {
|
|
152
|
+
if (!this.strapi) {
|
|
153
|
+
throw new Error('Not able to stream Configurations. Strapi instance not found');
|
|
154
|
+
}
|
|
155
|
+
const { strategy } = this.options;
|
|
156
|
+
if (strategy === 'restore') {
|
|
157
|
+
return strategies_1.restore.createConfigurationWriteStream(this.strapi);
|
|
158
|
+
}
|
|
159
|
+
throw new Error(`Invalid strategy supplied: "${strategy}"`);
|
|
160
|
+
}
|
|
161
|
+
async getLinksStream() {
|
|
162
|
+
if (!this.strapi) {
|
|
163
|
+
throw new Error('Not able to stream links. Strapi instance not found');
|
|
164
|
+
}
|
|
165
|
+
const { strategy } = this.options;
|
|
166
|
+
const mapID = (uid, id) => __classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[uid]?.[id];
|
|
167
|
+
if (strategy === 'restore') {
|
|
168
|
+
return strategies_1.restore.createLinksWriteStream(mapID, this.strapi);
|
|
169
|
+
}
|
|
170
|
+
throw new Error(`Invalid strategy supplied: "${strategy}"`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
_LocalStrapiDestinationProvider_entitiesMapper = new WeakMap(), _LocalStrapiDestinationProvider_instances = new WeakSet(), _LocalStrapiDestinationProvider_validateOptions = function _LocalStrapiDestinationProvider_validateOptions() {
|
|
174
|
+
if (!exports.VALID_CONFLICT_STRATEGIES.includes(this.options.strategy)) {
|
|
175
|
+
throw new Error(`Invalid stategy ${this.options.strategy}`);
|
|
176
|
+
}
|
|
177
|
+
}, _LocalStrapiDestinationProvider_deleteAll = async function _LocalStrapiDestinationProvider_deleteAll() {
|
|
178
|
+
if (!this.strapi) {
|
|
179
|
+
throw new Error('Strapi instance not found');
|
|
180
|
+
}
|
|
181
|
+
return strategies_1.restore.deleteRecords(this.strapi, this.options.restore);
|
|
182
|
+
};
|
|
183
|
+
const createLocalStrapiDestinationProvider = (options) => {
|
|
184
|
+
return new LocalStrapiDestinationProvider(options);
|
|
185
|
+
};
|
|
186
|
+
exports.createLocalStrapiDestinationProvider = createLocalStrapiDestinationProvider;
|
|
187
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as restore from './restore';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.restore = void 0;
|
|
27
|
+
exports.restore = __importStar(require("./restore"));
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
package/dist/providers/local-strapi-destination-provider/strategies/restore/configuration.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Writable } from 'stream';
|
|
3
|
+
import { IConfiguration } from '../../../../../types';
|
|
4
|
+
export declare const restoreConfigs: (strapi: Strapi.Strapi, config: IConfiguration) => Promise<any>;
|
|
5
|
+
export declare const createConfigurationWriteStream: (strapi: Strapi.Strapi) => Promise<Writable>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createConfigurationWriteStream = exports.restoreConfigs = void 0;
|
|
7
|
+
const stream_1 = require("stream");
|
|
8
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
+
const restoreCoreStore = async (strapi, data) => {
|
|
10
|
+
return strapi.db.query('strapi::core-store').create({
|
|
11
|
+
data: {
|
|
12
|
+
...data,
|
|
13
|
+
value: JSON.stringify(data.value),
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
const restoreWebhooks = async (strapi, data) => {
|
|
18
|
+
return strapi.db.query('webhook').create({ data });
|
|
19
|
+
};
|
|
20
|
+
const restoreConfigs = async (strapi, config) => {
|
|
21
|
+
if (config.type === 'core-store') {
|
|
22
|
+
return restoreCoreStore(strapi, config.value);
|
|
23
|
+
}
|
|
24
|
+
if (config.type === 'webhook') {
|
|
25
|
+
return restoreWebhooks(strapi, config.value);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
exports.restoreConfigs = restoreConfigs;
|
|
29
|
+
const createConfigurationWriteStream = async (strapi) => {
|
|
30
|
+
return new stream_1.Writable({
|
|
31
|
+
objectMode: true,
|
|
32
|
+
async write(config, _encoding, callback) {
|
|
33
|
+
try {
|
|
34
|
+
await (0, exports.restoreConfigs)(strapi, config);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
return callback(new Error(`Failed to import ${chalk_1.default.yellowBright(config.type)} (${chalk_1.default.greenBright(config.value.id)}`));
|
|
38
|
+
}
|
|
39
|
+
callback();
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
exports.createConfigurationWriteStream = createConfigurationWriteStream;
|
|
44
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { SchemaUID } from '@strapi/strapi/lib/types/utils';
|
|
3
|
+
import { Writable } from 'stream';
|
|
4
|
+
interface IEntitiesRestoreStreamOptions {
|
|
5
|
+
strapi: Strapi.Strapi;
|
|
6
|
+
updateMappingTable<T extends SchemaUID | string>(type: T, oldID: number, newID: number): void;
|
|
7
|
+
}
|
|
8
|
+
declare const createEntitiesWriteStream: (options: IEntitiesRestoreStreamOptions) => Writable;
|
|
9
|
+
export { createEntitiesWriteStream };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.createEntitiesWriteStream = void 0;
|
|
27
|
+
const fp_1 = require("lodash/fp");
|
|
28
|
+
const stream_1 = require("stream");
|
|
29
|
+
const utils_1 = require("../../../../utils");
|
|
30
|
+
const shared = __importStar(require("../../../shared"));
|
|
31
|
+
const createEntitiesWriteStream = (options) => {
|
|
32
|
+
const { strapi, updateMappingTable } = options;
|
|
33
|
+
const query = shared.strapi.entity.createEntityQuery(strapi);
|
|
34
|
+
return new stream_1.Writable({
|
|
35
|
+
objectMode: true,
|
|
36
|
+
async write(entity, _encoding, callback) {
|
|
37
|
+
const { type, id, data } = entity;
|
|
38
|
+
const { create, getDeepPopulateComponentLikeQuery } = query(type);
|
|
39
|
+
const contentType = strapi.getModel(type);
|
|
40
|
+
const resolveType = (paths) => {
|
|
41
|
+
let type = contentType;
|
|
42
|
+
let value = data;
|
|
43
|
+
for (const path of paths) {
|
|
44
|
+
value = (0, fp_1.get)(path, value);
|
|
45
|
+
if (typeof type === 'function') {
|
|
46
|
+
type = type(value);
|
|
47
|
+
}
|
|
48
|
+
if (path in type.attributes) {
|
|
49
|
+
const attribute = type.attributes[path];
|
|
50
|
+
if (attribute.type === 'component') {
|
|
51
|
+
type = strapi.getModel(attribute.component);
|
|
52
|
+
}
|
|
53
|
+
if (attribute.type === 'dynamiczone') {
|
|
54
|
+
type = ({ __component }) => strapi.getModel(__component);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return type.uid;
|
|
59
|
+
};
|
|
60
|
+
try {
|
|
61
|
+
// Create the entity
|
|
62
|
+
const created = await create({
|
|
63
|
+
data,
|
|
64
|
+
populate: getDeepPopulateComponentLikeQuery(contentType, { select: 'id' }),
|
|
65
|
+
select: 'id',
|
|
66
|
+
});
|
|
67
|
+
const diffs = utils_1.json.diff(data, created);
|
|
68
|
+
updateMappingTable(type, id, created.id);
|
|
69
|
+
diffs.forEach((diff) => {
|
|
70
|
+
if (diff.kind === 'modified' && diff.path.at(-1) === 'id') {
|
|
71
|
+
const target = resolveType(diff.path);
|
|
72
|
+
const [oldID, newID] = diff.values;
|
|
73
|
+
updateMappingTable(target, oldID, newID);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
if (e instanceof Error) {
|
|
79
|
+
return callback(e);
|
|
80
|
+
}
|
|
81
|
+
return callback(new Error(`Failed to create "${type}" (${id})`));
|
|
82
|
+
}
|
|
83
|
+
return callback(null);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
exports.createEntitiesWriteStream = createEntitiesWriteStream;
|
|
88
|
+
//# sourceMappingURL=entities.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ContentTypeSchema } from '@strapi/strapi';
|
|
2
|
+
export interface IRestoreOptions {
|
|
3
|
+
assets?: boolean;
|
|
4
|
+
configuration?: {
|
|
5
|
+
webhook?: boolean;
|
|
6
|
+
coreStore?: boolean;
|
|
7
|
+
};
|
|
8
|
+
entities?: {
|
|
9
|
+
include?: string[];
|
|
10
|
+
exclude?: string[];
|
|
11
|
+
filters?: ((contentType: ContentTypeSchema) => boolean)[];
|
|
12
|
+
params?: {
|
|
13
|
+
[uid: string]: unknown;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
interface IDeleteResults {
|
|
18
|
+
count: number;
|
|
19
|
+
aggregate: {
|
|
20
|
+
[uid: string]: {
|
|
21
|
+
count: number;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export declare const deleteRecords: (strapi: Strapi.Strapi, options?: IRestoreOptions | undefined) => Promise<{
|
|
26
|
+
count: number;
|
|
27
|
+
entities: IDeleteResults;
|
|
28
|
+
configuration: IDeleteResults;
|
|
29
|
+
}>;
|
|
30
|
+
export * from './entities';
|
|
31
|
+
export * from './configuration';
|
|
32
|
+
export * from './links';
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.deleteRecords = void 0;
|
|
30
|
+
const shared = __importStar(require("../../../shared"));
|
|
31
|
+
const deleteRecords = async (strapi, options) => {
|
|
32
|
+
const entities = await deleteEntitiesRecord(strapi, options);
|
|
33
|
+
const configuration = await deleteConfigurationRecords(strapi, options);
|
|
34
|
+
return {
|
|
35
|
+
count: entities.count + configuration.count,
|
|
36
|
+
entities,
|
|
37
|
+
configuration,
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
exports.deleteRecords = deleteRecords;
|
|
41
|
+
const deleteEntitiesRecord = async (strapi, options = {}) => {
|
|
42
|
+
const { entities } = options;
|
|
43
|
+
const query = shared.strapi.entity.createEntityQuery(strapi);
|
|
44
|
+
const contentTypes = Object.values(strapi.contentTypes);
|
|
45
|
+
const contentTypesToClear = contentTypes.filter((contentType) => {
|
|
46
|
+
let keep = true;
|
|
47
|
+
if (entities?.include) {
|
|
48
|
+
keep = entities.include.includes(contentType.uid);
|
|
49
|
+
}
|
|
50
|
+
if (entities?.exclude) {
|
|
51
|
+
keep = !entities.exclude.includes(contentType.uid);
|
|
52
|
+
}
|
|
53
|
+
if (entities?.filters) {
|
|
54
|
+
keep = entities.filters.every((filter) => filter(contentType));
|
|
55
|
+
}
|
|
56
|
+
return keep;
|
|
57
|
+
});
|
|
58
|
+
const [results, updateResults] = useResults(contentTypesToClear.map((contentType) => contentType.uid));
|
|
59
|
+
const deletePromises = contentTypesToClear.map(async (contentType) => {
|
|
60
|
+
const result = await query(contentType.uid).deleteMany(entities?.params);
|
|
61
|
+
if (result) {
|
|
62
|
+
updateResults(result.count || 0, contentType.uid);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
await Promise.all(deletePromises);
|
|
66
|
+
return results;
|
|
67
|
+
};
|
|
68
|
+
const deleteConfigurationRecords = async (strapi, options = {}) => {
|
|
69
|
+
const { coreStore = true, webhook = true } = options?.configuration ?? {};
|
|
70
|
+
const models = [];
|
|
71
|
+
if (coreStore) {
|
|
72
|
+
models.push('strapi::core-store');
|
|
73
|
+
}
|
|
74
|
+
if (webhook) {
|
|
75
|
+
models.push('webhook');
|
|
76
|
+
}
|
|
77
|
+
const [results, updateResults] = useResults(models);
|
|
78
|
+
const deletePromises = models.map(async (uid) => {
|
|
79
|
+
const result = await strapi.db.query(uid).deleteMany({});
|
|
80
|
+
if (result) {
|
|
81
|
+
updateResults(result.count, uid);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
await Promise.all(deletePromises);
|
|
85
|
+
return results;
|
|
86
|
+
};
|
|
87
|
+
const useResults = (keys) => {
|
|
88
|
+
const results = {
|
|
89
|
+
count: 0,
|
|
90
|
+
aggregate: keys.reduce((acc, key) => ({ ...acc, [key]: { count: 0 } }), {}),
|
|
91
|
+
};
|
|
92
|
+
const update = (count, key) => {
|
|
93
|
+
if (key) {
|
|
94
|
+
if (!(key in results.aggregate)) {
|
|
95
|
+
throw new Error(`Unknown key "${key}" provided in results update`);
|
|
96
|
+
}
|
|
97
|
+
results.aggregate[key].count += count;
|
|
98
|
+
}
|
|
99
|
+
results.count += count;
|
|
100
|
+
};
|
|
101
|
+
return [results, update];
|
|
102
|
+
};
|
|
103
|
+
__exportStar(require("./entities"), exports);
|
|
104
|
+
__exportStar(require("./configuration"), exports);
|
|
105
|
+
__exportStar(require("./links"), exports);
|
|
106
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLinksWriteStream = void 0;
|
|
4
|
+
const stream_1 = require("stream");
|
|
5
|
+
const link_1 = require("../../../shared/strapi/link");
|
|
6
|
+
const createLinksWriteStream = (mapID, strapi) => {
|
|
7
|
+
return new stream_1.Writable({
|
|
8
|
+
objectMode: true,
|
|
9
|
+
async write(link, _encoding, callback) {
|
|
10
|
+
const { left, right } = link;
|
|
11
|
+
const query = (0, link_1.createLinkQuery)(strapi);
|
|
12
|
+
// Map IDs if needed
|
|
13
|
+
left.ref = mapID(left.type, left.ref) ?? left.ref;
|
|
14
|
+
right.ref = mapID(right.type, right.ref) ?? right.ref;
|
|
15
|
+
try {
|
|
16
|
+
await query().insert(link);
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
if (e instanceof Error) {
|
|
20
|
+
return callback(e);
|
|
21
|
+
}
|
|
22
|
+
return callback(new Error(`An error happened while trying to import a ${left.type} link. ${e}`));
|
|
23
|
+
}
|
|
24
|
+
callback(null);
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
exports.createLinksWriteStream = createLinksWriteStream;
|
|
29
|
+
//# sourceMappingURL=links.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAssetsStream = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const fs_extra_1 = require("fs-extra");
|
|
6
|
+
const stream_1 = require("stream");
|
|
7
|
+
const IGNORED_FILES = ['.gitkeep'];
|
|
8
|
+
/**
|
|
9
|
+
* Generate and consume assets streams in order to stream each file individually
|
|
10
|
+
*/
|
|
11
|
+
const createAssetsStream = (strapi) => {
|
|
12
|
+
const assetsDirectory = (0, path_1.join)(strapi.dirs.static.public, 'uploads');
|
|
13
|
+
const generator = async function* () {
|
|
14
|
+
const files = await (0, fs_extra_1.readdir)(assetsDirectory);
|
|
15
|
+
const validFiles = files.filter((file) => !IGNORED_FILES.includes(file));
|
|
16
|
+
for (const filename of validFiles) {
|
|
17
|
+
const filepath = (0, path_1.join)(assetsDirectory, filename);
|
|
18
|
+
const stats = await (0, fs_extra_1.stat)(filepath);
|
|
19
|
+
const stream = (0, fs_extra_1.createReadStream)(filepath);
|
|
20
|
+
yield {
|
|
21
|
+
filename,
|
|
22
|
+
filepath,
|
|
23
|
+
stream,
|
|
24
|
+
stats: { size: stats.size },
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
return stream_1.Duplex.from(generator());
|
|
29
|
+
};
|
|
30
|
+
exports.createAssetsStream = createAssetsStream;
|
|
31
|
+
//# sourceMappingURL=assets.js.map
|
|
@@ -21,7 +21,7 @@ const createConfigurationStream = (strapi) => {
|
|
|
21
21
|
]);
|
|
22
22
|
const streams = [coreStoreStream, webhooksStream];
|
|
23
23
|
// Readable configuration stream
|
|
24
|
-
return stream_1.Readable.from((async function* () {
|
|
24
|
+
return stream_1.Readable.from((async function* configurationGenerator() {
|
|
25
25
|
for (const stream of streams) {
|
|
26
26
|
for await (const item of stream) {
|
|
27
27
|
yield item;
|
|
@@ -1,7 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.createEntitiesTransformStream = exports.createEntitiesStream = void 0;
|
|
4
27
|
const stream_1 = require("stream");
|
|
28
|
+
const shared = __importStar(require("../shared/strapi"));
|
|
5
29
|
/**
|
|
6
30
|
* Generate and consume content-types streams in order to stream each entity individually
|
|
7
31
|
*/
|
|
@@ -9,17 +33,20 @@ const createEntitiesStream = (strapi) => {
|
|
|
9
33
|
const contentTypes = Object.values(strapi.contentTypes);
|
|
10
34
|
async function* contentTypeStreamGenerator() {
|
|
11
35
|
for (const contentType of contentTypes) {
|
|
36
|
+
const query = shared.entity.createEntityQuery(strapi).call(null, contentType.uid);
|
|
12
37
|
const stream = strapi.db
|
|
13
38
|
// Create a query builder instance (default type is 'select')
|
|
14
39
|
.queryBuilder(contentType.uid)
|
|
40
|
+
// Fetch all columns
|
|
41
|
+
.select('*')
|
|
15
42
|
// Apply the populate
|
|
16
|
-
.populate(
|
|
43
|
+
.populate(query.deepPopulateComponentLikeQuery)
|
|
17
44
|
// Get a readable stream
|
|
18
45
|
.stream();
|
|
19
46
|
yield { contentType, stream };
|
|
20
47
|
}
|
|
21
48
|
}
|
|
22
|
-
return stream_1.Readable.from((async function* () {
|
|
49
|
+
return stream_1.Readable.from((async function* entitiesGenerator() {
|
|
23
50
|
for await (const { stream, contentType } of contentTypeStreamGenerator()) {
|
|
24
51
|
for await (const entity of stream) {
|
|
25
52
|
yield { entity, contentType };
|
|
@@ -48,11 +75,4 @@ const createEntitiesTransformStream = () => {
|
|
|
48
75
|
});
|
|
49
76
|
};
|
|
50
77
|
exports.createEntitiesTransformStream = createEntitiesTransformStream;
|
|
51
|
-
/**
|
|
52
|
-
* Get the list of attributes that needs to be populated for the entities streaming
|
|
53
|
-
*/
|
|
54
|
-
const getPopulateAttributes = (contentType) => {
|
|
55
|
-
const { attributes } = contentType;
|
|
56
|
-
return Object.keys(attributes).filter((key) => ['component', 'dynamiczone'].includes(attributes[key].type));
|
|
57
|
-
};
|
|
58
78
|
//# sourceMappingURL=entities.js.map
|