MariaDB connection using ORM in Deno


Let’s move into another interesting sections, the database where we store information processed. In this post we are connecting MariaDB local database using ORM , denodb.

We can use the same MySQL connector for MariaDB with almost no change

Denodb-ORM

This is a third party ORM module for deno which will help us to MariaDB in Deno. The ORM module work through Model, so we can perform operations using Model, no worry about confusing, queries and statements.

APP

Our app is a simple Todo application, which store Todo’s in a MariaDB database.

  • create a configuration
  • Create Models
  • Link Model and Sync

Configuration

Usually we kept the database file under a config folder, the name of our file will database.ts and content s follows. In the final steps of the configuration we export the model.

import { Model, Database, MySQLConnector, DataTypes } from 'https://deno.land/x/denodb/mod.ts';
//config for MariaDB using MySQKConnector
const connector = new MySQLConnector({
  database: 'todo_collections',
  host: '127.0.0.1',
  username: 'root',
  password: '123',
  port: 3306, // optional
});


const db = new Database(connector)

// NOTE Models 
class Todo extends Model {
    static table = 'todos';
    static fields = {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        item: {
            type: DataTypes.STRING,
        }
        ,
        description: {
            type: DataTypes.STRING,
        }
    };
}
// NOTE Linking Model with DB
db.link([Todo])
await db.sync()

 export default Todo;

You have to specify the host which is localhost, user and password, database name (you should create them first) and we are ready for the controllers.

Sync () – will create the tables for you. You have to create the database which in not created by the sync. Optionally you can use {drop:true} for deleting table every time, the app runs

SQLite app using denodb in Deno


Let’s move into another interesting sections, the database where we store information processed. In this post we are store data into local SQLite database/

Denodb-ORM

This is a third party ORM module for deno which will help us to connect MySQL, MariaDB, SQLite and Postgres databases. The ORM module work through Model, so we can perform operations using Model, no worry about confusing, queries and statements.

APP

Our app is a simple Todo application, which store Todo’s in a MySQL database.

  • create a configuration
  • Create a SQLite file
  • Create Models
  • Link Model and Sync

Configuration

Usually we kept the database file under a config folder, the name of our file will database.ts and content s follows. In the final steps of the configuration we export the model.

import { Model, Database, SQLite3Connector, DataTypes } from "../deps.ts";

const connector = new SQLite3Connector({
    filepath: './database.sqlite',
});

const db = new Database(connector)

// NOTE Models 
class Todo extends Model {
    static table = 'todos';
    static fields = {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
        },
        item: {
            type: DataTypes.STRING,
        }
        ,
        description: {
            type: DataTypes.STRING,
        }
    };
}
// NOTE Linking Model with DB
db.link([Todo])
await db.sync()

 export default Todo;

Sync () – will create the tables for you. You have to create the database which in not created by the sync.

MySQL connection using ORM in Deno


Let’s move into another interesting sections, the database where we store information processed. In this post we are connecting MySQL local database using ORM , denodb.

Denodb-ORM

This is a third party ORM module for deno which will help us to connectMySQL db in Deno. The ORM module work through Model, so we can perform operations using Model, no worry about confusing, queries and statements.

APP

Our app is a simple Todo application, which store Todo’s in a MySQL database.

  • create a configuration
  • Create Models
  • Link Model and Sync

Configuration

Usually we kept the database file under a config folder, the name of our file will database.ts and content s follows. In the final steps of the configuration we export the model.

import { Model, Database, MySQLConnector, DataTypes } from 'https://deno.land/x/denodb/mod.ts';

const connector = new MySQLConnector({
  database: 'todo_collections',
  host: '127.0.0.1',
  username: 'root',
  password: '123',
  port: 3306, // optional
});


const db = new Database(connector)

// NOTE Models 
class Todo extends Model {
    static table = 'todos';
    static fields = {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        item: {
            type: DataTypes.STRING,
        }
        ,
        description: {
            type: DataTypes.STRING,
        }
    };
}
// NOTE Linking Model with DB
db.link([Todo])
await db.sync()

 export default Todo;

You have to specify the host which is localhost, user and password, database name (you should create them first) and we are ready for the controllers. I use the default port for MySQL.

Sync () – will create the tables for you. You have to create the database which in not created by the sync. Optionally you can use {drop:true} for deleting table every time, the app runs

MySQL connection in Deno


Let’s move into another interesting sections, the database where we store information processed. In this post we are connecting MySQL local database using denodrivers.

APP

Our app is a simple Todo application, which store Todo’s in a MySQL database.

Denodrivers

This is a third party module for deno which will help us to build a connection to MySQL db. We are going todo the following

  • create a configuration
  • Insert,Update,Delete
  • Querying database

