Building Apps with Stampede
By: Anur Basic
Tags: Deno, Framework, Coding, Rest Api, Typescript

After two months of work, I have finally released the Alpha version of Stampede, a framework, or a very powerful eco-system based on Deno (a recently released TypeScript runtime). As I am very busy working full-time, it took more time than imagined to finally complete the initial version.

One of the coolest aspects of Stampede is that it already has a scaffolded Authentication system, tons of autowired modules which will be very useful once you start adding new features, as well as dozens of internal micro-libraries which speed up the whole development process.

Let’s take a look at the features below and a small tutorial on setting up your local environment and adding new features to your next project built with Stampede:

Link to the repository: https://github.com/bashovski/stampede

Homepage of Stampede’s UI layer (written in Vue.js)

Features of Stampede

Autowired modules and loaders — implicitly run codebase without piling up large imports

CLI — generate modules instantly and make progress without fuss!

Authentication system ready for use

Modified Koa Router utilization — easy-to-use Routing, HTTP request handlers, etc.

Deployment configuration which is production-ready

View layer written in Vue.js — includes auth pages, guards, etc.

DenoDB ORM along with PostgreSQL

Pre-written migrations with Umzug and Sequelize (there’s raw SQL as well)

Feature Flags — toggle which features should be running even during server runtime

Autowired config — adding and consuming environment variables kept elegant

Multi-level logger with DataDog support (allows usage of facets and tags)

Custom Mail module/internal micro-library compatible with SendGrid

Validators — validate and restrict invalid user input with large variety of options

Factories — populate your database with false records (powered by faker.js)

Route Guards for your Vue SPA — modify the behavior of page access layer the way you want to

Autowired CRON jobs

Unit tests with bootstrappers

Insomnia and Swagger doc for existing REST API endpoints

and many more…

The logo of the Stampede Framework

Setup/Installation

Stampede is made as a repository/template, so you can easily use it as a fundament for your next project.

In the image below, just click the ‘Use this template’ button.

Now you should pick an appropriate name for your project.

After creating and cloning your repository, we can already focus on using vital pieces of software which make Stampede very powerful at its very beginning.

Installing the CLI

Stampede’s CLI is used primarily to generate a few very consistent modules directly from templates. You can always update your templates inside /assets/module_templates directory.

Initially, CLI provides generation of models, routings, services and controllers. Later, new features will be added.

You can always check out the CLI repo: https://github.com/bashovski/stampede-cli

To install the CLI, as a MacOS user, run these commands:

You should be able to run the CLI normally. For other operating systems, check the official documentation/repo from the link above.

Setting up the environment variables

The next step is to create an .env file and update variables inside of it. In this example, we’ll only update mandatory ones. In the code sample below, we’ll create one using .env.example file as an example

The following four environment variables are considered mandatory:

Update them with your DB’s connection data, so we can later run migrations with Umzug.

The next step is to install Node dependencies, as Umzug can only be ran using Node. Please note that we won’t use Node at any other point, only while running migrations as running Node beats the purpose of using Deno. My own sophisticated migrations tool is in the Stampede Roadmap, but we are still far away from reaching that stage.

Once we have installed Node dependencies, we are ready to go.

Let’s first run the server (the first command), and later we’ll serve the UI.

To have both running simultaneously, open another tab/window in the terminal and run the second command from the root project directory.

If you did everything as it’s been shown in the previous steps, you have managed to have everything up and running. The migrations have been executed and the database is up-to-date with model schemas.

You should be receiving this response at: localhost:80/

For UI, you ‘ll need to install node modules by cd-ing into the ui directory and running ‘npm i’ command from it. After that you can serve UI.

Creating your own models, services and controllers

I personally consider this the most important part when it comes to not only understanding how Stampede works, but also seeing how easy it is to deliver swiftly and yet avoid a bunch of code written for no good reason.

Almost everything is autowired in the Stampede Framework, hence it’s important to develop a sense for what’s and what’s not already on the server.

Once you create a module (whose category considers them to be repeated), you don’t need to explicitly import and mention its usage. It’s already there.

Models, services, controllers, CRON jobs, factories, migrations, configuration files, routes and tests are autowired types of modules.

We’ll now create everything required for a model named Post — a replica of posts from literally any other website.

If you have installed a Stampede CLI, just run:

These three commands will create a model named Post, a service named PostService and a controller named PostController. Interestingly, when you’ve created a model, you also created something else: a routing module for the Post model (PostRouting.ts)

All of them will be placed at their respective subdirectories: /models, /routes, /services and /controllers.

Let’s add each required field and a function to generate/persist one record of a model named Post:

In the method generate(), we’ll need to know the Post owner and post’s data, hence we are passing the user’s ID and fields (derived from the request’s body).

This generate() function will be invoked from PostService.ts later:

Let’s now create a migration for the model above inside /db/migrations directory:

After adding those two, we should specify two routes for posts. Using one of them we are going to use to fetch all posts and create a new post and persist it into the database.

The first route doesn’t require the user to be logged in.

Now, let’s add a controller for our model. It handles request data and transports it into other layers of the server (services, helpers, model methods, etc.)

Now let’s add a service, which already knows the input data and prepares the request response, server state mutations and resource persistence:

Once you’ve added these, you ’ll be ready to restart the server and the new migration will be executed and you’ll be able to create new posts and list them all. Congrats!

The Stampede Framework at this stage is in alpha. I’ll keep on improving and stabilizing the framework and also provide guides on upgrading to the newer versions.

I am looking forward to seeing you use Stampede soon!