fix: add no-prisma-delete lint

This commit is contained in:
DecDuck
2025-11-21 23:04:00 +11:00
parent f1fccd9bff
commit 650a3ca98d
18 changed files with 97 additions and 54 deletions

View File

@ -2,6 +2,7 @@
import withNuxt from "./.nuxt/eslint.config.mjs";
import eslintConfigPrettier from "eslint-config-prettier/flat";
import vueI18n from "@intlify/eslint-plugin-vue-i18n";
import noPrismaDelete from "./rules/no-prisma-delete.mts";
export default withNuxt([
eslintConfigPrettier,
@ -19,6 +20,7 @@ export default withNuxt([
},
],
"@intlify/vue-i18n/no-missing-keys": "error",
"drop/no-prisma-delete": "error",
},
settings: {
"vue-i18n": {
@ -29,5 +31,8 @@ export default withNuxt([
messageSyntaxVersion: "^11.0.0",
},
},
plugins: {
drop: { rules: { "no-prisma-delete": noPrismaDelete } },
},
},
]);

View File

@ -0,0 +1,32 @@
import type { TSESLint } from "@typescript-eslint/utils";
export default {
meta: {
type: "problem",
docs: {
description: "Don't use Prisma error-prone .delete function",
},
messages: {
noPrismaDelete:
"Prisma .delete(...) function is used. Use .deleteMany(..) and check count instead.",
},
schema: [],
},
create(context) {
return {
CallExpression: function (node) {
const funcId = node.callee.property;
if (!funcId || funcId.name !== "delete") return;
const tableExpr = node.callee.object;
if (!tableExpr) return;
const prismaExpr = tableExpr.object;
if (!prismaExpr || prismaExpr.name !== "prisma") return;
context.report({
node,
messageId: "noPrismaDelete",
});
},
};
},
defaultOptions: [],
} satisfies TSESLint.RuleModule<"noPrismaDelete">;

View File

@ -17,6 +17,10 @@ export default defineEventHandler<{
const body = await readDropValidatedBody(h3, DeleteInvite);
await prisma.invitation.delete({ where: { id: body.id } });
const { count } = await prisma.invitation.deleteMany({
where: { id: body.id },
});
if (count == 0)
throw createError({ statusCode: 404, message: "Invitation not found." });
return {};
});

View File

@ -7,7 +7,7 @@ export default defineEventHandler(async (h3) => {
const gameId = getRouterParam(h3, "id")!;
libraryManager.deleteGame(gameId);
await libraryManager.deleteGame(gameId);
return {};
});

View File

