KHRM
KHRM
Explore posts from servers
PPrisma
Created by KHRM on 10/3/2024 in #help-and-questions
Sorting by Relations
Hello, I am trying to introduce infinite scrolling im my application but I am running into an issue. I am using Prisma ORM with sqlite through Turso here is what I curently have
const events = await db.event.findMany({
include: {
eventDates: {
orderBy: {
date: {
date: "asc",
},
},
include: { date: true },
},
},
skip,
take,
cursor,
});

const sortedEvents = events.sort((a, b) => {
const aDate = a.eventDates[0].date.date.getTime();
const bDate = b.eventDates[0].date.date.getTime();

return aDate - bDate;
});
const events = await db.event.findMany({
include: {
eventDates: {
orderBy: {
date: {
date: "asc",
},
},
include: { date: true },
},
},
skip,
take,
cursor,
});

const sortedEvents = events.sort((a, b) => {
const aDate = a.eventDates[0].date.date.getTime();
const bDate = b.eventDates[0].date.date.getTime();

return aDate - bDate;
});
I sort the events after I get them, but if I am only grabbing 5 results at a time, its only going to sort those 5 (which means it may be sorted within itself, but it might not be the 5 earliest events) here is a simplified version of my schema
model Event {
eventId String @id @default(cuid()) @map("event_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
title String
eventDates EventDate[]
@@map("events")
}

model EventDate {
event Event @relation(fields: [eventId], references: [eventId])
eventId String @map("event_id")
date Date @relation(fields: [dateId], references: [dateId])
dateId Int @map("date_id")

@@unique([eventId, dateId])
@@map("event_dates")
}

model Date {
dateId Int @id @default(autoincrement()) @map("date_id")
date DateTime @unique
eventDates EventDate[]
@@map("dates")
}
model Event {
eventId String @id @default(cuid()) @map("event_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
title String
eventDates EventDate[]
@@map("events")
}

model EventDate {
event Event @relation(fields: [eventId], references: [eventId])
eventId String @map("event_id")
date Date @relation(fields: [dateId], references: [dateId])
dateId Int @map("date_id")

@@unique([eventId, dateId])
@@map("event_dates")
}

model Date {
dateId Int @id @default(autoincrement()) @map("date_id")
date DateTime @unique
eventDates EventDate[]
@@map("dates")
}
How can I get all events sorted by their first event date, whcih is sorted by their date example query result
console.log(events[0])
console.log(events[0])
{
eventId: 'cm1s1bcxo0029jzxzoivivxr7',
createdAt: 2024-10-02T15:40:54.972Z,
updatedAt: 2024-10-02T15:40:54.972Z,
title: 'AXCN: Mobile Suit Gundam',
eventDates: [
{
eventId: 'cm1s1bcxo0029jzxzoivivxr7',
dateId: 105,
date: { dateId: 105, date: 2024-10-02T00:00:00.000Z }
},
{
eventId: 'cm1s1bcxo0029jzxzoivivxr7',
dateId: 106,
date: { dateId: 106, date: 2024-10-06T00:00:00.000Z }
}
]
{
eventId: 'cm1s1bcxo0029jzxzoivivxr7',
createdAt: 2024-10-02T15:40:54.972Z,
updatedAt: 2024-10-02T15:40:54.972Z,
title: 'AXCN: Mobile Suit Gundam',
eventDates: [
{
eventId: 'cm1s1bcxo0029jzxzoivivxr7',
dateId: 105,
date: { dateId: 105, date: 2024-10-02T00:00:00.000Z }
},
{
eventId: 'cm1s1bcxo0029jzxzoivivxr7',
dateId: 106,
date: { dateId: 106, date: 2024-10-06T00:00:00.000Z }
}
]
note eventDates array may not necessarily be in order but this is covered by
include: {
eventDates: {
orderBy: {
date: {
date: "asc",
},
},
include: { date: true },
},
},
include: {
eventDates: {
orderBy: {
date: {
date: "asc",
},
},
include: { date: true },
},
},
38 replies
DTDrizzle Team
Created by KHRM on 5/5/2024 in #help
Auth.js (next-auth) with Drizzle Provider Typescript
Hello so in the guide for the drizzle provider for Auth.js (next-auth) it says we can override the tables for example if i want to give me my users table a password and role field but then i run into a typescript error since the tables are typed in a specific way inside the adapter itself
adapter: {
/**
* Argument of type '{ usersTable: SQLiteTableWithColumns<{ name: "user"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "user"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; ... 5 more ...; role...' is not assignable to parameter of type 'DefaultSQLiteSchema'.
Type '{ usersTable: SQLiteTableWithColumns<{ name: "user"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "user"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; ... 5 more ...; role...' is missing the following properties from type 'DefaultSQLiteSchema': accountsTable, sessionsTable, verificationTokensTablets(2345)
*/
...DrizzleAdapter(db, { usersTable: users }),
adapter: {
/**
* Argument of type '{ usersTable: SQLiteTableWithColumns<{ name: "user"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "user"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; ... 5 more ...; role...' is not assignable to parameter of type 'DefaultSQLiteSchema'.
Type '{ usersTable: SQLiteTableWithColumns<{ name: "user"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "user"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; ... 5 more ...; role...' is missing the following properties from type 'DefaultSQLiteSchema': accountsTable, sessionsTable, verificationTokensTablets(2345)
*/
...DrizzleAdapter(db, { usersTable: users }),
4 replies
KKinde
Created by KHRM on 4/11/2024 in #💻┃support
useKindeBrowserClient shows error
Hello, I am using the useKindeBrowser client to recieve the isAuthenticated property
"use client";

import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs";
import { LoginLink } from "@kinde-oss/kinde-auth-nextjs/components";
import Link from "next/link";
import { Button } from "@/components/ui/button";

export const GetStartedBtn = () => {
const { isAuthenticated } = useKindeBrowserClient();

return (
<Button variant="secondary" size="lg" asChild>
{isAuthenticated ? (
<Link href="/journal">Get Started</Link>
) : (
<LoginLink postLoginRedirectURL="/login/redirect">
Get Started
</LoginLink>
)}
</Button>
);
};
"use client";

import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs";
import { LoginLink } from "@kinde-oss/kinde-auth-nextjs/components";
import Link from "next/link";
import { Button } from "@/components/ui/button";

export const GetStartedBtn = () => {
const { isAuthenticated } = useKindeBrowserClient();

return (
<Button variant="secondary" size="lg" asChild>
{isAuthenticated ? (
<Link href="/journal">Get Started</Link>
) : (
<LoginLink postLoginRedirectURL="/login/redirect">
Get Started
</LoginLink>
)}
</Button>
);
};
however when i am not logged in on the home page there is an error shown in the console.log is this intended behavior?, i just started my project and it already feels wrong haha (error is not shown if i am logged in)
✓ Compiled in 232ms (1085 modules)
Error: Cannot get user details, no authentication credential found
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:166:31)
at step (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:50:23)
at Object.eval [as next] (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:31:53)
at fulfilled (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:22:58)
Error: Cannot get user details, no authentication credential found
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@kinde-oss+kinde-typesc...
✓ Compiled in 232ms (1085 modules)
Error: Cannot get user details, no authentication credential found
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:166:31)
at step (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:50:23)
at Object.eval [as next] (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:31:53)
at fulfilled (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]/node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/clients/server/authorization-code.js:22:58)
Error: Cannot get user details, no authentication credential found
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@kinde-oss+kinde-typesc...
2 replies