Skip to content

Commit

Permalink
Merge pull request #137 from Serpico1899/main
Browse files Browse the repository at this point in the history
πŸ“ add E2E section to "add  relation function" document
  • Loading branch information
hemedani authored Jan 17, 2024
2 parents c4f12b1 + ea0577c commit 26b8830
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
10 changes: 9 additions & 1 deletion pages/src/add_more_relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,11 @@ Note that the `livedCities` field receives an array of IDs, you just need to ent

### Add E2E Test

Probably you know what to do! like before, you should change the country id and this time, change the city id. Of course that first you should add capture to lived cities.
Probably you know what to do! you should click on the e2e button (like bottom picture) to add your request to E2E section.

<img alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/fae58c10-f792-4041-89ca-186dc89bcee1">

And like before, you should change the country id and this time, change the city id. Of course that first you should add capture to lived cities.

In my example, i add the Hamedan city and Tehran city to my user. so , in the add city sequence of Hamedan, i click on the Add Capture button and fill the inputs like below picture.

Expand All @@ -629,3 +633,7 @@ Then, in the add user sequence, you can see the curent country id and also lived
Only thing you do is just to replace the country id and lived cities id with country id and cities id that you set in the sequences capture. in my example, country id is Β« {IranId} Β» and also my first city id is Β« {HamedanId} Β» and second one is Β«{TehranId}Β». like below picture.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/9582fd44-e8b1-4186-9335-b720be24190e">

And last thing you should done is that add capture to "add user" sequence for using in feature. i click on the Add Captue button and fill the inputs like below picture.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/a379db0f-614f-4ce5-8757-2994a12b26e7">
61 changes: 50 additions & 11 deletions pages/src/add_relation_fn.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# `addRelation` function

## Update Many to Many Relation

Pay attention to the following code:

```ts
const addUserLivedCityValidator = () => {
return object({
Expand All @@ -13,9 +16,7 @@ const addUserLivedCityValidator = () => {
};
const addUserLivedCity: ActFn = async (body) => {
const { livedCities, _id } = body.details.set;
const obIdLivedCities = livedCities.map(
(lc: string) => new ObjectId(lc),
);
const obIdLivedCities = livedCities.map((lc: string) => new ObjectId(lc));

return await users.addRelation({
_id: new ObjectId(_id),
Expand All @@ -37,27 +38,50 @@ coreApp.acts.setAct({
fn: addUserLivedCity,
});
```

In addition to the functions `insert`, `update`, `delete`, `find`, etc., for each model in the `Lesan`, there are two other functions in addition to `MongoDB` functions, named `addRelation` and `removeRelation`, which are prepared for managing relationships. In the code above, the `addRelation` function is used. This function receives an object input with the following keys:

