Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async _update(id, data) {
// Update the real data
const realData = pick(data, this.realKeys);
const query = this._query()
.table(this.tableName)
.where({ id });
if (Object.keys(realData).length) {
query.update(realData);
}
const item = (await query.returning(['id', ...this.realKeys]))[0];
// For every many-field, update the many-table
await this._processNonRealFields(data, async ({ path, value: newValues, adapter }) => {
const { refListId } = adapter;
const tableName = this._manyTable(path);
// Future task: Is there some way to combine the following three
// operations into a single query?
function graphQlQueryToMongoJoinQuery(query) {
const _query = {
...query.where,
...mapKeyNames(
// Grab all the modifiers
pick(query, ['search', 'orderBy', 'skip', 'first']),
// and prefix with a dollar symbol so they can be picked out by the
// query builder tokeniser
key => `$${key}`
),
};
return mapKeys(_query, field => {
if (getType(field) !== 'Object' || !field.where) {
return field;
}
// recurse on object (ie; relationship) types
return graphQlQueryToMongoJoinQuery(field);
});
}
async _create(data) {
const realData = pick(data, this.realKeys);
// Insert the real data into the table
const item = (await this._query()
.insert(realData)
.into(this.tableName)
.returning('*'))[0];
// For every many-field, update the many-table
const manyItem = await this._processNonRealFields(data, async ({ value, adapter }) =>
this._createOrUpdateField({ value, adapter, itemId: item.id })
);
return { ...item, ...manyItem };
}
const parseGranularAccessConfig = ({ accessTypes, access, defaultAccess, onGranularParseError, validateGranularType }) => {
const longHandAccess = pick(access, accessTypes);
// An object was supplied, but it has the wrong keys (it's probably a
// declarative access control config being used as a shorthand, which
// isn't possible [due to `create` not supporting declarative config])
if (Object.keys(longHandAccess).length === 0) {
onGranularParseError();
}
validateGranularConfigTypes(longHandAccess, validateGranularType);
// Construct an object with all keys
return {
...shorthandToObject(accessTypes, defaultAccess),
...longHandAccess,
};
};