import { resolve } from 'path'; import { ALLOWED_CONTENT_TYPES, CUSTOM_TRANSFER_TOKEN_ACCESS_KEY } from '../constants'; const { file: { providers: { createLocalFileSourceProvider }, }, strapi: { providers: { createRemoteStrapiDestinationProvider }, }, engine: { createTransferEngine }, } = require('@strapi/data-transfer'); /** * Reset the DB and import data from a DTS backup * This function ensures we keep all admin user's and roles in the DB * see: https://docs.strapi.io/developer-docs/latest/developer-resources/data-management.html * @param {String} filePath the path to a DTS backup * @param {Array} contentTypesToWipe remove all entries of these content types before importing */ export const resetDatabaseAndImportDataFromPath = async (filePath, contentTypesToWipe = []) => { const source = createSourceProvider(filePath); const destination = createDestinationProvider(contentTypesToWipe); const includedTypes = [...ALLOWED_CONTENT_TYPES, ...contentTypesToWipe]; const engine = createTransferEngine(source, destination, { versionStrategy: 'ignore', schemaStrategy: 'ignore', only: ['content', 'files'], transforms: { links: [ { // only transfer relations to+from requested content types filter(link) { return ( includedTypes.includes(link.left.type) && (includedTypes.includes(link.right.type) || link.right.type === undefined) ); }, }, ], entities: [ { // only include entities of requested content types filter(entity) { return includedTypes.includes(entity.type); }, }, ], }, }); engine.diagnostics.onDiagnostic(console.log); try { await engine.transfer(); } catch { console.error('Import process failed.'); process.exit(1); } }; const createSourceProvider = (filePath) => createLocalFileSourceProvider({ file: { path: resolve(filePath) }, encryption: { enabled: false }, compression: { enabled: false }, }); const createDestinationProvider = (contentTypesToWipe = []) => { return createRemoteStrapiDestinationProvider({ url: new URL(`http://127.0.0.1:${process.env.PORT ?? 1337}/admin`), auth: { type: 'token', token: CUSTOM_TRANSFER_TOKEN_ACCESS_KEY }, strategy: 'restore', restore: { assets: true, entities: { include: [...ALLOWED_CONTENT_TYPES, ...contentTypesToWipe], }, configuration: { coreStore: false, webhook: false, }, }, }); };