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

[BUG]: Sharing Schema/Types #3028

Open
Stephane-Pires opened this issue Sep 29, 2024 · 6 comments
Open

[BUG]: Sharing Schema/Types #3028

Stephane-Pires opened this issue Sep 29, 2024 · 6 comments
Labels
bug Something isn't working monorepo priority Will be worked on next

Comments

@Stephane-Pires
Copy link

Stephane-Pires commented Sep 29, 2024

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 💭

  • I'm running a Sveltekit Application (named : app)
  • I have extracted the schema into a different project (named: package-schema) that i consume inside App by doing npm link Package

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 🤔

  • I have tried various typescript configuration on app or on package-schema, didn't work.
  • Moved schema to app THIS IS WORKING and resolving the issue 👍 (But I don't want my schemas to live in the app project

Help needed 🙏

  • You can point me to the documentation if you think I miss something (even if I have extensively read trough it)
  • You can point me to an issue, that is related
  • If needed i can create a minimal repo(s) to reproduce it.

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)

@Stephane-Pires Stephane-Pires added the bug Something isn't working label Sep 29, 2024
@abaudhuin
Copy link

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
Copy link

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

@imMadsen
Copy link

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

@imMadsen
Copy link

imMadsen commented Oct 10, 2024

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
Copy link

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
Copy link

imMadsen commented Oct 11, 2024

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/

@L-Mario564 L-Mario564 added priority Will be worked on next monorepo labels Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working monorepo priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

5 participants