Skip to content

Commit

Permalink
feat: provide entry id for transformEntryForLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolink committed Jun 6, 2023
1 parent 522a291 commit a06b657
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ The transform function is expected to return an object with the desired target f
- **`contentType : string`** _(required)_ – Content type ID
- **`from : array`** _(required)_ – Array of the source field IDs
- **`to : array`** _(required)_ – Array of the target field IDs
- **`transformEntryForLocale : function (fields, locale): object`** _(required)_ – Transformation function to be applied.
- **`transformEntryForLocale : function (fields, locale, {id}): object`** _(required)_ – Transformation function to be applied.
- `fields` is an object containing each of the `from` fields. Each field will contain their current localized values (i.e. `fields == {myField: {'en-US': 'my field value'}}`)
- `locale` one of the locales in the space being transformed
- `id` id of the current entry in scope
The return value must be an object with the same keys as specified in `to`. Their values will be written to the respective entry fields for the current locale (i.e. `{nameField: 'myNewValue'}`). If it returns `undefined`, this the values for this locale on the entry will be left untouched.
- **`shouldPublish : bool | 'preserve'`** _(optional)_ – Flag that specifies publishing of target entries, `preserve` will keep current states of the source entries (default `'preserve'`)
Expand All @@ -299,7 +299,7 @@ migration.transformEntries({
contentType: 'newsArticle',
from: ['author', 'authorCity'],
to: ['byline'],
transformEntryForLocale: function (fromFields, currentLocale) {
transformEntryForLocale: function (fromFields, currentLocale, { id }) {
if (currentLocale === 'de-DE') {
return
}
Expand Down Expand Up @@ -333,10 +333,11 @@ The derive function is expected to return an object with the desired target fiel

- `fields` is an object containing each of the `from` fields. Each field will contain their current localized values (i.e. `fields == {myField: {'en-US': 'my field value'}}`)

- **`deriveEntryForLocale : function (fields, locale): object`** _(required)_ – Function that generates the field values for the derived entry.
- **`deriveEntryForLocale : function (fields, locale, {id}): object`** _(required)_ – Function that generates the field values for the derived entry.

- `fields` is an object containing each of the `from` fields. Each field will contain their current localized values (i.e. `fields == {myField: {'en-US': 'my field value'}}`)
- `locale` one of the locales in the space being transformed
- `id` id of the current entry in scope

The return value must be an object with the same keys as specified in `derivedFields`. Their values will be written to the respective new entry fields for the current locale (i.e. `{nameField: 'myNewValue'}`)

Expand All @@ -355,7 +356,7 @@ migration.deriveLinkedEntries({
return fromFields.owner['en-US'].toLowerCase().replace(' ', '-')
},
shouldPublish: true,
deriveEntryForLocale: async (inputFields, locale) => {
deriveEntryForLocale: async (inputFields, locale, { id }) => {
if (locale !== 'en-US') {
return
}
Expand Down Expand Up @@ -387,6 +388,7 @@ For the given (source) content type, transforms all its entries according to the

- `fields` is an object containing each of the `from` fields. Each field will contain their current localized values (i.e. `fields == {myField: {'en-US': 'my field value'}}`)
- `locale` one of the locales in the space being transformed
- `id` id of the current entry in scope

The return value must be an object with the same keys as specified in the `targetContentType`. Their values will be written to the respective entry fields for the current locale (i.e. `{nameField: 'myNewValue'}`). If it returns `undefined`, this the values for this locale on the entry will be left untouched.

Expand All @@ -406,7 +408,7 @@ migration.transformEntriesToType({
const value = fields.woofs['en-US'].toString()
return MurmurHash3(value).result().toString()
},
transformEntryForLocale: function (fromFields, currentLocale) {
transformEntryForLocale: function (fromFields, currentLocale, { id }) {
return {
woofs: `copy - ${fromFields.woofs[currentLocale]}`
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/action/entry-transform-to-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class EntryTransformToTypeAction extends APIAction {
private fromFields?: string[]
private sourceContentTypeId: string
private targetContentTypeId: string
private transformEntryForLocale: (inputFields: any, locale: string) => Promise<any>
private transformEntryForLocale: (
inputFields: any,
locale: string,
{ id }: { id: string }
) => Promise<any>
private identityKey: (fromFields: any) => Promise<string>
private shouldPublish: boolean | 'preserve'
private removeOldEntries: boolean
Expand Down Expand Up @@ -47,7 +51,9 @@ class EntryTransformToTypeAction extends APIAction {
for (const locale of locales) {
let outputsForCurrentLocale
try {
outputsForCurrentLocale = await this.transformEntryForLocale(inputs, locale)
outputsForCurrentLocale = await this.transformEntryForLocale(inputs, locale, {
id: entry.id
})
} catch (err) {
await api.recordRuntimeError(err)
continue
Expand Down
4 changes: 3 additions & 1 deletion src/lib/action/entry-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class EntryTransformAction extends APIAction {
for (const locale of locales) {
let outputsForCurrentLocale
try {
outputsForCurrentLocale = await this.transformEntryForLocale(inputs, locale)
outputsForCurrentLocale = await this.transformEntryForLocale(inputs, locale, {
id: entry.id
})
} catch (err) {
await api.recordRuntimeError(err)
continue
Expand Down

0 comments on commit a06b657

Please sign in to comment.