Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: Allow for hardcoded values in relations #2268

Open
Ziothh opened this issue May 7, 2024 · 0 comments
Open

[FEATURE]: Allow for hardcoded values in relations #2268

Ziothh opened this issue May 7, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Ziothh
Copy link

Ziothh commented May 7, 2024

Describe what you want

I want to add a media relation to my invoices table.
The media table can point to any table via the media.attachableType column.

In this case the attachable type is "invoice" which I want to hard code into the relation definition.
The media.attachableId column references the PK of the invoices table.

Is this supported in any way atm?

SELECT
    *
FROM
    "invoices"
    INNER JOIN "media" ON (
        "media"."attachable_type" = 'invoice' -- Hard coded value
        AND "media"."attachable_id" = "invoices"."id"
    )
export const media = pgTable(
    'media',
    {
        id: serial('id').primaryKey().notNull(),
        attachableType: varchar('attachable_type', { length: 255 }).$type<
            mediaSDK.MediaType | (string & {})
        >(),
        attachableId: integer('attachable_id'),

        // Snip...

        createdAt: timestamp('created_at', { withTimezone: true, mode: 'date' }).defaultNow(),
        updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date' }).defaultNow(),
    },
    (table) => ({
        mediaAttachabletypeAttachableidPathUnique: unique(
            'media_attachabletype_attachableid_path_unique',
        ).on(table.attachableType, table.attachableId, table.path),
    }),
)
export const invoices = pgTable('invoices', {
    id: serial('id').primaryKey().notNull(),
   
    // Snip...

    createdAt: timestamp('created_at', { withTimezone: true, mode: 'date' }).defaultNow(),
    updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date' }).defaultNow(),
})
export const invoiceRelations = relations(invoices, ({ one }) => ({
    deal: one(deals, {
        references: [deals.id],
        fields: [invoices.dealID],
    }),
    tenant: one(tenants, {
        references: [tenants.id],
        fields: [invoices.tenantID],
    }),
    media: one(media, {
        references: [media.attachableId, media.attachableType],
        fields: [invoices.id, 'invoice'], // I want to hard code this value
    }),
}))
@Ziothh Ziothh added the enhancement New feature or request label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant