Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createVisitor(selection, (path, accessor, list) => {
const { body } = path.node;
const item = t.identifier(singular(getListName(list)));
const forEachBody = t.isBlockStatement(body)
? body
: t.blockStatement([body]);
replaceListWithItemIn(forEachBody, list, accessor, item, path.scope);
// After we replaced, we check if there are remaining accessors.
const params = isAccessorReferencedIn(forEachBody, accessor)
? [item, accessor]
: [item];
path.replaceWith(t.forEach(list, params, forEachBody));
})
);
case 'belongsTo':
loadedModel[name] = function () {
return this.belongsTo(global[_.capitalize(details.model)], name);
};
break;
case 'belongsToMany':
const tableName = _.map(_.sortBy([strapi.models[details.collection].attributes[details.via], details], 'collection'), function (table) {
return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via));
}).join('__');
const relationship = _.clone(strapi.models[details.collection].attributes[details.via]);
// Force singular foreign key
relationship.attribute = pluralize.singular(relationship.collection);
details.attribute = pluralize.singular(details.collection);
// Define PK column
details.column = utils.getPK(model, strapi.models);
relationship.column = utils.getPK(details.collection, strapi.models);
// Sometimes the many-to-many relationships
// is on the same keys on the same models (ex: `friends` key in model `User`)
if (details.attribute + '_' + details.column === relationship.attribute + '_' + relationship.column) {
relationship.attribute = pluralize.singular(details.via);
}
loadedModel[name] = function () {
return this.belongsToMany(global[_.capitalize(details.collection)], tableName, relationship.attribute + '_' + relationship.column, details.attribute + '_' + details.column);
};
break;
util.makePivotCollectionName = function (modelName, relatedModelName, camelCase = false) {
modelName = _.camelCase(pluralize.singular(modelName))
relatedModelName = camelCase
? _.upperFirst(_.camelCase(pluralize.singular(relatedModelName)))
: _.snakeCase(pluralize.singular(relatedModelName))
return _.sortBy([modelName, relatedModelName]).join(camelCase ? '' : '_')
}
return async (root, args, ctx, info) => {
const {
getLoader = ctx => ctx.loader,
parent = root.constructor,
foreignKey = `${pluralize.singular(root.constructor.prototype.tableName)}_id`,
mappingKey = pluralize.singular(root.constructor.prototype.tableName),
list = true,
model
} = options
const loader = getLoader(ctx)
const data = await loader.acquire(`${parent.name}-${model.name}`, parents => {
if (list) {
return model.forge().query(q => q.whereIn(foreignKey, parents.map(parent => parent.id)))
.fetchAll().then(items => parents.map(parent => items.filter(item => {
if (item.get(foreignKey) === parent.id) {
item[mappingKey] = item
return true
}
return false
})))
} else {
export const toTitleCase: (word: string) => string = (word: string): string =>
pluralize.singular(word.charAt(0).toUpperCase() + word.substring(1));
function transformWord(word, toSingular) {
if (pluralize.isPlural(word)) {
return toSingular ? pluralize.singular(word) : word;
}
else {
return toSingular ? word : pluralize.plural(word);
}
}
function optsToArray(item) {
const getPopulatePipeline = ($populate, resource, config, request) => {
const prePopulate1 = `____${$populate}`;
const prePopulate2 = `__${$populate}`;
const foreignField = `${pluralize.singular(resource)}_id`
const lookupPipeline1 = config
? {
pipeline: getLookupPipeline(config, foreignField, request),
let: {[foreignField]: `$_id`} }
: false
const lookupPipeline2 = config
? {
pipeline: getLookupPipeline(config, foreignField+'s', request),
let: {[foreignField+'s']: `$_id`} }
: false
const fields1 = { localField: '_id', foreignField };
const fields2 = { localField: '_id', foreignField: foreignField+'s' };
const pipeline = [
return mapValues(schema, item => {
if (item && item.items) {
item.items = createSchemaForResolver(item.items);
return item;
}
if (item && item.relation) {
item.relation.typename = capitalizeFirstLetter(pluralize.singular(item.relation.to));
}
return item;
});
};
const toSingularInstName = (name) => {
const specialCommandNames = { 'keepDirectoriesInGit': 'keepDirectoryInGit' };
return specialCommandNames[name] || singular(name);
};
getName() {
const { query: { tab, id } } = this.props;
const { data } = this.state;
if (id === 'new') return `New ${singular(tab)}`;
if (data && data.name) return data.name;
return '-';
}