IF Commons GitHub

sql/typed/table

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0053-sql-typed FR-003

Typed tables (commons data 0053-sql-typed): a table is a value with the types of its rows (R, what a select gives), of what an insert takes (I, where what the database fills may be left out) and the name by which it is known in a query (N), and with a typed column for each of its keys. The keys are the names a program writes (camel case); the names in SQL are written as the table says. A table is made by hand with defineTable or by the generator of this unit from a model, with the types a model says.

Source: commons/data/sql/typed/table.ts

1 of 5 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

Columnstypeno test imports it
type Columns<R> = { readonly [K in keyof R & string]: Column<R[K]> };

The columns of a row type, each an expression that gives its value.

defineTableconst
const defineTable: <R, I, N extends string>(spec: TableSpec<R, N>) => Table<R, I, N>

Makes a table.

interface Author { id: number; name: string; bio: string | null }
interface AuthorInsert { id?: number; name: string; bio?: string | null }
const author = defineTable<Author, AuthorInsert, "author">({ alias: "author", name: "author", columns: { id: "id", name: "name", bio: "bio" }, primaryKey: ["id"] });

Imported by 20 tests of the run (20 passed, 0 failed): the library's results

ForeignKeyInfointerfaceno test imports it
interface ForeignKeyInfo

A foreign key as the model says it, by the keys of the columns.

MemberTypeWhat it is
columnsreadonly string[]
referencesstring
referencesColumnsreadonly string[]
NullableColumnstypeno test imports it
type NullableColumns<R> = { readonly [K in keyof R & string]: Column<R[K] | null> };

The same where the table may be missing from a row (the other side of an outer join): every column may be null.

TableSpecinterfaceno test imports it
interface TableSpec<R, N extends string>

What defineTable is told.

MemberTypeWhat it is
aliasNThe name in a query, which keys the scope of a select.
namestringThe name in SQL.
schema?string
columns{ readonly [K in keyof R & string]: string; }key → name in SQL, for each key of the row.
primaryKey?readonly (keyof R & string)[]
foreignKeys?readonly ForeignKeyInfo[]

Titles, headings, exports, tables, columns and requirements. Esc closes.