Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for updating link property #1118

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/update.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,53 @@ In the query builder this is represented with the following syntax.
}
}))

**Updating a single link property**

.. code-block:: typescript

e.update(e.Movie, (movie) => ({
filter_single: { title: "The Eternals" },
set: {
actors: {
"+=": e.select(movie.actors, (actor) => ({
"@character_name": e.str("Sersi"),
filter: e.op(actor.name, "=", "Gemma Chan")
}))
}
}
}));

**Updating many link properties**

.. code-block:: typescript

const q = e.params(
{
cast: e.array(e.tuple({ name: e.str, character_name: e.str })),
},
(params) =>
e.update(e.Movie, (movie) => ({
filter_single: { title: "The Eternals" },
set: {
actors: {
"+=": e.for(e.array_unpack(params.cast), (cast) =>
e.select(movie.characters, (character) => ({
"@character_name": cast.character_name,
filter: e.op(cast.name, "=", character.name),
})),
),
},
},
})),
).run(client, {
cast: [
{ name: "Gemma Chan", character_name: "Sersi" },
{ name: "Richard Madden", character_name: "Ikaris" },
{ name: "Angelina Jolie", character_name: "Thena" },
{ name: "Salma Hayek", character_name: "Ajak" },
],
});

Bulk updates
^^^^^^^^^^^^

Expand Down
33 changes: 32 additions & 1 deletion integration-tests/lts/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,41 @@ describe("update", () => {
}));
await q2.run(client);

const theAvengersCast: { name: string; character_name: string }[] = [
{ name: data.iron_man.name, character_name: "Tony Stark!" },
{ name: data.cap.name, character_name: "Steve Rogers!" },
{ name: data.thanos.name, character_name: "Thanos!" },
];

const q2CharName = e.params(
{ cast: e.array(e.tuple({ name: e.str, character_name: e.str })) },
(params) =>
e.update(theAvengers, (m) => ({
set: {
characters: {
"+=": e.for(e.array_unpack(params.cast), (cast) =>
e.select(m.characters, (c) => ({
"@character_name": cast.character_name,
filter: e.op(c.name, "=", cast.name),
})),
),
},
},
})),
);
await q2CharName.run(client, { cast: theAvengersCast });

const t2 = await e
.select(theAvengers, () => ({ id: true, characters: true }))
.select(theAvengers, () => ({
id: true,
characters: () => ({ "@character_name": true, name: true }),
}))
.run(client);
assert.equal(t2?.characters.length, 3);
const charSet = new Set(t2.characters.map((c) => c["@character_name"]));
assert.ok(charSet.has("Thanos!"));
assert.ok(charSet.has("Tony Stark!"));
assert.ok(charSet.has("Steve Rogers!"));

await e
.update(theAvengers, () => ({
Expand Down
Loading