@ -18,11 +18,13 @@ export default defineEventHandler<{ body: typeof DeleteLibrarySource.infer }>(
const body = await readDropValidatedBody(h3, DeleteLibrarySource);
await prisma.library.delete({
const { count } = await prisma.library.deleteMany({
where: {
id: body.id,
},
});
if (count == 0)
throw createError({ statusCode: 404, message: "Library not found." });
libraryManager.removeLibrary(body.id);
},

View File

@ -13,10 +13,10 @@ export default defineEventHandler(async (h3) => {
statusMessage: "No id in router params",
});
const deleted = await prisma.aPIToken.delete({
const { count } = await prisma.aPIToken.deleteMany({
where: { id: id, mode: APITokenMode.System },
})!;
if (!deleted)
if (count == 0)
throw createError({ statusCode: 404, statusMessage: "Token not found" });
return;

View File

@ -27,6 +27,7 @@ export default defineEventHandler(async (h3) => {
if (!user)
throw createError({ statusCode: 404, statusMessage: "User not found." });
// eslint-disable-next-line drop/no-prisma-delete
await prisma.user.delete({ where: { id: userId } });
await userStatsManager.deleteUser();
return { success: true };

View File

@ -84,7 +84,7 @@ export default defineEventHandler<{
user: true,
},
}),
prisma.invitation.delete({ where: { id: user.invitation } }),
prisma.invitation.deleteMany({ where: { id: user.invitation } }),
]);
await userStatsManager.addUser();

View File

@ -38,16 +38,14 @@ export default defineClientEventHandler(
if (!game)
throw createError({ statusCode: 400, statusMessage: "Invalid game ID" });
const save = await prisma.saveSlot.delete({
const { count } = await prisma.saveSlot.deleteMany({
where: {
id: {
userId: user.id,
gameId: gameId,
index: slotIndex,
},
userId: user.id,
gameId: gameId,
index: slotIndex,
},
});
if (!save)
if (count == 0)
throw createError({ statusCode: 404, statusMessage: "Save not found" });
},
);

View File

@ -20,14 +20,14 @@ export default defineEventHandler(async (h3) => {
userIds.push("system");
}
const notification = await prisma.notification.delete({
const { count } = await prisma.notification.deleteMany({
where: {
id: notificationId,
userId: { in: userIds },
},
});
if (!notification)
if (count == 0)
throw createError({
statusCode: 400,
statusMessage: "Invalid notification ID",

View File

@ -13,10 +13,10 @@ export default defineEventHandler(async (h3) => {
statusMessage: "No id in router params",
});
const deleted = await prisma.aPIToken.delete({
const { count } = await prisma.aPIToken.deleteMany({
where: { id: id, userId: userId, mode: APITokenMode.User },
})!;
if (!deleted)
if (count == 0)
throw createError({ statusCode: 404, statusMessage: "Token not found" });
return;

View File

@ -185,15 +185,19 @@ export class ClientHandler {
}
async removeClient(id: string) {
const client = await prisma.client.findUnique({ where: { id } });
if (!client) return false;
const ca = useCertificateAuthority();
await ca.blacklistClient(id);
// eslint-disable-next-line drop/no-prisma-delete
await prisma.client.delete({
where: {
id,
},
});
await userStatsManager.cacheUserStats();
return true;
}
}

View File

@ -378,12 +378,10 @@ class LibraryManager {
}
async deleteGameVersion(gameId: string, version: string) {
await prisma.gameVersion.delete({
await prisma.gameVersion.deleteMany({
where: {
gameId_versionName: {
gameId: gameId,
versionName: version,
},
gameId: gameId,
versionName: version,
},
});
@ -391,12 +389,12 @@ class LibraryManager {
}
async deleteGame(gameId: string) {
await prisma.game.delete({
await prisma.game.deleteMany({
where: {
id: gameId,
},
});
gameSizeManager.deleteGame(gameId);
await gameSizeManager.deleteGame(gameId);
}
async getGameVersionSize(

View File

@ -124,7 +124,10 @@ class NewsManager {
}
async delete(id: string) {
const article = await prisma.article.delete({
const article = await prisma.article.findUnique({ where: { id } });
if (!article) return false;
// eslint-disable-next-line drop/no-prisma-delete
await prisma.article.delete({
where: { id },
});
if (article.imageObjectId) {

View File

@ -259,16 +259,10 @@ class FsHashStore {
*/
async delete(id: ObjectReference) {
await this.cache.remove(id);
try {
// need to catch in case the object doesn't exist
await prisma.objectHash.delete({
where: {
id,
},
});
} catch {
/* empty */
}
await prisma.objectHash.deleteMany({
where: {
id,
},
});
}
}

View File

@ -53,12 +53,16 @@ class ScreenshotManager {
* @param id
*/
async delete(id: string) {
const deletedScreenshot = await prisma.screenshot.delete({
const screenshot = await prisma.screenshot.findUnique({ where: { id } });
if (!screenshot) return false;
// eslint-disable-next-line drop/no-prisma-delete
await prisma.screenshot.delete({
where: {
id,
},
});
await objectHandler.deleteAsSystem(deletedScreenshot.objectId);
await objectHandler.deleteAsSystem(screenshot.objectId);
return true;
}
/**

View File

@ -43,12 +43,12 @@ export default function createDBSessionHandler(): SessionProvider {
},
async removeSession(token) {
await cache.remove(token);
await prisma.session.delete({
const { count } = await prisma.session.deleteMany({
where: {
token,
},
});
return true;
return count > 0;
},
async cleanupSessions() {
const now = new Date();

View File

@ -101,19 +101,16 @@ class UserLibraryManager {
async collectionRemove(gameId: string, collectionId: string, userId: string) {
// Delete if exists
return (
(
await prisma.collectionEntry.deleteMany({
where: {
collectionId,
gameId,
collection: {
userId,
},
},
})
).count > 0
);
const { count } = await prisma.collectionEntry.deleteMany({
where: {
collectionId,
gameId,
collection: {
userId,
},
},
});
return count > 0;
}
async collectionCreate(name: string, userId: string) {
@ -133,12 +130,13 @@ class UserLibraryManager {
}
async deleteCollection(collectionId: string) {
await prisma.collection.delete({
const { count } = await prisma.collection.deleteMany({
where: {
id: collectionId,
isDefault: false,
},
});
return count > 0;
}
}