Interactions-as-a-Service / d1-orm

A simple, strictly typed ORM, to assist you in using Cloudflare's D1 product
https://docs.interactions.rest/d1-orm/
Apache License 2.0
162 stars 11 forks source link

Add Foreign Key Support #54

Open kingmesal opened 1 year ago

kingmesal commented 1 year ago

Adding on to #53 ...

For foreign keys there are a number of options a foreign key may have, but from a definition standpoint adding the foreign key to the column definition make the most sense as each column may only reference a single foreign column.

What I propose is the following:

{
  username: {
    type: DataTypes.STRING,
    unique: "some-arbitrary-key",
  },
  session: {
    type: DataTypes.STRING,
    foreignKey: {
      table: "session-table", // example like Model.tableName
      column: "session-table-field-name", // example like Model.columnName
      onDelete?: {
          cascade?: boolean,
          restrict?: boolean,
          setNull?: boolean,
          setDefault?: boolean,
      },
      onUpdate?: {
          cascade?: boolean,
          restrict?: boolean,
          setNull?: boolean,
          setDefault?: boolean,
      },
    },
  },
}

I think this approach is fairly straight-forward and easy to follow.

Thoughts?

Skye-31 commented 1 year ago

Duplicate of #23?

kingmesal commented 1 year ago

Why would foreign key definitions be a duplicate of join syntax ?

Foreign keys are not required to provide join support.

StationSoen commented 1 year ago

It seems great. 👍

chrisspiegl commented 1 year ago

Are you planning to add support for the foreign keys and join syntax to build relationships with this ORM?

Would be awesome to fetch things more easily this way.

Skye-31 commented 1 year ago

Are you planning to add support for the foreign keys and join syntax to build relationships with this ORM?

Would be awesome to fetch things more easily this way.

@chrisspiegl

Pulls welcome 😃