From 2ff3069d23aa688cea5a3bd204236f2f2f64c201 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 12 Apr 2019 01:52:25 +0900 Subject: トランザクションを使うようにしたり MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/api/private/signup.ts | 50 ++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'src/server/api/private') diff --git a/src/server/api/private/signup.ts b/src/server/api/private/signup.ts index 5ed25fa411..ed4a533dec 100644 --- a/src/server/api/private/signup.ts +++ b/src/server/api/private/signup.ts @@ -12,6 +12,7 @@ import { User } from '../../../models/entities/user'; import { UserKeypair } from '../../../models/entities/user-keypair'; import { toPuny } from '../../../misc/convert-host'; import { UserProfile } from '../../../models/entities/user-profile'; +import { getConnection } from 'typeorm'; export default async (ctx: Koa.BaseContext) => { const body = ctx.request.body as any; @@ -99,28 +100,33 @@ export default async (ctx: Koa.BaseContext) => { e ? j(e) : s([publicKey, privateKey]) )); - const account = await Users.save({ - id: genId(), - createdAt: new Date(), - username: username, - usernameLower: username.toLowerCase(), - host: toPuny(host), - token: secret, - isAdmin: config.autoAdmin && usersCount === 0, - } as User); - - await UserKeypairs.save({ - publicKey: keyPair[0], - privateKey: keyPair[1], - userId: account.id - } as UserKeypair); - - await UserProfiles.save({ - userId: account.id, - autoAcceptFollowed: true, - autoWatch: false, - password: hash, - } as Partial); + let account: User; + + // Start transaction + await getConnection().transaction(async transactionalEntityManager => { + account = await transactionalEntityManager.save(new User({ + id: genId(), + createdAt: new Date(), + username: username, + usernameLower: username.toLowerCase(), + host: toPuny(host), + token: secret, + isAdmin: config.autoAdmin && usersCount === 0, + })); + + await transactionalEntityManager.save(new UserKeypair({ + publicKey: keyPair[0], + privateKey: keyPair[1], + userId: account.id + })); + + await transactionalEntityManager.save(new UserProfile({ + userId: account.id, + autoAcceptFollowed: true, + autoWatch: false, + password: hash, + })); + }); usersChart.update(account, true); -- cgit v1.2.3-freya