/**
* This should be called by the server
*/
export const update = mutation({
args: {
accessTokenHash: v.string(),
userId: v.string(),
accounts: v.array(AccountValue()),
},
handler: async (ctx, args) => {
const tokenHash = args.accessTokenHash;
//invalidate all other user accounts caches
const caches = await ctx.db
.query('cache_userAccounts')
.withIndex('by_userId', (q) => q.eq('userId', args.userId))
.collect();
//grant access to updater
const existsForToken =
caches.find(
(v) => v.tokenHash === tokenHash && v.userId == args.userId
) != undefined;
if (!existsForToken) {
await ctx.db.insert('cache_userAccounts', {
tokenHash: tokenHash,
userId: args.userId,
accounts: args.accounts,
});
}
//update all caches
await Promise.allSettled(
caches.map(async (cache) => {
await ctx.db.patch(cache._id, {
tokenHash: tokenHash,
userId: args.userId,
accounts: args.accounts,
});
})
);
},
});
/**
* This should be called by the server
*/
export const update = mutation({
args: {
accessTokenHash: v.string(),
userId: v.string(),
accounts: v.array(AccountValue()),
},
handler: async (ctx, args) => {
const tokenHash = args.accessTokenHash;
//invalidate all other user accounts caches
const caches = await ctx.db
.query('cache_userAccounts')
.withIndex('by_userId', (q) => q.eq('userId', args.userId))
.collect();
//grant access to updater
const existsForToken =
caches.find(
(v) => v.tokenHash === tokenHash && v.userId == args.userId
) != undefined;
if (!existsForToken) {
await ctx.db.insert('cache_userAccounts', {
tokenHash: tokenHash,
userId: args.userId,
accounts: args.accounts,
});
}
//update all caches
await Promise.allSettled(
caches.map(async (cache) => {
await ctx.db.patch(cache._id, {
tokenHash: tokenHash,
userId: args.userId,
accounts: args.accounts,
});
})
);
},
});