mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
More conversions to Drizzle
This commit is contained in:
parent
ad0bf1a350
commit
05e45ff5aa
|
|
@ -1,8 +1,9 @@
|
||||||
import { apiRoute, applyConfig } from "@api";
|
import { apiRoute, applyConfig } from "@api";
|
||||||
import { errorResponse, jsonResponse } from "@response";
|
import { errorResponse, jsonResponse } from "@response";
|
||||||
import { client } from "~database/datasource";
|
import { eq } from "drizzle-orm";
|
||||||
import { userToAPI } from "~database/entities/User";
|
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({
|
export const meta = applyConfig({
|
||||||
allowedMethods: ["DELETE"],
|
allowedMethods: ["DELETE"],
|
||||||
|
|
@ -20,20 +21,16 @@ export const meta = applyConfig({
|
||||||
* Deletes a user avatar
|
* Deletes a user avatar
|
||||||
*/
|
*/
|
||||||
export default apiRoute(async (req, matchedRoute, extraData) => {
|
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
|
await db.update(user).set({ avatar: "" }).where(eq(user.id, self.id));
|
||||||
const newUser = await client.user.update({
|
|
||||||
where: {
|
return jsonResponse(
|
||||||
id: user.id,
|
userToAPI({
|
||||||
},
|
...self,
|
||||||
data: {
|
|
||||||
avatar: "",
|
avatar: "",
|
||||||
},
|
}),
|
||||||
include: userRelations,
|
);
|
||||||
});
|
|
||||||
|
|
||||||
return jsonResponse(userToAPI(newUser));
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import { apiRoute, applyConfig } from "@api";
|
import { apiRoute, applyConfig } from "@api";
|
||||||
import { errorResponse, jsonResponse } from "@response";
|
import { errorResponse, jsonResponse } from "@response";
|
||||||
import { client } from "~database/datasource";
|
import { eq } from "drizzle-orm";
|
||||||
import { userToAPI } from "~database/entities/User";
|
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({
|
export const meta = applyConfig({
|
||||||
allowedMethods: ["DELETE"],
|
allowedMethods: ["DELETE"],
|
||||||
|
|
@ -20,20 +21,17 @@ export const meta = applyConfig({
|
||||||
* Deletes a user header
|
* Deletes a user header
|
||||||
*/
|
*/
|
||||||
export default apiRoute(async (req, matchedRoute, extraData) => {
|
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
|
// Delete user header
|
||||||
const newUser = await client.user.update({
|
await db.update(user).set({ header: "" }).where(eq(user.id, self.id));
|
||||||
where: {
|
|
||||||
id: user.id,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
header: "",
|
|
||||||
},
|
|
||||||
include: userRelations,
|
|
||||||
});
|
|
||||||
|
|
||||||
return jsonResponse(userToAPI(newUser));
|
return jsonResponse(
|
||||||
|
userToAPI({
|
||||||
|
...self,
|
||||||
|
header: "",
|
||||||
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import { apiRoute, applyConfig } from "@api";
|
import { apiRoute, applyConfig } from "@api";
|
||||||
import { errorResponse, jsonResponse } from "@response";
|
import { errorResponse, jsonResponse } from "@response";
|
||||||
import { client } from "~database/datasource";
|
|
||||||
import { findFirstStatuses, statusToAPI } from "~database/entities/Status";
|
import { findFirstStatuses, statusToAPI } from "~database/entities/Status";
|
||||||
import { statusAndUserRelations } from "~database/entities/relations";
|
|
||||||
import { db } from "~drizzle/db";
|
import { db } from "~drizzle/db";
|
||||||
import { statusToUser } from "~drizzle/schema";
|
import { statusToUser } from "~drizzle/schema";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
import { apiRoute, applyConfig } from "@api";
|
import { apiRoute, applyConfig } from "@api";
|
||||||
import { errorResponse, jsonResponse } from "@response";
|
import { errorResponse, jsonResponse } from "@response";
|
||||||
import { fetchTimeline } from "@timelines";
|
import { fetchTimeline } from "@timelines";
|
||||||
import { client } from "~database/datasource";
|
|
||||||
import {
|
import {
|
||||||
type StatusWithRelations,
|
type StatusWithRelations,
|
||||||
findManyStatuses,
|
findManyStatuses,
|
||||||
statusToAPI,
|
statusToAPI,
|
||||||
} from "~database/entities/Status";
|
} from "~database/entities/Status";
|
||||||
import { statusAndUserRelations } from "~database/entities/relations";
|
|
||||||
|
|
||||||
export const meta = applyConfig({
|
export const meta = applyConfig({
|
||||||
allowedMethods: ["GET"],
|
allowedMethods: ["GET"],
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import { config } from "config-manager";
|
import { config } from "config-manager";
|
||||||
|
import { count } from "drizzle-orm";
|
||||||
import { LogLevel, type LogManager, type MultiLogManager } from "log-manager";
|
import { LogLevel, type LogManager, type MultiLogManager } from "log-manager";
|
||||||
import { Meilisearch } from "meilisearch";
|
import { Meilisearch } from "meilisearch";
|
||||||
import { client } from "~database/datasource";
|
|
||||||
import type { Status } from "~database/entities/Status";
|
import type { Status } from "~database/entities/Status";
|
||||||
import type { User } from "~database/entities/User";
|
import type { User } from "~database/entities/User";
|
||||||
|
import { db } from "~drizzle/db";
|
||||||
|
import { status, user } from "~drizzle/schema";
|
||||||
|
|
||||||
export const meilisearch = new Meilisearch({
|
export const meilisearch = new Meilisearch({
|
||||||
host: `${config.meilisearch.host}:${config.meilisearch.port}`,
|
host: `${config.meilisearch.host}:${config.meilisearch.port}`,
|
||||||
|
|
@ -81,19 +83,17 @@ export const getNthDatabaseAccountBatch = (
|
||||||
n: number,
|
n: number,
|
||||||
batchSize = 1000,
|
batchSize = 1000,
|
||||||
): Promise<Record<string, string | Date>[]> => {
|
): Promise<Record<string, string | Date>[]> => {
|
||||||
return client.user.findMany({
|
return db.query.user.findMany({
|
||||||
skip: n * batchSize,
|
offset: n * batchSize,
|
||||||
take: batchSize,
|
limit: batchSize,
|
||||||
select: {
|
columns: {
|
||||||
id: true,
|
id: true,
|
||||||
username: true,
|
username: true,
|
||||||
displayName: true,
|
displayName: true,
|
||||||
note: true,
|
note: true,
|
||||||
createdAt: true,
|
createdAt: true,
|
||||||
},
|
},
|
||||||
orderBy: {
|
orderBy: (user, { asc }) => asc(user.createdAt),
|
||||||
createdAt: "asc",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -101,17 +101,15 @@ export const getNthDatabaseStatusBatch = (
|
||||||
n: number,
|
n: number,
|
||||||
batchSize = 1000,
|
batchSize = 1000,
|
||||||
): Promise<Record<string, string | Date>[]> => {
|
): Promise<Record<string, string | Date>[]> => {
|
||||||
return client.status.findMany({
|
return db.query.status.findMany({
|
||||||
skip: n * batchSize,
|
offset: n * batchSize,
|
||||||
take: batchSize,
|
limit: batchSize,
|
||||||
select: {
|
columns: {
|
||||||
id: true,
|
id: true,
|
||||||
content: true,
|
content: true,
|
||||||
createdAt: true,
|
createdAt: true,
|
||||||
},
|
},
|
||||||
orderBy: {
|
orderBy: (status, { asc }) => asc(status.createdAt),
|
||||||
createdAt: "asc",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -120,7 +118,13 @@ export const rebuildSearchIndexes = async (
|
||||||
batchSize = 100,
|
batchSize = 100,
|
||||||
) => {
|
) => {
|
||||||
if (indexes.includes(MeiliIndexType.Accounts)) {
|
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++) {
|
for (let i = 0; i < accountCount / batchSize; i++) {
|
||||||
const accounts = await getNthDatabaseAccountBatch(i, batchSize);
|
const accounts = await getNthDatabaseAccountBatch(i, batchSize);
|
||||||
|
|
@ -147,7 +151,13 @@ export const rebuildSearchIndexes = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indexes.includes(MeiliIndexType.Statuses)) {
|
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++) {
|
for (let i = 0; i < statusCount / batchSize; i++) {
|
||||||
const statuses = await getNthDatabaseStatusBatch(i, batchSize);
|
const statuses = await getNthDatabaseStatusBatch(i, batchSize);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue