<commit_before># Component basics

@TODO<commit_msg>Add component-basics.md tutorial<commit_after>[pino-home]: https://www.npmjs.com/package/pino

# Component basics

## What is a component?

A component is (usually) a class which implements some kind of functionality that can be used by the framework or by your own code. Currently there are 3 types of components:

- [`Service`](writing-services.md) - interfaces with some foreign API/interface and exposes a set of functionality to work with the service (ie. a database or a remote REST API server)
- [`Hook`](writing-hooks.md) - listens for events fired by the Application and reacts to those events (ie. a hook which starts some workers just after the application has started up)
- [`Action`](writing-actions.md) - Implements some business-specific functionality which can then be called from ie. an http server's route handler or from a CLI utility

While each component serves a different purpose, they do share some similarities. Let's look what you can do inside a component.

## Inside a component

When implementing components, the following properties are available as soon as the class is constructed:

- `app`: The main `Application` instance - useful to get information about the `root` path, `env` selected or whether the app is `started` or `prepared`.
- `config`: Your component's configuration options as supplied by the user, with your component's default values already applied.
- `log`: A [pino][pino-home] instance with some pre-defined keys set so that it's clear that the logs are coming from this component. You can pass this logger instance down to your sub-components, if you like.
- `component`: Use this function to obtain another component

## Examples

Let's say we are writing an action component... We have a basic class like this one:

```js
import Action from '@atlas.js/service'

class MyAction extends Action {
  doThing() {

  }
}
```

From the `doThing()` method, we can do various things!

### Inspect the Action's config

```js
console.log(this.config)
```

### Inspect the current environment

```js
console.log(this.app.env)
```

### Get the path to the application folder

```js
console.log(this.app.root)
```

### Log something to the console

```js
this.log.info({ data: true }, 'log entry with data')
```
{'fraction_non_alphanumeric': 0.09205384281372124, 'fraction_numerical': 0.0004342162396873643, 'mean_word_length': 4.459715639810427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2988066', 'n_tokens_mistral': 633, 'n_tokens_neox': 595, 'n_words': 310}