# Example 1 - Simple “Posts” application

### Schema

This is a simple representative schema for a PostgreSQL datasource, which has 4 objects (tables) - links, posts, topics, users - with relationships between these objects.

* This schema is in the Neurelo Schema Language JSON specification

<details>

<summary>JSON Schema</summary>

```json
{
  "objects": {
    "links": {
      "properties": {
        "id": {
          "type": "integer",
          "default": {
            "function": "autoincrement"
          },
          "identifier": true
        },
        "link": {
          "type": "string",
          "sourceType": [
            "VarChar",
            500
          ],
          "nullable": true
        },
        "title": {
          "type": "string",
          "sourceType": [
            "VarChar",
            500
          ],
          "nullable": true
        },
        "topic_id": {
          "type": "integer",
          "nullable": true
        },
        "topics": {
          "nullable": true,
          "$ref": "#/objects/topics",
          "relation": {
            "attrKey": [
              "topic_id"
            ],
            "foreignAttrKey": [
              "id"
            ],
            "onUpdate": "NoAction",
            "onDelete": "NoAction"
          }
        }
      }
    },
    "posts": {
      "properties": {
        "content": {
          "type": "string",
          "nullable": true
        },
        "id": {
          "type": "integer",
          "default": {
            "function": "autoincrement"
          },
          "identifier": true
        },
        "title": {
          "type": "string",
          "sourceType": [
            "VarChar",
            200
          ],
          "nullable": true
        },
        "user_id": {
          "type": "integer",
          "nullable": true
        },
        "users": {
          "nullable": true,
          "$ref": "#/objects/users",
          "relation": {
            "attrKey": [
              "user_id"
            ],
            "foreignAttrKey": [
              "id"
            ],
            "onUpdate": "NoAction",
            "onDelete": "NoAction"
          }
        }
      }
    },
    "topics": {
      "properties": {
        "description": {
          "type": "string",
          "nullable": true
        },
        "id": {
          "type": "integer",
          "default": {
            "function": "autoincrement"
          },
          "identifier": true
        },
        "links": {
          "type": "array",
          "items": {
            "$ref": "#/objects/links"
          }
        },
        "title": {
          "type": "string",
          "sourceType": [
            "VarChar",
            200
          ],
          "nullable": true
        }
      }
    },
    "users": {
      "properties": {
        "email": {
          "type": "string",
          "sourceType": [
            "VarChar",
            200
          ],
          "nullable": true
        },
        "first_name": {
          "type": "string",
          "sourceType": [
            "VarChar",
            100
          ],
          "nullable": true
        },
        "id": {
          "type": "integer",
          "default": {
            "function": "autoincrement"
          },
          "identifier": true
        },
        "last_name": {
          "type": "string",
          "sourceType": [
            "VarChar",
            100
          ],
          "nullable": true
        },
        "password": {
          "type": "string",
          "sourceType": [
            "VarChar",
            500
          ],
          "nullable": true
        },
        "posts": {
          "type": "array",
          "items": {
            "$ref": "#/objects/posts"
          }
        }
      }
    }
  }
}
```

</details>

### <mark style="color:green;">GET</mark> endpoint examples

* Retrieve all posts

  ```json
  GET https://$ENV_API_URL/rest/posts
  ```
* Retrieve a specific post by id

  ```json
  GET https://$ENV_API_URL/rest/posts/3
  ```

  or

  ```json
  GET https://$ENV_API_URL/rest/posts?filter={"id":3}
  ```
* Retrieve a specific user

  ```json
  GET https://$ENV_API_URL/rest/users?filter={"first_name":"Jane", "last_name":"Doe"}
  ```
* Retrieve the first 5 posts

  ```json
  GET https://$ENV_API_URL/rest/posts?take=5
  ```
* Retrieve all users and their posts

  ```json
  GET https://$ENV_API_URL/rest/users?select={"$scalars": true, "posts": true}
  ```
* Retrieve user with id 3 and all their posts

  ```json
  GET https://$ENV_API_URL/rest/users/3?select={"$scalars": true, "posts": true}
  ```
* Retrieve specific user and all their posts

  ```json
  GET https://$ENV_API_URL/rest/users?select={"$scalars": true, "posts": true}&filter={"first_name":"Jane", "last_name":"Doe"}
  ```

### <mark style="color:blue;">POST</mark> endpoint examples

* Create a new post

  ```json
  POST https://$ENV_API_URL/rest/posts

  {
          "content": "This is my first blog post",
          "title": "My first blog post"
  }
  ```
* Create a new post, a new user, and link them to each other

  ```json
  POST https://$ENV_API_URL/rest/posts

  {
    "content": "New POST",
    "title": "The new post",
    "users": {
      "create": {
        "email": "new-user@example.com",
        "first_name": "new",
        "last_name": "user",
        "password": "password"
      }
    }
  }
  ```
* Create a new user, 3 posts, link the posts to the user AND return the user scalars + related data (the 3 posts we just created)

  ```json
  POST https://$ENV_API_URL/rest/users?select={"$scalars": true, "$related": true}

  {
    "email": "multi-post@example.com",
    "first_name": "multi",
    "last_name": "post",
    "password": "manymanyposts",
    "posts": {
      "createMany": {
        "data": [
          {
              "content": "this is the first one",
              "title": "first one"
          },
          {
              "content": "this is the second one",
              "title": "second one"
          },
          {
              "content": "this is the third one",
              "title": "third one"
          }
        ]
      }
    }
  }
  ```

### <mark style="color:purple;">PATCH</mark> endpoint examples

* Update a post

  ```json
  PATCH https://$ENV_API_URL/rest/posts/7

  {
          "content": "This is my seventh blog post",
          "title": "My seventh blog post"
  }
  ```
* Update a post and its linked user

  ```json
  PATCH https://$ENV_API_URL/rest/posts/1

  {
    "content": "Newest post",
    "title": "The new post (revised)",
    "users": {
      "update": {
        "first_name": "updated",
        "last_name": "name"
      }
    }
  }
  ```

### <mark style="color:red;">DELETE</mark> endpoint examples

* Delete a post

  ```json
  DELETE https://$ENV_API_URL/rest/posts/4
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.neurelo.com/neurelo-api-reference-rest/examples-of-neurelo-auto-generated-rest-api-endpoints/example-1-simple-posts-application.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
