TheNinjrKillr
TheNinjrKillr12mo ago

Error on 3-expression Index

I've got this model in my schema:
accounts: defineTable({
user: v.id('users'),

type: v.string(),

provider: v.string(),
providerAccountID: v.string(),
})
.index('by_user', ['user'])
.index('by_provider', ['provider', 'providerAccountID'])
.index('by_provider_and_type', ['provider', 'providerAccountID', 'type']),
accounts: defineTable({
user: v.id('users'),

type: v.string(),

provider: v.string(),
providerAccountID: v.string(),
})
.index('by_user', ['user'])
.index('by_provider', ['provider', 'providerAccountID'])
.index('by_provider_and_type', ['provider', 'providerAccountID', 'type']),
And this mutation:
export const link = mutation({
args: {
account: v.object({
user: v.id('users'),

type: v.string(),

provider: v.string(),
providerAccountID: v.string(),
})
},
handler: async (ctx, args) => {
const account = await ctx.db.query('accounts')
.withIndex('by_provider_and_type', q => q.eq('provider', args.account.provider)
.eq('providerAccountID', args.account.providerAccountID)
.eq('type', args.account.type))
.first();

if(!account) return undefined;

return await ctx.db.patch(account._id, args.account )
}
});
export const link = mutation({
args: {
account: v.object({
user: v.id('users'),

type: v.string(),

provider: v.string(),
providerAccountID: v.string(),
})
},
handler: async (ctx, args) => {
const account = await ctx.db.query('accounts')
.withIndex('by_provider_and_type', q => q.eq('provider', args.account.provider)
.eq('providerAccountID', args.account.providerAccountID)
.eq('type', args.account.type))
.first();

if(!account) return undefined;

return await ctx.db.patch(account._id, args.account )
}
});
And I'm getting these two errors: convex/auth/account.ts:27:40 - error TS2345: Argument of type '"by_provider_and_type"' is not assignable to parameter of type '"by_user" | "by_provider" | "by_creation_time"'. convex/auth/account.ts:27:109 - error TS2339: Property 'eq' does not exist on type 'IndexRange'. Anyone have any suggestions?
3 Replies
Michal Srb
Michal Srb12mo ago
I would guess the schema file is not saved, so TypeScript in terminal is going off of the schema without it existing?
TheNinjrKillr
TheNinjrKillrOP12mo ago
🤦‍♂️ Thanks! Too many tabs open lol
ian
ian12mo ago
FYI you could get away with just by_provider_and_type if you were ok with queries on ['provider', 'providerAccountID'] coming back sorted by type. You don't have to provide a query against every index field

Did you find this page helpful?