<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marco Iamonte</title>
    <description>The latest articles on DEV Community by Marco Iamonte (@briosheje).</description>
    <link>https://dev.to/briosheje</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F236881%2F9de957a8-a64b-4dd7-9785-3d5117f4625a.png</url>
      <title>DEV Community: Marco Iamonte</title>
      <link>https://dev.to/briosheje</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/briosheje"/>
    <language>en</language>
    <item>
      <title>Sample TodoApp in Fresh Deno framework</title>
      <dc:creator>Marco Iamonte</dc:creator>
      <pubDate>Sun, 03 Jul 2022 21:19:44 +0000</pubDate>
      <link>https://dev.to/briosheje/sample-todoapp-in-fresh-deno-framework-4ohi</link>
      <guid>https://dev.to/briosheje/sample-todoapp-in-fresh-deno-framework-4ohi</guid>
      <description>&lt;p&gt;New day and, yet, another framework. Yes, that's exactly how it has been for a few years, so why not trying it out?&lt;/p&gt;

&lt;p&gt;Recently, the &lt;a href="https://fresh.deno.dev" rel="noopener noreferrer"&gt;fresh&lt;/a&gt; framework came out and I've seen a whole lot of articles, drama and excitement about it, which made me wondering "fine, what's new about this?". So, I've decided to head to the homepage and find out what the hype is for, what's the good and what is (of course) the "bad".&lt;/p&gt;

&lt;p&gt;Heading to the website, you will immediately notice the awesome landing with the drop animation, which is a great start.&lt;br&gt;
Then, reading the core points, you will guess that the framework seems to be trying to take place where other bigger frameworks might fall: simplicity, clever SSR, reduced tooling and less overhead.&lt;/p&gt;

&lt;p&gt;Most of these points are something that usually triggers most of the frontend and full stack developers that needs to deliver performant, scalable, efficient and maintainable solutions: most of the times you spend most of the time dealing with &lt;code&gt;node_modules&lt;/code&gt;, making someone else able to compile your project because for unknown reasons he is using an older version of node (or a newer one), end up having your local build running but failing on the CI server for unknown reasons, needing to be able to customise the head tags for the SEO and other subtle things that makes the whole experience a bit less appealing that it should be.&lt;/p&gt;

&lt;p&gt;The goal of fresh, to me, seems to be reducing or removing the above steps, and it actually does it by:&lt;/p&gt;

&lt;p&gt;1) Not using node (it uses Deno instead).&lt;br&gt;
2) Gain appeal because it uses and supports &lt;a href="https://preactjs.com" rel="noopener noreferrer"&gt;preact&lt;/a&gt; out of box.&lt;br&gt;
3) Remove the build step.&lt;/p&gt;

&lt;p&gt;These three points are, in my opinion, the three reasons you might want to choose this framework over any other alternative (like nextjs, nuxtjs or whatever other one you like): it's simple, it handles basic features well, it's easy to use and reduces to the minimum the amount of javascript on the client side.&lt;/p&gt;

&lt;p&gt;I've tried the fresh framework yesterday and actually tried to replicate a project that was previously made in Vue and, although I had some small difficulties, the final project came out pretty well and the performances are actually admirable, though I do have some regrets of choosing this over nextjs which was, in this case, probably more suitable.&lt;/p&gt;

&lt;p&gt;Because every single time I see a tech news I usually see someone trying to run Doom on something new, I usually feel like for web devs the Doom equivalent is the Todo app. So, why not making one in Fresh and share the result? But hey, everyone would be able to do a Todo app, so why not adding complexity to it? Therefore, I came up with this idea: I want to create a fresh project that...:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shows how island works, meaning that some part of the project must use islands.&lt;/li&gt;
&lt;li&gt;Shows how things can be accomplished without Islands (and where).&lt;/li&gt;
&lt;li&gt;Shows how even an API can be developed with fresh.&lt;/li&gt;
&lt;li&gt;Deploy the application using Docker instead of the default way proposed by the fresh (using Deno deploy).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, in the next chapters, we will create an application that aims to show the core features of Fresh, with an extra attention on the backend layer, trying to focus on the core layer of the application, the data layer of the application and, finally, the application layer, by still keeping the opinionated structure created by the fresh authors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The next chapters assumes Deno is already installed in your environment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The final result will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztdbor2383bametdv1at.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztdbor2383bametdv1at.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The entire code that is explained and provided in this post can be found here: &lt;a href="https://github.com/briosheje/Fresh-Deno-Mongo-Docker-Todoapp" rel="noopener noreferrer"&gt;https://github.com/briosheje/Fresh-Deno-Mongo-Docker-Todoapp&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Chapter 1: scaffolding a project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To scaffold a project, simply run &lt;code&gt;deno run -A -r https://fresh.deno.dev my-app&lt;/code&gt;.&lt;br&gt;
In our case, since we already created a repository and already are in the target folder, we will rather run: &lt;code&gt;deno run -A -r https://fresh.deno.dev .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.&lt;/code&gt; specifies to create the project in the current folder.&lt;/p&gt;

&lt;p&gt;You might see this message: &lt;code&gt;The target directory is not empty (files could get overwritten). Do you want to continue anyway? [y/N]&lt;/code&gt; Hit yes in case you have nothing you need in the folder, otherwise move what you need and run the command again: the folder where you create a new project must be empty.&lt;/p&gt;

