Skip to content

Commit

Permalink
chore: clean redundant execute (#936)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Jan 18, 2025
1 parent b9f639b commit b71e198
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 233 deletions.
21 changes: 9 additions & 12 deletions lib/auth/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { create, get, revoke } from './session.ts';
import { sql } from 'drizzle-orm';

beforeEach(async () => {
await db.delete(chiiOsWebSessions).execute();
await db.delete(chiiOsWebSessions);
});

afterEach(async () => {
await db.delete(chiiOsWebSessions).execute();
await db.delete(chiiOsWebSessions);
});

test('should create and get session', async () => {
Expand All @@ -41,16 +41,13 @@ test('should create and get session', async () => {

test('should revoke session', async () => {
const token = 'fake-random-session-token';
await db
.insert(chiiOsWebSessions)
.values({
key: token,
value: Buffer.from(''),
userID: 0,
createdAt: DateTime.now().toUnixInteger(),
expiredAt: DateTime.now().toUnixInteger() + 60 * 60 * 242 * 30,
})
.execute();
await db.insert(chiiOsWebSessions).values({
key: token,
value: Buffer.from(''),
userID: 0,
createdAt: DateTime.now().toUnixInteger(),
expiredAt: DateTime.now().toUnixInteger() + 60 * 60 * 242 * 30,
});

await revoke(token);
const session = await db.query.chiiOsWebSessions.findFirst({
Expand Down
3 changes: 1 addition & 2 deletions lib/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ export async function revoke(sessionID: string) {
await db
.update(chiiOsWebSessions)
.set({ expiredAt: DateTime.now().toUnixInteger() })
.where(op.eq(chiiOsWebSessions.key, sessionID))
.execute();
.where(op.eq(chiiOsWebSessions.key, sessionID));

await sessionCache.del(sessionID);
}
2 changes: 1 addition & 1 deletion lib/like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function fetchTopicReactions(
id: number,
uid: number,
): Promise<Record<number, Reaction[]>> {
const data = await db.select().from(chiiLikes).where(op.eq(chiiLikes.mainID, id)).execute();
const data = await db.select().from(chiiLikes).where(op.eq(chiiLikes.mainID, id));

const r = lo.groupBy(data, (x) => x.relatedID);

Expand Down
6 changes: 2 additions & 4 deletions lib/rev/ep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ test('get episode rev', async () => {
op.eq(schema.chiiRevHistory.revMid, 8),
op.eq(schema.chiiRevHistory.revType, RevType.episodeEdit),
),
)
.execute();
);

await db.transaction(async (t) => {
await expect(
Expand Down Expand Up @@ -47,8 +46,7 @@ test('get episode rev', async () => {
op.eq(schema.chiiRevHistory.revMid, 8),
op.eq(schema.chiiRevHistory.revType, RevType.episodeEdit),
),
)
.execute();
);
expect(revs).toHaveLength(1);
expect(revs[0]).toMatchObject({
revMid: 8,
Expand Down
6 changes: 2 additions & 4 deletions lib/rev/ep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export async function pushRev(
op.eq(schema.chiiRevHistory.revMid, episodeID),
op.eq(schema.chiiRevHistory.revType, RevType.episodeEdit),
),
)
.execute();
);
const o = revs.pop();
if (!o) {
return await createRevRecords({
Expand Down Expand Up @@ -74,8 +73,7 @@ async function updatePreviousRevRecords({
const [revText] = await t
.select()
.from(schema.chiiRevText)
.where(op.eq(schema.chiiRevText.revTextId, previous.revTextId))
.execute();
.where(op.eq(schema.chiiRevText.revTextId, previous.revTextId));

if (!revText) {
throw new Error(`RevText not found for ID: ${previous.revTextId}`);
Expand Down
6 changes: 2 additions & 4 deletions lib/subject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ export async function edit({
nsfw,
infobox,
})
.where(op.eq(schema.chiiSubjects.id, subjectID))
.execute();
.where(op.eq(schema.chiiSubjects.id, subjectID));

const d: DATE = date ? DATE.parse(date) : extractDate(w, s.typeID, platform);

Expand All @@ -210,8 +209,7 @@ export async function edit({
year: d.year,
month: d.month,
})
.where(op.eq(schema.chiiSubjectFields.id, subjectID))
.execute();
.where(op.eq(schema.chiiSubjectFields.id, subjectID));
});
}

Expand Down
4 changes: 1 addition & 3 deletions lib/timeline/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export async function getTimelineInbox(
.select({ fid: schema.chiiFriends.fid })
.from(schema.chiiFriends)
.where(op.eq(schema.chiiFriends.uid, uid))
.execute()
.then((data) => data.map((d) => d.fid));
// 对于没有好友的用户,返回全站时间线
if (friendIDs.length === 0) {
Expand All @@ -41,8 +40,7 @@ export async function getTimelineInbox(
.from(schema.chiiTimeline)
.where(conditions.length > 0 ? op.and(...conditions) : undefined)
.orderBy(op.desc(schema.chiiTimeline.id))
.limit(limit)
.execute();
.limit(limit);
ids.push(...data.map((d) => d.id));
if (!until && ids.length > 0) {
// 回填第一页的数据
Expand Down
3 changes: 1 addition & 2 deletions lib/timeline/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ export async function fetchTimelineByIDs(
const data = await db
.select()
.from(schema.chiiTimeline)
.where(op.inArray(schema.chiiTimeline.id, missing))
.execute();
.where(op.inArray(schema.chiiTimeline.id, missing));
for (const d of data) {
const item = await toTimeline(d, allowNsfw);
result[d.id] = item;
Expand Down
3 changes: 1 addition & 2 deletions lib/timeline/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export async function getTimelineUser(
),
)
.orderBy(op.desc(schema.chiiTimeline.id))
.limit(limit)
.execute();
.limit(limit);
ids.push(...data.map((d) => d.id));
if (!until && ids.length > 0) {
// 回填第一页的数据
Expand Down
3 changes: 1 addition & 2 deletions lib/trending/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export async function updateTrendingSubjects(
)
.groupBy(schema.chiiSubjects.id)
.orderBy(op.desc(op.count(schema.chiiSubjects.id)))
.limit(1000)
.execute();
.limit(1000);

const ids = [];
for (const item of data) {
Expand Down
Loading

0 comments on commit b71e198

Please sign in to comment.