Skip to content

Commit 032e66c

Browse files
authored
Add missing docs (#16)
* up to db ext * add db specific docs
1 parent f476a28 commit 032e66c

20 files changed

+1270
-29
lines changed

TODO.md

-5
This file was deleted.

docs-eval/clickhouse-usage.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,69 @@
1-
TODO
1+
---
2+
title: Clickhouse Usage
3+
nav_order: 23
4+
parent: Examples
5+
layout: default
6+
---
7+
8+
<details open markdown="block">
9+
<summary>
10+
Table of contents
11+
</summary>
12+
{: .text-delta }
13+
1. TOC
14+
{:toc}
15+
</details>
16+
17+
```ts eval --replacePrintedInput=../src,sql-select-ts
18+
const ClickHouse = require("@apla/clickhouse");
19+
import { table, AnyStringifyable, RowsArray } from "../src";
20+
```
21+
22+
With a DB connector
23+
24+
```ts eval
25+
const db = new ClickHouse({
26+
host: "localhost",
27+
port: 8124,
28+
user: "default",
29+
password: "",
30+
dataObjects: true,
31+
});
32+
33+
const runS = async (q: string): Promise<any[]> =>
34+
db.querying(q).then((it: any) => it.data);
35+
```
36+
37+
We can implement a version that is aware of the types
38+
39+
```ts eval
40+
const run = <T extends AnyStringifyable>(it: T): Promise<RowsArray<T>> =>
41+
runS(it.stringify());
42+
```
43+
44+
Then, with some tables
45+
46+
```ts eval
47+
const t1 = table(["x", "y"], "t1");
48+
await runS(`DROP TABLE IF EXISTS t1`);
49+
await runS(`CREATE TABLE IF NOT EXISTS t1(x Int64, y Int64) ENGINE = Memory`);
50+
await runS(`INSERT INTO t1 VALUES(1,2)`);
51+
```
52+
53+
We can run queries
54+
55+
```ts eval --yield=json
56+
const value = await run(t1.selectStar());
57+
yield value;
58+
```
59+
60+
Typescript knows the identifiers
61+
62+
```ts eval --yield=json
63+
yield value.map((it) => it.x);
64+
```
65+
66+
```ts eval
67+
//@ts-expect-error
68+
value.map((it) => it.u);
69+
```

docs-eval/select.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ layout: default
1414
{:toc}
1515
</details>
1616

17-
# From Nothing
18-
1917
```ts eval --replacePrintedInput=../src,sql-select-ts
2018
import { fromNothing, sql, table, unionAll } from "../src";
2119
```
2220

21+
# From Nothing
22+
2323
## Select
2424

2525
```ts eval --yield=sql
@@ -42,6 +42,18 @@ yield fromNothing({
4242
.stringify();
4343
```
4444

45+
## Select from Select
46+
47+
```ts eval --yield=sql
48+
yield fromNothing({
49+
it: sql(0),
50+
})
51+
.selectStar()
52+
.select((f) => ({ it2: f.it }))
53+
.selectStar()
54+
.stringify();
55+
```
56+
4557
# From Tables
4658

4759
We will use these tables

docs-eval/sqlite-usage.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
TODO
1+
---
2+
title: SQLite Usage
3+
nav_order: 23
4+
parent: Examples
5+
layout: default
6+
---
7+
8+
<details open markdown="block">
9+
<summary>
10+
Table of contents
11+
</summary>
12+
{: .text-delta }
13+
1. TOC
14+
{:toc}
15+
</details>
16+
17+
```ts eval --replacePrintedInput=../src,sql-select-ts
18+
import sqlite from "sqlite3";
19+
import { table, AnyStringifyable, RowsArray } from "../src";
20+
```
21+
22+
With a DB connector
23+
24+
```ts eval
25+
const it = sqlite.verbose();
26+
const db = new it.Database(":memory:");
27+
28+
const runS = (q: string) =>
29+
new Promise<any[]>((rs, rj) =>
30+
db.all(q, (e: any, r: any) => (e ? rj(e) : rs(r)))
31+
);
32+
```
33+
34+
We can implement a version that is aware of the types
35+
36+
```ts eval
37+
const run = <T extends AnyStringifyable>(it: T): Promise<RowsArray<T>> =>
38+
runS(it.stringify());
39+
```
40+
41+
Then, with some tables
42+
43+
```ts eval
44+
const t1 = table(["a", "b", "c"], "t1");
45+
await runS(`CREATE TABLE t1(a,b,c);`);
46+
await runS(`INSERT INTO t1 VALUES(1,2,3);`);
47+
```
48+
49+
We can run queries
50+
51+
```ts eval --yield=json
52+
const value = await run(t1.selectStar());
53+
yield value;
54+
```
55+
56+
Typescript knows the identifiers
57+
58+
```ts eval --yield=json
59+
yield value.map((it) => it.a);
60+
```
61+
62+
```ts eval
63+
//@ts-expect-error
64+
value.map((it) => it.u);
65+
```

docs-eval/typed-response.md

+70-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,73 @@ layout: default
1414
{:toc}
1515
</details>
1616

17-
TODO
17+
```ts eval --replacePrintedInput=../src,sql-select-ts
18+
import * as io from "io-ts";
19+
import { AnyStringifyable, SelectionOf, table, RowOf, RowsArray } from "../src";
20+
```
21+
22+
```ts eval
23+
const t = table(["a", "b"], "t");
24+
const q = t.selectStar();
25+
```
26+
27+
# Getting Response Keys
28+
29+
```ts eval
30+
type K = SelectionOf<typeof q>; // typeof K = 'a' | 'b'
31+
32+
// @ts-expect-error
33+
const k2: K = "c";
34+
```
35+
36+
# Response Object
37+
38+
```ts eval
39+
type R1 = RowOf<typeof q>; // typeof Ret = {a: string | number | null | undefined, b: string | number | null | undefined, }
40+
const ret1: R1 = { a: 1, b: null };
41+
//@ts-expect-error
42+
ret1.c;
43+
```
44+
45+
# Response Array
46+
47+
```ts eval
48+
type R2 = RowsArray<typeof q>; // typeof Ret = {a: string | number | null | undefined, b: string | number | null | undefined, }[]
49+
const ret2: R2 = [] as any;
50+
//@ts-expect-error
51+
ret2?.[0]?.abc;
52+
```
53+
54+
# Usage with io-ts
55+
56+
```ts eval
57+
const ioTsResponse = <
58+
T extends AnyStringifyable,
59+
C extends { [key in SelectionOf<T>]: io.Mixed }
60+
>(
61+
_it: T,
62+
_codec: C
63+
): Promise<io.TypeOf<io.TypeC<C>>[]> => {
64+
// Get the query string with it.stringify()
65+
// and implement the DB comms.
66+
return Promise.resolve([]);
67+
};
68+
```
69+
70+
```ts eval
71+
const response = await ioTsResponse(t.selectStar(), {
72+
a: io.string,
73+
b: io.number,
74+
});
75+
76+
response[0]?.a.charAt(0);
77+
response[0]?.b.toPrecision(2);
78+
79+
//@ts-expect-error
80+
response[0]?.c;
81+
```
82+
83+
```ts eval
84+
// @ts-expect-error
85+
ioTsResponse(t.selectStar(), { a: io.string });
86+
```

docs-eval/union.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Union (Compound)
3+
nav_order: 25
4+
parent: Examples
5+
layout: default
6+
---
7+
8+
<details open markdown="block">
9+
<summary>
10+
Table of contents
11+
</summary>
12+
{: .text-delta }
13+
1. TOC
14+
{:toc}
15+
</details>
16+
17+
```ts eval --replacePrintedInput=../src,sql-select-ts
18+
import { table, union, unionAll, except, intersect } from "../src";
19+
```
20+
21+
We will use these tables
22+
23+
```sql
24+
CREATE TABLE users(id int, age int, name string);
25+
CREATE TABLE admins(id int, age int, name string);
26+
```
27+
28+
Which are defined in typescript as
29+
30+
```ts eval
31+
const users = table(
32+
/* columns: */ ["id", "age", "name"],
33+
/* db-name & alias: */ "users"
34+
);
35+
36+
const admins = table(
37+
/* columns: */ ["id", "age", "name"],
38+
/* alias: */ "adm",
39+
/* db-name: */ "admins"
40+
);
41+
```
42+
43+
# Union
44+
45+
```ts eval --yield=sql
46+
yield union([admins.selectStar(), users.selectStar()]).stringify();
47+
```
48+
49+
# Union All
50+
51+
```ts eval --yield=sql
52+
yield unionAll([admins.selectStar(), users.selectStar()]).stringify();
53+
```
54+
55+
# Except
56+
57+
```ts eval --yield=sql
58+
yield except([admins.selectStar(), users.selectStar()]).stringify();
59+
```
60+
61+
# Intersect
62+
63+
```ts eval --yield=sql
64+
yield intersect([admins.selectStar(), users.selectStar()]).stringify();
65+
```

0 commit comments

Comments
 (0)