&lt;p&gt;For the UI layer, we will be using tailwind as it is shipped out with Fresh and as far as I could see, it's the only feasible solution right now, so when the prompt ask this:&lt;br&gt;
&lt;code&gt;Do you want to use 'twind' (https://twind.dev/) for styling? [y/N]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Answer &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you inspect the created folders and data (using, for example &lt;code&gt;ls&lt;/code&gt; in MacOS) you will see that the core project has been created and that the core dependencies have been downloaded.&lt;br&gt;
The project created will have the following folders:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;routes&lt;/strong&gt;: This is where routing happens and is handled. If you come from nextjs or similar frameworks, this will be extremely familiar: each file matches a route (or a pattern), each folder matches a sub route. Routes can be handler, allowing you to handle the verb and what the route actually do and might render something or return a simple response. More on this can be directly checked on the documentation, I won't focus much on this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;static&lt;/strong&gt;: This is where static assets are placed. Static assets are resolved using &lt;code&gt;/&lt;/code&gt; in html templates and css files and caching can be handled using &lt;code&gt;asset&lt;/code&gt; (&lt;a href="https://fresh.deno.dev/docs/concepts/static-files" rel="noopener noreferrer"&gt;read more about caching here&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;utils&lt;/strong&gt;: This folder contains various stuff, right now out of box it comes with a tailwind wrapper for the final application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;islands&lt;/strong&gt;: This is the most unique folder of the framework, since it contains preact components that will be hydrated. This folder cannot have subfolders and preact components &lt;strong&gt;cannot&lt;/strong&gt; have complex props like Functions, Date instances and anything which isn't a primitive or something JSON-serializable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To run the project and check everything is working, run &lt;code&gt;deno task start&lt;/code&gt;, which will be running the &lt;code&gt;start&lt;/code&gt; task in the &lt;code&gt;deno.json&lt;/code&gt; file, which basically runs the &lt;code&gt;dev.ts&lt;/code&gt; file and watches for changes on &lt;code&gt;static&lt;/code&gt; and &lt;code&gt;routes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, side note but in my opinion relevant note, fresh seems to prefer the &lt;code&gt;import_map&lt;/code&gt; approach with Deno, meaning that dependencies are mapped in a file and referred in code through dependency names rather than the URL to the dependency, so we will follow the preferred approach in the next chapters.&lt;/p&gt;

&lt;p&gt;If everything is running properly, we can procede with the next chapter: preparing to work with Docker.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chapter 2: MongoDB and Docker for the dev environment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because the fresh framework is agnostic about this, I will be doing this in my way, which is based on my experience: in our case, we will surely need a database. Whichever the database is (spoiler: it will be MongoDB), I usually like to virtualise everything where possible, but I also like to keep multiple ways of accomplishing the same task for my collaborators, so we will set up this project with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;docker-compose&lt;/code&gt; for development where the entire folder is mounted and Deno self-refreshes upon changes.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;docker-compose&lt;/code&gt; for production use where the environment is used to pass secrets to the application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For both environments, the application will need to know where the database is, so we are going to use &lt;code&gt;environment&lt;/code&gt; variables to do so, therefore we will install a dependency called &lt;a href="https://deno.land/x/dotenv" rel="noopener noreferrer"&gt;dotenv&lt;/a&gt; by adding it to the &lt;code&gt;import_map.json&lt;/code&gt; file: in case you are using visual studio code to develop, remember to cache the dependency.&lt;br&gt;
Since we will be using mongo, we will also install mongo.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;import_map.json&lt;/code&gt; file will then look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "imports": {
    "mongo": "https://deno.land/x/mongo@v0.30.1/mod.ts",
    "dotenv": "https://deno.land/x/dotenv@v3.2.0/mod.ts",
    "$fresh/": "https://deno.land/x/fresh@1.0.0/",
    "preact": "https://esm.sh/preact@10.8.1",
    "preact/": "https://esm.sh/preact@10.8.1/",
    "preact-render-to-string": "https://esm.sh/preact-render-to-string@5.2.0?deps=preact@10.8.1",
    "@twind": "./utils/twind.ts",
    "twind": "https://esm.sh/twind@0.16.17",
    "twind/": "https://esm.sh/twind@0.16.17/"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the dependencies are setup, let's procede by creating an environment file, adding it to .gitignore and creating a .env.example file to guide future users through the creation of the initial .env file.&lt;/p&gt;

&lt;p&gt;The .env file will be structured like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGODB_URI=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where the value of the environment variable will point to the desired mongodb instance.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;.env&lt;/code&gt; file will also be ignored since it will only be used in development, so let's add this to the &lt;code&gt;.gitignore&lt;/code&gt; (or create one if it does not exist yet):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once this is done, we will create a new folder where the core of our backend lives in, and we will be naming the folder &lt;code&gt;core&lt;/code&gt;. Inside the &lt;code&gt;core&lt;/code&gt; folder, we will create a file called &lt;code&gt;app-env.ts&lt;/code&gt; where we will handle the environment and export variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { config as dotEnvConfig } from "dotenv";
dotEnvConfig({
  export: true,
});

const appEnv = {
  MONGODB_URI: Deno.env.get("MONGODB_URI"),
};
const everyEnvVariableFilled = Object.values(appEnv).every(
  (v) =&amp;gt; v !== null &amp;amp;&amp;amp; v !== undefined &amp;amp;&amp;amp; v !== "" &amp;amp;&amp;amp; !Number.isNaN(v)
);
if (!everyEnvVariableFilled) {
  console.error(
    `Not all env variables are correctly compiled, please check that each env variable has a value.`
  );
  Deno.exit(1);
}

export default appEnv;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, the process will gracefully exit with an error if an environment variable is missing and needed to start the application.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;core&lt;/code&gt; folder, we will create a &lt;code&gt;data&lt;/code&gt; folder where we will be exposing the mongodb client. In a better architecture the data layer would be separated from the application layer, but because we are just making an example app I'm not going to separate this layer now.&lt;/p&gt;

&lt;p&gt;We will create the following structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;/data/models will hold the mongo/application models (in this case, these will be shared).&lt;/li&gt;
&lt;li&gt;/data/mongo-client.ts will export the client.&lt;/li&gt;
&lt;li&gt;/data/collections.ts will be an enum with the supported collections (currently one)&lt;/li&gt;
&lt;li&gt;/data/[entity] will hold utility functions for the specified database entity, like create, get, list and other actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;mongo-client.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { MongoClient } from "mongo";
import appEnv from "../app-env.ts";

const { MONGODB_URI } = appEnv;

const client = new MongoClient();

if (!MONGODB_URI) {
  console.error(`MONGODB Uri missing. Exiting.`);
  Deno.exit(1);
}

await client.connect(MONGODB_URI);
const database = client.database("TODO_APP");

export default database;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above file will use the environment to know where to connect to.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;collections.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export enum Collections {
  TODOS = "todos",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;models/todo.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ObjectId } from "mongo";

export interface Todo {
  _id: ObjectId;
  name: string;
  done: boolean;
}

export type NewTodo = Omit&amp;lt;Todo, "_id"&amp;gt;;

export function isNewTodo(item: any): item is NewTodo {
  return Boolean(item?.name) &amp;amp;&amp;amp; !item._id;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that we will also be providing utility methods like the type guard &lt;code&gt;isNewTodo&lt;/code&gt; in the models, these will be needed later.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;todo/create-todo.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Collections } from "../collections.ts";

import { Todo, NewTodo } from "../models/todo.ts";
import client from "../mongo-client.ts";

export default function createTodo(todo: NewTodo) {
  return client.collection&amp;lt;Todo&amp;gt;(Collections.TODOS).insertOne(todo);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;todo/get-todo.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Collections } from "../collections.ts";

import { ObjectId } from "mongo";
import { Todo } from "../models/todo.ts";
import client from "../mongo-client.ts";

export default function getTodo(id: string) {
  return client.collection&amp;lt;Todo&amp;gt;(Collections.TODOS).findOne({
    _id: new ObjectId(id),
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;todo/list-todo.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Collections } from "../collections.ts";

import { Todo } from "../models/todo.ts";
import client from "../mongo-client.ts";

export default function listAllTodos() {
  return client.collection&amp;lt;Todo&amp;gt;(Collections.TODOS).find().toArray();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;todo/toggle-todo-done.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ObjectId } from "mongo";

import { Collections } from "../collections.ts";
import { Todo } from "../models/todo.ts";
import client from "../mongo-client.ts";
import getTodo from "./get-todo.ts";

export default async function toggleTodoDone(todoId: string) {
  const collection = client.collection&amp;lt;Todo&amp;gt;(Collections.TODOS);
  const todoDoc = await getTodo(todoId);

  if (!todoDoc) {
    throw new Error(`Record with id ${todoId} was not found.`);
  }

  return collection.updateOne(
    {
      _id: new ObjectId(todoId),
    },
    {
      $set: {
        ...todoDoc,
        done: !todoDoc.done,
      },
    }
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that all the above methods were wrote down just to test the framework, these should be written better in a production-ready environment.&lt;/p&gt;

&lt;p&gt;This data layer will allow us to later retrieve and alter data from the database, but because we actually need a database, we will take advantage of docker and the docker environment plus Makefile commands to create routines that allow us to test everything locally.&lt;/p&gt;

&lt;p&gt;Because we are planning to support at least two environments, we will be shipping two docker-compose files: although the production environment might not use docker-compose, we will still create a sample one for production.&lt;/p&gt;

&lt;p&gt;In our dev docker-compose, we want to have a stack that includes mongo and a UI to manage the database. I wish I could also mount Deno and use it with Fresh, but it looks like it currently does not work, so our &lt;code&gt;docker-compose-dev.yml&lt;/code&gt; will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3.4"

services:
  todo-mongo-dev:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    ports:
      - 27017:27017
    networks:
      - todo-app-dev-network

  mongo-express:
    image: mongo-express:latest
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@todo-mongo-dev:27017/
    networks:
      - todo-app-dev-network
networks:
  todo-app-dev-network:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because we are using this only in localhost, we don't care about any environment variable and any secure credential: this stack will be only used in development mode.&lt;/p&gt;

&lt;p&gt;Next step is configuring our Deno application to know what is the url to connect for the mongo database, and this is where the &lt;code&gt;.env&lt;/code&gt; file we have created before will be needed: in our .env file we will define the mongodb url so that our application can connect to the database, so our .env file will now become this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGODB_URI=mongodb://root:example@127.0.0.1:27017/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, when the Deno application runs, &lt;code&gt;dotenv&lt;/code&gt; will load the environment variables from the &lt;code&gt;.env&lt;/code&gt; file directly.&lt;/p&gt;

&lt;p&gt;To run the stack, we will create the following &lt;code&gt;Makefile&lt;/code&gt; and  run the &lt;code&gt;run-dev&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;run-dev:
    docker-compose -f ./docker-compose-dev.yml down
    docker-compose -f ./docker-compose-dev.yml rm
    docker-compose -f ./docker-compose-dev.yml build
    docker-compose -f ./docker-compose-dev.yml up -d --remove-orphans
    # Sleep below is needed to wait for mongo to start, otherwise we would need 
    # to handle that in code.
    sleep 5
    deno task start

dev-down:
    docker-compose -f ./docker-compose-dev.yml down
    docker-compose -f ./docker-compose-dev.yml rm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;make run-dev&lt;/code&gt; should take up the stack (or down in case it is up and then up again), should wait 5 seconds (which should be enough for mongo to start listening) and then will run &lt;code&gt;deno task start&lt;/code&gt; to run the stack that will load the .env file and connect to the database.&lt;/p&gt;

&lt;p&gt;We now should be good to go with the next chapter: wiring the client and the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chapter 3: wiring client and server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a database inside our stack and we have our data layer done, we can focus on the frontend and the backend: because this is a demo, the core idea is to show the features of the framework, therefore we are going to implement the following:&lt;/p&gt;

&lt;p&gt;1) Form testing: use a POST form submit to the same page to create a new Todo record.&lt;br&gt;
2) API testing: make an api endpoint like &lt;code&gt;/api/v1/todo/[id]/toggleState&lt;/code&gt; with PUT verb to toggle the state of a todo. In this case, a page refresh should not occur.&lt;/p&gt;

&lt;p&gt;Before doing both, however, we will need to first display the current todos, so our "index" will first need to do so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/routes/index.tsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @jsx h */
import { h } from "preact";
import { tw } from "@twind";
import { Handlers, HandlerContext, PageProps } from "$fresh/server.ts";

import { Todo } from "../core/data/models/todo.ts";
import listAllTodos from "../core/data/todo/list-todo.ts";

type TodosProps = {
  allTodos: Todo[];
};

export const handler: Handlers&amp;lt;TodosProps&amp;gt; = {
  async GET(_req, ctx) {
    const allTodos = await listAllTodos();

    return ctx.render({
      allTodos: allTodos ?? [],
    });
  },
}

export default function Todos(props: PageProps&amp;lt;TodosProps&amp;gt;) {
  const { data } = props;
  const { allTodos } = data;

  return (
    &amp;lt;div
      className={tw`h-100 w-full flex flex-col items-center justify-center bg-teal-lightest font-sans`}
    &amp;gt;
      &amp;lt;div
        className={tw`bg-white rounded shadow p-6 m-4 w-full lg:w-3/4 lg:max-w-lg`}
      &amp;gt;
        &amp;lt;h2&amp;gt;Todo Items - Items uses the API endpoints to update data.&amp;lt;/h2&amp;gt;
        &amp;lt;hr className={tw`mb-1`} /&amp;gt;
        {allTodos.map((todo) =&amp;gt; (
          &amp;lt;span&amp;gt;TODO todo island component&amp;lt;/span&amp;gt;
        ))}
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will handle the &lt;code&gt;GET&lt;/code&gt; request, fetch all the current todos (beware: a distributed cache here would be awesome) and render the page by injecting in a react-way the current todos. Everything you see above happens at server side, no javascript will be injected to the client side and the template is compiled at server side.&lt;/p&gt;

&lt;p&gt;Once the todos are ready, we then need to populate these, so we need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a form&lt;/li&gt;
&lt;li&gt;Find a way to handle the form submit on the same page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To accomplish so, we will start with a simple form, which will be placed under our Todo list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      &amp;lt;hr /&amp;gt;
      &amp;lt;div
        className={tw`bg-white rounded shadow p-6 m-4 w-full lg:w-3/4 lg:max-w-lg`}
      &amp;gt;
        &amp;lt;h2&amp;gt;
          Create a new todo: this approach uses a SUBMIT action against the same
          page.
        &amp;lt;/h2&amp;gt;
        &amp;lt;form method="POST" className={tw`flex flex-col font-sans`}&amp;gt;
          &amp;lt;input /&amp;gt;
          &amp;lt;div className={tw`mb-4`}&amp;gt;
            &amp;lt;label
              className={tw`block text-gray-700 text-sm font-bold mb-2`}
              for="todo-name"
            &amp;gt;
              Name
            &amp;lt;/label&amp;gt;
            &amp;lt;input
              className={tw`shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline`}
              id="todo-name"
              type="text"
              name="name"
            /&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;button
            className={tw`bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline`}
            type="submit"
          &amp;gt;
            Add todo
          &amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
      &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This form will perform submit a POST request against our index, so we will handle that and make it re-render the page with the list of the current todos, so our handler will become:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const handler: Handlers&amp;lt;TodosProps&amp;gt; = {
  async GET(_req, ctx) {
    const allTodos = await listAllTodos();

    return ctx.render({
      allTodos: allTodos ?? [],
    });
  },
  async POST(req, ctx) {
    const formData = await req.formData();

    const jsonData = Object.fromEntries(formData);
    if (isNewTodo(jsonData)) {
      await createTodo({
        ...jsonData,
        done: false,
      });
    }
    const allTodos = await listAllTodos();

    return ctx.render({
      allTodos: allTodos ?? [],
    });
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we will also need to import &lt;code&gt;isNewTodo&lt;/code&gt;, and that in the &lt;code&gt;req&lt;/code&gt; object we can actually fetch the form data from the client: through he form data, the record is created and all the records are returned.&lt;/p&gt;

&lt;p&gt;Hence, upon a post request, the server will create the record in case it is a valid Todo item and will render the page with the updated list of Todo Items.&lt;/p&gt;

&lt;p&gt;Next, we need to handle the second point: we should allow the final user to toggle the state of a todo, and for the sake of testing the framework we want to do that through an api endpoint (although it's of course unnecessary).&lt;/p&gt;

&lt;p&gt;To accomplish so, we will create 4 nested folders inside the routes folder: &lt;code&gt;api&lt;/code&gt;, &lt;code&gt;v1&lt;/code&gt;, &lt;code&gt;todo&lt;/code&gt; and &lt;code&gt;[todoId]&lt;/code&gt;, next we will create inside the &lt;code&gt;[todoId]&lt;/code&gt; folder the &lt;code&gt;toggleState.ts&lt;/code&gt; file: the final endpoint will be &lt;code&gt;/api/v1/todo/[todoId]/toggleState&lt;/code&gt;, and &lt;code&gt;toggleState.ts&lt;/code&gt; will hold this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Handlers } from "$fresh/server.ts";

import toggleTodoDone from "../../../../../core/data/todo/toggle-todo-done.ts";

export type ToggleStateResponse = {
  modifiedCount: number;
};

export const handler: Handlers&amp;lt;ToggleStateResponse&amp;gt; = {
  async PUT(_req, ctx) {
    const { modifiedCount } = await toggleTodoDone(ctx.params.todoId);

    return new Response(
      JSON.stringify({
        modifiedCount,
      }),
      {
        headers: {
          "content-type": "application/json",
        },
      }
    );
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, differently from the previous index file, we are only using the Handler to actually handle the &lt;code&gt;PUT&lt;/code&gt; method against this route, to alter the data on the database and to return a simple response with content-type application/json.&lt;/p&gt;

&lt;p&gt;Finally, we should now handle how the client will request the change to the backend and, because it is preact... I do that with an hook.&lt;/p&gt;

&lt;p&gt;Because such hook might be used somewhere else for whatever reasons, I usually like to keep ui elements and utilities in a separate folder, so we are going to make a &lt;code&gt;ui&lt;/code&gt; folder with an &lt;code&gt;hooks&lt;/code&gt; folder inside it, so that we can later use it.&lt;/p&gt;

&lt;p&gt;Since our endpoint returns an information that we don't need, we will just need to handle three scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The request is pending (loading).&lt;/li&gt;
&lt;li&gt;The request raised an error.&lt;/li&gt;
&lt;li&gt;The request ended with a positive status.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To handle all of these, our &lt;code&gt;/ui/hooks/useToggleTodoState.tsx&lt;/code&gt; hook will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @jsx h */
import { h } from "preact";
import { useCallback, useState } from "preact/hooks";

export default function useToggleTodoState(onSuccess: () =&amp;gt; void) {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState&amp;lt;Error&amp;gt;();

  const toggleTodoState = useCallback(
    (todoId: string) =&amp;gt; {
      setLoading(true);
      setError(void 0);

      return fetch(`/api/v1/todo/${todoId}/toggleState`, {
        method: "PUT",
      })
        .then((data) =&amp;gt; {
          setLoading(false);
          if ([200, 204].includes(data.status)) {
            onSuccess();
          }
        })
        .catch((error) =&amp;gt; setError(error))
        .finally(() =&amp;gt; setLoading(false));
    },
    [setLoading, setError, onSuccess]
  );

  return {
    loading,
    error,
    toggleTodoState,
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note the &lt;code&gt;onSuccess&lt;/code&gt; callback that is meant to be invoked upon a successful response: this hook will allow us to invoke the API endpoint and update the status of a todo, now we just need to wire things up with a component that displays the todo and allows us to update its status.&lt;/p&gt;

&lt;p&gt;Because the component is meant to add interaction to the index page, it will be located in the &lt;code&gt;islands&lt;/code&gt; folder and will be hydrated after the page is rendered: therefore, since our component will render a TodoItem, we will call it &lt;code&gt;TodoItem.tsx&lt;/code&gt; and it will be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @jsx h */
import { h } from "preact";
import { useCallback, useState } from "preact/hooks";
import { tw } from "@twind";

import useToggleTodoState from "../ui/hooks/useToggleTodoState.tsx";
import { Todo } from "../core/data/models/todo.ts";

export type TodoItemProps = {
  value: Todo;
};

export default function TodoItem(props: TodoItemProps) {
  const { value } = props;

  const [done, setDone] = useState(value.done);

  const onStatusUpdated = useCallback(() =&amp;gt; {
    setDone((prev) =&amp;gt; !prev);
  }, [setDone]);

  const { loading, error, toggleTodoState } =
    useToggleTodoState(onStatusUpdated);

  const toggleState = useCallback(() =&amp;gt; {
    toggleTodoState(value._id.toString());
  }, [value]);

  return (
    &amp;lt;div className={tw`flex mb-4 items-center`}&amp;gt;
      &amp;lt;p className={tw`w-full text-grey-darkest`}&amp;gt;{value.name}&amp;lt;/p&amp;gt;
      &amp;lt;button
        disabled={loading}
        onClick={toggleState}
        className={tw`flex-no-shrink p-2 ml-4 mr-2 border-2 rounded text-green border-green hover:bg-green`}
      &amp;gt;
        {done ? "Mark as not done" : "Mark as done"}
      &amp;lt;/button&amp;gt;
      {error}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see it just looks like a regular react component, although you might notice that it is using models taken from the &lt;code&gt;core&lt;/code&gt; folder but because in the models file we are using nothing incompatible it won't affect the rendering of the page.&lt;/p&gt;

&lt;p&gt;Now, we just need to use the component in our homepage, so we will replace the list placeholder with this item and our homepage list section will be this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   &amp;lt;div
        className={tw`bg-white rounded shadow p-6 m-4 w-full lg:w-3/4 lg:max-w-lg`}
      &amp;gt;
        &amp;lt;h2&amp;gt;Todo Items - Items uses the API endpoints to update data.&amp;lt;/h2&amp;gt;
        &amp;lt;hr className={tw`mb-1`} /&amp;gt;
        {allTodos.map((todo) =&amp;gt; (
          &amp;lt;TodoItem value={todo} /&amp;gt;
        ))}
      &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that everything is wired up, we will also configure Docker to run in a production-like environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chapter 4: Dockerfile and configuring for production&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a production environment, two steps should be taken:&lt;/p&gt;

&lt;p&gt;1) Build the docker image and tag it.&lt;br&gt;
2) Use it in some way.&lt;/p&gt;

&lt;p&gt;In our case, we will be assuming for testing purposes that for scenario 2 we will be using docker-compose, but this solution actually works with anything that can actually run an image.&lt;/p&gt;

&lt;p&gt;First of all, we will need to write our Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM denoland/deno:alpine-v1.23.0

EXPOSE 8000
WORKDIR /app

COPY . .

# Cache dependencies
RUN deno cache main.ts --import-map=import_map.json

# No need to check about --allow-all because it's already sandboxed
CMD ["run", "--allow-all", "main.ts"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Briefly explained: we are using the 1.23.0 alpine Deno image, exposing port 8000 (used by fresh by default), caching the dependencies and running &lt;code&gt;main.ts&lt;/code&gt;. This will listen on port 8000 and won't watch any file.&lt;/p&gt;

&lt;p&gt;Now, we need to make a docker-compose that uses the image an we need to tag it. For this example, we will assume our image will be named &lt;code&gt;fresh-todo-app-test&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our docker-compose will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3.4"

services:
  todo-app-fresh:
    image: fresh-todo-app-test:latest
    container_name: todo-app-fresh
    restart: always
    ports:
      - ${PUBLIC_PORT:-8000}:8000
    environment:
      MONGODB_URI: mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@todo-mongo:27017/
    networks:
      - todo-app-network

  todo-mongo:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD}
    networks:
      - todo-app-network
    volumes:
      - mongo-data:/data/db

  mongo-express:
    image: mongo-express:latest
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGODB_USERNAME}
      ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGODB_PASSWORD}
      ME_CONFIG_MONGODB_URL: mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@todo-mongo:27017/
    networks:
      - todo-app-network

networks:
  todo-app-network:

volumes:
  mongo-data:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please not that the &lt;code&gt;environment&lt;/code&gt; in docker-compose is set up  assuming that the mongodb username and password are in the .env file, so we will also need to update our &lt;code&gt;.env&lt;/code&gt; file to have these:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.env&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGODB_URI=mongodb://root:example@127.0.0.1:27017/
MONGODB_USERNAME=root
MONGODB_PASSWORD=test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the first variable will be used locally, while the second and third variables will be used in docker-compose.&lt;/p&gt;

&lt;p&gt;Now that everything is set up, we will update our &lt;code&gt;Makefile&lt;/code&gt; with the commands needed to build and deploy the stack:&lt;br&gt;
&lt;code&gt;Makefile&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build-and-tag-image:
    docker build -t fresh-todo-app-test .

deploy-stack:
    docker-compose -f ./docker-compose.yml --env-file .env down
    docker-compose -f ./docker-compose.yml --env-file .env rm
    docker-compose -f ./docker-compose.yml --env-file .env build
    docker-compose -f ./docker-compose.yml --env-file .env up -d --remove-orphans

build-and-deploy-stack: build-and-tag-image deploy-stack

stack-down:
    docker-compose -f ./docker-compose.yml --env-file .env down
    docker-compose -f ./docker-compose.yml --env-file .env rm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, running &lt;code&gt;make build-and-deploy-stack&lt;/code&gt; you should see the stack coming up and it should be available in seconds, you might notice some restarts in case the Deno app starts but the mongodb database is not yet ready.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fth4m4x2qfj1tit4uo9hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fth4m4x2qfj1tit4uo9hj.png" alt="Stack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, the application should be ready and we did a todo app with fresh!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chapter 5: conclusions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fresh seems to be a very interesting framework with, in my opinion, a very specific target: monolithic applications (or at least something with frontend and backend together) with SSR on steroids and an opinionated structure with a particular attention on keeping things simple but at the same time incredibly elastic.&lt;/p&gt;

&lt;p&gt;It's a pleasure, in my opinion, to see something built on Deno that is currently at least trying to shade a bit the current huge javascript frameworks, although I'm feeling that many targets are still way more suitable for next.js and similar frameworks.&lt;/p&gt;

&lt;p&gt;However, since we talked about great things only, I feel we should also mention that there actually are scenarios where the framework does not yet shine that much, which is everything where styling is involved.&lt;/p&gt;

&lt;p&gt;Currently, the framework supports tailwind out of box, but in case you want to use a simple custom CSS you will need to wrap your head around assets and &lt;strong&gt;manually&lt;/strong&gt; do assets optimisation, meaning that there is literally no tooling around this issue. To give a comparison, next.js compiles .scss files and whatever kind of style you want through react and will ship the bundled styles by tree shaking and, thus, optimising the final bundle.&lt;/p&gt;

&lt;p&gt;That said, it looks like this framework can have a bright future, I hope you've found the post useful!&lt;/p&gt;

</description>
      <category>deno</category>
      <category>fresh</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
