Replace mysql2 with prisma
also I updated packages, and properly typed api input a lot of time was spent, I don't remeber what really I did x3 but everything was related to replacing mysql2 with prisma
This commit is contained in:
parent
be1e3909b6
commit
eebf25198d
39 changed files with 1081 additions and 1292 deletions
|
@ -1,19 +1,99 @@
|
|||
import mysql, { type Connection } from "mysql2/promise";
|
||||
import { PrismaClient, Prisma } from "@prisma/client";
|
||||
|
||||
const connectionOptions: mysql.ConnectionOptions = {
|
||||
host: process.env.DB_HOST,
|
||||
port: Number(process.env.DB_PORT),
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_SCHEMA,
|
||||
decimalNumbers: true,
|
||||
supportBigNumbers: true,
|
||||
};
|
||||
import { type pageData } from "./baaPageParsing";
|
||||
|
||||
export const database =
|
||||
await mysql.createConnection(connectionOptions) as Connection & {
|
||||
new: (localConnectionOptions?: mysql.ConnectionOptions | undefined) => Promise<Connection>
|
||||
};
|
||||
database.new = (localConnectionOptions?: mysql.ConnectionOptions | undefined) => { return mysql.createConnection({ ...localConnectionOptions, ...connectionOptions }); };
|
||||
type model = PrismaClient[Uncapitalize<Prisma.ModelName>];
|
||||
|
||||
export type data<T> = [T[], mysql.FieldPacket[]];
|
||||
function getBeforeParameters<T, A>(
|
||||
pageData: pageData<false>,
|
||||
fetchArgs: Prisma.Args<T, "findMany">,
|
||||
) {
|
||||
const _fetchArgs = Object.assign({}, fetchArgs);
|
||||
return Object.assign(_fetchArgs, {
|
||||
take: pageData.count,
|
||||
orderBy: [
|
||||
{ id: "desc" },
|
||||
],
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
id: {
|
||||
_lt: pageData.id,
|
||||
},
|
||||
},
|
||||
fetchArgs.where,
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function getAfterParameters<T>(
|
||||
pageData: pageData<false>,
|
||||
fetchArgs: Prisma.Args<T, "findMany">,
|
||||
) {
|
||||
const _fetchArgs = Object.assign({}, fetchArgs);
|
||||
return Object.assign(_fetchArgs, {
|
||||
take: pageData.count,
|
||||
orderBy: [
|
||||
{ id: "desc" },
|
||||
],
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
id: {
|
||||
_gt: pageData.id,
|
||||
},
|
||||
},
|
||||
fetchArgs.where,
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function getNullParameters<T>(
|
||||
pageData: pageData<true>,
|
||||
fetchArgs: Prisma.Args<T, "findMany">,
|
||||
) {
|
||||
const _fetchArgs = Object.assign({}, fetchArgs);
|
||||
return Object.assign(_fetchArgs, {
|
||||
take: pageData.count,
|
||||
orderBy: [
|
||||
{ id: "desc" },
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export const database = new PrismaClient().$extends({
|
||||
model: {
|
||||
$allModels: {
|
||||
findPaginated<T, A>(
|
||||
this: T,
|
||||
pageData: pageData,
|
||||
fetchArgs: Prisma.Exact<A, Prisma.Args<T, "findMany">>,
|
||||
): Promise<Prisma.Result<T, A, "findMany">> {
|
||||
const context = Prisma.getExtensionContext(this) as any;
|
||||
switch (pageData.type) {
|
||||
case "before":
|
||||
return context.findMany(getBeforeParameters(pageData, fetchArgs));
|
||||
case "after":
|
||||
return context.findMany(getAfterParameters(pageData, fetchArgs));
|
||||
case "around":
|
||||
return Promise.all([
|
||||
context.findMany(getBeforeParameters({
|
||||
type: "before",
|
||||
id: pageData.id,
|
||||
count: Math.ceil(pageData.count),
|
||||
}, fetchArgs)),
|
||||
context.findMany(getAfterParameters({
|
||||
type: "after",
|
||||
id: pageData.id,
|
||||
count: Math.floor(pageData.count),
|
||||
}, fetchArgs)),
|
||||
]).then(rv => rv.flat()) as Promise<any>;
|
||||
case null:
|
||||
return context.findMany(getNullParameters(pageData, fetchArgs));
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue