mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-07 14:39:25 +08:00
fix: the firstOrCreate and updateOrCreate methods of the repository lose context (#5973)
This commit is contained in:
parent
98fb2063bc
commit
e47f0ff342
@ -18,8 +18,8 @@ import { Model } from '../model';
|
||||
import { OptionsParser } from '../options-parser';
|
||||
import { updateAssociations } from '../update-associations';
|
||||
import { UpdateGuard } from '../update-guard';
|
||||
import { CreateOptions, Filter, FindOptions, TargetKey, UpdateOptions, FirstOrCreateOptions } from './types';
|
||||
import { valuesToFilter } from '../utils/filter-utils';
|
||||
import { CreateOptions, Filter, FindOptions, FirstOrCreateOptions, TargetKey, UpdateOptions } from './types';
|
||||
|
||||
export const transaction = transactionWrapperBuilder(function () {
|
||||
return this.sourceCollection.model.sequelize.transaction();
|
||||
@ -143,24 +143,24 @@ export abstract class RelationRepository {
|
||||
|
||||
@transaction()
|
||||
async firstOrCreate(options: FirstOrCreateOptions) {
|
||||
const { filterKeys, values, transaction, hooks } = options;
|
||||
const { filterKeys, values, transaction, hooks, context } = options;
|
||||
const filter = valuesToFilter(values, filterKeys);
|
||||
|
||||
const instance = await this.findOne({ filter, transaction });
|
||||
const instance = await this.findOne({ filter, transaction, context });
|
||||
|
||||
if (instance) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
return this.create({ values, transaction, hooks });
|
||||
return this.create({ values, transaction, hooks, context });
|
||||
}
|
||||
|
||||
@transaction()
|
||||
async updateOrCreate(options: FirstOrCreateOptions) {
|
||||
const { filterKeys, values, transaction, hooks } = options;
|
||||
const { filterKeys, values, transaction, hooks, context } = options;
|
||||
const filter = valuesToFilter(values, filterKeys);
|
||||
|
||||
const instance = await this.findOne({ filter, transaction });
|
||||
const instance = await this.findOne({ filter, transaction, context });
|
||||
|
||||
if (instance) {
|
||||
return await this.update({
|
||||
@ -170,10 +170,11 @@ export abstract class RelationRepository {
|
||||
values,
|
||||
transaction,
|
||||
hooks,
|
||||
context,
|
||||
});
|
||||
}
|
||||
|
||||
return this.create({ values, transaction, hooks });
|
||||
return this.create({ values, transaction, hooks, context });
|
||||
}
|
||||
|
||||
@transaction()
|
||||
|
@ -7,12 +7,12 @@
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { AssociationKeysToBeUpdate, BlackList, Values, WhiteList } from '@nocobase/database';
|
||||
import { Transaction } from 'sequelize';
|
||||
import {
|
||||
CreateOptions as SequelizeCreateOptions,
|
||||
UpdateOptions as SequelizeUpdateOptions,
|
||||
} from 'sequelize/types/model';
|
||||
import { AssociationKeysToBeUpdate, BlackList, Values, WhiteList } from '@nocobase/database';
|
||||
|
||||
export type TargetKey = string | number | { [key: string]: any };
|
||||
|
||||
@ -80,6 +80,7 @@ export interface FirstOrCreateOptions extends CommonOptions {
|
||||
filterKeys: string[];
|
||||
values: any;
|
||||
hooks?: boolean;
|
||||
context?: any;
|
||||
}
|
||||
|
||||
export interface ThroughValues {
|
||||
|
@ -7,24 +7,23 @@
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { flatten } from 'flat';
|
||||
import { isValidFilter } from '@nocobase/utils';
|
||||
import lodash from 'lodash';
|
||||
import {
|
||||
Association,
|
||||
BulkCreateOptions,
|
||||
CountOptions as SequelizeCountOptions,
|
||||
CreateOptions as SequelizeCreateOptions,
|
||||
DestroyOptions as SequelizeDestroyOptions,
|
||||
FindAndCountOptions as SequelizeAndCountOptions,
|
||||
FindOptions as SequelizeFindOptions,
|
||||
ModelStatic,
|
||||
Op,
|
||||
Sequelize,
|
||||
Transactionable,
|
||||
FindAndCountOptions as SequelizeAndCountOptions,
|
||||
CountOptions as SequelizeCountOptions,
|
||||
CreateOptions as SequelizeCreateOptions,
|
||||
DestroyOptions as SequelizeDestroyOptions,
|
||||
FindOptions as SequelizeFindOptions,
|
||||
UpdateOptions as SequelizeUpdateOptions,
|
||||
Transactionable,
|
||||
WhereOperators,
|
||||
} from 'sequelize';
|
||||
import { isValidFilter } from '@nocobase/utils';
|
||||
|
||||
import { Collection } from './collection';
|
||||
import { Database } from './database';
|
||||
@ -38,6 +37,7 @@ import FilterParser from './filter-parser';
|
||||
import { Model } from './model';
|
||||
import operators from './operators';
|
||||
import { OptionsParser } from './options-parser';
|
||||
import { BelongsToArrayRepository } from './relation-repository/belongs-to-array-repository';
|
||||
import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
|
||||
import { BelongsToRepository } from './relation-repository/belongs-to-repository';
|
||||
import { HasManyRepository } from './relation-repository/hasmany-repository';
|
||||
@ -45,7 +45,6 @@ import { HasOneRepository } from './relation-repository/hasone-repository';
|
||||
import { RelationRepository } from './relation-repository/relation-repository';
|
||||
import { updateAssociations, updateModelByValues } from './update-associations';
|
||||
import { UpdateGuard } from './update-guard';
|
||||
import { BelongsToArrayRepository } from './relation-repository/belongs-to-array-repository';
|
||||
import { valuesToFilter } from './utils/filter-utils';
|
||||
|
||||
const debug = require('debug')('noco-database');
|
||||
@ -239,6 +238,7 @@ export interface FirstOrCreateOptions extends Transactionable {
|
||||
filterKeys: string[];
|
||||
values?: Values;
|
||||
hooks?: boolean;
|
||||
context?: any;
|
||||
}
|
||||
|
||||
export class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> {
|
||||
@ -479,23 +479,23 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
* Get the first record matching the attributes or create it.
|
||||
*/
|
||||
async firstOrCreate(options: FirstOrCreateOptions) {
|
||||
const { filterKeys, values, transaction, hooks } = options;
|
||||
const { filterKeys, values, transaction, hooks, context } = options;
|
||||
const filter = Repository.valuesToFilter(values, filterKeys);
|
||||
|
||||
const instance = await this.findOne({ filter, transaction });
|
||||
const instance = await this.findOne({ filter, transaction, context });
|
||||
|
||||
if (instance) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
return this.create({ values, transaction, hooks });
|
||||
return this.create({ values, transaction, hooks, context });
|
||||
}
|
||||
|
||||
async updateOrCreate(options: FirstOrCreateOptions) {
|
||||
const { filterKeys, values, transaction, hooks } = options;
|
||||
const { filterKeys, values, transaction, hooks, context } = options;
|
||||
const filter = Repository.valuesToFilter(values, filterKeys);
|
||||
|
||||
const instance = await this.findOne({ filter, transaction });
|
||||
const instance = await this.findOne({ filter, transaction, context });
|
||||
|
||||
if (instance) {
|
||||
return await this.update({
|
||||
@ -503,10 +503,11 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
values,
|
||||
transaction,
|
||||
hooks,
|
||||
context,
|
||||
});
|
||||
}
|
||||
|
||||
return this.create({ values, transaction, hooks });
|
||||
return this.create({ values, transaction, hooks, context });
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user