mirror of
https://github.com/strapi/strapi.git
synced 2025-10-08 14:55:42 +00:00
Fix spread issue
This commit is contained in:
parent
8633f86a2f
commit
d8b14f6b67
@ -233,15 +233,8 @@ module.exports = function(strapi) {
|
|||||||
// Expose ORM functions through the `strapi.models` object.
|
// Expose ORM functions through the `strapi.models` object.
|
||||||
strapi.models[model] = _.assign(global[globalName], strapi.models[model]);
|
strapi.models[model] = _.assign(global[globalName], strapi.models[model]);
|
||||||
|
|
||||||
// Push model to strapi global variables.
|
|
||||||
strapi.bookshelf.collections[globalName.toLowerCase()] = global[
|
|
||||||
globalName
|
|
||||||
];
|
|
||||||
|
|
||||||
// Push attributes to be aware of model schema.
|
// Push attributes to be aware of model schema.
|
||||||
strapi.bookshelf.collections[
|
strapi.models[model]._attributes = definition.attributes;
|
||||||
globalName.toLowerCase()
|
|
||||||
]._attributes = definition.attributes;
|
|
||||||
|
|
||||||
loadedHook();
|
loadedHook();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -147,8 +147,6 @@ module.exports = function (strapi) {
|
|||||||
// Push model to strapi global variables.
|
// Push model to strapi global variables.
|
||||||
collection = global[definition.globalName];
|
collection = global[definition.globalName];
|
||||||
|
|
||||||
console.log(collection.associations);
|
|
||||||
|
|
||||||
// Push attributes to be aware of model schema.
|
// Push attributes to be aware of model schema.
|
||||||
strapi.models[model]._attributes = definition.attributes;
|
strapi.models[model]._attributes = definition.attributes;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
find: async function (params) {
|
find: async function (params) {
|
||||||
return await this
|
return await this
|
||||||
@ -32,13 +34,136 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
update: async function (params) {
|
update: async function (params) {
|
||||||
return await this
|
const virtualFields = [];
|
||||||
|
const response = await module.exports.findOne.call(this, params);
|
||||||
|
|
||||||
|
// Only update fields which are on this document.
|
||||||
|
const values = params.parseRelationships === false ? params.values : Object.keys(JSON.parse(JSON.stringify(params.values))).reduce((acc, current) => {
|
||||||
|
const association = this.associations.filter(x => x.alias === current)[0];
|
||||||
|
const details = this._attributes[current];
|
||||||
|
|
||||||
|
if (_.get(this._attributes, `${current}.isVirtual`) !== true && _.isUndefined(association)) {
|
||||||
|
acc[current] = params.values[current];
|
||||||
|
} else {
|
||||||
|
switch (association.nature) {
|
||||||
|
case 'oneToOne':
|
||||||
|
if (response[current] !== params.values[current]) {
|
||||||
|
const value = _.isNull(params.values[current]) ? response[current] : params.values;
|
||||||
|
|
||||||
|
const recordId = _.isNull(params.values[current]) ? value[this.primaryKey] || value.id || value._id : value[current];
|
||||||
|
|
||||||
|
if (response[current] && _.isObject(response[current]) && response[current][this.primaryKey] !== value[current]) {
|
||||||
|
virtualFields.push(
|
||||||
|
strapi.query(details.collection || details.model).update({
|
||||||
|
id: response[current][this.primaryKey],
|
||||||
|
values: {
|
||||||
|
[details.via]: null
|
||||||
|
},
|
||||||
|
parseRelationships: false
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove previous relationship asynchronously if it exists.
|
||||||
|
virtualFields.push(
|
||||||
|
strapi.query(details.model || details.collection).findOne({ id : recordId })
|
||||||
|
.then(record => {
|
||||||
|
if (record && _.isObject(record[details.via])) {
|
||||||
|
return module.exports.update.call(this, {
|
||||||
|
id: record[details.via][this.primaryKey] || record[details.via].id,
|
||||||
|
values: {
|
||||||
|
[current]: null
|
||||||
|
},
|
||||||
|
parseRelationships: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update the record on the other side.
|
||||||
|
// When params.values[current] is null this means that we are removing the relation.
|
||||||
|
virtualFields.push(strapi.query(details.model || details.collection).update({
|
||||||
|
id: recordId,
|
||||||
|
values: {
|
||||||
|
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
|
||||||
|
},
|
||||||
|
parseRelationships: false
|
||||||
|
}));
|
||||||
|
|
||||||
|
acc[current] = _.isNull(params.values[current]) ? null : value[current];
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'oneToMany':
|
||||||
|
case 'manyToOne':
|
||||||
|
case 'manyToMany':
|
||||||
|
if (details.dominant === true) {
|
||||||
|
acc[current] = params.values[current];
|
||||||
|
} else if (response[current] && _.isArray(response[current]) && current !== 'id') {
|
||||||
|
// Records to add in the relation.
|
||||||
|
const toAdd = _.differenceWith(params.values[current], response[current], (a, b) =>
|
||||||
|
a[this.primaryKey].toString() === b[this.primaryKey].toString()
|
||||||
|
);
|
||||||
|
// Records to remove in the relation.
|
||||||
|
const toRemove = _.differenceWith(response[current], params.values[current], (a, b) =>
|
||||||
|
a[this.primaryKey].toString() === b[this.primaryKey].toString()
|
||||||
|
)
|
||||||
|
.filter(x => toAdd.find(y => x.id === y.id) === undefined);
|
||||||
|
|
||||||
|
// Push the work into the flow process.
|
||||||
|
toAdd.forEach(value => {
|
||||||
|
if (association.nature === 'manyToMany' && !_.isArray(params.values[this.primaryKey])) {
|
||||||
|
value[details.via] = (value[details.via] || []).concat([params.values[this.primaryKey]]);
|
||||||
|
} else {
|
||||||
|
value[details.via] = params.values[this.primaryKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
virtualFields.push(strapi.query(details.model || details.collection).addRelation({
|
||||||
|
id: value[this.primaryKey] || value.id || value._id,
|
||||||
|
values: value,
|
||||||
|
foreignKey: current
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
toRemove.forEach(value => {
|
||||||
|
if (association.nature === 'manyToMany' && !_.isArray(params.values[this.primaryKey])) {
|
||||||
|
value[details.via] = value[details.via].filter(x => x.toString() !== params.values[this.primaryKey].toString());
|
||||||
|
} else {
|
||||||
|
value[details.via] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtualFields.push(strapi.query(details.model || details.collection).removeRelation({
|
||||||
|
id: value[this.primaryKey] || value.id || value._id,
|
||||||
|
values: value,
|
||||||
|
foreignKey: current
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
} else if (_.get(this._attributes, `${current}.isVirtual`) !== true) {
|
||||||
|
acc[current] = params.values[current];
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
virtualFields.push(this
|
||||||
.forge({
|
.forge({
|
||||||
[this.primaryKey]: params[this.primaryKey]
|
[this.primaryKey]: params[this.primaryKey]
|
||||||
})
|
})
|
||||||
.save(params.values, {
|
.save(params.values, {
|
||||||
patch: true
|
patch: true
|
||||||
});
|
}));
|
||||||
|
|
||||||
|
// Update virtuals fields.
|
||||||
|
const process = await Promise.all(virtualFields);
|
||||||
|
|
||||||
|
return process[process.length - 1];
|
||||||
},
|
},
|
||||||
|
|
||||||
delete: async function (params) {
|
delete: async function (params) {
|
||||||
|
@ -34,10 +34,9 @@ module.exports = {
|
|||||||
|
|
||||||
return module.exports.update.call(this, {
|
return module.exports.update.call(this, {
|
||||||
[this.primaryKey]: entry[this.primaryKey],
|
[this.primaryKey]: entry[this.primaryKey],
|
||||||
values: {
|
values: _.merge({
|
||||||
id: entry[this.primaryKey],
|
id: entry[this.primaryKey]
|
||||||
...params.values
|
}, params.values)
|
||||||
}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@
|
|||||||
"plop": "^1.8.1",
|
"plop": "^1.8.1",
|
||||||
"prettier": "^1.7.0",
|
"prettier": "^1.7.0",
|
||||||
"rimraf": "^2.6.2",
|
"rimraf": "^2.6.2",
|
||||||
"strapi-helper-plugin": "file:../strapi-helper-plugin",
|
"strapi-helper-plugin": "3.0.0-alpha.5.5",
|
||||||
"webpack": "^3.6.0"
|
"webpack": "^3.6.0"
|
||||||
},
|
},
|
||||||
"author": {
|
"author": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user