move strategy check to destination provider

This commit is contained in:
Bassel 2022-11-24 10:19:02 +02:00
parent 40e76c2fa8
commit 2e7cc82064
6 changed files with 37 additions and 55 deletions

View File

@ -294,50 +294,6 @@ describe('Transfer engine', () => {
});
describe('transfer', () => {
test('requires strategy to be either restore or merge', async () => {
const engineOptions = {
versionMatching: 'exact',
exclude: [],
} as unknown as ITransferEngineOptions;
const restoreEngine = createTransferEngine(minimalSource, minimalDestination, {
...engineOptions,
strategy: 'restore',
});
await restoreEngine.transfer();
expect(restoreEngine).toBeValidTransferEngine();
const mergeEngine = createTransferEngine(minimalSource, minimalDestination, {
...engineOptions,
strategy: 'merge',
});
await mergeEngine.transfer();
expect(mergeEngine).toBeValidTransferEngine();
// undefined strategy
await expect(
(async () => {
const invalidEngine = createTransferEngine(
minimalSource,
minimalDestination,
engineOptions
);
await invalidEngine.transfer();
})()
).rejects.toThrow();
// invalid strategy
await expect(
(async () => {
const invalidEngine = createTransferEngine(minimalSource, minimalDestination, {
...engineOptions,
strategy: 'foo',
} as unknown as ITransferEngineOptions);
await invalidEngine.transfer();
})()
).rejects.toThrow();
});
test('calls all provider stages', async () => {
const engine = createTransferEngine(completeSource, completeDestination, defaultOptions);
expect(completeSource).toHaveSourceStagesCalledTimes(0);

View File

@ -32,8 +32,6 @@ type TransferEngineProgress = {
stream: PassThrough;
};
export const VALID_STRATEGIES = ['restore', 'merge'];
class TransferEngine<
S extends ISourceProvider = ISourceProvider,
D extends IDestinationProvider = IDestinationProvider
@ -236,16 +234,8 @@ class TransferEngine<
}
}
validateTransferOptions() {
if (!VALID_STRATEGIES.includes(this.options.strategy)) {
throw new Error('Invalid stategy ' + this.options.strategy);
}
}
async transfer(): Promise<ITransferResults<S, D>> {
try {
this.validateTransferOptions();
await this.bootstrap();
await this.init();

View File

@ -24,6 +24,33 @@ describe('Local Strapi Source Destination', () => {
});
describe('Strategy', () => {
test('requires strategy to be either restore or merge', async () => {
const restoreProvider = createLocalStrapiDestinationProvider({
getStrapi: getStrapiFactory(),
strategy: 'restore',
});
await restoreProvider.bootstrap();
expect(restoreProvider.strapi).toBeDefined();
const mergeProvider = createLocalStrapiDestinationProvider({
getStrapi: getStrapiFactory(),
strategy: 'merge',
});
await mergeProvider.bootstrap();
expect(mergeProvider.strapi).toBeDefined();
await expect(
(async () => {
const invalidProvider = createLocalStrapiDestinationProvider({
getStrapi: getStrapiFactory(),
/* @ts-ignore: disable-next-line */
strategy: 'foo',
});
await invalidProvider.bootstrap();
})()
).rejects.toThrow();
});
test('Should delete all entities if it is a restore', async () => {
const entities = [
{

View File

@ -7,6 +7,8 @@ import { Duplex } from 'stream';
import { mapSchemasValues } from '../../utils';
export const VALID_STRATEGIES = ['restore', 'merge'];
interface ILocalStrapiDestinationProviderOptions {
getStrapi(): Strapi.Strapi | Promise<Strapi.Strapi>;
restore?: DeleteOptions;
@ -35,6 +37,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
}
async bootstrap(): Promise<void> {
this.validateOptions();
this.strapi = await this.options.getStrapi();
}
@ -42,6 +45,12 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
await this.strapi?.destroy?.();
}
validateOptions() {
if (!VALID_STRATEGIES.includes(this.options.strategy)) {
throw new Error('Invalid stategy ' + this.options.strategy);
}
}
async deleteAll() {
if (!this.strapi) {
throw new Error('Strapi instance not found');

View File

@ -24,7 +24,6 @@ export const deleteAllRecords = async (strapi: Strapi.Strapi, deleteOptions?: De
);
const contentTypes: ContentTypeSchema[] = deleteOptions?.contentTypes ?? defaultContentTypes;
let count = 0;
console.log(deleteOptions?.contentTypes);
await Promise.all(
contentTypes.map(async (contentType) => {
const filters = conditions[contentType.uid] ?? {};

View File

@ -19,6 +19,7 @@ interface IProvider {
close?(): Promise<void> | void;
getMetadata(): IMetadata | null | Promise<IMetadata | null>;
beforeStreaming?(): Promise<void>;
validateOptions?(): void;
}
export interface ISourceProvider extends IProvider {