fix(database): missing context in hook

This commit is contained in:
chenos 2022-01-25 17:08:35 +08:00
parent a648adace8
commit 6e95278ce4
2 changed files with 26 additions and 19 deletions

View File

@ -1,19 +1,16 @@
import { import {
Sequelize,
ModelCtor,
Model,
DataTypes,
Utils,
Association, Association,
HasOne,
BelongsTo, BelongsTo,
BelongsToMany, BelongsToMany,
HasMany, HasMany,
Transactionable, HasOne,
Hookable, Hookable,
Model,
ModelCtor,
Transactionable
} from 'sequelize'; } from 'sequelize';
import { UpdateGuard } from './update-guard';
import { TransactionAble } from './repository'; import { TransactionAble } from './repository';
import { UpdateGuard } from './update-guard';
function isUndefinedOrNull(value: any) { function isUndefinedOrNull(value: any) {
return typeof value === 'undefined' || value === null; return typeof value === 'undefined' || value === null;
@ -66,6 +63,7 @@ interface UpdateOptions extends TransactionAble {
interface UpdateAssociationOptions extends Transactionable, Hookable { interface UpdateAssociationOptions extends Transactionable, Hookable {
updateAssociationValues?: string[]; updateAssociationValues?: string[];
sourceModel?: Model; sourceModel?: Model;
context?: any;
} }
export async function updateModelByValues(instance: Model, values: UpdateValue, options?: UpdateOptions) { export async function updateModelByValues(instance: Model, values: UpdateValue, options?: UpdateOptions) {
@ -155,6 +153,7 @@ export async function updateAssociations(instance: Model, values: any, options:
await throughModel.update(values[throughModel.name], { await throughModel.update(values[throughModel.name], {
where, where,
context: options.context,
transaction, transaction,
}); });
} }
@ -180,6 +179,9 @@ export async function updateAssociation(
) { ) {
const association = modelAssociationByKey(instance, key); const association = modelAssociationByKey(instance, key);
// @ts-ignore
console.log(key, options.context);
if (!association) { if (!association) {
return false; return false;
} }
@ -217,7 +219,7 @@ export async function updateSingleAssociation(
return false; return false;
} }
const { updateAssociationValues = [], transaction = await model.sequelize.transaction() } = options; const { context, updateAssociationValues = [], transaction = await model.sequelize.transaction() } = options;
const keys = getKeysByPrefix(updateAssociationValues, key); const keys = getKeysByPrefix(updateAssociationValues, key);
try { try {
@ -238,7 +240,7 @@ export async function updateSingleAssociation(
} }
if (isStringOrNumber(value)) { if (isStringOrNumber(value)) {
await model[setAccessor](value, { transaction }); await model[setAccessor](value, { context, transaction });
if (!options.transaction) { if (!options.transaction) {
await transaction.commit(); await transaction.commit();
} }
@ -246,7 +248,7 @@ export async function updateSingleAssociation(
} }
if (value instanceof Model) { if (value instanceof Model) {
await model[setAccessor](value); await model[setAccessor](value, { context, transaction });
model.setDataValue(key, value); model.setDataValue(key, value);
if (!options.transaction) { if (!options.transaction) {
await transaction.commit(); await transaction.commit();
@ -275,7 +277,7 @@ export async function updateSingleAssociation(
}); });
if (instance) { if (instance) {
await model[setAccessor](instance); await model[setAccessor](instance, { context, transaction });
if (updateAssociationValues.includes(key)) { if (updateAssociationValues.includes(key)) {
await instance.update(value, { ...options, transaction }); await instance.update(value, { ...options, transaction });
@ -290,7 +292,7 @@ export async function updateSingleAssociation(
} }
} }
const instance = await model[createAccessor](value, { transaction }); const instance = await model[createAccessor](value, { context, transaction });
await updateAssociations(instance, value, { ...options, transaction, updateAssociationValues: keys }); await updateAssociations(instance, value, { ...options, transaction, updateAssociationValues: keys });
model.setDataValue(key, instance); model.setDataValue(key, instance);
// @ts-ignore // @ts-ignore
@ -331,7 +333,7 @@ export async function updateMultipleAssociation(
return false; return false;
} }
const { updateAssociationValues = [], transaction = await model.sequelize.transaction() } = options; const { context, updateAssociationValues = [], transaction = await model.sequelize.transaction() } = options;
const keys = getKeysByPrefix(updateAssociationValues, key); const keys = getKeysByPrefix(updateAssociationValues, key);
try { try {
@ -339,13 +341,13 @@ export async function updateMultipleAssociation(
const createAccessor = association.accessors.create; const createAccessor = association.accessors.create;
if (isUndefinedOrNull(value)) { if (isUndefinedOrNull(value)) {
await model[setAccessor](null, { transaction }); await model[setAccessor](null, { transaction, context });
model.setDataValue(key, null); model.setDataValue(key, null);
return; return;
} }
if (isStringOrNumber(value)) { if (isStringOrNumber(value)) {
await model[setAccessor](value, { transaction }); await model[setAccessor](value, { transaction, context });
return; return;
} }
@ -371,7 +373,7 @@ export async function updateMultipleAssociation(
} }
// associate targets in lists1 // associate targets in lists1
await model[setAccessor](list1, { transaction }); await model[setAccessor](list1, { transaction, context });
const list3 = []; const list3 = [];
for (const item of list2) { for (const item of list2) {
@ -380,6 +382,7 @@ export async function updateMultipleAssociation(
const through = (<any>association).through ? (<any>association).through.model.name : null; const through = (<any>association).through ? (<any>association).through.model.name : null;
const accessorOptions = { const accessorOptions = {
context,
transaction, transaction,
}; };

View File

@ -4,7 +4,7 @@ export function beforeCreateForChildrenCollection(db: Database) {
const Collection = db.getCollection('collections'); const Collection = db.getCollection('collections');
const Field = db.getCollection('fields'); const Field = db.getCollection('fields');
return async (model, { transaction }) => { return async (model, { transaction, context }) => {
const parentKey = model.get('parentKey'); const parentKey = model.get('parentKey');
if (!parentKey) { if (!parentKey) {
return; return;
@ -19,7 +19,11 @@ export function beforeCreateForChildrenCollection(db: Database) {
}, },
}); });
if (!collection) { if (!collection) {
await Collection.model.create({ name: parentTarget }, { transaction }); await Collection.repository.create({
values: { name: parentTarget },
transaction,
context,
});
} }
}; };
} }