@daydream-technologies/3d-db-mcp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -0
- package/dist/http.js +144 -0
- package/dist/index.js +44 -0
- package/dist/mcp.js +112 -0
- package/dist/setup.js +89 -0
- package/dist/token.js +9 -0
- package/package.json +39 -0
- package/static/assets/index-B0dnwIo5.css +1 -0
- package/static/assets/index-MBzCJJWx.js +5054 -0
- package/static/docs/IMPORT_MONGODB.md +63 -0
- package/static/docs/IMPORT_MYSQL.md +52 -0
- package/static/docs/IMPORT_POSTGRES.md +15 -0
- package/static/docs/IMPORT_SUPABASE.md +82 -0
- package/static/docs/SCHEMA_FORMAT.md +63 -0
- package/static/index.html +14 -0
- package/static/learn/lessons.md +242 -0
- package/static/samples/blog.json +83 -0
- package/static/samples/ecommerce.json +103 -0
- package/static/samples/library.json +79 -0
- package/static/samples/restaurant/13_cte_repeat_customers.sql +20 -0
- package/static/samples/restaurant/14_union_active_contacts.sql +25 -0
- package/static/samples/restaurant/15_distinct_customer_methods.sql +10 -0
- package/static/samples/restaurant/16_full_join_staff_shifts.sql +12 -0
- package/static/samples/restaurant/17_like_menu_search.sql +12 -0
- package/static/samples/restaurant/18_is_null_missing_data.sql +13 -0
- package/static/samples/restaurant/19_offset_pagination.sql +13 -0
- package/static/samples/restaurant/20_from_subquery_top_items.sql +20 -0
- package/static/samples/restaurant/21_between_date_range.sql +13 -0
- package/static/samples/restaurant/22_right_join_unused_promos.sql +14 -0
- package/static/samples/restaurant/proposal-add-loyalty.json +131 -0
- package/static/samples/restaurant/proposal-baseline.json +66 -0
- package/static/samples/restaurant/schema_postgres.sql +221 -0
- package/static/samples/restaurant-antipatterns.json +111 -0
- package/static/samples/restaurant.json +425 -0
- package/static/vite.svg +5 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Import from MongoDB
|
|
2
|
+
|
|
3
|
+
MongoDB has no universal FK metadata. This workflow **infers a logical schema** from:
|
|
4
|
+
|
|
5
|
+
- collection **names** → tables
|
|
6
|
+
- **estimated document count** → `rowCount`
|
|
7
|
+
- **sampling one document** → column names and simple types
|
|
8
|
+
|
|
9
|
+
Run in **mongosh** connected to your database. Output is extended JSON you can paste into the app.
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
const dbName = db.getName();
|
|
13
|
+
const sampleSize = 1;
|
|
14
|
+
function guessType(v) {
|
|
15
|
+
if (v === null || v === undefined) return "null";
|
|
16
|
+
const t = typeof v;
|
|
17
|
+
if (t === "number") return Number.isInteger(v) ? "int" : "double";
|
|
18
|
+
if (t === "boolean") return "bool";
|
|
19
|
+
if (v instanceof Date) return "date";
|
|
20
|
+
if (Array.isArray(v)) return "array";
|
|
21
|
+
if (t === "object" && v._bsontype === "ObjectId") return "objectId";
|
|
22
|
+
if (t === "object") return "object";
|
|
23
|
+
return "string";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const tables = [];
|
|
27
|
+
for (const name of db.getCollectionNames()) {
|
|
28
|
+
if (name.startsWith("system.")) continue;
|
|
29
|
+
const coll = db.getCollection(name);
|
|
30
|
+
const rowCount = coll.estimatedDocumentCount();
|
|
31
|
+
const doc = coll.findOne() || {};
|
|
32
|
+
const columns = [];
|
|
33
|
+
let i = 0;
|
|
34
|
+
for (const [k, v] of Object.entries(doc)) {
|
|
35
|
+
columns.push({
|
|
36
|
+
name: k,
|
|
37
|
+
type: guessType(v),
|
|
38
|
+
primaryKey: i++ === 0 && guessType(v) === "objectId",
|
|
39
|
+
nullable: true,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (columns.length === 0) {
|
|
43
|
+
columns.push({ name: "_placeholder", type: "unknown", nullable: true });
|
|
44
|
+
}
|
|
45
|
+
tables.push({ name, rowCount, columns });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
printjson({ name: dbName, tables });
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Adding relationships
|
|
52
|
+
|
|
53
|
+
The visualizer only draws FK lines from `foreignKey` fields. After export, **hand-edit JSON** (or merge with a mapping file) to add:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
"foreignKey": { "table": "orders", "column": "_id" }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
…where your application enforces references.
|
|
60
|
+
|
|
61
|
+
### Atlas
|
|
62
|
+
|
|
63
|
+
Use **mongosh** against your Atlas cluster URI, or run the script in **Compass** playground and copy the printed JSON.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Import from MySQL / MariaDB
|
|
2
|
+
|
|
3
|
+
MySQL exposes metadata in `information_schema` and approximate row counts on `information_schema.tables`.
|
|
4
|
+
|
|
5
|
+
The snippet below prints **one JSON document** as text. Paste it into the app (JSON → Visualize).
|
|
6
|
+
|
|
7
|
+
> Adjust `TABLE_SCHEMA` to your database name.
|
|
8
|
+
|
|
9
|
+
```sql
|
|
10
|
+
SELECT JSON_OBJECT(
|
|
11
|
+
'name', DATABASE(),
|
|
12
|
+
'tables', (
|
|
13
|
+
SELECT JSON_ARRAYAGG(
|
|
14
|
+
JSON_OBJECT(
|
|
15
|
+
'name', t.table_name,
|
|
16
|
+
'rowCount', COALESCE(t.table_rows, 0),
|
|
17
|
+
'columns', (
|
|
18
|
+
SELECT JSON_ARRAYAGG(
|
|
19
|
+
JSON_OBJECT(
|
|
20
|
+
'name', c.column_name,
|
|
21
|
+
'type', c.column_type,
|
|
22
|
+
'nullable', c.is_nullable = 'YES',
|
|
23
|
+
'primaryKey', c.column_key = 'PRI',
|
|
24
|
+
'foreignKey', (
|
|
25
|
+
SELECT JSON_OBJECT(
|
|
26
|
+
'table', k.referenced_table_name,
|
|
27
|
+
'column', k.referenced_column_name
|
|
28
|
+
)
|
|
29
|
+
FROM information_schema.key_column_usage k
|
|
30
|
+
WHERE k.table_schema = c.table_schema
|
|
31
|
+
AND k.table_name = c.table_name
|
|
32
|
+
AND k.column_name = c.column_name
|
|
33
|
+
AND k.referenced_table_name IS NOT NULL
|
|
34
|
+
LIMIT 1
|
|
35
|
+
),
|
|
36
|
+
'indexed', c.column_key IN ('PRI', 'UNI', 'MUL')
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
FROM information_schema.columns c
|
|
40
|
+
WHERE c.table_schema = t.table_schema
|
|
41
|
+
AND c.table_name = t.table_name
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
)
|
|
45
|
+
FROM information_schema.tables t
|
|
46
|
+
WHERE t.table_schema = DATABASE()
|
|
47
|
+
AND t.table_type = 'BASE TABLE'
|
|
48
|
+
)
|
|
49
|
+
) AS paste_json;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Note:** `JSON_ARRAYAGG` order may vary by version; reordering in the app is not required. Row counts are approximate (`TABLE_ROWS`).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Import from PostgreSQL (generic)
|
|
2
|
+
|
|
3
|
+
Use the same approach as Supabase: run the SQL in [IMPORT_SUPABASE.md](./IMPORT_SUPABASE.md) from `psql`, pgAdmin, or any client.
|
|
4
|
+
|
|
5
|
+
1. Connect to your database.
|
|
6
|
+
2. Run the final `SELECT json_build_object(...)`.
|
|
7
|
+
3. Copy the `paste_json` cell into this app (JSON tab → Visualize).
|
|
8
|
+
|
|
9
|
+
### Permissions
|
|
10
|
+
|
|
11
|
+
You need read access to `information_schema`, `pg_stat_user_tables`, and `pg_indexes`.
|
|
12
|
+
|
|
13
|
+
### Other schemas
|
|
14
|
+
|
|
15
|
+
Change `'public'` in the `WHERE` clauses to your schema name (e.g. `'app'`).
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Import from Supabase (Postgres)
|
|
2
|
+
|
|
3
|
+
Supabase is Postgres. Use the **SQL Editor** to run a query that builds JSON matching [SCHEMA_FORMAT.md](./SCHEMA_FORMAT.md).
|
|
4
|
+
|
|
5
|
+
## 1. Columns, PKs, FKs, row counts
|
|
6
|
+
|
|
7
|
+
Run the query below, copy the **single JSON row** result, and paste it into the app as **JSON** → **Visualize**.
|
|
8
|
+
|
|
9
|
+
> Replace `public` with your main schema if different.
|
|
10
|
+
|
|
11
|
+
```sql
|
|
12
|
+
WITH cols AS (
|
|
13
|
+
SELECT
|
|
14
|
+
c.table_name,
|
|
15
|
+
json_agg(
|
|
16
|
+
json_build_object(
|
|
17
|
+
'name', c.column_name,
|
|
18
|
+
'type', c.data_type,
|
|
19
|
+
'nullable', c.is_nullable = 'YES',
|
|
20
|
+
'primaryKey', EXISTS (
|
|
21
|
+
SELECT 1 FROM information_schema.table_constraints tc
|
|
22
|
+
JOIN information_schema.key_column_usage kcu
|
|
23
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
24
|
+
AND tc.table_schema = kcu.table_schema
|
|
25
|
+
WHERE tc.table_schema = c.table_schema
|
|
26
|
+
AND tc.table_name = c.table_name
|
|
27
|
+
AND tc.constraint_type = 'PRIMARY KEY'
|
|
28
|
+
AND kcu.column_name = c.column_name
|
|
29
|
+
),
|
|
30
|
+
'foreignKey', (
|
|
31
|
+
SELECT json_build_object(
|
|
32
|
+
'table', ccu.table_name,
|
|
33
|
+
'column', ccu.column_name
|
|
34
|
+
)
|
|
35
|
+
FROM information_schema.table_constraints tc
|
|
36
|
+
JOIN information_schema.key_column_usage kcu
|
|
37
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
38
|
+
AND tc.table_schema = kcu.table_schema
|
|
39
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
40
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
41
|
+
AND ccu.table_schema = tc.table_schema
|
|
42
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
|
43
|
+
AND tc.table_schema = c.table_schema
|
|
44
|
+
AND tc.table_name = c.table_name
|
|
45
|
+
AND kcu.column_name = c.column_name
|
|
46
|
+
LIMIT 1
|
|
47
|
+
),
|
|
48
|
+
'indexed', EXISTS (
|
|
49
|
+
SELECT 1 FROM pg_indexes i
|
|
50
|
+
WHERE i.schemaname = c.table_schema
|
|
51
|
+
AND i.tablename = c.table_name
|
|
52
|
+
AND i.indexdef ILIKE '%' || quote_ident(c.column_name) || '%'
|
|
53
|
+
)
|
|
54
|
+
) ORDER BY c.ordinal_position
|
|
55
|
+
) AS columns
|
|
56
|
+
FROM information_schema.columns c
|
|
57
|
+
WHERE c.table_schema = 'public'
|
|
58
|
+
GROUP BY c.table_schema, c.table_name
|
|
59
|
+
),
|
|
60
|
+
counts AS (
|
|
61
|
+
SELECT relname AS table_name, COALESCE(n_live_tup, 0)::bigint AS row_count
|
|
62
|
+
FROM pg_stat_user_tables
|
|
63
|
+
WHERE schemaname = 'public'
|
|
64
|
+
)
|
|
65
|
+
SELECT json_build_object(
|
|
66
|
+
'name', current_database(),
|
|
67
|
+
'tables', json_agg(
|
|
68
|
+
json_build_object(
|
|
69
|
+
'name', cols.table_name,
|
|
70
|
+
'rowCount', COALESCE(counts.row_count, 0),
|
|
71
|
+
'columns', cols.columns
|
|
72
|
+
) ORDER BY cols.table_name
|
|
73
|
+
)
|
|
74
|
+
)::text AS paste_json
|
|
75
|
+
FROM cols
|
|
76
|
+
LEFT JOIN counts USING (table_name);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 2. Tips
|
|
80
|
+
|
|
81
|
+
- If `indexed` heuristics are noisy, you can strip the `indexed` keys from JSON and rely on manual row edits in the **Table** tab.
|
|
82
|
+
- Export **JSON / Markdown** from the app for LLM context after import.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Extended JSON schema format
|
|
2
|
+
|
|
3
|
+
This app accepts a single JSON object with:
|
|
4
|
+
|
|
5
|
+
- `name` (optional): display name for the schema.
|
|
6
|
+
- `tables` (required): array of table objects.
|
|
7
|
+
|
|
8
|
+
Each **table**:
|
|
9
|
+
|
|
10
|
+
| Field | Type | Description |
|
|
11
|
+
| ------------ | ------ | ------------------------------------------------ |
|
|
12
|
+
| `name` | string | Table name (unique in this file). |
|
|
13
|
+
| `columns` | array | Column definitions. |
|
|
14
|
+
| `rowCount` | number | Optional. Used to size boxes (∛ scaling). |
|
|
15
|
+
| `sizeBytes` | number | Optional. Reserved for future use. |
|
|
16
|
+
|
|
17
|
+
Each **column**:
|
|
18
|
+
|
|
19
|
+
| Field | Type | Description |
|
|
20
|
+
| ------------- | ------ | ----------- |
|
|
21
|
+
| `name` | string | Column name |
|
|
22
|
+
| `type` | string | Logical / SQL type label (informational). |
|
|
23
|
+
| `primaryKey` | bool | Primary key marker. |
|
|
24
|
+
| `foreignKey` | object | `{ "table": "other", "column": "id" }` |
|
|
25
|
+
| `nullable` | bool | Default treated as nullable if omitted. |
|
|
26
|
+
| `indexed` | bool | If `false` on FK columns, tips may suggest indexes. |
|
|
27
|
+
|
|
28
|
+
**Relationships** are inferred only from `foreignKey` on columns (no separate edges array).
|
|
29
|
+
|
|
30
|
+
### Minimal example
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
{
|
|
34
|
+
"name": "Demo",
|
|
35
|
+
"tables": [
|
|
36
|
+
{
|
|
37
|
+
"name": "users",
|
|
38
|
+
"rowCount": 100,
|
|
39
|
+
"columns": [
|
|
40
|
+
{ "name": "id", "type": "int", "primaryKey": true },
|
|
41
|
+
{ "name": "email", "type": "text", "nullable": false, "indexed": true }
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "posts",
|
|
46
|
+
"rowCount": 5000,
|
|
47
|
+
"columns": [
|
|
48
|
+
{ "name": "id", "type": "int", "primaryKey": true },
|
|
49
|
+
{
|
|
50
|
+
"name": "user_id",
|
|
51
|
+
"type": "int",
|
|
52
|
+
"foreignKey": { "table": "users", "column": "id" },
|
|
53
|
+
"indexed": true
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### CSV / stats import
|
|
62
|
+
|
|
63
|
+
There is no separate CSV parser in the UI; merge stats in SQL or a small script, then paste JSON. See import guides for row-count queries.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<link rel="icon" type="image/svg+xml" href="./vite.svg" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>3D Database Visualizer</title>
|
|
8
|
+
<script type="module" crossorigin src="./assets/index-MBzCJJWx.js"></script>
|
|
9
|
+
<link rel="stylesheet" crossorigin href="./assets/index-B0dnwIo5.css">
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<div id="root"></div>
|
|
13
|
+
</body>
|
|
14
|
+
</html>
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# Practice curriculum (SQL + schema design)
|
|
2
|
+
#
|
|
3
|
+
# This file is the bundled default for the Practice tab. A copy is kept at
|
|
4
|
+
# public/learn/lessons.md so the same text can be fetched in production when
|
|
5
|
+
# the URL resolves; if fetch fails, the app uses this import.
|
|
6
|
+
|
|
7
|
+
## Lesson: SELECT and WHERE
|
|
8
|
+
`id: sql-select-where, track: sql, sample: ecommerce.json`
|
|
9
|
+
|
|
10
|
+
### Step 1: List all user emails
|
|
11
|
+
Return every `email` from the `users` table.
|
|
12
|
+
|
|
13
|
+
```sql starter
|
|
14
|
+
SELECT
|
|
15
|
+
FROM users
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```sql answer
|
|
19
|
+
SELECT email
|
|
20
|
+
FROM users;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```check yaml
|
|
24
|
+
must_from: users
|
|
25
|
+
must_select_columns:
|
|
26
|
+
- email
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Step 2: Filter with WHERE
|
|
30
|
+
List `email` and `created_at` for users, but only rows you could describe as "recent" by requiring `email` to contain the `@` sign (a stand-in for *valid* addresses in this exercise). Use a `LIKE` pattern: `email LIKE '%@%'`.
|
|
31
|
+
|
|
32
|
+
```sql answer
|
|
33
|
+
SELECT email, created_at
|
|
34
|
+
FROM users
|
|
35
|
+
WHERE email LIKE '%@%';
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```check yaml
|
|
39
|
+
must_from: users
|
|
40
|
+
must_select_columns:
|
|
41
|
+
- email
|
|
42
|
+
- created_at
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Lesson: ORDER BY, LIMIT, and DISTINCT
|
|
46
|
+
`id: sql-order-limit, track: sql, sample: ecommerce.json`
|
|
47
|
+
|
|
48
|
+
### Step 1: Newest first
|
|
49
|
+
List `id` and `placed_at` for `orders`, with the most recent `placed_at` first. (Use `ORDER BY` with `DESC`.)
|
|
50
|
+
|
|
51
|
+
```sql starter
|
|
52
|
+
SELECT id, placed_at
|
|
53
|
+
FROM orders
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```sql answer
|
|
57
|
+
SELECT id, placed_at
|
|
58
|
+
FROM orders
|
|
59
|
+
ORDER BY placed_at DESC;
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```check yaml
|
|
63
|
+
must_from: orders
|
|
64
|
+
must_select_columns:
|
|
65
|
+
- id
|
|
66
|
+
- placed_at
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Step 2: Top 10 (LIMIT)
|
|
70
|
+
From `products`, return `id`, `title`, and `price_cents` for the **10 cheapest** products. Order by `price_cents` ascending, then `LIMIT` 10.
|
|
71
|
+
|
|
72
|
+
```sql answer
|
|
73
|
+
SELECT id, title, price_cents
|
|
74
|
+
FROM products
|
|
75
|
+
ORDER BY price_cents ASC
|
|
76
|
+
LIMIT 10;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```check yaml
|
|
80
|
+
must_from: products
|
|
81
|
+
must_select_columns:
|
|
82
|
+
- id
|
|
83
|
+
- title
|
|
84
|
+
- price_cents
|
|
85
|
+
disallow:
|
|
86
|
+
- cte
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Lesson: JOINs
|
|
90
|
+
`id: sql-joins, track: sql, sample: ecommerce.json`
|
|
91
|
+
|
|
92
|
+
### Step 1: Users and their orders
|
|
93
|
+
Return each **user email** together with the **order id** (`orders.id`). `INNER JOIN` `orders` to `users` on `orders.user_id = users.id` (or equivalent).
|
|
94
|
+
|
|
95
|
+
```sql starter
|
|
96
|
+
SELECT
|
|
97
|
+
FROM users u
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```sql answer
|
|
101
|
+
SELECT u.email, o.id
|
|
102
|
+
FROM users u
|
|
103
|
+
INNER JOIN orders o ON o.user_id = u.id;
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```check yaml
|
|
107
|
+
must_from: users
|
|
108
|
+
must_join_to: orders
|
|
109
|
+
must_select_columns:
|
|
110
|
+
- email
|
|
111
|
+
- id
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Step 2: Orphan order lines (LEFT JOIN)
|
|
115
|
+
Return each `order_items.id` and the matching `products.title` when a product link exists, but **include** `order_items` even when a join might not find a `products` row (in a well-formed DB they always will — the point is to practice `LEFT JOIN`).
|
|
116
|
+
|
|
117
|
+
In practice, use: `FROM order_items oi LEFT JOIN products p ON p.id = oi.product_id`, and `SELECT` e.g. `oi.id, p.title`.
|
|
118
|
+
|
|
119
|
+
```sql answer
|
|
120
|
+
SELECT oi.id, p.title
|
|
121
|
+
FROM order_items oi
|
|
122
|
+
LEFT JOIN products p ON p.id = oi.product_id;
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
```check yaml
|
|
126
|
+
must_from: order_items
|
|
127
|
+
must_join_to: products
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Lesson: GROUP BY and aggregates
|
|
131
|
+
`id: sql-group, track: sql, sample: ecommerce.json`
|
|
132
|
+
|
|
133
|
+
### Step 1: How many line items per order?
|
|
134
|
+
Count rows in `order_items` **per** `order_id` using `GROUP BY order_id` and an aggregate (e.g. `COUNT(*)` as `n`).
|
|
135
|
+
|
|
136
|
+
```sql answer
|
|
137
|
+
SELECT order_id, COUNT(*) AS n
|
|
138
|
+
FROM order_items
|
|
139
|
+
GROUP BY order_id;
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
```check yaml
|
|
143
|
+
must_from: order_items
|
|
144
|
+
require_group_by: true
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Lesson: CTEs (WITH)
|
|
148
|
+
`id: sql-cte, track: sql, sample: ecommerce.json`
|
|
149
|
+
|
|
150
|
+
### Step 1: Name a subquery
|
|
151
|
+
Write a `WITH` clause (CTE) that selects `id` and `email` from `users` into a named CTE, then `SELECT` from that CTE.
|
|
152
|
+
|
|
153
|
+
Example pattern: `WITH u AS (SELECT id, email FROM users) SELECT email FROM u;`
|
|
154
|
+
|
|
155
|
+
```sql answer
|
|
156
|
+
WITH u AS (
|
|
157
|
+
SELECT id, email
|
|
158
|
+
FROM users
|
|
159
|
+
)
|
|
160
|
+
SELECT email
|
|
161
|
+
FROM u;
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
```check yaml
|
|
165
|
+
require_cte: true
|
|
166
|
+
must_cte: u
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Lesson: Add a many-to-many bridge table
|
|
170
|
+
`id: schema-book-tags, track: schema, sample: library.json`
|
|
171
|
+
|
|
172
|
+
### Step 1: New tables
|
|
173
|
+
Create a `tags` table for tag labels and a `book_tags` table that models **which books have which tags**.
|
|
174
|
+
- `tags`: a primary key and a unique `name` (text) per tag.
|
|
175
|
+
- `book_tags`: composite key via **two foreign keys** — to `books.id` and `tags.id` (both required). Index FK columns to match good practices.
|
|
176
|
+
|
|
177
|
+
```check yaml
|
|
178
|
+
table_must_exist: [tags, book_tags]
|
|
179
|
+
columns_by_table:
|
|
180
|
+
tags:
|
|
181
|
+
- name: id
|
|
182
|
+
primaryKey: true
|
|
183
|
+
- name: name
|
|
184
|
+
notNull: true
|
|
185
|
+
type_like: text
|
|
186
|
+
book_tags:
|
|
187
|
+
- name: book_id
|
|
188
|
+
notNull: true
|
|
189
|
+
foreignKey: { table: books, column: id }
|
|
190
|
+
indexed: true
|
|
191
|
+
- name: tag_id
|
|
192
|
+
notNull: true
|
|
193
|
+
foreignKey: { table: tags, column: id }
|
|
194
|
+
indexed: true
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
```text answer
|
|
198
|
+
Expected shape:
|
|
199
|
+
- tags: id (PK), name (text, NOT NULL)
|
|
200
|
+
- book_tags: (book_id FK->books.id, tag_id FK->tags.id) both NOT NULL, indexed; typically you also add a SERIAL PK on book_tags or a composite unique(book_id, tag_id) in real systems — the automated check keys off FKs + common columns.
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Lesson: Link books to a category
|
|
204
|
+
`id: schema-book-category, track: schema, sample: library.json`
|
|
205
|
+
|
|
206
|
+
### Step 1: A categories table + FK
|
|
207
|
+
Create a new table `categories` with a surrogate key (`id`, PK) and a `name` (text, NOT NULL).
|
|
208
|
+
Add a nullable `category_id` column on `books` that is a **foreign key** to `categories.id` and is **indexed**.
|
|
209
|
+
|
|
210
|
+
```check yaml
|
|
211
|
+
min_table_count: 6
|
|
212
|
+
table_must_exist: [categories]
|
|
213
|
+
columns_by_table:
|
|
214
|
+
books:
|
|
215
|
+
- name: category_id
|
|
216
|
+
type_like: int
|
|
217
|
+
foreignKey: { table: categories, column: id }
|
|
218
|
+
indexed: true
|
|
219
|
+
categories:
|
|
220
|
+
- name: id
|
|
221
|
+
primaryKey: true
|
|
222
|
+
- name: name
|
|
223
|
+
notNull: true
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
```text answer
|
|
227
|
+
- categories: id (PK), name
|
|
228
|
+
- books.category_id -> categories.id, nullable, with an index (good for look-ups and many DBs for FKs).
|
|
229
|
+
If your editor created the FK before the column name matched exactly, use the Table tab to match the spec.
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Step 2: Relationship present
|
|
233
|
+
The validator checks the FK; click **Check** after step 1 passes to confirm the relationship is still there.
|
|
234
|
+
|
|
235
|
+
```check yaml
|
|
236
|
+
relationships_must_include:
|
|
237
|
+
- { fromTable: books, fromColumn: category_id, toTable: categories, toColumn: id }
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
```text answer
|
|
241
|
+
Success means inferRelationships() sees the FK: books.category_id -> categories.id.
|
|
242
|
+
```
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "Blog (sample)",
|
|
3
|
+
"tables": [
|
|
4
|
+
{
|
|
5
|
+
"name": "users",
|
|
6
|
+
"rowCount": 3400,
|
|
7
|
+
"columns": [
|
|
8
|
+
{ "name": "id", "type": "serial", "primaryKey": true },
|
|
9
|
+
{ "name": "username", "type": "varchar(64)", "nullable": false, "indexed": true },
|
|
10
|
+
{ "name": "display_name", "type": "text", "nullable": true }
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"name": "posts",
|
|
15
|
+
"rowCount": 18200,
|
|
16
|
+
"columns": [
|
|
17
|
+
{ "name": "id", "type": "serial", "primaryKey": true },
|
|
18
|
+
{
|
|
19
|
+
"name": "author_id",
|
|
20
|
+
"type": "int",
|
|
21
|
+
"nullable": false,
|
|
22
|
+
"foreignKey": { "table": "users", "column": "id" },
|
|
23
|
+
"indexed": true
|
|
24
|
+
},
|
|
25
|
+
{ "name": "slug", "type": "varchar(160)", "nullable": false, "indexed": true },
|
|
26
|
+
{ "name": "title", "type": "text", "nullable": false },
|
|
27
|
+
{ "name": "published_at", "type": "timestamptz", "nullable": true, "indexed": true }
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"name": "comments",
|
|
32
|
+
"rowCount": 96000,
|
|
33
|
+
"columns": [
|
|
34
|
+
{ "name": "id", "type": "bigserial", "primaryKey": true },
|
|
35
|
+
{
|
|
36
|
+
"name": "post_id",
|
|
37
|
+
"type": "int",
|
|
38
|
+
"nullable": false,
|
|
39
|
+
"foreignKey": { "table": "posts", "column": "id" },
|
|
40
|
+
"indexed": true
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"name": "user_id",
|
|
44
|
+
"type": "int",
|
|
45
|
+
"nullable": true,
|
|
46
|
+
"foreignKey": { "table": "users", "column": "id" },
|
|
47
|
+
"indexed": false
|
|
48
|
+
},
|
|
49
|
+
{ "name": "body", "type": "text", "nullable": false },
|
|
50
|
+
{ "name": "created_at", "type": "timestamptz", "nullable": false }
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "tags",
|
|
55
|
+
"rowCount": 420,
|
|
56
|
+
"columns": [
|
|
57
|
+
{ "name": "id", "type": "serial", "primaryKey": true },
|
|
58
|
+
{ "name": "name", "type": "varchar(48)", "nullable": false, "indexed": true }
|
|
59
|
+
]
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"name": "post_tags",
|
|
63
|
+
"rowCount": 38000,
|
|
64
|
+
"columns": [
|
|
65
|
+
{ "name": "id", "type": "bigserial", "primaryKey": true },
|
|
66
|
+
{
|
|
67
|
+
"name": "post_id",
|
|
68
|
+
"type": "int",
|
|
69
|
+
"nullable": false,
|
|
70
|
+
"foreignKey": { "table": "posts", "column": "id" },
|
|
71
|
+
"indexed": true
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"name": "tag_id",
|
|
75
|
+
"type": "int",
|
|
76
|
+
"nullable": false,
|
|
77
|
+
"foreignKey": { "table": "tags", "column": "id" },
|
|
78
|
+
"indexed": true
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
}
|