- A `filter` key that receives [MongoDB findOne filter](https://www.mongodb.com/docs/manual/reference/method/db.collection.findOne/) and finds only one document to change its relationships.
- The `relations` key receives an object from the relations of this model. We talk about the relation input [here](https://miaadteam.github.io/lesan/add_relation.html#add-new-act-with-relation)
- The `projection` key is used to receive written data. Also we talk about `projection` key [here](https://miaadteam.github.io/lesan/getting_start.html#the-fn-function)
- And another key called `replace`, which is not used here, and receives a boolean value of `false` or `true`. We will talk about this key in the next step.

In the function above, we add one or more cities to the set of cities where a user has lived. In fact, in the validation function, the user ID is received along with an array of city IDs, and in the Act function, we convert the array of city IDs into an array of object IDs and give it to the `addRelation` function along with the user ID. As a result, on the `user` side, one or more cities are added to the `livedCities` array, and on the `city` side, this user is added to each of the cities whose IDs have been sent.
In the function above, we add one or more cities to the set of cities where a user has lived. In fact, in the validation function, the user ID is received along with an array of city IDs, and in the Act function, we convert the array of city IDs into an array of object IDs and give it to the `addRelation` function along with the user ID. As a result, on the `user` side, one or more cities are added to the `livedCities` array, and on the `city` side, this user is added to each of the cities whose IDs have been sent.

### Run the code

Since all the code is getting bigger and bigger, we put it on [GitHub](https://github.com/MiaadTeam/lesan/tree/main/examples/document), you can see and download it [here](https://raw.githubusercontent.com/MiaadTeam/lesan/main/examples/document/05-add-relation-fn-1.ts).
By running the codes and going to the playground, you can see and test the functions added to the user.
before execute `main` β†’ `country` β†’ `addUserLivedCities`:
before execute `main` β†’ `country` β†’ `addUserLivedCities`:
![Screenshot from 2024-01-07 13-30-44](https://github.com/MiaadTeam/lesan/assets/6236123/4d3c2797-2935-43f6-9ec3-d69c72874804)
executing `main` β†’ `country` β†’ `addUserLivedCities`:
executing `main` β†’ `country` β†’ `addUserLivedCities`:
![Screenshot 2024-01-07 at 19-12-39 Lesan Playground](https://github.com/MiaadTeam/lesan/assets/6236123/7e800a14-9e08-4f20-9b68-2fc537131f57)
after execute `main` β†’ `country` β†’ `addUserLivedCities`:
after execute `main` β†’ `country` β†’ `addUserLivedCities`:
![Screenshot from 2024-01-07 19-21-46](https://github.com/MiaadTeam/lesan/assets/6236123/aad25354-2ef6-4b5c-8b46-285f05488fc0)

### Add E2E Test

For adding "addUserLivedCities" request to E2E test section, you should click on the e2e button, like bottom picture.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/fae58c10-f792-4041-89ca-186dc89bcee1">

Then, you should click on the "Add Capture" button in the add city sequence and fill the inputs like below picture. in my example i add "Mashhad" city.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/ec9f2807-f547-4b40-b10f-d1d915239481">

After that, you should change the default user id add lived cities id with capture that you set in the past. the default id is like below picture.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/88a6aa3f-b2bd-4c4d-aeb2-a18807b57705">

And replace the capture you set in the past like below picture.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/7a886e68-0a11-4f50-a20a-408c9944cb11">

## Update One to Many Relation

What if in one of these sides our field is an object instead of an array (In fact, the type of relationship is one-to-many or many-to-one.)? For example, let's change the country on the user side.
Look at code below:

```ts
const addUserCountryValidator = () => {
return object({
Expand Down Expand Up @@ -92,12 +116,15 @@ coreApp.acts.setAct({
fn: addUserCountry,
});
```
In the code above, we get the ID of a user along with the ID of a country and give it to the `addRelation` function. Please note that the country is not `optional` in the user model and is defined with a `single` type. Therefore, if we change the country of a user, we must first find and delete this user in the country he was in before, then add the new country to the user and the user to the new country. For this reason, we have given the value `true` to the `replace` key in the `addRelation` entry. In this function, if we set `replace` equal to `false` or do not enter it, no operation will be performed and we will get an error.

In the code above, we get the ID of a user along with the ID of a country and give it to the `addRelation` function. Please note that the country is not `optional` in the user model and is defined with a `single` type. Therefore, if we change the country of a user, we must first find and delete this user in the country he was in before, then add the new country to the user and the user to the new country. For this reason, we have given the value `true` to the `replace` key in the `addRelation` entry. In this function, if we set `replace` equal to `false` or do not enter it, no operation will be performed and we will get an error.

### Steps to add a country to a user

> The bottom line is a bit complicated but has its own charm.
To change the country in a user, we must do the following steps:

- Find the user
- Finding the user's old country
- Find the user's new country
Expand All @@ -116,14 +143,26 @@ To change the country in a user, we must do the following steps:
- Execution of the `command` to insert the new country instead of the old country in this user.

### Run the code

Since all the code is getting bigger and bigger, we put it on [GitHub](https://github.com/MiaadTeam/lesan/tree/main/examples/document), you can see and download it [here](https://raw.githubusercontent.com/MiaadTeam/lesan/main/examples/document/05-add-relation-fn-2.ts).
By running the codes and going to the playground, you can see and test the functions added to the user.
before execute `main` β†’ `country` β†’ `addUserCountry`:
before execute `main` β†’ `country` β†’ `addUserCountry`:
![Screenshot from 2024-01-08 11-44-40](https://github.com/MiaadTeam/lesan/assets/6236123/9463858c-6fb6-422c-8757-56aa36813044)
executing `main` β†’ `country` β†’ `addUserCountry`:
executing `main` β†’ `country` β†’ `addUserCountry`:
![Screenshot 2024-01-08 at 11-45-55 Lesan Playground](https://github.com/MiaadTeam/lesan/assets/6236123/98fd47dc-02f7-44fa-be07-9e2478218465)
before execute `main` β†’ `country` β†’ `addUserCountry`:
before execute `main` β†’ `country` β†’ `addUserCountry`:
![Screenshot from 2024-01-08 11-47-02](https://github.com/MiaadTeam/lesan/assets/6236123/35a56cfd-c1c3-4ccd-8f09-5574725618cb)

### Add E2E Test

Like before, for adding "addUserCountry" request to E2E section you should click on the e2e button (like bottom picture) to add your request to E2E section.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/fae58c10-f792-4041-89ca-186dc89bcee1">

Then, also like before, you should replace the user id and country id that you set capture in own sequence with default user id and country id. default user and country id is like below picture.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/86eb134d-72ae-4294-ba7e-1e1008b74910">

The relaced user and country id is like below picture.

<img width="1680" alt="e2e sequence" src="https://github.com/MiaadTeam/lesan/assets/96171913/09a13ecc-e961-4427-8a53-284cf409f272">

0 comments on commit 26b8830

Please sign in to comment.