# Add your first operation · Your first Rell app on Chromia · Learn Chromia

> Here you build the create_book operation, which lets you add new book entries to the blockchain by changing the dApp's state.

Source: https://chromia.com/learn/book-review/book-entity/basic-operations/

Your first Rell app on ChromiaLesson 6 of 26

1. [01Your first Rell app on Chromia](https://chromia.com/learn/book-review/introduction/)
2. [02Set up your project](https://chromia.com/learn/book-review/setup/)
3. [03Rell project structure](https://chromia.com/learn/book-review/rell-structure/)

Part 1: Define your first entity

1. [04Overview](https://chromia.com/learn/book-review/book-entity/)
2. [05Create your first entity](https://chromia.com/learn/book-review/book-entity/tables/)
3. [06Add your first operation](https://chromia.com/learn/book-review/book-entity/basic-operations/)
4. [07Query every book](https://chromia.com/learn/book-review/book-entity/write-queries/)

Part 2: Add a related entity

1. [08Overview](https://chromia.com/learn/book-review/book-review-entity/)
2. [09Define the book review entity](https://chromia.com/learn/book-review/book-review-entity/tables/)
3. [10Add an operation for creating book reviews](https://chromia.com/learn/book-review/book-review-entity/basic-operations/)
4. [11Query all reviews for a book](https://chromia.com/learn/book-review/book-review-entity/write-queries/)

Part 3: Checking and validating inputs

1. [12Overview](https://chromia.com/learn/book-review/input-verification/)
2. [13Verify inputs](https://chromia.com/learn/book-review/input-verification/input-verification/)
3. [14Return structured results from queries](https://chromia.com/learn/book-review/input-verification/structure/)

Part 4: Signing transactions and filtering queries

1. [15Overview](https://chromia.com/learn/book-review/sign-transaction/)
2. [16Sign a transaction](https://chromia.com/learn/book-review/sign-transaction/sign-transaction/)
3. [17Filter and sort query results](https://chromia.com/learn/book-review/sign-transaction/structure/)

Part 5: Blockchain state and transactions explained

1. [18Overview](https://chromia.com/learn/book-review/blockchain-transactions/)
2. [19How blockchain state and transactions work](https://chromia.com/learn/book-review/blockchain-transactions/query-transaction/)
3. [20Walking through an example](https://chromia.com/learn/book-review/blockchain-transactions/example/)

Part 6: Create the client

1. [21Overview](https://chromia.com/learn/book-review/build-client/)
2. [22Prerequisites](https://chromia.com/learn/book-review/build-client/prerequisites/)
3. [23Establishing a connection to Chromia](https://chromia.com/learn/book-review/build-client/sign-transaction/)
4. [24Query the blockchain using postchain-client](https://chromia.com/learn/book-review/build-client/query-blockchain/)
5. [25Complete the example](https://chromia.com/learn/book-review/build-client/complete-example/)

1. [26What’s next?](https://chromia.com/learn/book-review/what-next/)

[Learn](https://chromia.com/learn/)[Your first Rell app on Chromia](https://chromia.com/learn/book-review/)

06 / 26Part 1: Define your first entityBeginner

# Add your first operation

Here you build the `create_book` operation, which lets you add new book entries to the blockchain by changing the dApp's state.

## Define the `create_book` operation

An operation in Rell is a public function that must be signed and that changes blockchain state, specifically the state of your dapp. Operations are the main way you interact with your dapp and modify its state.

Add the following Rell code to `src/main/operations.rell`:

src/main/operations.rell

```
operation create_book(isbn: text, title: text, author: text) {
  create book(isbn = isbn, title = title, author = author);
}
```

### Breakdown of the code

* **`operation` keyword:** Declares a new operation. It behaves like a public function you can call to perform actions that modify your dapp's state.
* **Parameters:** `create_book` accepts `isbn`, `title`, and `author`, which map to the attributes of the `book` entity.
* **`create` command:** Within the operation, `create` adds a new book entry to the dApp's state from the supplied parameters.

## Understanding the process

The following shows, at a high level, what occurs when you run `create_book`:

The diagram below shows the transaction flow in more detail:

1. **User signs a transaction:** The user starts a transaction that calls `create_book`.
2. **Transaction validation:** A Chromia node checks the transaction and passes it on to the blockchain.
3. **State update:** `create_book` writes a new book entry to the dApp's state.
4. **Blockchain confirmation:** Once confirmed, the transaction becomes part of the blockchain and the dApp's state reflects the change.

## Add unit tests

Tests confirm that your operations behave correctly. Next, you add one for `create_book`.

Update `src/test/book_review_test.rell` with the test code so the file matches the following:

src/test/book\_review\_test.rell

```
@test module;

import main.{ book, create_book };

function test_add_book() {
    rell.test.tx()
        .op(create_book("123", "Book1", "Author1"))
        .op(create_book("124", "Book2", "Author2"))
        .run();

    val all_books = book @* { };

    assert_equals(all_books.size(), 2);
    assert_equals(all_books[0].title, "Book1");
    assert_equals(all_books[0].author, "Author1");
}
```

### Breakdown of the test

* **Transaction creation:** `rell.test.tx()` builds a test transaction.
* **Adding operations:** The transaction receives two `create_book` operations.
* **Execution:** `run()` runs the transaction.
* **Assertions:** The test fetches every book and checks that exactly two entries exist, each with the expected attributes.

## Running the test

From your project folder, run this command in the terminal:

shell

```nolines
chr test
```

## Testing on a local node

You can also try `create_book` against a local Chromia node:

1. **Start a local Chromia node:** In your terminal, run:

   shell

   ```nolines
   chr node start
   ```
2. **Add a book:** Open a separate terminal window or tab and run:

   shell

   ```nolines
   chr tx --await create_book "ISBN1234" "'1984'" "George Orwell"
   ```

You should see:

shell

```nolines
transaction with rid TxRid(rid=<RID>) was posted CONFIRMED
```

This output means the transaction is now on the blockchain and the `book` table holds a new row containing the book's data.

Written for this site from Chromia’s public course. Code samples are the course’s own.

[← Previous**Create your first entity**](https://chromia.com/learn/book-review/book-entity/tables/)[Next →**Query every book**](https://chromia.com/learn/book-review/book-entity/write-queries/)

On this page

* [Define the create\_book operation](#define-the-create_book-operation)
* [Understanding the process](#understanding-the-process)
* [Add unit tests](#add-unit-tests)
* [Running the test](#running-the-test)
* [Testing on a local node](#testing-on-a-local-node)
