Hey everyone. I have this query ``` const result...
# orm-help
c
Hey everyone. I have this query
Copy code
const results = await prisma.token.findMany({
    select: {
      id: true,
      name: true,
      symbol: true,
      telegram: true,
      _count: { select: { votes: true } },
    },
    orderBy: { votes: { count: "desc" } },
  });
However I would like this to be a range query, so I can select tokens in descending order where the date on votes are less than 24 hours ago If possible, I would also like to select the count of these votes too (but total count is fine if that's not possible).
d
Copy code
const results = await prisma.token.findMany({
  select: {
    id: true,
    name: true,
    symbol: true,
    telegram: true,
    _count: { select: { votes: true } },
  },
  orderBy: { votes: { count: 'desc' } },
  where: {
    votes: {
      some: {
        createdAt: {
          gt: '24 hours ago' // send actual date object,
        },
      },
    },
  },
})
its hard to do it without your schema to hand but I believe it is something like this
c
The count returned from this is still the total count, whereas I would want the count of votes that happened in the past 24 hours. I think all this does is reduce it to tokens that have been voted on in the past 24H
d
probably group the requests by token and filter where created at greater than last 24 hours
c
I really wanted to avoid 2 queries
d
I think you can do it with 1 query
can you send me your schema and I can test it out
c
Copy code
// This is your Prisma schema file,
// learn more about it in the docs: <https://pris.ly/d/prisma-schema>

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["selectRelationCount", "orderByRelation"]
}

model Token {
  id Int @id @default(autoincrement())
  createdAt DateTime @default(now())
  launchDate DateTime
  name  String
  symbol String
  address String
  chain String
  description String
  website String
  telegram String
  twitter String?
  discord String?
  approved Boolean @default(false)
  listed Boolean @default(false)
  voteCount Int @default(0)
  votesToday Int @default(0)
  votes Vote[]
  sponsoredListings SponsoredListing[]
}

model SponsoredListing {
  id Int @id @default(autoincrement())
  token Token @relation(fields: [tokenId], references: [id])
  tokenId Int
  campaignStart DateTime
  campaignEnd DateTime
}

model Vote {
  id Int @id @default(autoincrement())
  createdAt DateTime @default(now())
  user User @relation(fields: [userId], references: [id])
  userId Int
  token Token @relation(fields: [tokenId], references: [id])
  tokenId Int
}

model Account {
  id                 Int       @default(autoincrement()) @id
  compoundId         String    @unique @map(name: "compound_id")
  userId             Int       @map(name: "user_id")
  providerType       String    @map(name: "provider_type")
  providerId         String    @map(name: "provider_id")
  providerAccountId  String    @map(name: "provider_account_id")
  refreshToken       String?   @map(name: "refresh_token")
  accessToken        String?   @map(name: "access_token")
  accessTokenExpires DateTime? @map(name: "access_token_expires")
  createdAt          DateTime  @default(now()) @map(name: "created_at")
  updatedAt          DateTime  @default(now()) @map(name: "updated_at")

  @@index([providerAccountId], name: "providerAccountId")
  @@index([providerId], name: "providerId")
  @@index([userId], name: "userId")

  @@map(name: "accounts")
}

model Session {
  id           Int      @default(autoincrement()) @id
  userId       Int      @map(name: "user_id")
  expires      DateTime
  sessionToken String   @unique @map(name: "session_token")
  accessToken  String   @unique @map(name: "access_token")
  createdAt    DateTime @default(now()) @map(name: "created_at")
  updatedAt    DateTime @default(now()) @map(name: "updated_at")

  @@map(name: "sessions")
}

model User {
  id            Int       @default(autoincrement()) @id
  name          String?
  email         String?   @unique
  emailVerified DateTime? @map(name: "email_verified")
  image         String?
  createdAt     DateTime  @default(now()) @map(name: "created_at")
  updatedAt     DateTime  @default(now()) @map(name: "updated_at")
  votes         Vote[]

  @@map(name: "users")
}

model VerificationRequest {
  id         Int      @default(autoincrement()) @id
  identifier String
  token      String   @unique
  expires    DateTime
  createdAt  DateTime  @default(now()) @map(name: "created_at")
  updatedAt  DateTime  @default(now()) @map(name: "updated_at")

  @@map(name: "verification_requests")
}
Thank you
All the accounts / user relations can be simplified
d
yeah, I cant get it down into less than 2 requests
sorry about that
I mean, you could but it would require pulling all votes in the past 24 hours, including the tokens and the server grouping them and calculating the count
could be a large data set though
c
Do you mind sharing the 2 request solution? Thank you man
@Dominic Hadfield