From 7f8f1a7bee1960601d7b72852cb77258d6298aae Mon Sep 17 00:00:00 2001 From: Chase Adams Date: Sun, 5 Jan 2025 22:33:17 -0700 Subject: [PATCH] fix: fixed types for integration --- .npmignore | 2 ++ README.md | 4 ++-- integration/tests/integration.test.ts | 10 +++++++++- integration/vitest.config.ts | 1 - package.json | 2 +- src/index.ts | 3 +-- src/relations.ts | 14 +++++++++++--- src/types.ts | 4 +--- tests/relationships.test.ts | 8 ++++---- tsconfig.build.json | 7 +++++++ 10 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 tsconfig.build.json diff --git a/.npmignore b/.npmignore index ad4db91..73fedc5 100644 --- a/.npmignore +++ b/.npmignore @@ -1,10 +1,12 @@ integration/ tests/ +coverage/ .gitignore .github/ .prettierrc .vscode/ vitest.config.ts tsconfig.json +tsconfig.build.json pnpm-lock.yaml pnpm-workspace.yaml diff --git a/README.md b/README.md index 40cff12..8c46e38 100644 --- a/README.md +++ b/README.md @@ -54,10 +54,10 @@ import * as drizzleSchema from "./drizzle-schema"; export const schema = createSchema( createZeroSchema(drizzleSchema, { version: 1, - // Specify which columns to include in the Zero schema + // Specify which tables and columns to include in the Zero schema // this allows for the "expand/migrate/contract" pattern recommended in the Zero docs // e.g. when a column is first added, it should be set to false, and then changed to true - // once the migration has been run. + // once the migration has been run tables: { user: { id: true, diff --git a/integration/tests/integration.test.ts b/integration/tests/integration.test.ts index 4861e37..79dcb37 100644 --- a/integration/tests/integration.test.ts +++ b/integration/tests/integration.test.ts @@ -54,14 +54,22 @@ test("can query messages with relationships", async () => { const preloadedMessages = await zero.query.message .related("medium") + .related("sender") .preload(); await preloadedMessages.complete; - const messages = await zero.query.message.related("medium").one().run(); + const messages = await zero.query.message + .related("sender") + .related("medium") + .one() + .run(); expect(messages?.medium).toHaveLength(1); expect(messages?.medium[0]?.name).toBe("email"); + expect(messages?.sender).toHaveLength(1); + expect(messages?.sender[0]?.name).toBe("James"); + preloadedMessages.cleanup(); }); diff --git a/integration/vitest.config.ts b/integration/vitest.config.ts index 5c4d665..d3857e6 100644 --- a/integration/vitest.config.ts +++ b/integration/vitest.config.ts @@ -6,6 +6,5 @@ export default defineConfig({ include: ["tests/**/*.test.ts"], testTimeout: 60000, }, - plugins: [tsconfigPaths()], }); diff --git a/package.json b/package.json index 5895c55..5bfe5fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "drizzle-zero", - "version": "0.1.3", + "version": "0.1.4", "description": "Generate Zero schemas from Drizzle ORM schemas", "type": "module", "scripts": { diff --git a/src/index.ts b/src/index.ts index f565d3f..94c8cce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { createZeroSchema, type CreateZeroSchema } from "./relations"; import { createZeroTableSchema, type CreateZeroTableSchema } from "./tables"; -import type { ColumnsConfig, RelationsForTable, ZeroColumns } from "./types"; +import type { ColumnsConfig, ZeroColumns } from "./types"; export { createZeroSchema, @@ -8,6 +8,5 @@ export { type ColumnsConfig, type CreateZeroSchema, type CreateZeroTableSchema, - type RelationsForTable, type ZeroColumns, }; diff --git a/src/relations.ts b/src/relations.ts index 84fe8ee..812e2e0 100644 --- a/src/relations.ts +++ b/src/relations.ts @@ -10,7 +10,7 @@ import { getTableConfig } from "drizzle-orm/pg-core"; import { createZeroTableSchema, type CreateZeroTableSchema } from "./tables"; import type { AtLeastOne, - ColumnNames, + ColumnName, Columns, ColumnsConfig, FindPrimaryKeyFromTable, @@ -55,6 +55,14 @@ type TableColumnsConfig> = { type RelationsConfig = ReturnType; +type ColumnIndexKeys = { + [K in keyof Columns]: Columns[K] extends { + dataType: "string" | "number"; + } + ? ColumnName[K]> + : never; +}[keyof Columns]; + type ReferencedZeroSchemas< TSchema extends Record, TColumns extends TableColumnsConfig, @@ -94,7 +102,7 @@ type ReferencedZeroSchemas< }; } ? TSchema[P] extends Table - ? ColumnNames + ? ColumnIndexKeys : never : never; }[keyof TSchema] @@ -106,7 +114,7 @@ type ReferencedZeroSchemas< >[ColumnName]["_"] extends { name: string; } - ? ColumnNames + ? ColumnIndexKeys : never; }[keyof Columns] >; diff --git a/src/types.ts b/src/types.ts index a9d4e64..d809d58 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ import type { JSONValue } from "@rocicorp/zero"; -import type { Column, Relations, Table } from "drizzle-orm"; +import type { Column, Table } from "drizzle-orm"; import type { DrizzleColumnTypeToZeroType, DrizzleDataTypeToZeroType, @@ -18,8 +18,6 @@ type ColumnDefinition> = { : never; }[keyof Columns]; -export type RelationsForTable = Relations>; - export type FindPrimaryKeyFromTable = { [K in keyof Columns]: Columns[K]["_"]["isPrimaryKey"] extends true ? ColumnName[K]> diff --git a/tests/relationships.test.ts b/tests/relationships.test.ts index 27d90e4..9a58713 100644 --- a/tests/relationships.test.ts +++ b/tests/relationships.test.ts @@ -151,7 +151,7 @@ describe.concurrent("relationships", () => { relationships: { profileInfo: { sourceField: ["id"] as AtLeastOne<"id" | "name">, - destField: ["user_id"] as AtLeastOne<"id" | "user_id" | "metadata">, + destField: ["user_id"] as AtLeastOne<"id" | "user_id">, destSchema: () => expectedProfileInfo, }, }, @@ -179,7 +179,7 @@ describe.concurrent("relationships", () => { primaryKey: ["id"], relationships: { user: { - sourceField: ["user_id"] as AtLeastOne<"id" | "user_id" | "metadata">, + sourceField: ["user_id"] as AtLeastOne<"id" | "user_id">, destField: ["id"] as AtLeastOne<"id" | "name">, destSchema: () => expectedUsers, }, @@ -328,7 +328,7 @@ describe.concurrent("relationships", () => { primaryKey: ["id"], relationships: { messages: { - sourceField: ["id"] as AtLeastOne<"id" | "name" | "partner">, + sourceField: ["id"] as AtLeastOne<"id" | "name">, destField: ["senderId"] as AtLeastOne< "id" | "senderId" | "mediumId" | "body" >, @@ -400,7 +400,7 @@ describe.concurrent("relationships", () => { sourceField: ["senderId"] as AtLeastOne< "id" | "senderId" | "mediumId" | "body" >, - destField: ["id"] as AtLeastOne<"id" | "name" | "partner">, + destField: ["id"] as AtLeastOne<"id" | "name">, destSchema: () => expectedUsers, }, }, diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..3325a60 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": false, + "emitDeclarationOnly": true + } +}