-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #127. Link derived entry based on the type of the reference field (either as first item in Array or single entry)
- Loading branch information
Phoebe Schmidt
authored
Sep 21, 2018
1 parent
282d892
commit 7186467
Showing
4 changed files
with
205 additions
and
9 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
examples/15-derive-entry.js → examples/15-derive-entry-n-to-1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// In this example, we want to turn the dog's owner field into its own entry | ||
// and link it back to the dog. To do this, we create an "owner" | ||
// content type and a link field on the "dog" content type. The link field will be of | ||
// type 'Array', and the derived entry will be the first item in that Array field. | ||
// In the identity function, we define the criterion for when a new owner should | ||
// be created: If the name joined by a hyphen is the same, then the same owner entry is | ||
// linked. | ||
// In the deriveLinkedEntries function, we define what values should go into the new | ||
// owner entries. We don't create any values for the locale 'en-US' on the derived entries. | ||
|
||
module.exports = function (migration) { | ||
const owner = migration.createContentType('owner').name('Owner').description('An owner of a dog'); | ||
owner.createField('firstName').type('Symbol').name('First Name'); | ||
owner.createField('lastName').type('Symbol').name('Last Name'); | ||
owner.displayField('firstName'); | ||
|
||
const dog = migration.editContentType('dog'); | ||
dog.createField('ownersRef').type('Array').items({ type: 'Link', linkType: 'Entry' }).name('The Owner'); | ||
migration.deriveLinkedEntries({ | ||
contentType: 'dog', | ||
derivedContentType: 'owner', | ||
from: ['owner'], | ||
toReferenceField: 'ownersRef', | ||
derivedFields: ['firstName', 'lastName'], | ||
identityKey: async (fromFields) => { | ||
return fromFields.owner['en-US'].toLowerCase().replace(' ', '-'); | ||
}, | ||
shouldPublish: true, | ||
deriveEntryForLocale: async (inputFields, locale) => { | ||
if (locale !== 'en-US') { | ||
return; | ||
} | ||
const [firstName, lastName] = inputFields.owner[locale].split(' '); | ||
|
||
return { | ||
firstName, | ||
lastName | ||
}; | ||
} | ||
}); | ||
|
||
dog.deleteField('owner'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
'use strict'; | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { EntryDeriveAction } from '../../../../src/lib/action/entry-derive'; | ||
import OfflineApi from '../../../../src/lib/offline-api/index'; | ||
import { Entry } from '../../../../src/lib/entities/entry'; | ||
import makeApiEntry from '../../../helpers/make-api-entry'; | ||
import ContentType from '../../../../src/lib/entities/content-type'; | ||
|
||
describe('Entry Derive', function () { | ||
it('derives an entry from n to 1', async function () { | ||
const action = new EntryDeriveAction('dog', { | ||
derivedContentType: 'owner', | ||
from: ['owner'], | ||
toReferenceField: 'ownerRef', | ||
derivedFields: ['firstName', 'lastName'], | ||
identityKey: async (fromFields) => { | ||
return fromFields.owner['en-US'].toLowerCase().replace(' ', '-'); | ||
}, | ||
shouldPublish: true, | ||
deriveEntryForLocale: async (inputFields, locale) => { | ||
if (locale !== 'en-US') { | ||
return; | ||
} | ||
const [firstName, lastName] = inputFields.owner[locale].split(' '); | ||
return { | ||
firstName, | ||
lastName | ||
}; | ||
} | ||
}); | ||
|
||
const contentTypes = new Map(); | ||
contentTypes.set('dog', new ContentType({ | ||
sys: { | ||
id: 'dog' | ||
}, | ||
fields: [{ | ||
name: 'ownerRef', | ||
id: 'ownerRef', | ||
type: 'Symbol' | ||
}] | ||
}) | ||
); | ||
|
||
const entries = [ | ||
new Entry(makeApiEntry({ | ||
id: '246', | ||
contentTypeId: 'dog', | ||
version: 1, | ||
fields: { | ||
owner: { | ||
'en-US': 'john doe' | ||
} | ||
} | ||
})) | ||
]; | ||
|
||
const api = new OfflineApi(contentTypes, entries, ['en-US']); | ||
api.startRecordingRequests(null); | ||
await action.applyTo(api); | ||
api.stopRecordingRequests(); | ||
const batches = await api.getRequestBatches(); | ||
expect(batches[0].requests.length).to.eq(4); | ||
const createTargetEntryFields = batches[0].requests[0].data.fields; | ||
const updateEntryWithLinkFields = batches[0].requests[2].data.fields; | ||
expect(createTargetEntryFields.firstName['en-US']).to.eq('john'); // target entry has first and last name | ||
expect(createTargetEntryFields.lastName['en-US']).to.eq('doe'); | ||
expect(typeof updateEntryWithLinkFields.ownerRef['en-US'].sys).to.eq('object'); // request to update entry is n to 1 link | ||
expect(updateEntryWithLinkFields.ownerRef['en-US'].sys.type).to.eq('Link'); | ||
expect(updateEntryWithLinkFields.ownerRef['en-US'].sys.id).to.eq(batches[0].requests[0].data.sys.id); // id of linked object is same as id of target object | ||
}); | ||
|
||
it('derives an entry from n to n', async function () { | ||
const action = new EntryDeriveAction('dog', { | ||
derivedContentType: 'owner', | ||
from: ['owner'], | ||
toReferenceField: 'owners', | ||
derivedFields: ['firstName', 'lastName'], | ||
identityKey: async (fromFields) => { | ||
return fromFields.owner['en-US'].toLowerCase().replace(' ', '-'); | ||
}, | ||
shouldPublish: true, | ||
deriveEntryForLocale: async (inputFields, locale) => { | ||
if (locale !== 'en-US') { | ||
return; | ||
} | ||
const [firstName, lastName] = inputFields.owner[locale].split(' '); | ||
|
||
return { | ||
firstName, | ||
lastName | ||
}; | ||
} | ||
}); | ||
|
||
const contentTypes = new Map(); | ||
contentTypes.set('dog', new ContentType( | ||
{ | ||
sys: { | ||
id: 'dog' | ||
}, | ||
name: 'dog content type', | ||
fields: [{ | ||
name: 'owners', | ||
id: 'owners', | ||
type: 'Array', | ||
items: { | ||
type: 'Link', | ||
linkType: 'Entry' | ||
} | ||
}, | ||
{ | ||
id: 'owner', | ||
name: 'owner', | ||
type: 'Symbol', | ||
localized: true | ||
} | ||
] | ||
})); | ||
|
||
const entries = [ | ||
new Entry(makeApiEntry({ | ||
id: '246', | ||
contentTypeId: 'dog', | ||
version: 1, | ||
fields: { | ||
owner: { | ||
'en-US': 'johnny depp' | ||
} | ||
} | ||
})) | ||
]; | ||
|
||
const api = new OfflineApi(contentTypes, entries, ['en-US']); | ||
api.startRecordingRequests(null); | ||
await action.applyTo(api); | ||
api.stopRecordingRequests(); | ||
const batches = await api.getRequestBatches(); | ||
|
||
expect(batches[0].requests.length).to.eq(4); | ||
const createTargetEntryFields = batches[0].requests[0].data.fields; | ||
const updateEntryWithLinkFields = batches[0].requests[2].data.fields; | ||
expect(createTargetEntryFields.firstName['en-US']).to.eq('johnny'); // target entry has first and last name | ||
expect(createTargetEntryFields.lastName['en-US']).to.eq('depp'); | ||
expect(typeof updateEntryWithLinkFields.owners['en-US'][0].sys).to.eq('object'); // request to update entry is n to n link | ||
expect(updateEntryWithLinkFields.owners['en-US'][0].sys.type).to.eq('Link'); | ||
expect(updateEntryWithLinkFields.owners['en-US'][0].sys.id).to.eq(batches[0].requests[0].data.sys.id); // id of linked object is same as id of target object | ||
}); | ||
}); |