0.0.32 • Published 4 years ago

dabs v0.0.32

Weekly downloads
2
License
MIT
Repository
-
Last release
4 years ago

DaBs

Simple SQL Query Builder.

Uses MySQL2 as DB engine.

Quickstart

npm install dabs

###CONNECT

const dabs = require("dabs");
const db = await dabs.connect("mysql://user:pass@host/database");

###SELECT

const u = db.table("users");
const n = db.table("data");

let q = u.select( u.name, u.mail )
  .join(n, n.uid.eq(u.uid) )
  .fields( n.title.as("title") )
  .fields( db.fn.COUNT(n.id).as("alias_count") )
  .fields( db.fn.AVG(n.field).as("alias_avg") )
  .where( u.uid.equals(db._("placeholder")) )
  .where( n.id.gt(10) )
  .sortBy( n.id.asc(), u.uid.desc() )
  .groupBy( n.id )
  .having( db.$("alias_avg").gt(100) );

let result = await q.exec({ placeholder: 5 });
console.info(q.toString());
SELECT
`users`.`name`,`users`.`mail`,`data`.`title` AS title,COUNT(`data`.`id`) AS alias_count,AVG(`data`.`field`) AS alias_avg
FROM users
JOIN `data` ON (`data`.`uid` = `users`.`uid`)
WHERE (`users`.`uid` = ? AND `data`.`id` > ?)
GROUP BY `data`.`id`
HAVING (`alias_avg` > ?)
ORDER BY `data`.`id` ASC,`users`.`uid` DESC

###INSERT

u.insert()
  .fields(u.name, u.email)
  .exec({ name: "test", email: "test@test.com"})
  .then((data) => console.log(data))
  .catch((e) => console.log(e));

###UPDATE

n.update()
  .fields( n.f1 )
  .where( n.f1.equals(db._("placeholder")) )
  .whereAny( n.f2.equals("x"), n.f3.equals("y") )
  .exec({ f1: "a specific value", placeholder: "test" })
  .then((data) => console.log(data))
  .catch((err) => console.log(err));

###DELETE

n.delete()
  .where( n.f1.equals(db._("placeholder")) )
  .exec({ placeholder: "what" })
  .then((data) => console.log(data))
  .catch((err) => console.log(err));
0.0.32

4 years ago

0.0.31

4 years ago

0.0.30

4 years ago

0.0.28

4 years ago

0.0.29

4 years ago

0.0.27

4 years ago

0.0.26

4 years ago

0.0.25

4 years ago

0.0.24

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago