Skip to content

Not able to generate dbml when using drizzle 'pgPolicy' #27

@gpaiva985

Description

@gpaiva985

First of all, thanks for your work in this library!
This is what was missing to use drizzle as the single source of true and also have good visualization to archictect DB structure changes.

I just face a small issue when I added the new drizzle RLS.
That was my table which I can generate the DBML without problems:

import { toSnakeCase } from "drizzle-orm/casing";
import { check, pgTable, primaryKey, text, unique } from "drizzle-orm/pg-core";

import type { types } from "@/db/shared";

import { constants, functions } from "@/db/shared";

const tableName: types.TableName = "foods";

export const foods = pgTable(toSnakeCase(tableName), {
  ...constants.defaultColumns,
  name: text().notNull(),
  code: text().notNull(),
}, t => [
  primaryKey({ columns: [t.id], name: functions.generateConstraintName({ constraintType: "primaryKey", tableName }) }),
  check(functions.generateConstraintName({ constraintType: "check", tableName, columnNames: ["code"] }), sql`${t.code} ~ '^[A-Z]{3}[0-9]{4}[A-Z]$'`),
  unique(functions.generateConstraintName({ constraintType: "unique", tableName, columnNames: ["code", "name"] })).on(t.code, t.name),
]);

But when I add drizzle 'pgPolicy' like this:

import { toSnakeCase } from "drizzle-orm/casing";
import { check, pgPolicy, pgTable, primaryKey, text, unique } from "drizzle-orm/pg-core";

import type { types } from "@/db/shared";

import { constants, functions } from "@/db/shared";

const tableName: types.TableName = "foods";

export const foods = pgTable(toSnakeCase(tableName), {
  ...constants.defaultColumns,
  name: text().notNull(),
  code: text().notNull(),
}, t => [
  primaryKey({ columns: [t.id], name: functions.generateConstraintName({ constraintType: "primaryKey", tableName }) }),
  check(functions.generateConstraintName({ constraintType: "check", tableName, columnNames: ["code"] }), sql`${t.code} ~ '^[A-Z]{3}[0-9]{4}[A-Z]$'`),
  unique(functions.generateConstraintName({ constraintType: "unique", tableName, columnNames: ["code", "name"] })).on(t.code, t.name),
  pgPolicy("Enable read access for all users", { as: "permissive", to: "public", for: "select", using: sql`true` }), <-- add this
]);

I got this error:

178 |     }
179 |     const extraConfigBuilder = table[ExtraConfigBuilder];
180 |     const extraConfigColumns = table[ExtraConfigColumns];
181 |     const extraConfig = extraConfigBuilder?.(extraConfigColumns ?? {});
182 |     const builtIndexes = (Array.isArray(extraConfig) ? extraConfig : Object.values(extraConfig ?? {})).map((b) => b?.build(table)).filter((b) => b !== void 0).filter((index) => !((0, import_drizzle_orm.is)(index, import_pg_core.Check) || (0, import_drizzle_orm.is)(index, import_mysql_core.Check) || (0, import_drizzle_orm.is)(index, import_sqlite_core.Check)));
                                                                                                                           ^
TypeError: b?.build is not a function. (In 'b?.build(table)', 'b?.build' is undefined)
      at <anonymous> (/Users/gabrielpaiva/Projects/panelinha/panelinha-sb-api/node_modules/drizzle-dbml-generator/dist/index.cjs:182:118)
      at map (1:11)
      at generateTable (/Users/gabrielpaiva/Projects/panelinha/panelinha-sb-api/node_modules/drizzle-dbml-generator/dist/index.cjs:182:104)
      at generate (/Users/gabrielpaiva/Projects/panelinha/panelinha-sb-api/node_modules/drizzle-dbml-generator/dist/index.cjs:317:35)
      at pgGenerate (/Users/gabrielpaiva/Projects/panelinha/panelinha-sb-api/node_modules/drizzle-dbml-generator/dist/index.cjs:363:68)
      at /Users/gabrielpaiva/Projects/panelinha/panelinha-sb-api/supabase/dbml.ts:8:1

Bun v1.2.1 (macOS arm64)
error: script "db:dbml" exited with code 1

This is the versions I am using:
"drizzle-orm": "^0.39.1",
"drizzle-kit": "^0.30.4",
"drizzle-dbml-generator": "^0.10.0",

Let me know if further information is needed.

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions