-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
20 changed files
with
1,270 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,69 @@ | ||
TODO | ||
--- | ||
title: Clickhouse Usage | ||
nav_order: 23 | ||
parent: Examples | ||
layout: default | ||
--- | ||
|
||
<details open markdown="block"> | ||
<summary> | ||
Table of contents | ||
</summary> | ||
{: .text-delta } | ||
1. TOC | ||
{:toc} | ||
</details> | ||
|
||
```ts eval --replacePrintedInput=../src,sql-select-ts | ||
const ClickHouse = require("@apla/clickhouse"); | ||
import { table, AnyStringifyable, RowsArray } from "../src"; | ||
``` | ||
|
||
With a DB connector | ||
|
||
```ts eval | ||
const db = new ClickHouse({ | ||
host: "localhost", | ||
port: 8124, | ||
user: "default", | ||
password: "", | ||
dataObjects: true, | ||
}); | ||
|
||
const runS = async (q: string): Promise<any[]> => | ||
db.querying(q).then((it: any) => it.data); | ||
``` | ||
|
||
We can implement a version that is aware of the types | ||
|
||
```ts eval | ||
const run = <T extends AnyStringifyable>(it: T): Promise<RowsArray<T>> => | ||
runS(it.stringify()); | ||
``` | ||
|
||
Then, with some tables | ||
|
||
```ts eval | ||
const t1 = table(["x", "y"], "t1"); | ||
await runS(`DROP TABLE IF EXISTS t1`); | ||
await runS(`CREATE TABLE IF NOT EXISTS t1(x Int64, y Int64) ENGINE = Memory`); | ||
await runS(`INSERT INTO t1 VALUES(1,2)`); | ||
``` | ||
|
||
We can run queries | ||
|
||
```ts eval --yield=json | ||
const value = await run(t1.selectStar()); | ||
yield value; | ||
``` | ||
|
||
Typescript knows the identifiers | ||
|
||
```ts eval --yield=json | ||
yield value.map((it) => it.x); | ||
``` | ||
|
||
```ts eval | ||
//@ts-expect-error | ||
value.map((it) => it.u); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
TODO | ||
--- | ||
title: SQLite Usage | ||
nav_order: 23 | ||
parent: Examples | ||
layout: default | ||
--- | ||
|
||
<details open markdown="block"> | ||
<summary> | ||
Table of contents | ||
</summary> | ||
{: .text-delta } | ||
1. TOC | ||
{:toc} | ||
</details> | ||
|
||
```ts eval --replacePrintedInput=../src,sql-select-ts | ||
import sqlite from "sqlite3"; | ||
import { table, AnyStringifyable, RowsArray } from "../src"; | ||
``` | ||
|
||
With a DB connector | ||
|
||
```ts eval | ||
const it = sqlite.verbose(); | ||
const db = new it.Database(":memory:"); | ||
|
||
const runS = (q: string) => | ||
new Promise<any[]>((rs, rj) => | ||
db.all(q, (e: any, r: any) => (e ? rj(e) : rs(r))) | ||
); | ||
``` | ||
|
||
We can implement a version that is aware of the types | ||
|
||
```ts eval | ||
const run = <T extends AnyStringifyable>(it: T): Promise<RowsArray<T>> => | ||
runS(it.stringify()); | ||
``` | ||
|
||
Then, with some tables | ||
|
||
```ts eval | ||
const t1 = table(["a", "b", "c"], "t1"); | ||
await runS(`CREATE TABLE t1(a,b,c);`); | ||
await runS(`INSERT INTO t1 VALUES(1,2,3);`); | ||
``` | ||
|
||
We can run queries | ||
|
||
```ts eval --yield=json | ||
const value = await run(t1.selectStar()); | ||
yield value; | ||
``` | ||
|
||
Typescript knows the identifiers | ||
|
||
```ts eval --yield=json | ||
yield value.map((it) => it.a); | ||
``` | ||
|
||
```ts eval | ||
//@ts-expect-error | ||
value.map((it) => it.u); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: Union (Compound) | ||
nav_order: 25 | ||
parent: Examples | ||
layout: default | ||
--- | ||
|
||
<details open markdown="block"> | ||
<summary> | ||
Table of contents | ||
</summary> | ||
{: .text-delta } | ||
1. TOC | ||
{:toc} | ||
</details> | ||
|
||
```ts eval --replacePrintedInput=../src,sql-select-ts | ||
import { table, union, unionAll, except, intersect } from "../src"; | ||
``` | ||
|
||
We will use these tables | ||
|
||
```sql | ||
CREATE TABLE users(id int, age int, name string); | ||
CREATE TABLE admins(id int, age int, name string); | ||
``` | ||
|
||
Which are defined in typescript as | ||
|
||
```ts eval | ||
const users = table( | ||
/* columns: */ ["id", "age", "name"], | ||
/* db-name & alias: */ "users" | ||
); | ||
|
||
const admins = table( | ||
/* columns: */ ["id", "age", "name"], | ||
/* alias: */ "adm", | ||
/* db-name: */ "admins" | ||
); | ||
``` | ||
|
||
# Union | ||
|
||
```ts eval --yield=sql | ||
yield union([admins.selectStar(), users.selectStar()]).stringify(); | ||
``` | ||
|
||
# Union All | ||
|
||
```ts eval --yield=sql | ||
yield unionAll([admins.selectStar(), users.selectStar()]).stringify(); | ||
``` | ||
|
||
# Except | ||
|
||
```ts eval --yield=sql | ||
yield except([admins.selectStar(), users.selectStar()]).stringify(); | ||
``` | ||
|
||
# Intersect | ||
|
||
```ts eval --yield=sql | ||
yield intersect([admins.selectStar(), users.selectStar()]).stringify(); | ||
``` |
Oops, something went wrong.