ufoaz
ufoaz
CCConvex Community
Created by ufoaz on 6/2/2024 in #support-community
Locking mechanism in custom Action (getOrCreate)?
When implementing an Action to getOrCreate a particular doc in a table, we'd like to make sure that the doc, which has a unique location_id column doesn't get created twice. However, we don't currently have a schema defined. Is there a way to handle the duplication of doc when this function is called in quick succession without defining a schema for all our tables and specifying the location_id column as unique? We are using an action here because the create involves a 3rd party API request.
export const getOrCreate = action({
args: {
location_id: v.string(),
},
handler: async (ctx, { location_id }) => {
const location: null | any = await ctx.runQuery(internal.locations.get, {
location_id,
});

if (location) {
return location;
}
const combinedObj = await fetchLocationWithPhotos(location_id);
await ctx.runMutation(internal.locations.create, {
combinedObj,
location_id,
});

return {
...combinedObj,
location_id,
};
},
});

export const get = internalQuery({
args: {
location_id: v.string(),
},
handler: async (ctx, { location_id }) => {
return await ctx.db
.query("locations")
.filter((q) => q.eq(q.field("location_id"), location_id))
.unique();
},
});

export const create = internalMutation({
args: {
location_id: v.string(),
combinedObj: v.any(),
},
handler: async (ctx, { location_id, combinedObj }) => {
return await ctx.db.insert("locations", {
location_id,
...combinedObj,
});
},
});
export const getOrCreate = action({
args: {
location_id: v.string(),
},
handler: async (ctx, { location_id }) => {
const location: null | any = await ctx.runQuery(internal.locations.get, {
location_id,
});

if (location) {
return location;
}
const combinedObj = await fetchLocationWithPhotos(location_id);
await ctx.runMutation(internal.locations.create, {
combinedObj,
location_id,
});

return {
...combinedObj,
location_id,
};
},
});

export const get = internalQuery({
args: {
location_id: v.string(),
},
handler: async (ctx, { location_id }) => {
return await ctx.db
.query("locations")
.filter((q) => q.eq(q.field("location_id"), location_id))
.unique();
},
});

export const create = internalMutation({
args: {
location_id: v.string(),
combinedObj: v.any(),
},
handler: async (ctx, { location_id, combinedObj }) => {
return await ctx.db.insert("locations", {
location_id,
...combinedObj,
});
},
});
18 replies
CCConvex Community
Created by ufoaz on 5/13/2024 in #support-community
Uncaught Error: Documents read from or written to the "itineraries" table changed while this mut...
Is it bad practice to stream from 3 different threads from an LLM and then run respective mutations to update the same document (at the same time)? Seems there are some 'Error managing thread' errors appearing. Could imagine storing references to 3 different documents instead, but would ideally keep this structure. Full error:
Uncaught Error: Documents read from or written to the "itineraries" table changed while this mutation was being run and on every subsequent retry. Another call to this mutation changed the document with ID "j57bsqexd56n4fnee9y4k2v1dx6s1e46". See https://docs.convex.dev/error#1
at async recursiveStreamHandler (../convex/openai_v2.ts:264:64)
at async recursiveStreamHandler (../convex/openai_v2.ts:307:12)
at async handler (../convex/openai_v2.ts:92:8)
Uncaught Error: Documents read from or written to the "itineraries" table changed while this mutation was being run and on every subsequent retry. Another call to this mutation changed the document with ID "j57bsqexd56n4fnee9y4k2v1dx6s1e46". See https://docs.convex.dev/error#1
at async recursiveStreamHandler (../convex/openai_v2.ts:264:64)
at async recursiveStreamHandler (../convex/openai_v2.ts:307:12)
at async handler (../convex/openai_v2.ts:92:8)
4 replies
CCConvex Community
Created by ufoaz on 4/27/2024 in #show-and-tell
Remi - AI Travel Assistant
No description
15 replies
CCConvex Community
Created by ufoaz on 4/7/2024 in #support-community
What's the recommendation to include a 3rd party auth provider without OIDC?
Seems the convex authentication experience requires using an auth provider through OIDC. However, we still implement the fetchAccessToken field in the ConvexProviderWithAuth useAuth parameter as well as isAuthenticated. It seems that when you actually getUserIdentity in a mutation it will separately use the issuer and app id w/ oidc endpoints to validate the token. So, the token must be an id token rather than access token/refresh token pair?
3 replies