Replies: 1 comment 2 replies
-
I'll start with the schema:
A couple of notes on the schema as compared to that in the Prisma example:
With that schema, you can insert a post with new categories using this code in TypeScript: const client = createClient();
const insertPostQuery = e.insert(e.Post, {
title: "How to be Bob",
categories: e.set(
e.insert(e.Category, { name: "Category 1" }),
e.insert(e.Category, { name: "Category 2" })
),
});
await insertPostQuery.run(client); That's what you'd do if you needed to insert new categories. If const client = createClient();
const insertPostQuery = e.insert(e.Post, {
title: "How to be Bob Part 2",
categories: e.set(
e.select(e.Category, () => ({ filter_single: { name: "Category 1" } })),
e.select(e.Category, () => ({ filter_single: { name: "Category 2" } }))
),
});
await insertPostQuery.run(client); If you have existing categories and know the IDs of those categories rather than the names, you could do this instead: const client = createClient();
const insertPostQuery = e.insert(e.Post, {
title: "How to be Bob Part 2",
categories: e.set(
e.select(e.Category, () => ({ filter_single: { id: "<category-1-id>" } })),
e.select(e.Category, () => ({ filter_single: { id: "<category-2-id>" } }))
),
});
await insertPostQuery.run(client); replacing If you don't know whether the categories exist, you could do this: const client = createClient();
const insertPostQuery = e.insert(e.Post, {
title: "How to be Bob Part 2",
categories: e.set(
e.insert(e.Category, { name: "Category 1" }).unlessConflict((category) => ({on: category.name, else: category})),
e.insert(e.Category, { name: "Category 2" }).unlessConflict((category) => ({on: category.name, else: category})),
),
});
await insertPostQuery.run(client); Note that, as @Syzuna noted in their reply,
I hope this helps! |
Beta Was this translation helpful? Give feedback.
-
How to do create Post and CategoriesOnPosts in edgedb? can you give example using typescript please, thanks.
Beta Was this translation helpful? Give feedback.
All reactions