2022-02-22 23:38:13 +08:00
|
|
|
import lodash from 'lodash';
|
2022-12-24 16:30:01 +08:00
|
|
|
import { ModelStatic } from 'sequelize';
|
2022-02-23 18:18:38 +08:00
|
|
|
import { Model } from './model';
|
2021-12-06 21:12:54 +08:00
|
|
|
import { AssociationKeysToBeUpdate, BlackList, WhiteList } from './repository';
|
|
|
|
|
|
|
|
type UpdateValueItem = string | number | UpdateValues;
|
|
|
|
|
|
|
|
type UpdateValues = {
|
|
|
|
[key: string]: UpdateValueItem | Array<UpdateValueItem>;
|
|
|
|
};
|
|
|
|
|
2022-01-19 10:09:30 +08:00
|
|
|
type UpdateAction = 'create' | 'update';
|
2021-12-06 21:12:54 +08:00
|
|
|
export class UpdateGuard {
|
2022-12-24 16:30:01 +08:00
|
|
|
model: ModelStatic<any>;
|
2022-01-19 10:09:30 +08:00
|
|
|
action: UpdateAction;
|
2021-12-06 21:12:54 +08:00
|
|
|
private associationKeysToBeUpdate: AssociationKeysToBeUpdate;
|
|
|
|
private blackList: BlackList;
|
|
|
|
private whiteList: WhiteList;
|
|
|
|
|
2022-01-19 10:09:30 +08:00
|
|
|
setAction(action: UpdateAction) {
|
|
|
|
this.action = action;
|
|
|
|
}
|
|
|
|
|
2022-12-24 16:30:01 +08:00
|
|
|
setModel(model: ModelStatic<any>) {
|
2021-12-06 21:12:54 +08:00
|
|
|
this.model = model;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
setAssociationKeysToBeUpdate(associationKeysToBeUpdate: AssociationKeysToBeUpdate) {
|
2022-01-19 10:09:30 +08:00
|
|
|
if (this.action == 'create') {
|
|
|
|
this.associationKeysToBeUpdate = Object.keys(this.model.associations);
|
|
|
|
} else {
|
|
|
|
this.associationKeysToBeUpdate = associationKeysToBeUpdate;
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setWhiteList(whiteList: WhiteList) {
|
|
|
|
this.whiteList = whiteList;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBlackList(blackList: BlackList) {
|
|
|
|
this.blackList = blackList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize values by whitelist blacklist
|
|
|
|
* @param values
|
|
|
|
*/
|
|
|
|
sanitize(values: UpdateValues) {
|
|
|
|
values = lodash.clone(values);
|
|
|
|
|
|
|
|
if (!this.model) {
|
|
|
|
throw new Error('please set model first');
|
|
|
|
}
|
|
|
|
|
|
|
|
const associations = this.model.associations;
|
|
|
|
const associationsValues = lodash.pick(values, Object.keys(associations));
|
|
|
|
|
|
|
|
// build params of association update guard
|
|
|
|
const listOfAssociation = (list, association) => {
|
|
|
|
if (list) {
|
|
|
|
list = list
|
|
|
|
.filter((whiteListKey) => whiteListKey.startsWith(`${association}.`))
|
|
|
|
.map((whiteListKey) => whiteListKey.replace(`${association}.`, ''));
|
|
|
|
|
|
|
|
if (list.length == 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
// sanitize association values
|
|
|
|
Object.keys(associationsValues).forEach((association) => {
|
|
|
|
let associationValues = associationsValues[association];
|
|
|
|
|
|
|
|
const filterAssociationToBeUpdate = (value) => {
|
|
|
|
const associationKeysToBeUpdate = this.associationKeysToBeUpdate || [];
|
|
|
|
|
|
|
|
if (associationKeysToBeUpdate.includes(association)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const associationObj = associations[association];
|
|
|
|
|
|
|
|
const associationKeyName =
|
2021-12-06 21:23:34 +08:00
|
|
|
associationObj.associationType == 'BelongsTo' || associationObj.associationType == 'HasOne'
|
2021-12-06 21:12:54 +08:00
|
|
|
? (<any>associationObj).targetKey
|
|
|
|
: associationObj.target.primaryKeyAttribute;
|
|
|
|
|
|
|
|
if (value[associationKeyName]) {
|
2021-12-06 21:23:34 +08:00
|
|
|
return lodash.pick(value, [associationKeyName, ...Object.keys(associationObj.target.associations)]);
|
2021-12-06 21:12:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
const sanitizeValue = (value) => {
|
|
|
|
const associationUpdateGuard = new UpdateGuard();
|
|
|
|
associationUpdateGuard.setModel(associations[association].target);
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
['whiteList', 'blackList', 'associationKeysToBeUpdate'].forEach((optionKey) => {
|
|
|
|
associationUpdateGuard[`set${lodash.upperFirst(optionKey)}`](listOfAssociation(this[optionKey], association));
|
|
|
|
});
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
return associationUpdateGuard.sanitize(filterAssociationToBeUpdate(value));
|
2021-12-06 21:12:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Array.isArray(associationValues)) {
|
|
|
|
associationValues = associationValues.map((value) => {
|
|
|
|
if (typeof value == 'string' || typeof value == 'number') {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
return sanitizeValue(value);
|
|
|
|
}
|
|
|
|
});
|
2021-12-06 21:23:34 +08:00
|
|
|
} else if (typeof associationValues === 'object' && associationValues !== null) {
|
2021-12-06 21:12:54 +08:00
|
|
|
associationValues = sanitizeValue(associationValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set association values to sanitized value
|
|
|
|
values[association] = associationValues;
|
|
|
|
});
|
|
|
|
|
2022-02-12 12:36:22 +08:00
|
|
|
if (values instanceof Model) {
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2022-02-22 23:38:13 +08:00
|
|
|
let valuesKeys = Object.keys(values || {});
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
// handle whitelist
|
|
|
|
if (this.whiteList) {
|
|
|
|
valuesKeys = valuesKeys.filter((valueKey) => {
|
|
|
|
return (
|
|
|
|
this.whiteList.findIndex((whiteKey) => {
|
|
|
|
const keyPaths = whiteKey.split('.');
|
|
|
|
return keyPaths[0] === valueKey;
|
|
|
|
}) !== -1
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle blacklist
|
|
|
|
if (this.blackList) {
|
2021-12-06 21:23:34 +08:00
|
|
|
valuesKeys = valuesKeys.filter((valueKey) => !this.blackList.includes(valueKey));
|
2021-12-06 21:12:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = valuesKeys.reduce((obj, key) => {
|
|
|
|
lodash.set(obj, key, values[key]);
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromOptions(model, options) {
|
|
|
|
const guard = new UpdateGuard();
|
|
|
|
guard.setModel(model);
|
|
|
|
guard.setWhiteList(options.whitelist);
|
|
|
|
guard.setBlackList(options.blacklist);
|
2022-01-19 10:09:30 +08:00
|
|
|
guard.setAction(lodash.get(options, 'action', 'update'));
|
2021-12-06 21:12:54 +08:00
|
|
|
guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
|
|
|
|
return guard;
|
|
|
|
}
|
|
|
|
}
|