# #6 ⚙️Connect Database

## Create custom pipes

We will add two custom pipes in our app

* **Excerpt**: which will show a summary of post in blog card.
* **Slug**: which will show the URL slug for a post.

We will create a a new pipe named ExcerptPipe. Right-click on the folder 📁`custompipes` and then navigate to 'Angular Generator', select 'Pipe' and provide the name ‘excerpt.

Replace the `transform` method in the file 📝 `src/app/custompipes/excerpt.pipe.ts` with the following code :

```
transform(content: string) {
    const postSummary = content.replace(/(<([^>]+)>)/ig, '');
    if (postSummary.length > 300) {
    return postSummary.substr(0, 300) + ' [...]';
    } else {
    return postSummary;
    }
}
```

Furthermor we create a slug pipe. Right-click on the folder 📁`custompipes` and then navigate to 'Angular Generator', select 'Pipe' and provide the name ‘slug’.

Replace the `transform` method in the file 📝 `src/app/custompipes/slug.pipe.ts` with the following code:

```
transform(title: string) {
    const urlSlug = title.trim().toLowerCase().replace(/ /g, '-');
    return urlSlug;
}
```

## Get the blogs from database

We will add the `getAllPosts` method in 📝`src/app/services/blog.service.ts` file to fetch all the blog posts from database. The definition for `getAllPosts` is shown at [blog.service.ts (Github)](https://github.com/ng-girls/todo-list-tutorial/blob/master/workshops/blog-editor/code/06-services/blog.service.ts#L19-L29)

```
getAllPosts(): Observable<any> {
    const blogs = this.db.collection('blogs', ref => ref.orderBy('createdDate', 'desc')).snapshotChanges().pipe(
    map(actions => {
        return actions.map(
        c => ({
            postId: c.payload.doc.id,
            ...c.payload.doc.data()
        }));
    }));
    return blogs;
}
```


---

# 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://ng-girls.gitbook.io/todo-list-tutorial/more-workshops/index/06_connect_database.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.
