https://github.com/btd1337/hadros
Postgres SQL Builder for Deno
https://github.com/btd1337/hadros
deno postgres postgresql sql sql-builder
Last synced: 4 days ago
JSON representation
Postgres SQL Builder for Deno
- Host: GitHub
- URL: https://github.com/btd1337/hadros
- Owner: btd1337
- Created: 2023-09-08T03:38:27.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-09T00:34:58.000Z (almost 3 years ago)
- Last Synced: 2024-11-22T00:52:20.931Z (over 1 year ago)
- Topics: deno, postgres, postgresql, sql, sql-builder
- Language: TypeScript
- Homepage:
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# Hadros
Hadros is a Postgres SQL builder for Deno based on @yourtion/deno-sql that is
focused on MySQL
## Usage
```typescript
import { expr, query, table } from "https://deno.land/x/hadros/mod.ts";
// simple query
table("test")
.select("a", "b")
.where({ a: 1 })
.and("b=?", [2])
.orderBy("b DESC")
.offset(10)
.limit(5)
.build();
// SELECT a, b FROM test WHERE a=1 AND b=2 ORDER BY b DESC LIMIT 10,5
// join
table("hello")
.select("*")
.as("A")
.leftJoin("world")
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
// SELECT "A".* FROM hello AS "A" LEFT JOIN world AS "B" ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3
// insert
table("test1")
.insert({
a: 123,
b: 456,
})
.build();
// INSERT INTO test1 (a, b) VALUES (123, 456);
// batch insert
table("test1")
.insert([
{
a: 123,
b: 456,
},
{
a: 789,
b: 110,
},
])
.build();
// INSERT INTO test1 (a, b) VALUES (123, 456),
// (789, 110)");
// update
table("test1")
.update({
a: 123,
b: 456,
})
.where({
b: 777,
})
.limit(12)
.build();
// UPDATE test1 SET a=123, b=456 WHERE b=777 LIMIT 12
// insert onDuplicateKeyUpdate
table("test1")
.insert({ a: 123, b: 456 })
.onDuplicateKeyUpdate()
.set({ a: "xxx" })
.build();
// INSERT INTO test1 (a, b) VALUES (123, 456) ON DUPLICATE KEY UPDATE a='xxx'
// delete
table("test1")
.delete()
.where({
b: 777,
})
.limit(12)
.build();
// DELETE FROM test1 WHERE b=777 LIMIT 12
// sub query
table("Test_2")
.select("*")
.where("a=? AND b IN ???", [
123,
table("test3")
.select("id")
.where({ id: { $lt: 10 } })
.limit(100),
])
.build();
// SELECT * FROM "Test_2" WHERE a=123 AND b IN (SELECT id FROM test2 WHERE id<10 LIMIT 100)
// clone query
const q = table("test1")
.select("*")
.where({ a: 123 });
q.clone()
.where({ b: 456 })
.offset(10)
.limit(20)
.build();
// SELECT * FROM test1 WHERE a=123 AND b=456 LIMIT 10,20
q.clone()
.where({ b: 789, c: 666 })
.orderBy("a DESC")
.build();
// SELECT * FROM test1 WHERE a=123 AND b=789 AND c=666 ORDER BY a DESC
// query with expr
table("test")
.select("*")
.where(
expr()
.and("a=?", [123])
.or({ b: 456 })
.and({ c: { $in: [789] } })
.or("d=:d", { d: 666 }),
)
.build();
// SELECT * FROM test WHERE (a=123 OR b=456 AND c IN (789) OR d=666)
```
## Usege with Postgres
```ts
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
const client = new Client({
user: "user",
database: "test",
hostname: "localhost",
port: 5432,
});
await client.connect();
const array_result = await client.queryArray(
table("People").select("id", "name").build(),
);
console.log(array_result.rows); // [[1, 'Carlos'], [2, 'John'], ...]
const object_result = await client.queryObject(
table("People").select("id", "name").build(),
);
console.log(object_result.rows); // [{id: 1, name: 'Carlos'}, {id: 2, name: 'John'}, ...]
await client.end();
```
## License
```text
MIT License
- Copyright (c) 2018 Zongmin Lei
- Copyright (c) 2020 Yourtion Guo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```