besimking
besimking
CCConvex Community
Created by besimking on 12/28/2024 in #support-community
Generic Query and Mutations
@erquhart @Lee Thanks a lot! You are the best
8 replies
CCConvex Community
Created by besimking on 12/28/2024 in #support-community
Generic Query and Mutations
Has anyone done something like this before?
8 replies
CCConvex Community
Created by besimking on 12/28/2024 in #support-community
Generic Query and Mutations
I need a structure like
const { data, isPending } = useConvexQuery(api.example.getExamples);

const { mutate, isPending } = useConvexMutation(api.example.createExamples);

const { mutate, isPending } = useConvexMutation(api.example.editExamples,{name:"Something",text:"Another Thing");
const { data, isPending } = useConvexQuery(api.example.getExamples);

const { mutate, isPending } = useConvexMutation(api.example.createExamples);

const { mutate, isPending } = useConvexMutation(api.example.editExamples,{name:"Something",text:"Another Thing");
8 replies
CCConvex Community
Created by besimking on 12/28/2024 in #support-community
Generic Query and Mutations
or
import { useMutation } from "convex/react";
import { useCallback, useMemo, useState } from "react";
import { Id } from "../../../../convex/_generated/dataModel";
import { api } from "../../../../convex/_generated/api";


type ResponseType = Id<"restaurants"> | null;
type RequestType = {
name: string;
text: string;

};

type Options = {
onSuccess?: (data: ResponseType) => void;
onError?: (error: Error) => void;
onSettled?: () => void;
throwError?: boolean;
};

export const useCreateExample = () => {
const [data, setData] = useState<ResponseType>(null);
const [error, setError] = useState<Error | null>(null);

const [status, setStatus] = useState<
"success" | "error" | "settled" | "pending" | null
>(null);

const isPending = useMemo(() => status === "pending", [status]);
const isSuccess = useMemo(() => status === "success", [status]);
const isError = useMemo(() => status === "error", [status]);
const isSettled = useMemo(() => status === "settled", [status]);

const mutation = useMutation(api.examples.createExamples);

const mutate = useCallback(
async (values: RequestType, options?: Options) => {
try {
setData(null);
setError(null);
setStatus("pending");

const response = await mutation({
name: values.name,
currency: values.currency,
image: values.image,
});
options?.onSuccess?.(response);
return response;
} catch (error) {
setStatus("error");
options?.onError?.(error as Error);
if (options?.throwError) {
throw error;
}
} finally {
setStatus("settled");
options?.onSettled?.();
}
},
[mutation],
);

return { mutate, data, error, isPending, isSuccess, isError, isSettled };
};
import { useMutation } from "convex/react";
import { useCallback, useMemo, useState } from "react";
import { Id } from "../../../../convex/_generated/dataModel";
import { api } from "../../../../convex/_generated/api";


type ResponseType = Id<"restaurants"> | null;
type RequestType = {
name: string;
text: string;

};

type Options = {
onSuccess?: (data: ResponseType) => void;
onError?: (error: Error) => void;
onSettled?: () => void;
throwError?: boolean;
};

export const useCreateExample = () => {
const [data, setData] = useState<ResponseType>(null);
const [error, setError] = useState<Error | null>(null);

const [status, setStatus] = useState<
"success" | "error" | "settled" | "pending" | null
>(null);

const isPending = useMemo(() => status === "pending", [status]);
const isSuccess = useMemo(() => status === "success", [status]);
const isError = useMemo(() => status === "error", [status]);
const isSettled = useMemo(() => status === "settled", [status]);

const mutation = useMutation(api.examples.createExamples);

const mutate = useCallback(
async (values: RequestType, options?: Options) => {
try {
setData(null);
setError(null);
setStatus("pending");

const response = await mutation({
name: values.name,
currency: values.currency,
image: values.image,
});
options?.onSuccess?.(response);
return response;
} catch (error) {
setStatus("error");
options?.onError?.(error as Error);
if (options?.throwError) {
throw error;
}
} finally {
setStatus("settled");
options?.onSettled?.();
}
},
[mutation],
);

return { mutate, data, error, isPending, isSuccess, isError, isSettled };
};
8 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
Problem solved. Princess saved. Thanks Spider-Man World Peace ☮️
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
I am trying
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
Yeah I've found that out 🤣 I am sorry for this
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
How exactly?
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
'Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
' +
'1. You might have mismatching versions of React and the renderer (such as React DOM)
' +
'2. You might be breaking the Rules of Hooks
' +
'3. You might have more than one copy of React in the same app
' +
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.'
'Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
' +
'1. You might have mismatching versions of React and the renderer (such as React DOM)
' +
'2. You might be breaking the Rules of Hooks
' +
'3. You might have more than one copy of React in the same app
' +
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.'
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
This is the thing and still no world peace
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
import { cronJobs } from "convex/server";
import { api } from "./_generated/api";

const crons = cronJobs();

crons.interval(
"get topics",
{ minutes: 50 },
api.geminiAction.getTopics
);


export default crons;
import { cronJobs } from "convex/server";
import { api } from "./_generated/api";

const crons = cronJobs();

crons.interval(
"get topics",
{ minutes: 50 },
api.geminiAction.getTopics
);


export default crons;
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
import { v } from "convex/values";
import { mutation } from "./_generated/server";
export const writeTopics = mutation({
args: {
data: v.object({
primaryKeyword: v.string(),
secondaryKeywords: v.array(v.string()),
})
},
handler: async (ctx, args) => {
return await ctx.db.insert('topics', args.data);
},
});
import { v } from "convex/values";
import { mutation } from "./_generated/server";
export const writeTopics = mutation({
args: {
data: v.object({
primaryKeyword: v.string(),
secondaryKeywords: v.array(v.string()),
})
},
handler: async (ctx, args) => {
return await ctx.db.insert('topics', args.data);
},
});
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
"use node";

import { GoogleGenerativeAI } from "@google/generative-ai";
import { action } from "./_generated/server"
import { api } from "./_generated/api";
import { useMutation } from "convex/react";


const genAi = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);

const model = genAi.getGenerativeModel({
model: "gemini-1.5-flash",
generationConfig: {
temperature: 0.7,
},
});

const targetAudience = "Restaurant and Cafe Owners";
const language = "English";
const prompt = `
Generate a JSON list containing 5 elements. Each element should be an object with the following structure:
.
.
.
`;

export const getTopics = action({
args: {},
handler: async (ctx) => {
const result = await model.generateContent(prompt);
const cleanData: Topic[] = JSON.parse(result.response.text().replace(/`|json/g, '').trim());
for (const data of cleanData) {
const mutate = useMutation(api.geminiMutation.writeTopics)
mutate({ data })
}
},
});

type Topic = {
primaryKeyword: string;
secondaryKeywords: string[];
}
"use node";

import { GoogleGenerativeAI } from "@google/generative-ai";
import { action } from "./_generated/server"
import { api } from "./_generated/api";
import { useMutation } from "convex/react";


const genAi = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);

const model = genAi.getGenerativeModel({
model: "gemini-1.5-flash",
generationConfig: {
temperature: 0.7,
},
});

const targetAudience = "Restaurant and Cafe Owners";
const language = "English";
const prompt = `
Generate a JSON list containing 5 elements. Each element should be an object with the following structure:
.
.
.
`;

export const getTopics = action({
args: {},
handler: async (ctx) => {
const result = await model.generateContent(prompt);
const cleanData: Topic[] = JSON.parse(result.response.text().replace(/`|json/g, '').trim());
for (const data of cleanData) {
const mutate = useMutation(api.geminiMutation.writeTopics)
mutate({ data })
}
},
});

type Topic = {
primaryKeyword: string;
secondaryKeywords: string[];
}
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
BTW if it was a need to make a schedule according to user interaction we have to use ctx.scheduler.runAfter. Is that so?
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
Let's say that I want to send a requst to gemini api every 5 minutes and I will directly record it to the database. I will create blog posts automatically. No direct or indirect user interaction is included for this process
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
Generally Scheduled
28 replies
CCConvex Community
Created by besimking on 12/4/2024 in #support-community
Cron Jobs, Actions, Internal Mutation
Just to be clear Create a Cron Job Define a cron job that will execute at regular intervals. The cron job will trigger an internal mutation. Call an Internal Mutation The cron job calls an internal mutation where the main logic is handled. Invoke the Action Inside the internal mutation, call an action that sends a request to the Gemini API. The action is responsible for fetching the necessary data. Process the Response Take the response from the action in the internal mutation, validate it, and ensure it's in the correct format. Insert into the Database in Internal Mutation Save the processed data into your database using the mutation. Is that correct?
28 replies