More conversions to Drizzle

This commit is contained in:
Jesse Wierzbinski 2024-04-13 02:24:57 -10:00
parent ad0bf1a350
commit 05e45ff5aa
No known key found for this signature in database
5 changed files with 51 additions and 50 deletions

View file

@ -1,8 +1,9 @@
import { apiRoute, applyConfig } from "@api";
import { errorResponse, jsonResponse } from "@response";
import { client } from "~database/datasource";
import { eq } from "drizzle-orm";
import { userToAPI } from "~database/entities/User";
import { userRelations } from "~database/entities/relations";
import { db } from "~drizzle/db";
import { user } from "~drizzle/schema";
export const meta = applyConfig({
allowedMethods: ["DELETE"],
@ -20,20 +21,16 @@ export const meta = applyConfig({
* Deletes a user avatar
*/
export default apiRoute(async (req, matchedRoute, extraData) => {
const { user } = extraData.auth;
const { user: self } = extraData.auth;
if (!user) return errorResponse("Unauthorized", 401);
if (!self) return errorResponse("Unauthorized", 401);
// Delete user avatar
const newUser = await client.user.update({
where: {
id: user.id,
},
data: {
await db.update(user).set({ avatar: "" }).where(eq(user.id, self.id));
return jsonResponse(
userToAPI({
...self,
avatar: "",
},
include: userRelations,
});
return jsonResponse(userToAPI(newUser));
}),
);
});

View file

@ -1,8 +1,9 @@
import { apiRoute, applyConfig } from "@api";
import { errorResponse, jsonResponse } from "@response";
import { client } from "~database/datasource";
import { eq } from "drizzle-orm";
import { userToAPI } from "~database/entities/User";
import { userRelations } from "~database/entities/relations";
import { db } from "~drizzle/db";
import { user } from "~drizzle/schema";
export const meta = applyConfig({
allowedMethods: ["DELETE"],
@ -20,20 +21,17 @@ export const meta = applyConfig({
* Deletes a user header
*/
export default apiRoute(async (req, matchedRoute, extraData) => {
const { user } = extraData.auth;
const { user: self } = extraData.auth;
if (!user) return errorResponse("Unauthorized", 401);
if (!self) return errorResponse("Unauthorized", 401);
// Delete user header
const newUser = await client.user.update({
where: {
id: user.id,
},
data: {
header: "",
},
include: userRelations,
});
await db.update(user).set({ header: "" }).where(eq(user.id, self.id));
return jsonResponse(userToAPI(newUser));
return jsonResponse(
userToAPI({
...self,
header: "",
}),
);
});

View file

@ -1,8 +1,6 @@
import { apiRoute, applyConfig } from "@api";
import { errorResponse, jsonResponse } from "@response";
import { client } from "~database/datasource";
import { findFirstStatuses, statusToAPI } from "~database/entities/Status";
import { statusAndUserRelations } from "~database/entities/relations";
import { db } from "~drizzle/db";
import { statusToUser } from "~drizzle/schema";

View file

@ -1,13 +1,11 @@
import { apiRoute, applyConfig } from "@api";
import { errorResponse, jsonResponse } from "@response";
import { fetchTimeline } from "@timelines";
import { client } from "~database/datasource";
import {
type StatusWithRelations,
findManyStatuses,
statusToAPI,
} from "~database/entities/Status";
import { statusAndUserRelations } from "~database/entities/relations";
export const meta = applyConfig({
allowedMethods: ["GET"],

View file

@ -1,10 +1,12 @@
import chalk from "chalk";
import { config } from "config-manager";
import { count } from "drizzle-orm";
import { LogLevel, type LogManager, type MultiLogManager } from "log-manager";
import { Meilisearch } from "meilisearch";
import { client } from "~database/datasource";
import type { Status } from "~database/entities/Status";
import type { User } from "~database/entities/User";
import { db } from "~drizzle/db";
import { status, user } from "~drizzle/schema";
export const meilisearch = new Meilisearch({
host: `${config.meilisearch.host}:${config.meilisearch.port}`,
@ -81,19 +83,17 @@ export const getNthDatabaseAccountBatch = (
n: number,
batchSize = 1000,
): Promise<Record<string, string | Date>[]> => {
return client.user.findMany({
skip: n * batchSize,
take: batchSize,
select: {
return db.query.user.findMany({
offset: n * batchSize,
limit: batchSize,
columns: {
id: true,
username: true,
displayName: true,
note: true,
createdAt: true,
},
orderBy: {
createdAt: "asc",
},
orderBy: (user, { asc }) => asc(user.createdAt),
});
};
@ -101,17 +101,15 @@ export const getNthDatabaseStatusBatch = (
n: number,
batchSize = 1000,
): Promise<Record<string, string | Date>[]> => {
return client.status.findMany({
skip: n * batchSize,
take: batchSize,
select: {
return db.query.status.findMany({
offset: n * batchSize,
limit: batchSize,
columns: {
id: true,
content: true,
createdAt: true,
},
orderBy: {
createdAt: "asc",
},
orderBy: (status, { asc }) => asc(status.createdAt),
});
};
@ -120,7 +118,13 @@ export const rebuildSearchIndexes = async (
batchSize = 100,
) => {
if (indexes.includes(MeiliIndexType.Accounts)) {
const accountCount = await client.user.count();
const accountCount = (
await db
.select({
count: count(),
})
.from(user)
)[0].count;
for (let i = 0; i < accountCount / batchSize; i++) {
const accounts = await getNthDatabaseAccountBatch(i, batchSize);
@ -147,7 +151,13 @@ export const rebuildSearchIndexes = async (
}
if (indexes.includes(MeiliIndexType.Statuses)) {
const statusCount = await client.status.count();
const statusCount = (
await db
.select({
count: count(),
})
.from(status)
)[0].count;
for (let i = 0; i < statusCount / batchSize; i++) {
const statuses = await getNthDatabaseStatusBatch(i, batchSize);