Skip to content

Redesign table config #401

@romeerez

Description

@romeerez

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"),
    }),
  }));

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions