Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
disconnect() {
return resolveAllKeys(
mapKeys(this.adapters, adapter => adapter.disconnect())
// Chain an empty function so that the result of this promise
// isn't unintentionally leaked to the caller
).then(() => {});
}
const createRelationships = (lists, relationships, createdItems) => {
return resolveAllKeys(
mapObject(relationships, (relationList, listKey) => {
const listFieldsConfig = lists[listKey].config.fields;
return resolveAllKeys(
// NOTE: Sparse array / indexes match the indexes from the `createdItems`
mapObject(relationList, (relationItem, relationItemIndex) => {
const createdItem = createdItems[listKey][relationItemIndex];
// Results in something like:
// Promise<{ author: , ... }>
return resolveAllKeys(
mapObject(relationItem, (relationConditions, relationshipField) => {
const relatedListKey = listFieldsConfig[relationshipField].ref;
return relateTo({
relatedFrom: {
async createItems(itemsToCreate) {
// 1. Split it apart
const { relationships, data } = unmergeRelationships(this.lists, itemsToCreate);
// 2. Create the items
// NOTE: Only works if all relationships fields are non-"required"
const createdItems = await resolveAllKeys(
mapKeys(data, (items, listKey) =>
Promise.all(items.map(itemData => this.createItem(listKey, itemData)))
)
);
let createdRelationships;
try {
// 3. Create the relationships
createdRelationships = await createRelationships(this.lists, relationships, createdItems);
} catch (error) {
// 3.5. If creation of relationships didn't work, unwind the createItems
Promise.all(
Object.entries(createdItems).map(([listKey, items]) =>
Promise.all(items.map(({ id }) => this.lists[listKey].adapter.delete(id)))
)
);
async _processNonRealFields(data, processFunction) {
return resolveAllKeys(
arrayToObject(
Object.entries(omit(data, this.realKeys)).map(([path, value]) => ({
path,
value,
adapter: this.fieldAdaptersByPath[path],
})),
'path',
processFunction
)
);
}
_mapToFields(fields, action) {
return resolveAllKeys(arrayToObject(fields, 'path', action)).catch(error => {
if (!error.errors) {
throw error;
}
const errorCopy = new Error(error.message || error.toString());
errorCopy.errors = Object.values(error.errors);
throw errorCopy;
});
}
connect() {
const { adapters, name } = this;
return resolveAllKeys(mapKeys(adapters, adapter => adapter.connect({ name }))).then(() => {
if (this.eventHandlers.onConnect) {
return this.eventHandlers.onConnect(this);
}
});
}