#make an example of using internalQuery

1 messages · Page 1 of 1 (latest)

warm orchid
#

@polar vale Make an example where you use internalQuery to retrieve value1 from a table. then use value1 to do whatever, for example create a new entry and put it as the value in a field in another table different from the one of value1

polar valeBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Sure, I can provide an example based on the information from the knowledge sources.

First, let's define an internalQuery to retrieve value1 from a table. Let's assume the table is named table1 and value1 is a field in this table.

import { internalQuery } from "./_generated/server";  
import { v } from "convex/values";  

export const getValue1 = internalQuery({  
 args: { id: v.id("table1") },  
 handler: async (ctx, args) => {  
  const doc = await ctx.db.get(args.id);
  return doc.value1;  
 },  
});  

Next, we can define an action that uses this internalQuery to retrieve value1 and then uses value1 to create a new entry in another table, let's say table2.

import { action } from "./_generated/server";  
import { internal } from "./_generated/api";  
import { v } from "convex/values";  

export const doSomething = action({  
 args: { id: v.id("table1") },  
 handler: async (ctx, args) => {  
  const value1 = await ctx.runQuery(internal.myFunctions.getValue1, {  
   id: args.id,  
  });  

  const newEntry = { field: value1 };
  await ctx.db.table("table2").insert(newEntry);
 },  
});  

In this example, doSomething is an action that takes an id of a document in table1, retrieves value1 from that document using the getValue1 internal query, and then inserts a new document in table2 with value1 as the value of a field.

Please note that you need to replace myFunctions with the actual filename where these functions are defined.

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@warm orchid kapa.ai is still learning and improving, please let us know how it did by reacting below

#

Let me check the knowledge base for you. One moment! :books:

#

Sure, you can also call an internalQuery from another query. Here's an example:

First, let's define an internalQuery to retrieve value1 from a table. Let's assume the table is named table1 and value1 is a field in this table.

import { internalQuery } from "./_generated/server";  
import { v } from "convex/values";  

export const getValue1 = internalQuery({  
 args: { id: v.id("table1") },  
 handler: async (ctx, args) => {  
  const doc = await ctx.db.get(args.id);
  return doc.value1;  
 }  
});  

Next, we can define a query that uses this internalQuery to retrieve value1 and then uses value1 to create a new entry in another table, let's say table2.

import { query } from "./_generated/server";  
import { internal } from "./_generated/api";  
import { v } from "convex/values";  

export const doSomething = query({  
 args: { id: v.id("table1") },  
 handler: async (ctx, args) => {  
  const value1 = await ctx.runQuery(internal.myFunctions.getValue1, {  
   id: args.id,  
  });  

  const newEntry = { field: value1 };
  await ctx.db.table("table2").insert(newEntry);
  return newEntry;
 }  
});  

In this example, doSomething is a query that takes an id of a document in table1, retrieves value1 from that document using the getValue1 internal query, and then inserts a new document in table2 with value1 as the value of a field.

Please note that you need to replace myFunctions with the actual filename where these functions are defined.

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@warm orchid kapa.ai is still learning and improving, please let us know how it did by reacting below

warm orchid
#

@polar vale sorry I wanted to say, call the internalQuery from a mutation action

polar valeBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Sure, you can call an internalQuery from a mutation. Here's an example:

First, let's define an internalQuery to retrieve value1 from a table. Let's assume the table is named table1 and value1 is a field in this table.

import { internalQuery } from "./_generated/server";  
import { v } from "convex/values";  

export const getValue1 = internalQuery({  
 args: { id: v.id("table1") },  
 handler: async (ctx, args) => {  
  const doc = await ctx.db.get(args.id);
  return doc.value1;  
 }  
});  

Next, we can define a mutation that uses this internalQuery to retrieve value1 and then uses value1 to create a new entry in another table, let's say table2.

import { mutation } from "./_generated/server";  
import { internal } from "./_generated/api";  
import { v } from "convex/values";  

export const doSomething = mutation({  
 args: { id: v.id("table1") },  
 handler: async (ctx, args) => {  
  const value1 = await ctx.runQuery(internal.myFunctions.getValue1, {  
   id: args.id,  
  });  

  const newEntry = { field: value1 };
  await ctx.db.table("table2").insert(newEntry);
  return newEntry;
 }  
});  

In this example, doSomething is a mutation that takes an id of a document in table1, retrieves value1 from that document using the getValue1 internal query, and then inserts a new document in table2 with value1 as the value of a field.

Please note that you need to replace myFunctions with the actual filename where these functions are defined.