Logan Lee
09/22/2020, 12:48 PMawait prisma.A.create({ data }).then((e) => {
await prisma.B.delete({ where: { seq: e.seq } });
});
but delete API didn’t work even create was successed.
can i get help with it?afharo
09/22/2020, 12:51 PM.them
?Logan Lee
09/22/2020, 12:52 PMLogan Lee
09/22/2020, 12:53 PMafharo
09/22/2020, 12:55 PMseq
is seq
from A?Fran Zekan
09/22/2020, 1:03 PMconst e = await prisma.A.create({ data });
await prisma.B.delete({ where: { seq: e.seq } });
Or using regular then
prisma.A.create({data})
.then(e => prisma.B.delete({
where: { seq: e.seq }
}))
Ryan
09/22/2020, 1:08 PMLogan Lee
09/22/2020, 1:44 PMLogan Lee
09/22/2020, 1:45 PMLogan Lee
09/22/2020, 1:45 PMLogan Lee
09/22/2020, 3:48 PMBen Schwartz
09/22/2020, 7:58 PMawait
in front of the function, the other is to use .then() / .catch()
after the function
https://levelup.gitconnected.com/async-await-vs-promises-4fe98d11038fBen Schwartz
09/22/2020, 9:28 PMLogan Lee
09/22/2020, 9:28 PMprisma.posts.findMany().then((posts) => {
log('target posts ', posts.length);
posts.forEach(
({ author, content, create_dt, flag, from, hit, link, seq: origin_seq, title, upload_date }) => {
const data: past_postsCreateInput = {
from,
link,
title,
author,
content,
flag,
hit,
upload_date,
};
prisma.past_posts
.create({ data })
.then(({ seq }) => {
log(`CREATE ${seq}`);
prisma.posts.delete({ where: { seq: origin_seq } }).then(({ seq: delete_seq }) => {
log(`DELETE ${delete_seq}`);
});
})
.catch((e) => {
e;
});
},
);
});
delete API isn’t fired even thow seq is exist on table posts
that’s why i made this inqueryLogan Lee
09/22/2020, 9:28 PMBen Schwartz
09/22/2020, 9:30 PM.then()
calls. Go for something more like this
const posts = await prisma.posts.findMany()
log("target posts ", posts.length);
for await (const post of posts) {
const data: past_postsCreateInput = {
from: post.from,
link: post.link,
title: post.title,
author: post.author,
content: post.content,
flag: post.flag,
hit: post.hit,
upload_date: post.upload_date,
};
const newPost = await prisma.past_posts.create({ data })
log(`CREATE ${newPost.seq}`);
const deletedPost = await prisma.posts.delete({ where: { seq: origin_seq } });
log(`DELETE ${deletedPost.delete_seq}`);
}
Ben Schwartz
09/22/2020, 9:31 PMLogan Lee
09/22/2020, 9:47 PMBen Schwartz
09/22/2020, 9:50 PMPosts
with the origin_seq
Logan Lee
09/22/2020, 9:52 PMconst movePostsToPastPosts = async () => {
const posts = await prisma.posts.findMany();
log('target posts ', posts.length);
for await (const { from, link, title, author, content, flag, hit, upload_date, seq } of posts) {
const data: past_postsCreateInput = {
from,
link,
title,
author,
content,
flag,
hit,
upload_date,
};
const newPost = await prisma.past_posts.create({ data });
log(`CREATE ${newPost.seq}`);
const deletedPost = await prisma.posts.delete({ where: { seq } });
log(`DELETE ${deletedPost.seq}`);
}
};
Logan Lee
09/22/2020, 9:53 PMLogan Lee
09/22/2020, 9:53 PMLogan Lee
09/22/2020, 9:57 PMLogan Lee
09/22/2020, 9:58 PMconst movePostsToPastPosts = async () => {
const posts = await prisma.posts.findMany();
log('target posts ', posts.length);
// for await (const { from, link, title, author, content, flag, hit, upload_date, seq } of posts) {
await posts.forEach(async ({ from, link, title, author, content, flag, hit, upload_date, seq }) => {
const data: past_postsCreateInput = {
from,
link,
title,
author,
content,
flag,
hit,
upload_date,
};
const newPost = await prisma.past_posts.create({ data });
log(`CREATE ${newPost.seq}`);
const deletedPost = await prisma.posts.delete({ where: { seq } });
log(`DELETE ${deletedPost.seq}`);
});
};
Ben Schwartz
09/22/2020, 9:58 PMLogan Lee
09/22/2020, 10:04 PMLogan Lee
09/22/2020, 10:04 PMLogan Lee
09/22/2020, 10:04 PMBen Schwartz
09/23/2020, 2:49 PMafharo
09/24/2020, 3:03 PMfor
and use the array methods like .map
or .forEach
, you need to be aware of this:
.forEach
doesn’t expect you to return anything, so it will simply blindly skip anything you return.
In this case you are returning promises in that array (you are calling promise methods for each element), so you need to return those promises to await them or your program will finish before they complete.
One way is to do it with for
as suggested by @Ben Schwartz. The only problem is that you are awaiting each operation to be performed one-by-one.
If you want concurrency, you can use .map
in a similar fashion as you were using .forEach
, but you need to make sure you await all the promises you are returning:
prisma.posts.findMany().then((posts) => {
log('target posts ', posts.length);
const allPostPromises = posts.map(
({ author, content, create_dt, flag, from, hit, link, seq: origin_seq, title, upload_date }) => {
const data: past_postsCreateInput = {
from,
link,
title,
author,
content,
flag,
hit,
upload_date,
};
// We need to return the promise
return prisma.past_posts
.create({ data })
.then(({ seq }) => {
log(`CREATE ${seq}`);
// returning the promise
return prisma.posts.delete({ where: { seq: origin_seq } }).then(({ seq: delete_seq }) => {
log(`DELETE ${delete_seq}`);
});
})
.catch((e) => {
e;
});
},
);
// Let's return the promise that wraps all the list of promises we've created;
return Promise.all(allPostPromises);
});
FYI: creating async
methods and `await`ing the promises to finish can achieve a similar result. Following your last example:
const movePostsToPastPosts = async () => {
const posts = await prisma.posts.findMany();
log('target posts ', posts.length);
await Promise.all(
posts.map(async ({ from, link, title, author, content, flag, hit, upload_date, seq }) => {
const data: past_postsCreateInput = {
from,
link,
title,
author,
content,
flag,
hit,
upload_date,
};
const newPost = await prisma.past_posts.create({ data });
log(`CREATE ${newPost.seq}`);
const deletedPost = await prisma.posts.delete({ where: { seq } });
log(`DELETE ${deletedPost.seq}`);
})
);
};
As mentioned earlier, with .map
you’ll achieve concurrency (all the posts are created y and deleted in parallel), instead of going one by one as you’d do with a for
loop.
But beware, Promise.all
will fail as soon as 1 of the promises throws an error. You might want to use Promise.allSettled
(only available in NodeJS v12.9+) if you want to wait for all the promises to be completed (even when some have failed).Ben Schwartz
09/24/2020, 3:31 PMLogan Lee
09/24/2020, 9:03 PM