W
Wasp-langβ€’2y ago
imartinat

Processing List to add in DB

Thank you very much for your work on WASP and for the great documentation. I am having an issue, I am sending from the frontend 3 inputs at the same time to the backend. And from the action function I processed these 3 inputs and I created a list
[{input1,output1} ...]
[{input1,output1} ...]
then I tried to loop through this list add to the database each item of this list using context.entities.ENTITY.create but it doesn't work. It seems that an action function can only process 1 entity to add to the database. I also tried to use createMany with context entities but I have a TypeError in the console :
createMany is not a function
createMany is not a function
` Hope it makes sense. Please let me know if want me to share some code.
8 Replies
matijash
matijashβ€’2y ago
Hey @imartinat glad to hear you find the docs helpful! πŸ™‚ Action can deal with multiple entities, but you have to specify them in entities property in your .wasp file. Can you share your action code with us (wasp declaration and js/ts code)? Thanks!
imartinat
imartinatβ€’2y ago
Thanks for your quick answer, in my wasp file I have :
action createPrompt {
fn: import { createPrompt } from "@server/actions.js",
entities: [Prompt]
}
action createPrompt {
fn: import { createPrompt } from "@server/actions.js",
entities: [Prompt]
}
I should rename to createPrompList In actions.js, I have :
export const createPrompt = async (args, context) => {

const promptList = [args.input1, args.input2, args.input3] ;
const results = await getOuputs(promptList);
//Loop through results to create a row in DB for each //prompt(input,output) in results.
for (var i = 0; i < results.length; i++) {
const prompt = results[0];
context.entities.Prompt.create({
data: { input: prompt.input, output:prompt.output }
});
export const createPrompt = async (args, context) => {

const promptList = [args.input1, args.input2, args.input3] ;
const results = await getOuputs(promptList);
//Loop through results to create a row in DB for each //prompt(input,output) in results.
for (var i = 0; i < results.length; i++) {
const prompt = results[0];
context.entities.Prompt.create({
data: { input: prompt.input, output:prompt.output }
});
MEE6
MEE6β€’2y ago
Wohooo @imartinat, you just became a Waspeteer level 1!
miho
mihoβ€’2y ago
Since create is an async operation, you need to await each of them. For example like this:
for (var i = 0; i < results.length; i++) {
const prompt = results[i];
// Added await here
await context.entities.Prompt.create({
data: {
input: prompt.input,
output: prompt.output
}
});
}
for (var i = 0; i < results.length; i++) {
const prompt = results[i];
// Added await here
await context.entities.Prompt.create({
data: {
input: prompt.input,
output: prompt.output
}
});
}
imartinat
imartinatβ€’2y ago
Thank you very much. It now works. Would it be possible to use createMany from Prisma? I tried to use it with context entities but I have a TypeError in the console :
`createMany is not a function
`createMany is not a function
miho
mihoβ€’2y ago
Prisma doesn't support createMany unfortunately when using SQLite database 😒 https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#remarks-10 Your options are to move to PostgrSQL by adding
app XYZ {
...
db: {
system: PostgreSQL
}
}
app XYZ {
...
db: {
system: PostgreSQL
}
}
and providing a DATABASE_URL env variable in the .env.server https://wasp-lang.dev/docs/language/features#postgresql ... or continue using create in a loop as you are using it now.
imartinat
imartinatβ€’2y ago
Thank you very much for your explanation. I am just testing now with SQLite so I will keep using loops. But it's good to know if I switch to PostgreSQL . Thanks again for your help!
matijash
matijashβ€’2y ago
no problem! btw what are you building? would love to see it in action πŸ˜„ ah I see now you mentioned it in general, very cool πŸ™‚
Want results from more Discord servers?
Add your server