Skip to content

Commit bd577c2

Browse files
committed
chore: add example customizer
1 parent 32cb917 commit bd577c2

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
## Pipeline Customizer Example
2+
3+
4+
`````typescript
5+
import { createFilter, MigrationConfig, PipelineCustomizer, SourceDynamoDbClient } from "@webiny/data-transfer";
6+
import { Logger } from "~/tools/Logger/index.js";
7+
8+
interface Folder {
9+
id: string;
10+
entryId: string;
11+
}
12+
13+
interface FetchFolderParams {
14+
id: string;
15+
tenant: string;
16+
locale: string;
17+
}
18+
19+
interface PossibleFolderRecord extends SourceDynamoDbClient.Record {
20+
PK: string;
21+
SK: string;
22+
id: string;
23+
entryId: string;
24+
modelId: string;
25+
}
26+
27+
class ExampleCustomizerImpl implements PipelineCustomizer.Interface {
28+
public name = "ExampleCustomizer";
29+
30+
private readonly cache: Record<string, Promise<Folder | null> | undefined> = {};
31+
32+
public constructor(
33+
private readonly migrationConfig: MigrationConfig.Interface,
34+
private readonly sourceDynamoDbClient: SourceDynamoDbClient.Interface,
35+
private readonly logger: Logger.Interface
36+
) {
37+
}
38+
39+
public canUse(pipelineName: string): boolean {
40+
return pipelineName === "CmsEntries";
41+
}
42+
43+
public async configure(builder: PipelineCustomizer.Builder): Promise<void> {
44+
builder.filter(createFilter(async record => {
45+
/**
46+
* Do you want to allow this record to be processed if there is no folderId, tenant or locale?
47+
*/
48+
const folderId = record.location?.folderId;
49+
if(!folderId || !record.tenant || !record.locale) {
50+
return true;
51+
}
52+
const folder = await this.getFolder({
53+
id: folderId.split("#")[0],
54+
tenant: record.tenant,
55+
locale: record.locale
56+
});
57+
/**
58+
* Folder does not exist, lets skip this record and log it, so we can investigate later.
59+
*/
60+
if(!folder) {
61+
this.logger.error(`Folder with id "${folderId}" not found for entry with id "${record.entryId}". Skipping this record.`);
62+
return false;
63+
}
64+
/**
65+
* Folder exists, check if its ok so we can transfer the record.
66+
*/
67+
// CUSTOM LOGIC and return false if you want to skip the record.
68+
// yes, transfer the record
69+
return true;
70+
})
71+
);
72+
}
73+
/**
74+
* This method gets the folder promise - cached one.
75+
*/
76+
private async getFolder(params: FetchFolderParams): Promise<Folder | null> {
77+
const { tenant, locale, id } = params;
78+
if (!id) {
79+
return null;
80+
} else if (this.cache[id]) {
81+
return this.cache[id];
82+
}
83+
return this.cache[id] = this.fetchFolder({ tenant, locale, id });
84+
}
85+
/**
86+
* This method fetches the folder from DynamoDB.
87+
* Method should not get hit every time folder is requested, we have getFolder() with cache for that.
88+
*/
89+
private async fetchFolder(params: FetchFolderParams): Promise<Folder | null> {
90+
const result = await this.sourceDynamoDbClient.get<PossibleFolderRecord>(
91+
this.migrationConfig.source.dynamodb.tableName,
92+
`T#${params.tenant}#L#${params.locale}#CMS#CME#${params.id}`,
93+
"L"
94+
);
95+
if(!result) {
96+
return null;
97+
}
98+
else if(result.modelId === "acoFolder") {
99+
return result;
100+
}
101+
return null;
102+
}
103+
}
104+
105+
export const ExampleCustomizer = PipelineCustomizer.createImplementation({
106+
implementation: ExampleCustomizerImpl,
107+
dependencies: [MigrationConfig, SourceDynamoDbClient, Logger]
108+
});
109+
`````

0 commit comments

Comments
 (0)