refactor: migrate to typeorm 3.0 (#8443)

* wip

* wip

* wip

* Update following.ts

* wip

* wip

* wip

* Update resolve-user.ts

* maxQueryExecutionTime

* wip

* wip
This commit is contained in:
syuilo
2022-03-26 15:34:00 +09:00
committed by GitHub
parent 41c87074e6
commit 1c67c26bd8
325 changed files with 1314 additions and 1494 deletions

View File

@ -7,7 +7,7 @@ import { Notes, Users } from '@/models/index.js';
* Get note for API processing
*/
export async function getNote(noteId: Note['id']) {
const note = await Notes.findOne(noteId);
const note = await Notes.findOneBy({ id: noteId });
if (note == null) {
throw new IdentifiableError('9725d0ce-ba28-4dde-95a7-2cbb2c15de24', 'No such note.');
@ -20,7 +20,7 @@ export async function getNote(noteId: Note['id']) {
* Get user for API processing
*/
export async function getUser(userId: User['id']) {
const user = await Users.findOne(userId);
const user = await Users.findOneBy({ id: userId });
if (user == null) {
throw new IdentifiableError('15348ddd-432d-49c2-8a5a-8069753becff', 'No such user.');

View File

@ -11,7 +11,7 @@ export async function injectFeatured(timeline: Note[], user?: User | null) {
if (timeline.length < 5) return;
if (user) {
const profile = await UserProfiles.findOneOrFail(user.id);
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
if (!profile.injectFeaturedNote) return;
}

View File

@ -8,7 +8,7 @@ export async function injectPromo(timeline: Note[], user?: User | null) {
// TODO: readやexpireフィルタはクエリ側でやる
const reads = user ? await PromoReads.find({
const reads = user ? await PromoReads.findBy({
userId: user.id,
}) : [];
@ -22,10 +22,10 @@ export async function injectPromo(timeline: Note[], user?: User | null) {
// Pick random promo
const promo = promos[Math.floor(Math.random() * promos.length)];
const note = await Notes.findOneOrFail(promo.noteId);
const note = await Notes.findOneByOrFail({ id: promo.noteId });
// Join
note.user = await Users.findOneOrFail(note.userId);
note.user = await Users.findOneByOrFail({ id: note.userId });
(note as any)._prId_ = rndstr('a-z0-9', 8);

View File

@ -23,7 +23,7 @@ export async function readUserMessagingMessage(
) {
if (messageIds.length === 0) return;
const messages = await MessagingMessages.find({
const messages = await MessagingMessages.findBy({
id: In(messageIds),
});
@ -64,7 +64,7 @@ export async function readGroupMessagingMessage(
if (messageIds.length === 0) return;
// check joined
const joining = await UserGroupJoinings.findOne({
const joining = await UserGroupJoinings.findOneBy({
userId: userId,
userGroupId: groupId,
});
@ -73,7 +73,7 @@ export async function readGroupMessagingMessage(
throw new IdentifiableError('930a270c-714a-46b2-b776-ad27276dc569', 'Access denied (group).');
}
const messages = await MessagingMessages.find({
const messages = await MessagingMessages.findBy({
id: In(messageIds),
});

View File

@ -36,7 +36,7 @@ export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) {
ip: ctx.ip,
headers: ctx.headers,
success: true,
}).then(x => Signins.findOneOrFail(x.identifiers[0]));
}).then(x => Signins.findOneByOrFail(x.identifiers[0]));
// Publish signin event
publishMainStream(user.id, 'signin', await Signins.pack(record));

View File

@ -4,12 +4,13 @@ import generateUserToken from './generate-native-user-token.js';
import { User } from '@/models/entities/user.js';
import { Users, UsedUsernames } from '@/models/index.js';
import { UserProfile } from '@/models/entities/user-profile.js';
import { getConnection } from 'typeorm';
import { IsNull } from 'typeorm';
import { genId } from '@/misc/gen-id.js';
import { toPunyNullable } from '@/misc/convert-host.js';
import { UserKeypair } from '@/models/entities/user-keypair.js';
import { usersChart } from '@/services/chart/index.js';
import { UsedUsername } from '@/models/entities/used-username.js';
import { db } from '@/db/postgre.js';
export async function signup(opts: {
username: User['username'];
@ -40,12 +41,12 @@ export async function signup(opts: {
const secret = generateUserToken();
// Check username duplication
if (await Users.findOne({ usernameLower: username.toLowerCase(), host: null })) {
if (await Users.findOneBy({ usernameLower: username.toLowerCase(), host: IsNull() })) {
throw new Error('DUPLICATED_USERNAME');
}
// Check deleted username duplication
if (await UsedUsernames.findOne({ username: username.toLowerCase() })) {
if (await UsedUsernames.findOneBy({ username: username.toLowerCase() })) {
throw new Error('USED_USERNAME');
}
@ -69,10 +70,10 @@ export async function signup(opts: {
let account!: User;
// Start transaction
await getConnection().transaction(async transactionalEntityManager => {
const exist = await transactionalEntityManager.findOne(User, {
await db.transaction(async transactionalEntityManager => {
const exist = await transactionalEntityManager.findOneBy(User, {
usernameLower: username.toLowerCase(),
host: null,
host: IsNull(),
});
if (exist) throw new Error(' the username is already used');
@ -84,8 +85,8 @@ export async function signup(opts: {
usernameLower: username.toLowerCase(),
host: toPunyNullable(host),
token: secret,
isAdmin: (await Users.count({
host: null,
isAdmin: (await Users.countBy({
host: IsNull(),
})) === 0,
}));