#What's the right way to get the number of documents in a table in the last 5 minutes?
17 messages · Page 1 of 1 (latest)
Let me check the knowledge base for you. One moment! :books:
Sorry @misty brook, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?
Something like this?
const numCreatedRecently = (
await ctx.db.query("itineraries").filter(
(q) => q.gte("_creationTime", Date.now() - 1000 * 60)
)
.take(10)
).length;
if (numCreatedRecently >= 10) {
throw new Error("Too many requests, please try again later");
}
ctx.db.query("itineraries").withIndex("by_creation_time", q=>q.gte("_creationTime", Date.now() - 5*60*1000)).take(10)
hmm but i dont have a schema defined. maybe that's why it's giving type errors?
Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)
Oh huh. That sounds like a bug. You should be able to do "by_creation_time" as any to get around that
this at least has no linter errors:
ctx.db.query("itineraries").withIndex("by_creation_time" as never, q=>q.gte("_creationTime" as never, Date.now() - 5*60*1000 as never)).take(10)
huh. I'll look into why this is necessary and we should probably fix it, but that seems like a reasonable workaround.
You can also do the filter thing (from your original question) if you're not worried about performance, but note you should use q.field("_creationTime") instead of just "_creationTime"