Skip to content

Commit 2c1bb3e

Browse files
committed
introduction and beginning of types section.
1 parent 3a6a68a commit 2c1bb3e

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Tabs from '@theme/Tabs'
2+
import TabItem from '@theme/TabItem'
3+
import { Types } from './json-traversal/Types'
4+
5+
# JSON Traversal
6+
7+
In the era of data-driven applications, SQL databases such as PostgreSQL, MySQL,
8+
and SQLite have evolved to support JSON traversal. JSON provides a flexible and
9+
dynamic data structure, and these databases now offer native support for storing,
10+
querying, and manipulating JSON data within the SQL framework.
11+
12+
PostgreSQL, MySQL, and SQLite have introduced native JSON data types, along with
13+
functions and operators for extracting values, filtering conditions, aggregating
14+
arrays, and indexing JSON data. This enables developers to leverage the strengths
15+
of SQL and JSON together, combining relational querying with the flexibility of
16+
JSON manipulation.
17+
18+
These capabilities have always fascinated us, and finally we are able to bring
19+
them to Kysely and through some exploration and experimentation, we'll hopefully
20+
land together on a solution that is both elegant, practical, and most importantly,
21+
type-safe.
22+
23+
Each dialect Kysely supports at its core, has a different way of handling JSON
24+
traversal. So this recipe will require choosing a dialect to work with:
25+
26+
<Tabs queryString="dialect">
27+
<TabItem value="postgresql" label="PostgreSQL" default></TabItem>
28+
<TabItem value="mysql" label="MySQL"></TabItem>
29+
<TabItem value="sqlite" label="SQLite"></TabItem>
30+
</Tabs>
31+
32+
## Defining Types
33+
34+
<Types />
35+
36+
## Querying
37+
38+
## Inference
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react'
2+
import { Dialect } from '@site/docs/getting-started/shared'
3+
import CodeBlock from '@theme/CodeBlock'
4+
5+
export interface TypesProps {
6+
dialect: Dialect
7+
}
8+
9+
export function Types(props: TypesProps) {
10+
return (
11+
<>
12+
<p>
13+
When defining a JSON column's type, it must follow the following rules:
14+
<br />
15+
<br />
16+
<strong>Root column type</strong> - The root select type must be of
17+
object or array type. It can be nullable, but cannot be optional (
18+
<code>jsonColumn?:</code>), just like any other column type definition.
19+
Its insert and update types must be strings, as you'd{' '}
20+
<code>JSON.stringify</code> JSON parameters.
21+
<br />
22+
<br />
23+
<strong>Nested field type</strong> - Nested fields must have a JSON
24+
native type (string, number, boolean, null, object or array). Unlike the
25+
root column, nested fields can be optional (<code>field?:</code>).
26+
<br />
27+
<br />
28+
<strong>
29+
Unknowns, JSON (Discriminated) Unions and other complexities
30+
</strong>{' '}
31+
- Supporting traversal to not-well-defined JSONs or complex types was
32+
not part of phase 1. It could work right now, but we haven't tested it.
33+
We'd appreciate any feedback or real-world examples, as we prepare for
34+
the next phases.
35+
<br />
36+
<hr />
37+
</p>
38+
<CodeBlock language="ts" title="src/types.ts">
39+
{`import { ColumnType, Generated, JSONColumnType } from 'kysely'
40+
41+
export interface Database {
42+
person_metadata: PersonMetadataTable
43+
}
44+
45+
export interface PersonMetadataTable {
46+
profile: JSONColumnType<{ created_at: string }> // short for ColumnType<T, string, string>
47+
}`}
48+
</CodeBlock>
49+
</>
50+
)
51+
}

0 commit comments

Comments
 (0)