drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.67k stars 650 forks source link

[BUG]: Sharing Schema/Types #3028

Open Stephane-Pires opened 1 month ago

Stephane-Pires commented 1 month ago

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.23.1

Describe the Bug

Hello ! Overall wonderful library, you really helped me understand SQL, and databases.


Context 💭

Configuration 🛞

App

drizzle-orm : 0.33.0 typescript : 5.6.2"

//tsconfig.json
{
    "extends": "./.svelte-kit/tsconfig.json",
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true,
        "moduleResolution": "bundler"
    }
}

package-schema

drizzle-orm : 0.33.0 drizzle-zod : 0.5.1 typescript : 5.6.2

{
//package.json
//...
 "devDependencies": {
    "typescript": "^5.5.4"
  },
  "dependencies": {
    "drizzle-orm": "^0.33.0",
    "drizzle-zod": "^0.5.1",
    "zod": "^3.23.8"
  }
}
//tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "Bundler",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "declarationMap": true
  },
  "include": ["src/**/*"],
  "exclude": ["dist", "node_modules"]
}

The ERROR (maybe BUG) 🐛

The error from typescript


// catToMaster come from `package-schema`

const allCatToMaster = await db
        .select()
                // The typescript error is on the line below
        .from(catToMaster)
Argument of type 'PgTableWithColumns<{ name: "catToMaster"; schema: undefined; columns: { catId: PgColumn<{ name: "cat_id"; tableName: "catToMaster"; dataType: "string"; columnType: "PgText"; data: string; driverParam: string; notNull: true; ... 6 more ...; generated: undefined; }, {}, {}>; userId: PgColumn<...>; createdAt: PgCol...' is not assignable to parameter of type 'SQL<unknown> | PgTable<TableConfig> | Subquery<string, Record<string, unknown>> | PgViewBase<string, boolean, ColumnsSelection>'.
  Type 'PgTableWithColumns<{ name: "catToMaster"; schema: undefined; columns: { catId: PgColumn<{ name: "cat_id"; tableName: "catToMaster"; dataType: "string"; columnType: "PgText"; data: string; driverParam: string; notNull: true; ... 6 more ...; generated: undefined; }, {}, {}>; userId: PgColumn<...>; createdAt: PgCol...' is not assignable to type 'PgTable<TableConfig>'.
    The types of '_.config.columns' are incompatible between these types.
      Type '{ catId: PgColumn<{ name: "cat_id"; tableName: "catToMaster"; dataType: "string"; columnType: "PgText"; data: string; driverParam: string; notNull: true; hasDefault: false; isPrimaryKey: false; isAutoincrement: false; hasRuntimeDefault: false; enumValues: [...]; baseColumn: never; generated: undefined; }, {}, {}...' is not assignable to type 'Record<string, PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>>'.
        Property 'catId' is incompatible with index signature.
          Type 'PgColumn<{ name: "cat_id"; tableName: "catToMaster"; dataType: "string"; columnType: "PgText"; data: string; driverParam: string; notNull: true; hasDefault: false; isPrimaryKey: false; isAutoincrement: false; hasRuntimeDefault: false; enumValues: [...]; baseColumn: never; generated: undefined; }, {}, {}>' is not assignable to type 'PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>'.
            The types of 'table._.config.columns' are incompatible between these types.
              Type 'Record<string, import("/Users/xxx/Documents/Projets/package-schema/node_modules/drizzle-orm/pg-core/columns/common").PgColumn<import("/Users/xxx/Documents/Projets/package-schema/node_modules/drizzle-orm/column").ColumnBaseConfig<import("/Users/xxx/Documents/Projets/package-schema/node_m...' is not assignable to type 'Record<string, import("/Users/xxx/Documents/Projets/app/node_modules/drizzle-orm/pg-core/columns/common").PgColumn<import("/Users/xxx/Documents/Projets/app/node_modules/drizzle-orm/column").ColumnBaseConfig<import("/Users/xxx/Documents/Projets/app/node_modules/drizzle-orm/co...'.
                'string' index signatures are incompatible.
                  Type 'import("/Users/xxx/Documents/Projets/package-schema/node_modules/drizzle-orm/pg-core/columns/common").PgColumn<import("/Users/xxx/Documents/Projets/package-schema/node_modules/drizzle-orm/column").ColumnBaseConfig<import("/Users/xxx/Documents/Projets/package-schema/node_modules/drizzle-...' is not assignable to type 'import("/Users/xxx/Documents/Projets/app/node_modules/drizzle-orm/pg-core/columns/common").PgColumn<import("/Users/xxx/Documents/Projets/app/node_modules/drizzle-orm/column").ColumnBaseConfig<import("/Users/xxx/Documents/Projets/app/node_modules/drizzle-orm/column-builder")....'.ts(2345)

What I have tried 🤔

Help needed 🙏

Thanks again for this wonderful projet ! You rocks

Expected behavior

I expect to be able to share schema and types between project without having issues regarding typing.

Environment & setup

vscode : 1.93.1 macos : 15.0 (24A335)

abaudhuin commented 1 month ago

I reproduce this exact issue. I need to share my schema between 2 projects. The only workaround I found is to duplicate the schema declaration in both projects...

szolowicz commented 1 month ago

Same typing error here, but for example select returns correct values.

imMadsen commented 1 month ago

I am having the same issue... tried creating a package for my schema to use between my two web apps

imMadsen commented 1 month ago

I found a temporary solution. Since the issue seems to be with the fact that TypeScript can not validate them since they are separate packages, one can use NPM workspaces, this way it will use the "same" package

abaudhuin commented 1 month ago

I found a temporary solution. Since the issue seems to be with the fact that TypeScript can not validate them since they are separate packages, one can use NPM workspaces, this way it will use the "same" package

Can you detail your setup please ? I tried with workspaces without success

imMadsen commented 1 month ago

I found a temporary solution. Since the issue seems to be with the fact that TypeScript can not validate them since they are separate packages, one can use NPM workspaces, this way it will use the "same" package

Can you detail your setup please ? I tried with workspaces without success

I mostly just followed this guide here.

My current repo structure looks like this:

my-monorepo
    - package.json
    - node_modules/
    - packages/
        - drizzle-schema/
        - webapp_1/
        - webapp_2/