SQL query builder for Deno

SQL query builder for Deno

sql-builder SQL query builder for Deno

Deno SQL QueryBuilder

Docs

More Useage see:

Changes Log

Example

import { assertEquals, runTests, test } from "./deps.ts";
import { Query } from "https://deno.land/x/sql_builder/mod.ts";

test(function testQueryInsert() {
  const builder = new Query();
  const records = [
    {
      name: "Enok",
      password: "foo",
      id: 1
    },
    {
      id: 2,
      name: "Man",
      password: "bar"
    }
  ];

  const sql = builder
    .table("users")
    .insert(records)
    .build();

  assertEquals(
    sql.trim(),
    'INSERT INTO `users` (`name`,`password`,`id`) VALUES ("Enok","foo",1) ("Man","bar",2)'
  );
});

test(function testQueryUpdate() {
  const builder = new Query();
  const record = {
    name: "Enok",
    password: "foo",
    id: 1
  };

  const sql = builder
    .table("users")
    .update(record)
    .build();

  assertEquals(
    sql.trim(),
    'UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1'
  );
});

test(function testQueryUpdateWithWhere() {
  const builder = new Query();
  const record = {
    name: "Enok",
    password: "foo",
    id: 1
  };

  const sql = builder
    .table("users")
    .where("id", "=", 1)
    .where("name", "like", "%n%")
    .update(record)
    .build();

  assertEquals(
    sql.trim(),
    'UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1 WHERE `id` = 1 AND `name` like "%n%"'
  );
});

test(function testQueryDelete() {
  const builder = new Query();

  const sql = builder
    .table("users")
    .delete()
    .build();

  assertEquals(sql.trim(), "DELETE FROM `users`");
});

test(function testQueryDeleteWithWhere() {
  const builder = new Query();

  const sql = builder
    .table("users")
    .where("id", "=", 1)
    .where("name", "like", "%n%")
    .delete()
    .build();

  assertEquals(
    sql.trim(),
    'DELETE FROM `users` WHERE `id` = 1 AND `name` like "%n%"'
  );
});

test(function testQuerySelectSimple() {
  const builder = new Query();

  const sql = builder
    .table("users")
    .select("name", "id")
    .build();

  assertEquals(sql.trim(), "SELECT `name`, `id` FROM `users`");
});

test(function testQuerySelectWhere() {
  const builder = new Query();

  const sql = builder
    .table("users")
    .where("id", ">", 1)
    .where("name", "like", "%n%")
    .select("name", "id")
    .build();

  assertEquals(
    sql.trim(),
    'SELECT `name`, `id` FROM `users` WHERE `id` > 1 AND `name` like "%n%"'
  );
});

test(function testQuerySelectOrder() {
  const builder = new Query();

  const sql = builder
    .table("users")
    .where("id", ">", 1)
    .where("name", "like", "%n%")
    .select("name", "id")
    .order("id", "DESC")
    .order("name", "asc")
    .build();

  assertEquals(
    sql.trim(),
    'SELECT `name`, `id` FROM `users` WHERE `id` > 1 AND `name` like "%n%" ORDER BY `id` DESC, `name` ASC'
  );
});

test(function testQuerySelectJoin() {
  const builder = new Query();

  const sql = builder
    .table("users")
    .where("id", ">", 1)
    .where("name", "like", "%n%")
    .select("users.id", "users.name", "`uses`.`avatar` as `uavatar`")
    .join("LEFT JOIN posts ON posts.id = users.id")
    .build();

  assertEquals(
    sql.trim(),
    'SELECT `users`.`id`, `users`.`name`, `uses`.`avatar` as `uavatar` FROM `users` LEFT JOIN posts ON posts.id = users.id WHERE `id` > 1 AND `name` like "%n%"'
  );
});

test(function testQuerySelectLimit() {
  const builder = new Query();

  const sql = builder
    .table("users")
    .where("id", ">", 1)
    .where("name", "like", "%n%")
    .select("*")
    .limit(0, 10)
    .build();

  assertEquals(
    sql.trim(),
    'SELECT * FROM `users` WHERE `id` > 1 AND `name` like "%n%" LIMIT 0, 10'
  );
});

runTests();

Download Details:

Author: manyuanrong

Source Code: https://github.com/manyuanrong/sql-builder

deno node nodejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings