2021-09-23 00:16:04 +08:00
|
|
|
import {
|
|
|
|
Association,
|
2021-12-06 21:12:54 +08:00
|
|
|
BelongsTo,
|
|
|
|
BelongsToMany,
|
|
|
|
HasMany,
|
2022-01-25 17:08:35 +08:00
|
|
|
HasOne,
|
2021-12-08 13:00:09 +08:00
|
|
|
Hookable,
|
2022-01-25 17:08:35 +08:00
|
|
|
ModelCtor,
|
2022-03-06 19:12:44 +08:00
|
|
|
Transactionable,
|
2021-09-23 00:16:04 +08:00
|
|
|
} from 'sequelize';
|
2022-02-23 18:18:38 +08:00
|
|
|
import { Model } from './model';
|
2022-01-25 17:08:35 +08:00
|
|
|
import { UpdateGuard } from './update-guard';
|
2021-09-23 00:16:04 +08:00
|
|
|
|
|
|
|
function isUndefinedOrNull(value: any) {
|
|
|
|
return typeof value === 'undefined' || value === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isStringOrNumber(value: any) {
|
|
|
|
return typeof value === 'string' || typeof value === 'number';
|
|
|
|
}
|
|
|
|
|
2021-12-08 13:00:09 +08:00
|
|
|
function getKeysByPrefix(keys: string[], prefix: string) {
|
|
|
|
return keys.filter((key) => key.startsWith(`${prefix}.`)).map((key) => key.substring(prefix.length + 1));
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
export function modelAssociations(instance: Model) {
|
|
|
|
return (<typeof Model>instance.constructor).associations;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export function belongsToManyAssociations(instance: Model): Array<BelongsToMany> {
|
2021-12-06 21:12:54 +08:00
|
|
|
const associations = modelAssociations(instance);
|
|
|
|
return Object.entries(associations)
|
|
|
|
.filter((entry) => {
|
|
|
|
const [key, association] = entry;
|
|
|
|
return association.associationType == 'BelongsToMany';
|
|
|
|
})
|
|
|
|
.map((association) => {
|
|
|
|
return <BelongsToMany>association[1];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export function modelAssociationByKey(instance: Model, key: string): Association {
|
2021-12-06 21:12:54 +08:00
|
|
|
return modelAssociations(instance)[key] as Association;
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateValue = { [key: string]: any };
|
|
|
|
|
2022-05-22 08:48:19 +08:00
|
|
|
interface UpdateOptions extends Transactionable {
|
2021-12-06 21:12:54 +08:00
|
|
|
filter?: any;
|
2022-01-07 20:08:01 +08:00
|
|
|
filterByTk?: number | string;
|
2021-12-06 21:12:54 +08:00
|
|
|
// 字段白名单
|
|
|
|
whitelist?: string[];
|
|
|
|
// 字段黑名单
|
|
|
|
blacklist?: string[];
|
|
|
|
// 关系数据默认会新建并建立关联处理,如果是已存在的数据只关联,但不更新关系数据
|
|
|
|
// 如果需要更新关联数据,可以通过 updateAssociationValues 指定
|
|
|
|
updateAssociationValues?: string[];
|
|
|
|
sanitized?: boolean;
|
|
|
|
sourceModel?: Model;
|
|
|
|
}
|
|
|
|
|
2021-12-08 13:00:09 +08:00
|
|
|
interface UpdateAssociationOptions extends Transactionable, Hookable {
|
|
|
|
updateAssociationValues?: string[];
|
|
|
|
sourceModel?: Model;
|
2022-01-25 17:08:35 +08:00
|
|
|
context?: any;
|
2022-03-06 19:12:44 +08:00
|
|
|
associationContext?: any;
|
2021-12-08 13:00:09 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export async function updateModelByValues(instance: Model, values: UpdateValue, options?: UpdateOptions) {
|
2021-12-06 21:12:54 +08:00
|
|
|
if (!options?.sanitized) {
|
|
|
|
const guard = new UpdateGuard();
|
|
|
|
//@ts-ignore
|
|
|
|
guard.setModel(instance.constructor);
|
|
|
|
guard.setBlackList(options.blacklist);
|
|
|
|
guard.setWhiteList(options.whitelist);
|
|
|
|
guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
|
|
|
|
values = guard.sanitize(values);
|
|
|
|
}
|
|
|
|
|
2021-12-07 21:19:28 +08:00
|
|
|
await instance.update(values, options);
|
2021-12-06 21:12:54 +08:00
|
|
|
await updateAssociations(instance, values, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateThroughTableValue(
|
|
|
|
instance: Model,
|
|
|
|
throughName: string,
|
|
|
|
throughValues: any,
|
|
|
|
source: Model,
|
|
|
|
transaction = null,
|
|
|
|
) {
|
|
|
|
// update through table values
|
|
|
|
for (const belongsToMany of belongsToManyAssociations(instance)) {
|
|
|
|
// @ts-ignore
|
|
|
|
const throughModel = belongsToMany.through.model;
|
|
|
|
const throughModelName = throughModel.name;
|
|
|
|
|
|
|
|
if (throughModelName === throughModelName) {
|
|
|
|
const where = {
|
|
|
|
[belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
|
|
|
|
[belongsToMany.otherKey]: source.get(belongsToMany.targetKey),
|
|
|
|
};
|
|
|
|
|
|
|
|
return await throughModel.update(throughValues, {
|
|
|
|
where,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update association of instance by values
|
|
|
|
* @param instance
|
|
|
|
* @param values
|
|
|
|
* @param options
|
|
|
|
*/
|
2021-12-08 13:00:09 +08:00
|
|
|
export async function updateAssociations(instance: Model, values: any, options: UpdateAssociationOptions = {}) {
|
2021-12-06 21:12:54 +08:00
|
|
|
// if no values set, return
|
|
|
|
if (!values) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let newTransaction = false;
|
|
|
|
let transaction = options.transaction;
|
|
|
|
|
|
|
|
if (!transaction) {
|
|
|
|
newTransaction = true;
|
|
|
|
transaction = await instance.sequelize.transaction();
|
|
|
|
}
|
|
|
|
|
2021-12-08 13:00:09 +08:00
|
|
|
const keys = Object.keys(values);
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
for (const key of Object.keys(modelAssociations(instance))) {
|
2021-12-08 13:00:09 +08:00
|
|
|
if (keys.includes(key)) {
|
2021-12-06 21:12:54 +08:00
|
|
|
await updateAssociation(instance, key, values[key], {
|
|
|
|
...options,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
// update through table values
|
|
|
|
for (const belongsToMany of belongsToManyAssociations(instance)) {
|
|
|
|
// @ts-ignore
|
|
|
|
const throughModel = belongsToMany.through.model;
|
|
|
|
const throughModelName = throughModel.name;
|
|
|
|
|
|
|
|
if (values[throughModelName] && options.sourceModel) {
|
|
|
|
const where = {
|
|
|
|
[belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
|
2021-12-06 21:23:34 +08:00
|
|
|
[belongsToMany.otherKey]: options.sourceModel.get(belongsToMany.targetKey),
|
2021-12-06 21:12:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
await throughModel.update(values[throughModel.name], {
|
|
|
|
where,
|
2022-01-25 17:08:35 +08:00
|
|
|
context: options.context,
|
2021-12-06 21:12:54 +08:00
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newTransaction) {
|
2021-09-23 00:16:04 +08:00
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 19:12:44 +08:00
|
|
|
function isReverseAssociationPair(a: any, b: any) {
|
|
|
|
const typeSet = new Set();
|
|
|
|
typeSet.add(a.associationType);
|
|
|
|
typeSet.add(b.associationType);
|
|
|
|
|
|
|
|
if (typeSet.size == 1 && typeSet.has('BelongsToMany')) {
|
|
|
|
return (
|
|
|
|
a.through.tableName === b.through.tableName &&
|
|
|
|
a.target.name === b.source.name &&
|
|
|
|
b.target.name === a.source.name &&
|
|
|
|
a.foreignKey === b.otherKey &&
|
|
|
|
a.sourceKey === b.targetKey &&
|
|
|
|
a.otherKey === b.foreignKey &&
|
|
|
|
a.targetKey === b.sourceKey
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((typeSet.has('HasOne') && typeSet.has('BelongsTo')) || (typeSet.has('HasMany') && typeSet.has('BelongsTo'))) {
|
|
|
|
const sourceAssoc = a.associationType == 'BelongsTo' ? b : a;
|
|
|
|
const targetAssoc = sourceAssoc == a ? b : a;
|
|
|
|
|
|
|
|
return (
|
|
|
|
sourceAssoc.source.name === targetAssoc.target.name &&
|
|
|
|
sourceAssoc.foreignKey === targetAssoc.foreignKey &&
|
|
|
|
sourceAssoc.sourceKey === targetAssoc.targetKey
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* update model association by key
|
|
|
|
* @param instance
|
|
|
|
* @param key
|
|
|
|
* @param value
|
|
|
|
* @param options
|
|
|
|
*/
|
2021-12-08 13:00:09 +08:00
|
|
|
export async function updateAssociation(
|
|
|
|
instance: Model,
|
|
|
|
key: string,
|
|
|
|
value: any,
|
|
|
|
options: UpdateAssociationOptions = {},
|
|
|
|
) {
|
2021-12-06 21:12:54 +08:00
|
|
|
const association = modelAssociationByKey(instance, key);
|
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (!association) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2022-03-06 19:12:44 +08:00
|
|
|
if (options.associationContext && isReverseAssociationPair(association, options.associationContext)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
switch (association.associationType) {
|
|
|
|
case 'HasOne':
|
|
|
|
case 'BelongsTo':
|
2021-09-27 15:28:32 +08:00
|
|
|
return updateSingleAssociation(instance, key, value, options);
|
2021-09-23 00:16:04 +08:00
|
|
|
case 'HasMany':
|
|
|
|
case 'BelongsToMany':
|
2021-09-27 15:28:32 +08:00
|
|
|
return updateMultipleAssociation(instance, key, value, options);
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* update belongsTo and HasOne
|
|
|
|
* @param model
|
|
|
|
* @param key
|
|
|
|
* @param value
|
|
|
|
* @param options
|
|
|
|
*/
|
2021-12-08 13:00:09 +08:00
|
|
|
export async function updateSingleAssociation(
|
|
|
|
model: Model,
|
|
|
|
key: string,
|
|
|
|
value: any,
|
|
|
|
options: UpdateAssociationOptions = {},
|
|
|
|
) {
|
2021-12-06 21:12:54 +08:00
|
|
|
const association = <HasOne | BelongsTo>modelAssociationByKey(model, key);
|
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (!association) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2022-01-25 17:08:35 +08:00
|
|
|
const { context, updateAssociationValues = [], transaction = await model.sequelize.transaction() } = options;
|
2021-12-08 13:00:09 +08:00
|
|
|
const keys = getKeysByPrefix(updateAssociationValues, key);
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
try {
|
2021-12-06 21:12:54 +08:00
|
|
|
// set method of association
|
2021-09-23 00:16:04 +08:00
|
|
|
const setAccessor = association.accessors.set;
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
const removeAssociation = async () => {
|
2021-09-27 15:28:32 +08:00
|
|
|
await model[setAccessor](null, { transaction });
|
|
|
|
model.setDataValue(key, null);
|
|
|
|
if (!options.transaction) {
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
return true;
|
2021-12-06 21:12:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (isUndefinedOrNull(value)) {
|
|
|
|
return await removeAssociation();
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (isStringOrNumber(value)) {
|
2022-01-25 17:08:35 +08:00
|
|
|
await model[setAccessor](value, { context, transaction });
|
2021-09-27 15:28:32 +08:00
|
|
|
if (!options.transaction) {
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-08 13:00:09 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
if (value instanceof Model) {
|
2022-01-25 17:08:35 +08:00
|
|
|
await model[setAccessor](value, { context, transaction });
|
2021-09-27 15:28:32 +08:00
|
|
|
model.setDataValue(key, value);
|
2022-04-12 00:14:33 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
if (!options.transaction) {
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
return true;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
const createAccessor = association.accessors.create;
|
2021-09-27 15:28:32 +08:00
|
|
|
let dataKey: string;
|
2021-09-23 00:16:04 +08:00
|
|
|
let M: ModelCtor<Model>;
|
|
|
|
if (association.associationType === 'BelongsTo') {
|
2022-02-23 18:18:38 +08:00
|
|
|
M = association.target as ModelCtor<Model>;
|
2021-09-23 00:16:04 +08:00
|
|
|
// @ts-ignore
|
2021-09-27 15:28:32 +08:00
|
|
|
dataKey = association.targetKey;
|
|
|
|
} else {
|
2022-02-23 18:18:38 +08:00
|
|
|
M = association.source as ModelCtor<Model>;
|
2021-09-27 15:28:32 +08:00
|
|
|
dataKey = M.primaryKeyAttribute;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
if (isStringOrNumber(value[dataKey])) {
|
2021-09-23 00:16:04 +08:00
|
|
|
let instance: any = await M.findOne({
|
|
|
|
where: {
|
2021-09-27 15:28:32 +08:00
|
|
|
[dataKey]: value[dataKey],
|
2021-09-23 00:16:04 +08:00
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
2021-12-08 13:00:09 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
if (instance) {
|
2022-01-25 17:08:35 +08:00
|
|
|
await model[setAccessor](instance, { context, transaction });
|
2021-12-08 13:00:09 +08:00
|
|
|
|
|
|
|
if (updateAssociationValues.includes(key)) {
|
|
|
|
await instance.update(value, { ...options, transaction });
|
|
|
|
}
|
|
|
|
|
2022-03-06 19:12:44 +08:00
|
|
|
await updateAssociations(instance, value, {
|
|
|
|
...options,
|
|
|
|
transaction,
|
|
|
|
associationContext: association,
|
|
|
|
updateAssociationValues: keys,
|
|
|
|
});
|
2021-09-27 15:28:32 +08:00
|
|
|
model.setDataValue(key, instance);
|
|
|
|
if (!options.transaction) {
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
return true;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2022-01-25 17:08:35 +08:00
|
|
|
const instance = await model[createAccessor](value, { context, transaction });
|
2022-03-06 19:12:44 +08:00
|
|
|
await updateAssociations(instance, value, {
|
|
|
|
...options,
|
|
|
|
transaction,
|
|
|
|
associationContext: association,
|
|
|
|
updateAssociationValues: keys,
|
|
|
|
});
|
2021-09-27 15:28:32 +08:00
|
|
|
model.setDataValue(key, instance);
|
|
|
|
// @ts-ignore
|
|
|
|
if (association.targetKey) {
|
|
|
|
model.setDataValue(association.foreignKey, instance[dataKey]);
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
|
|
|
if (!options.transaction) {
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (!options.transaction) {
|
|
|
|
await transaction.rollback();
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* update multiple association of model by value
|
|
|
|
* @param model
|
|
|
|
* @param key
|
|
|
|
* @param value
|
|
|
|
* @param options
|
|
|
|
*/
|
2021-12-08 13:00:09 +08:00
|
|
|
export async function updateMultipleAssociation(
|
|
|
|
model: Model,
|
|
|
|
key: string,
|
|
|
|
value: any,
|
|
|
|
options: UpdateAssociationOptions = {},
|
|
|
|
) {
|
2021-12-06 21:23:34 +08:00
|
|
|
const association = <BelongsToMany | HasMany>modelAssociationByKey(model, key);
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (!association) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2022-01-25 17:08:35 +08:00
|
|
|
const { context, updateAssociationValues = [], transaction = await model.sequelize.transaction() } = options;
|
2021-12-08 13:00:09 +08:00
|
|
|
const keys = getKeysByPrefix(updateAssociationValues, key);
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
try {
|
|
|
|
const setAccessor = association.accessors.set;
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
const createAccessor = association.accessors.create;
|
|
|
|
if (isUndefinedOrNull(value)) {
|
2022-01-25 17:08:35 +08:00
|
|
|
await model[setAccessor](null, { transaction, context });
|
2021-09-27 15:28:32 +08:00
|
|
|
model.setDataValue(key, null);
|
|
|
|
return;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (isStringOrNumber(value)) {
|
2022-01-25 17:08:35 +08:00
|
|
|
await model[setAccessor](value, { transaction, context });
|
2021-09-27 15:28:32 +08:00
|
|
|
return;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (!Array.isArray(value)) {
|
|
|
|
value = [value];
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
const list1 = []; // to be setted
|
|
|
|
const list2 = []; // to be added
|
|
|
|
for (const item of value) {
|
|
|
|
if (isUndefinedOrNull(item)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isStringOrNumber(item)) {
|
|
|
|
list1.push(item);
|
|
|
|
} else if (item instanceof Model) {
|
|
|
|
list1.push(item);
|
|
|
|
} else if (item.sequelize) {
|
|
|
|
list1.push(item);
|
|
|
|
} else if (typeof item === 'object') {
|
|
|
|
list2.push(item);
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
// associate targets in lists1
|
2022-01-25 17:08:35 +08:00
|
|
|
await model[setAccessor](list1, { transaction, context });
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
const list3 = [];
|
2021-09-23 00:16:04 +08:00
|
|
|
for (const item of list2) {
|
|
|
|
const pk = association.target.primaryKeyAttribute;
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
const through = (<any>association).through ? (<any>association).through.model.name : null;
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
const accessorOptions = {
|
2022-01-25 17:08:35 +08:00
|
|
|
context,
|
2021-12-06 21:12:54 +08:00
|
|
|
transaction,
|
|
|
|
};
|
|
|
|
|
|
|
|
const throughValue = item[through];
|
|
|
|
|
|
|
|
if (throughValue) {
|
|
|
|
accessorOptions['through'] = throughValue;
|
|
|
|
}
|
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
if (isUndefinedOrNull(item[pk])) {
|
2021-12-06 21:12:54 +08:00
|
|
|
// create new record
|
|
|
|
const instance = await model[createAccessor](item, accessorOptions);
|
2022-03-06 19:12:44 +08:00
|
|
|
await updateAssociations(instance, item, {
|
|
|
|
...options,
|
|
|
|
transaction,
|
|
|
|
associationContext: association,
|
|
|
|
updateAssociationValues: keys,
|
|
|
|
});
|
2021-09-27 15:28:32 +08:00
|
|
|
list3.push(instance);
|
2021-09-23 00:16:04 +08:00
|
|
|
} else {
|
2021-12-06 21:12:54 +08:00
|
|
|
// set & update record
|
2022-02-23 18:18:38 +08:00
|
|
|
const instance = await association.target.findByPk<any>(item[pk], {
|
2021-12-06 21:12:54 +08:00
|
|
|
transaction,
|
|
|
|
});
|
2021-09-23 00:16:04 +08:00
|
|
|
const addAccessor = association.accessors.add;
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
await model[addAccessor](item[pk], accessorOptions);
|
2021-12-08 13:00:09 +08:00
|
|
|
if (updateAssociationValues.includes(key)) {
|
|
|
|
await instance.update(item, { ...options, transaction });
|
|
|
|
}
|
2022-03-06 19:12:44 +08:00
|
|
|
await updateAssociations(instance, item, {
|
|
|
|
...options,
|
|
|
|
transaction,
|
|
|
|
associationContext: association,
|
|
|
|
updateAssociationValues: keys,
|
|
|
|
});
|
2021-09-27 15:28:32 +08:00
|
|
|
list3.push(instance);
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
model.setDataValue(key, list1.concat(list3));
|
2021-09-23 00:16:04 +08:00
|
|
|
if (!options.transaction) {
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|