Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/manyuanrong/sql-builder
SQL query builder for Deno
https://github.com/manyuanrong/sql-builder
Last synced: about 1 month ago
JSON representation
SQL query builder for Deno
- Host: GitHub
- URL: https://github.com/manyuanrong/sql-builder
- Owner: manyuanrong
- License: mit
- Created: 2019-06-12T13:27:53.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T08:55:53.000Z (almost 2 years ago)
- Last Synced: 2024-10-24T09:19:31.782Z (about 2 months ago)
- Language: TypeScript
- Size: 43.9 KB
- Stars: 55
- Watchers: 4
- Forks: 15
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.MD
- License: LICENSE
Awesome Lists containing this project
- awesome-deno - sql-builder - SQL查询语句生成器。 (Uncategorized / Uncategorized)
- awesome-deno-cn - @manyuanrong/sql-builder
- awesome-deno - sql-builder - An sql query builder.![GitHub stars](https://img.shields.io/github/stars/manyuanrong/sql-builder?style=plastic) (Modules / Online Playgrounds)
- awesome-deno - sql-builder - An sql query builder. (Modules / Database)
README
## Deno SQL QueryBuilder
[![Build Status](https://www.travis-ci.org/manyuanrong/sql-builder.svg?branch=master)](https://www.travis-ci.org/manyuanrong/sql-builder)
![GitHub](https://img.shields.io/github/license/manyuanrong/sql-builder.svg)
![GitHub release](https://img.shields.io/github/release/manyuanrong/sql-builder.svg)
![(Deno)](https://img.shields.io/badge/deno-1.0.0-green.svg)### Docs
More Useage see:
- [Query](./docs/query.md)
- [Where](./docs/where.md)
- [Order](./docs/order.md)
- [Join](./docs/join.md)### [Changes Log](./CHANGELOG.MD)
### Example
```ts
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();
```