refactor(server): use insert instead of save

This commit is contained in:
syuilo 2022-01-03 02:20:30 +09:00
parent 7b838bf462
commit 21a7cb9546
9 changed files with 19 additions and 18 deletions

View File

@ -46,13 +46,13 @@ export async function importUserLists(job: Bull.Job<DbUserImportJobData>, done:
}); });
if (list == null) { if (list == null) {
list = await UserLists.save({ list = await UserLists.insert({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
userId: user.id, userId: user.id,
name: listName, name: listName,
userIds: [], userIds: [],
}); }).then(x => UserLists.findOneOrFail(x.identifiers[0]));
} }
let target = isSelfHost(host!) ? await Users.findOne({ let target = isSelfHost(host!) ? await Users.findOne({

View File

@ -342,7 +342,7 @@ export async function extractEmojis(tags: IObject | IObject[], host: string): Pr
logger.info(`register emoji host=${host}, name=${name}`); logger.info(`register emoji host=${host}, name=${name}`);
return await Emojis.save({ return await Emojis.insert({
id: genId(), id: genId(),
host, host,
name, name,
@ -350,6 +350,6 @@ export async function extractEmojis(tags: IObject | IObject[], host: string): Pr
url: tag.icon!.url, url: tag.icon!.url,
updatedAt: new Date(), updatedAt: new Date(),
aliases: [], aliases: [],
} as Partial<Emoji>); } as Partial<Emoji>).then(x => Emojis.findOneOrFail(x.identifiers[0]));
})); }));
} }

View File

@ -29,14 +29,14 @@ export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) {
(async () => { (async () => {
// Append signin history // Append signin history
const record = await Signins.save({ const record = await Signins.insert({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
userId: user.id, userId: user.id,
ip: ctx.ip, ip: ctx.ip,
headers: ctx.headers, headers: ctx.headers,
success: true, success: true,
}); }).then(x => Signins.findOneOrFail(x.identifiers[0]));
// Publish signin event // Publish signin event
publishMainStream(user.id, 'signin', await Signins.pack(record)); publishMainStream(user.id, 'signin', await Signins.pack(record));

View File

@ -59,14 +59,14 @@ export const meta = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, async (ps) => { export default define(meta, async (ps) => {
const announcement = await Announcements.save({ const announcement = await Announcements.insert({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
updatedAt: null, updatedAt: null,
title: ps.title, title: ps.title,
text: ps.text, text: ps.text,
imageUrl: ps.imageUrl, imageUrl: ps.imageUrl,
}); }).then(x => Announcements.findOneOrFail(x.identifiers[0]));
return announcement; return announcement;
}); });

View File

@ -38,7 +38,7 @@ export default define(meta, async (ps, me) => {
const name = file.name.split('.')[0].match(/^[a-z0-9_]+$/) ? file.name.split('.')[0] : `_${rndstr('a-z0-9', 8)}_`; const name = file.name.split('.')[0].match(/^[a-z0-9_]+$/) ? file.name.split('.')[0] : `_${rndstr('a-z0-9', 8)}_`;
const emoji = await Emojis.save({ const emoji = await Emojis.insert({
id: genId(), id: genId(),
updatedAt: new Date(), updatedAt: new Date(),
name: name, name: name,
@ -47,7 +47,7 @@ export default define(meta, async (ps, me) => {
aliases: [], aliases: [],
url: file.url, url: file.url,
type: file.type, type: file.type,
}); }).then(x => Emojis.findOneOrFail(x.identifiers[0]));
await getConnection().queryResultCache!.remove(['meta_emojis']); await getConnection().queryResultCache!.remove(['meta_emojis']);

View File

@ -57,12 +57,12 @@ export default define(meta, async (ps) => {
const token = uuid(); const token = uuid();
// Create session token document // Create session token document
const doc = await AuthSessions.save({ const doc = await AuthSessions.insert({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
appId: app.id, appId: app.id,
token: token, token: token,
}); }).then(x => AuthSessions.findOneOrFail(x.identifiers[0]));
return { return {
token: doc.token, token: doc.token,

View File

@ -107,7 +107,7 @@ export default define(meta, async (ps, user) => {
} }
}); });
const page = await Pages.save(new Page({ const page = await Pages.insert(new Page({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
@ -123,7 +123,7 @@ export default define(meta, async (ps, user) => {
alignCenter: ps.alignCenter, alignCenter: ps.alignCenter,
hideTitleWhenPinned: ps.hideTitleWhenPinned, hideTitleWhenPinned: ps.hideTitleWhenPinned,
font: ps.font, font: ps.font,
})); })).then(x => Pages.findOneOrFail(x.identifiers[0]));
return await Pages.pack(page); return await Pages.pack(page);
}); });

View File

@ -62,7 +62,7 @@ export default define(meta, async (ps, me) => {
throw new ApiError(meta.errors.cannotReportAdmin); throw new ApiError(meta.errors.cannotReportAdmin);
} }
const report = await AbuseUserReports.save({ const report = await AbuseUserReports.insert({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
targetUserId: user.id, targetUserId: user.id,
@ -70,7 +70,7 @@ export default define(meta, async (ps, me) => {
reporterId: me.id, reporterId: me.id,
reporterHost: null, reporterHost: null,
comment: ps.comment, comment: ps.comment,
}); }).then(x => AbuseUserReports.findOneOrFail(x.identifiers[0]));
// Publish event to moderators // Publish event to moderators
setTimeout(async () => { setTimeout(async () => {

View File

@ -20,7 +20,7 @@ export async function createNotification(
const isMuted = profile?.mutingNotificationTypes.includes(type); const isMuted = profile?.mutingNotificationTypes.includes(type);
// Create notification // Create notification
const notification = await Notifications.save({ const notification = await Notifications.insert({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
notifieeId: notifieeId, notifieeId: notifieeId,
@ -28,7 +28,8 @@ export async function createNotification(
// 相手がこの通知をミュートしているようなら、既読を予めつけておく // 相手がこの通知をミュートしているようなら、既読を予めつけておく
isRead: isMuted, isRead: isMuted,
...data, ...data,
} as Partial<Notification>); } as Partial<Notification>)
.then(x => Notifications.findOneOrFail(x.identifiers[0]));
const packed = await Notifications.pack(notification, {}); const packed = await Notifications.pack(notification, {});