Configuration

Usually we kept the database file under a config folder, the name of our file will database.ts and content s follows

import { Client } from "https://deno.land/x/mysql/mod.ts";
const client = await new Client().connect({
  hostname: "127.0.0.1",
  username: "root",
  db: "todo_collections",
  poolSize: 3, // connection limit
  password: "123",
});

export default  { client }

You have to specify the host which is localhost, user and password, database name (you should create them first) and we are ready for the controllers.

How to create CLI scripts in Deno Part II:cliffy


If you miss the first part here is the links

cliffy

This is a third party CLI framework for deno which will help you to build command line application using,

  • options
  • commands
  • event handlers
  • sub commands

For more info and example visit the official repository. To explore the frame work go through the example. The repo lack documentation, I know but the developer provide a good set of examples.

Greet CLI

Here is a quick example.

 import { Command } from "https://deno.land/x/cliffy@v0.17.0/command/mod.ts";

new Command()
    .name('Greet')
    .version('0.0.1')
    .description('Greet')
    .example('Greeting people',"greet -g good-night")  
    .option("-g --msg <msg:string>","Greeting with no space",{
        default:'Good morning' ,               
        action:async({msg})=>{
            console.log(msg);
        }
    })
    .parse(Deno.args)

The above is simple greet program with Cliffy framework, which use option for -g for message, also have default value for the option

Run the script in deno

//run the script
deno run app.ts -g good-evening
//output 
good-evening
//bring help
deno run app.ts -h

Install the script locally

We can install our script locally using the deno install command so that can run without a deno command along shell.

deno install --unstable -f -n greet app.ts

Here -f is used to overwrite the existing file which is optional , -n is used to specify the name of the command which also optional (it will take the name of the .ts file instead, if no name is specified). The install command will place the file in your deno home directory.

Run the command

Running the command is as simple as running dir in shell.

greet -g Good-evening
//try bring some help
greet -h
greet -anything

A complete CLI project for Deno users is hosted at GitHub, have a look.

You may love to read these Deno posts

How to create CLI scripts in Deno Part I


Deno is asynchronous JavaScript/TypeScript runtime. If you are not sure how and what’s of Deno, here is the links

CLI / command line script

With Deno you can make beautiful scripts with command line arguments and install them locally in your system, even an executable is plausible.

catching the command line

So how do we catch command line arguments, for example

sayHi manoj

The above CLI supposed to say hi to first argument(manoj) , hi manoj. Now you under stand what an argument in command line is .

Let’s build a simple command line program with Deno

 //app.ts
const {args:[name]}=Deno
console.log(`hi ${name}`);

and you can run it as

deno run app.ts Joe
//result
hi jol

How to catch multiple argument separated by white space ? Here is a quick inundation to our script.

const {args:[hus,wife]}=Deno
console.log(`hi ${hus} ,${wife}`);

Install the script locally

We can install our script locally using the deno install command so that can run without a deno command along shell.

deno install --unstable -f -n sayHi app.ts

Here -f is used to overwrite the existing file which is optional , -n is used to specify the name of the command which also optional (it will take the name of the .ts file instead, if no name is specified). The install command will place the file in your deno home directory.

Run the command

Running the command is as simple as running dir in shell.

sayHi Geethu

We can build much more advanced CLI with options and commands using some of the interesting deno CLI framework. I leave it to the second part.

Inbuilt watch mode for reloading server automatically in Deno


Deno is asynchronous JavaScript/TypeScript runtime. If you are not sure how and what’s of Deno, here is the links

watch mode

We have third party module like denon for hot reloading feature but deno itself have a standard feature watch (CLI flag) for us. It enable use to reloading the server when changes made to the main file.

You can specify the watch along with the running command. Remember it is in an early stage of development, as most of the libraries, so that you have specify a –unstable flag

 deno run --watch --unstable --allow-net  app.ts

It keep reloading the server since you made changes to it.

One of thing I love in Deno is they kept a decent manual for everything , all you need todo is , have a look

All of my Deno projects will be published on my GitHub Repo, so keep watching.

Hot reloading in Deno


Deno is asynchronous JavaScript/TypeScript runtime. If you are not sure how and what’s of Deno, here is the links

Hot reloading with denon

denon is Deno version of nodemon for Nodejs and it is third party module. It help you to restart the server automatically. As the project expands stopping and restarting server become boring and tiresome, denon will be great help in such occasions.

Deno don’t have package.json file but denon optionally require a script file either json or a simple ts file

Follow the official instructions to setup denon in Deno

All of my Deno projects will be published on my GitHub Repo, so keep watching.