-
-
Notifications
You must be signed in to change notification settings - Fork 16
Open
Labels
enhancementNew feature or requestNew feature or request
Description
The current way of defining tables is weird, to say the least.
You can't simply tell TS that books belong to authors and authors have many books, because TS is confused with the type recursion (book -> author -> book -> author -> ...).
The limitation can be only bypassed by using class syntax.
I've been looking for a workaround to make it look nicer for years, and finally I got it!
I have a working proof-of-concept, so this should work out well, here is the spoiler of the future table syntax:
const BookTable = defineTable("book")
.columns((t) => ({
id: t.identity().primaryKey(),
title: t.string(),
authorId: t.integer().references(() => AuthorTable("id")),
}))
.relations((t) => ({
author: t.belongsTo({
from: ["authorId"],
to: () => AuthorTable("id"),
}),
}));
const AuthorTable = defineTable("author")
.columns((t) => ({
id: t.identity().primaryKey(),
name: t.string(),
}))
.relations((t) => ({
books: t.hasMany({
from: ["id"],
to: () => BookTable("id"),
}),
}));IlyaSemenov
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request