diff --git a/packages/core/actions/src/actions/create.ts b/packages/core/actions/src/actions/create.ts index 34c00da40..2a08776de 100644 --- a/packages/core/actions/src/actions/create.ts +++ b/packages/core/actions/src/actions/create.ts @@ -12,6 +12,7 @@ export async function create(ctx: Context, next) { updateAssociationValues, context: ctx, }); + ctx.body = instance; await next(); diff --git a/packages/core/database/src/__tests__/relation-repository/belongs-to-many-repository.test.ts b/packages/core/database/src/__tests__/relation-repository/belongs-to-many-repository.test.ts index 98799a82f..c55f09eee 100644 --- a/packages/core/database/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +++ b/packages/core/database/src/__tests__/relation-repository/belongs-to-many-repository.test.ts @@ -235,6 +235,29 @@ describe('belongs to many', () => { expect(findFilterResult[0].name).toEqual('t2'); }); + test('create with array', async () => { + const p1 = await Post.repository.create({ + values: { + title: 'p1', + }, + }); + + const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id); + + const results = await PostTagRepository.create({ + values: [ + { + name: 't1', + }, + { + name: 't2', + }, + ], + }); + + expect(results.length).toEqual(2); + }); + test('find and count', async () => { const p1 = await Post.repository.create({ values: { diff --git a/packages/core/database/src/__tests__/relation-repository/has-many-repository.test.ts b/packages/core/database/src/__tests__/relation-repository/has-many-repository.test.ts index dbb342cd7..659c4a70b 100644 --- a/packages/core/database/src/__tests__/relation-repository/has-many-repository.test.ts +++ b/packages/core/database/src/__tests__/relation-repository/has-many-repository.test.ts @@ -203,6 +203,26 @@ describe('has many repository', () => { expect(post.userId).toEqual(u1.id); }); + test('create with array', async () => { + const u1 = await User.repository.create({ + values: { name: 'u1' }, + }); + + const UserPostRepository = new HasManyRepository(User, 'posts', u1.id); + const posts = await UserPostRepository.create({ + values: [ + { + title: 't1', + }, + { + title: 't2', + }, + ], + }); + + expect(posts.length).toEqual(2); + }); + test('update', async () => { const u1 = await User.repository.create({ values: { name: 'u1' }, diff --git a/packages/core/database/src/__tests__/repository.test.ts b/packages/core/database/src/__tests__/repository.test.ts index 59cd7aa27..6af56a3ff 100644 --- a/packages/core/database/src/__tests__/repository.test.ts +++ b/packages/core/database/src/__tests__/repository.test.ts @@ -234,6 +234,21 @@ describe('repository.create', () => { const comments = await Comment.model.findAll(); expect(comments.map((m) => m.get('postId'))).toEqual([post.get('id'), post.get('id'), post.get('id')]); }); + + it('can create with array of values', async () => { + const users = await User.repository.create({ + values: [ + { + name: 'u1', + }, + { + name: 'u2', + }, + ], + }); + + expect(users.length).toEqual(2); + }); }); describe('repository.update', () => { diff --git a/packages/core/database/src/decorators/transaction-decorator.ts b/packages/core/database/src/decorators/transaction-decorator.ts index 44be03bf0..989ec637f 100644 --- a/packages/core/database/src/decorators/transaction-decorator.ts +++ b/packages/core/database/src/decorators/transaction-decorator.ts @@ -37,13 +37,12 @@ export function transactionWrapperBuilder(transactionGenerator) { throw new Error(`please provide transactionInjector for ${name} call`); } - const results = await oldValue.apply(this, [callArguments]); + const results = await oldValue.call(this, callArguments); await transaction.commit(); return results; } catch (err) { - // console.error({ err }); await transaction.rollback(); throw err; } diff --git a/packages/core/database/src/relation-repository/belongs-to-many-repository.ts b/packages/core/database/src/relation-repository/belongs-to-many-repository.ts index e45fd5369..c91144783 100644 --- a/packages/core/database/src/relation-repository/belongs-to-many-repository.ts +++ b/packages/core/database/src/relation-repository/belongs-to-many-repository.ts @@ -31,6 +31,10 @@ interface IBelongsToManyRepository { export class BelongsToManyRepository extends MultipleRelationRepository implements IBelongsToManyRepository { @transaction() async create(options?: CreateBelongsToManyOptions): Promise { + if (Array.isArray(options.values)) { + return Promise.all(options.values.map((record) => this.create({ ...options, values: record }))); + } + const transaction = await this.getTransaction(options); const createAccessor = this.accessors().create; diff --git a/packages/core/database/src/relation-repository/relation-repository.ts b/packages/core/database/src/relation-repository/relation-repository.ts index 3193692f4..84ce40e3d 100644 --- a/packages/core/database/src/relation-repository/relation-repository.ts +++ b/packages/core/database/src/relation-repository/relation-repository.ts @@ -2,12 +2,12 @@ import lodash from 'lodash'; import { Association, BelongsTo, BelongsToMany, HasMany, HasOne, ModelCtor, Transaction } from 'sequelize'; import { Collection } from '../collection'; import Database from '../database'; +import { transactionWrapperBuilder } from '../decorators/transaction-decorator'; import { RelationField } from '../fields/relation-field'; import FilterParser from '../filter-parser'; import { Model } from '../model'; import { OptionsParser } from '../options-parser'; import { CreateOptions, Filter, FindOptions } from '../repository'; -import { transactionWrapperBuilder } from '../decorators/transaction-decorator'; import { updateAssociations } from '../update-associations'; import { UpdateGuard } from '../update-guard'; @@ -53,7 +53,11 @@ export abstract class RelationRepository { } @transaction() - async create(options?: CreateOptions): Promise { + async create(options?: CreateOptions): Promise { + if (Array.isArray(options.values)) { + return Promise.all(options.values.map((record) => this.create({ ...options, values: record }))); + } + const createAccessor = this.accessors().create; const guard = UpdateGuard.fromOptions(this.targetModel, options); diff --git a/packages/core/database/src/repository.ts b/packages/core/database/src/repository.ts index 6ae282794..38b514440 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -100,7 +100,7 @@ interface FindAndCountOptions extends Omit(options: CreateOptions): Promise { + async create(options: CreateOptions) { + if (Array.isArray(options.values)) { + return this.createMany({ + ...options, + records: options.values, + }); + } + const transaction = await this.getTransaction(options); const guard = UpdateGuard.fromOptions(this.model, { ...options, action: 'create' }); @@ -356,6 +363,7 @@ export class Repository