Begin moving project to use Drizzle instead of prisma

This commit is contained in:
Jesse Wierzbinski 2024-04-11 01:39:07 -10:00
parent b107bed935
commit f7abe06a60
No known key found for this signature in database
49 changed files with 7602 additions and 1267 deletions

View file

@ -1,9 +1,10 @@
import type { Status, User } from "@prisma/client";
import chalk from "chalk";
import { config } from "config-manager";
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";
export const meilisearch = new Meilisearch({
host: `${config.meilisearch.host}:${config.meilisearch.port}`,

View file

@ -1,20 +1,23 @@
import type { Status, User, Prisma, Notification } from "@prisma/client";
import type { findManyStatuses, Status } from "~database/entities/Status";
import type { findManyUsers, User } from "~database/entities/User";
import type { Notification } from "~database/entities/Notification";
import type { db } from "~drizzle/db";
export async function fetchTimeline<T extends User | Status | Notification>(
model:
| Prisma.StatusDelegate
| Prisma.UserDelegate
| Prisma.NotificationDelegate,
| typeof findManyStatuses
| typeof findManyUsers
| typeof db.query.notification.findMany,
args:
| Prisma.StatusFindManyArgs
| Prisma.UserFindManyArgs
| Prisma.NotificationFindManyArgs,
| Parameters<typeof findManyStatuses>[0]
| Parameters<typeof findManyUsers>[0]
| Parameters<typeof db.query.notification.findMany>[0],
req: Request,
) {
// BEFORE: Before in a top-to-bottom order, so the most recent posts
// AFTER: After in a top-to-bottom order, so the oldest posts
// @ts-expect-error This is a hack to get around the fact that Prisma doesn't have a common base type for all models
const objects = (await model.findMany(args)) as T[];
const objects = (await model(args)) as T[];
// Constuct HTTP Link header (next and prev) only if there are more statuses
const linkHeader = [];
@ -22,15 +25,11 @@ export async function fetchTimeline<T extends User | Status | Notification>(
if (objects.length > 0) {
// Check if there are statuses before the first one
// @ts-expect-error This is a hack to get around the fact that Prisma doesn't have a common base type for all models
const objectsBefore = await model.findMany({
const objectsBefore = await model({
...args,
where: {
...args.where,
id: {
gt: objects[0].id,
},
},
take: 1,
// @ts-expect-error this hack breaks typing :(
where: (object, { gt }) => gt(object.id, objects[0].id),
limit: 1,
});
if (objectsBefore.length > 0) {
@ -41,18 +40,16 @@ export async function fetchTimeline<T extends User | Status | Notification>(
);
}
if (objects.length < (args.take ?? Number.POSITIVE_INFINITY)) {
if (
objects.length < (Number(args?.limit) ?? Number.POSITIVE_INFINITY)
) {
// Check if there are statuses after the last one
// @ts-expect-error This is a hack to get around the fact that Prisma doesn't have a common base type for all models
const objectsAfter = await model.findMany({
// @ts-expect-error hack again
const objectsAfter = await model({
...args,
where: {
...args.where,
id: {
lt: objects.at(-1)?.id,
},
},
take: 1,
// @ts-expect-error this hack breaks typing :(
where: (object, { lt }) => lt(object.id, objects.at(-1).id),
limit: 1,
});
if (objectsAfter.length > 0) {