|
| 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