A complete guide for developing a backend application with CRUD operations along with database connection to MySQL.

Deno.js is a new backend language based on the javascript framework. Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. In this tutorial, we’ll learn how to develop a complete CRUD web application using Deno js (Oak as a framework) and using Mysql as database.

Overview

This project takes an example of an Employee object having four attributes id, name, department, and isActive. We will proceed with adding an employee object in our DB and then performing further operations.

Dependencies

No dependency required. Just install Deno using curl -fsSL https://deno.land/x/install/install.sh | shand set up your DENO_INSTALL path in .bashrc.

Configuration File(server.ts):

server.ts is the main configuration file where all the configuration combines such as route definitions, port, and even logging component.

TypeScript

const app = new Application();
const port: number = 8080;
app.use(logger.logger);
app.use(logger.responseTime);
app.use(employeeRouter.routes());
app.use(employeeRouter.allowedMethods());
app.addEventListener("listen", ({ secure, hostname, port }) => {
  const protocol = secure ? "https://" : "http://";
  const url = `${protocol}${hostname ?? "localhost"}:${port}`;
  console.log(
    `${yellow("Listening on:")} ${green(url)}`,
  );
});
await app.listen({ port });

Interface Layer(Employee.ts):

Define your models here for persisting in the database. Here we have created an Employee model in Employee.ts file

TypeScript

export default interface Employee {
  id?: number,
  name?: string,
  department?:string,
  isActive?:boolean
}

Database Layer(client.ts):

Define your SQL Connection and Create table script here.

TypeScript

const client = await new Client();
client.connect({
  hostname: "127.0.0.1",
  username: "your db username",
  password: "your db password",
  db: "",
});
const run = async () => {
  await client.execute(`CREATE DATABASE IF NOT EXISTS ${DATABASE}`);
  await client.execute(`USE ${DATABASE}`);
};
run();
export default client;

#deno #mysql #database #developer

Deno JS: CRUD and MySQL Connection
6.80 GEEK