Skip to content

Commit

Permalink
Merge pull request strapi#3612 from strapi/feature/group-cm-mongoose
Browse files Browse the repository at this point in the history
CRUD Group mongoose
  • Loading branch information
alexandrebodin authored Jul 15, 2019
2 parents dc80457 + 8d872da commit b49e5a5
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 78 deletions.
1 change: 1 addition & 0 deletions packages/strapi-hook-mongoose/lib/mount-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
definition.associations = [];
definition.globalName = _.upperFirst(_.camelCase(definition.globalId));
definition.loadedModel = {};
definition.ObjectId = mongoose.Types.ObjectId;
// Set the default values to model settings.
_.defaults(definition, {
primaryKey: '_id',
Expand Down
93 changes: 36 additions & 57 deletions packages/strapi/lib/core-api/queries/bookshelf.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ module.exports = ({ model, modelKey }) => {
});
const excludedKeys = assocKeys.concat(groupKeys);

function excludeExernalValues(values) {
const defaultPopulate = model.associations
.filter(ast => ast.autoPopulate !== false)
.map(ast => ast.alias);

const pickRelations = values => {
return _.pick(values, assocKeys);
};
const omitExernalValues = values => {
return _.omit(values, excludedKeys);
}
};

/**
*
* @param {Object} entry - the entity the groups are linked ot
* @param {Object} values - Input values
* @param {Object} options
* @param {Object} options.model - the ref model
* @param {Object} options.transacting - transaction option
*/
async function createGroups(entry, values, { transacting }) {
if (groupKeys.length === 0) return;

Expand Down Expand Up @@ -91,7 +90,7 @@ module.exports = ({ model, modelKey }) => {

const groupValue = values[key];

const updateOreateGroupAndLink = async ({ value, order }) => {
const updateOrCreateGroupAndLink = async ({ value, order }) => {
// check if value has an id then update else create
if (_.has(value, groupModel.primaryKey)) {
return groupModel
Expand All @@ -115,23 +114,23 @@ module.exports = ({ model, modelKey }) => {
{ transacting, patch: true, require: false }
);
});
} else {
return groupModel
.forge()
.save(value, { transacting })
.then(group => {
return joinModel.forge().save(
{
[foreignKey]: entry.id,
slice_type: groupModel.collectionName,
slice_id: group.id,
field: key,
order,
},
{ transacting }
);
});
}
// create
return groupModel
.forge()
.save(value, { transacting })
.then(group => {
return joinModel.forge().save(
{
[foreignKey]: entry.id,
slice_type: groupModel.collectionName,
slice_id: group.id,
field: key,
order,
},
{ transacting }
);
});
};

if (repeatable === true) {
Expand All @@ -146,7 +145,7 @@ module.exports = ({ model, modelKey }) => {

await Promise.all(
groupValue.map((value, idx) => {
return updateOreateGroupAndLink({ value, order: idx + 1 });
return updateOrCreateGroupAndLink({ value, order: idx + 1 });
})
);
} else {
Expand All @@ -159,7 +158,7 @@ module.exports = ({ model, modelKey }) => {
transacting,
});

await updateOreateGroupAndLink({ value: groupValue, order: 1 });
await updateOrCreateGroupAndLink({ value: groupValue, order: 1 });
}
}
return;
Expand Down Expand Up @@ -196,7 +195,7 @@ module.exports = ({ model, modelKey }) => {
});

const idsToDelete = _.difference(allIds, idsToKeep);
if (idsToDelete > 0) {
if (idsToDelete.length > 0) {
await joinModel
.forge()
.query(qb => qb.whereIn('slice_id', idsToDelete))
Expand Down Expand Up @@ -252,11 +251,7 @@ module.exports = ({ model, modelKey }) => {

return {
find(params, populate) {
const withRelated =
populate ||
model.associations
.filter(ast => ast.autoPopulate !== false)
.map(ast => ast.alias);
const withRelated = populate || defaultPopulate;

const filters = convertRestQueryParams(params);

Expand All @@ -266,11 +261,7 @@ module.exports = ({ model, modelKey }) => {
},

findOne(params, populate) {
const withRelated =
populate ||
model.associations
.filter(ast => ast.autoPopulate !== false)
.map(ast => ast.alias);
const withRelated = populate || defaultPopulate;

return model
.forge({
Expand All @@ -288,12 +279,8 @@ module.exports = ({ model, modelKey }) => {
},

async create(values) {
const relations = _.pick(
values,
model.associations.map(ast => ast.alias)
);

const data = excludeExernalValues(values);
const relations = pickRelations(values);
const data = omitExernalValues(values);

const runCreate = async trx => {
// Create entry with no-relational data.
Expand All @@ -318,12 +305,8 @@ module.exports = ({ model, modelKey }) => {
}

// Extract values related to relational data.
const relations = _.pick(
values,
model.associations.map(ast => ast.alias)
);

const data = excludeExernalValues(values);
const relations = pickRelations(values);
const data = omitExernalValues(values);

const runUpdate = async trx => {
const entry = await model
Expand Down Expand Up @@ -386,11 +369,7 @@ module.exports = ({ model, modelKey }) => {
const filters = strapi.utils.models.convertParams(modelKey, params);

// Select field to populate.
const withRelated =
populate ||
model.associations
.filter(ast => ast.autoPopulate !== false)
.map(ast => ast.alias);
const withRelated = populate || defaultPopulate;

return model
.query(qb => {
Expand Down
Loading

0 comments on commit b49e5a5

Please sign in to comment.