DEV Community

Cover image for Quick tip: Using Deno and npm to persist and query data in SingleStoreDB
Akmal Chaudhri for SingleStore

Posted on • Updated on

Quick tip: Using Deno and npm to persist and query data in SingleStoreDB

Abstract

This short article will show how to install and use Deno, a modern runtime for JavaScript and TypeScript. We'll use Deno to run a small program to connect to SingleStoreDB and perform some simple database operations.

Create a SingleStoreDB Cloud account

A previous article showed the steps required to create a free SingleStoreDB Cloud account. We'll use Deno Demo Group as our Workspace Group Name and deno-demo as our Workspace Name. We'll make a note of our password and host name.

Install Deno

Installation of Deno is straightforward on a Linux platform:

curl -fsSL https://deno.land/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

Once installed, we may also need the following:

export DENO_INSTALL="/path/to/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

We'll replace /path/to/ with the actual path to the installation directory.

We can check if the installation was successful by running the following command:

deno --version
Enter fullscreen mode Exit fullscreen mode

Create and Read operations

We'll use an example from GitHub and create a small JavaScript file, s2_test.js, as follows:

import mysql from "npm:mysql2@^2.3.3/promise";

const connection = await mysql.createConnection({
  host: "<host>",
  user: "admin",
  password: "<password>",
});

await connection.query("DROP DATABASE IF EXISTS denos");
await connection.query("CREATE DATABASE denos");
await connection.query("USE denos");

await connection.query(
  "CREATE TABLE dinosaurs (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, description VARCHAR(255))",
);

await connection.query(
  "INSERT INTO dinosaurs (id, name, description) VALUES (1, 'Aardonyx', 'An early stage in the evolution of sauropods.'), (2, 'Abelisaurus', 'Abels lizard has been reconstructed from a single skull.'), (3, 'Deno', 'The fastest dinosaur that ever lived.')",
);

const [results, fields] = await connection.query("SELECT * FROM dinosaurs ORDER BY id");
console.log(results);

const [result, field] = await connection.query(
  "SELECT description FROM dinosaurs WHERE name = 'Deno'",
);
console.log(result);

await connection.end();
Enter fullscreen mode Exit fullscreen mode

We'll replace the <host> and <password> with the values from our SingleStoreDB Cloud account.

After running our program:

deno run s2_test.js
Enter fullscreen mode Exit fullscreen mode

the output should be as follows:

[
  {
    id: 1,
    name: "Aardonyx",
    description: "An early stage in the evolution of sauropods."
  },
  {
    id: 2,
    name: "Abelisaurus",
    description: "Abels lizard has been reconstructed from a single skull."
  },
  { id: 3, name: "Deno", description: "The fastest dinosaur that ever lived." }
]
[ { description: "The fastest dinosaur that ever lived." } ]
Enter fullscreen mode Exit fullscreen mode

Summary

In this short article, we have quickly tested Deno with SingleStoreDB.

Top comments (0)