refactor: migrate to tsup for builds and update tsconfigs fix: update error handling and validation in payment service chore: update package scripts and dependencies docs: update README and env examples
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { ResultAsync, ok } from "neverthrow";
|
|
import { PrismaClientArg, Prisma, File } from "@repo/db";
|
|
import { fileNotFound, FileStorageError } from "../errors";
|
|
import {
|
|
wrapDbOperation,
|
|
wrapDbOperationWithNull,
|
|
DatabaseError,
|
|
databaseError,
|
|
} from "@repo/exceptions";
|
|
import { CreateFileInput } from "../types";
|
|
|
|
const create = (
|
|
client: PrismaClientArg,
|
|
data: CreateFileInput,
|
|
): ResultAsync<File, DatabaseError> => {
|
|
const fileData: Prisma.FileCreateInput = {
|
|
filename: data.filename,
|
|
originalName: data.originalName,
|
|
mimeType: data.mimeType,
|
|
size: data.size,
|
|
path: data.path,
|
|
};
|
|
|
|
return wrapDbOperation(
|
|
() => client.file.create({ data: fileData }),
|
|
(e) => databaseError("createFile", e as Error),
|
|
);
|
|
};
|
|
|
|
const findById = (
|
|
client: PrismaClientArg,
|
|
id: string,
|
|
): ResultAsync<File, DatabaseError | FileStorageError> =>
|
|
wrapDbOperationWithNull<File, DatabaseError | FileStorageError>(
|
|
() => client.file.findUnique({ where: { id } }),
|
|
fileNotFound(id),
|
|
(e) => databaseError("findFileById", e as Error),
|
|
);
|
|
|
|
const findByFilename = (
|
|
client: PrismaClientArg,
|
|
filename: string,
|
|
): ResultAsync<File | null, DatabaseError> =>
|
|
ResultAsync.fromPromise(
|
|
client.file.findFirst({ where: { filename } }),
|
|
(error) => databaseError("findByFilename", error as Error),
|
|
);
|
|
|
|
const deleteById = (
|
|
client: PrismaClientArg,
|
|
id: string,
|
|
): ResultAsync<void, DatabaseError> =>
|
|
ResultAsync.fromPromise(client.file.delete({ where: { id } }), (error) =>
|
|
databaseError("deleteFile", error as Error),
|
|
).andThen(() => ok(undefined));
|
|
|
|
const exists = (
|
|
client: PrismaClientArg,
|
|
filename: string,
|
|
): ResultAsync<boolean, DatabaseError> =>
|
|
ResultAsync.fromPromise(client.file.exists({ filename }), (error) =>
|
|
databaseError("checkFileExists", error as Error),
|
|
);
|
|
|
|
export const FileRepository = {
|
|
create,
|
|
findById,
|
|
findByFilename,
|
|
deleteById,
|
|
exists,
|
|
};
|