Skip to content

Commit

Permalink
fix: fixed types for integration
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcadams committed Jan 6, 2025
1 parent 52590d2 commit 7f8f1a7
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion integration/tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
1 change: 0 additions & 1 deletion integration/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ export default defineConfig({
include: ["tests/**/*.test.ts"],
testTimeout: 60000,
},

plugins: [tsconfigPaths()],
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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,
createZeroTableSchema,
type ColumnsConfig,
type CreateZeroSchema,
type CreateZeroTableSchema,
type RelationsForTable,
type ZeroColumns,
};
14 changes: 11 additions & 3 deletions src/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -55,6 +55,14 @@ type TableColumnsConfig<TSchema extends Record<string, unknown>> = {

type RelationsConfig<T extends Relations> = ReturnType<T["config"]>;

type ColumnIndexKeys<T extends Table> = {
[K in keyof Columns<T>]: Columns<T>[K] extends {
dataType: "string" | "number";
}
? ColumnName<Columns<T>[K]>
: never;
}[keyof Columns<T>];

type ReferencedZeroSchemas<
TSchema extends Record<string, unknown>,
TColumns extends TableColumnsConfig<TSchema>,
Expand Down Expand Up @@ -94,7 +102,7 @@ type ReferencedZeroSchemas<
};
}
? TSchema[P] extends Table<any>
? ColumnNames<TSchema[P]>
? ColumnIndexKeys<TSchema[P]>
: never
: never;
}[keyof TSchema]
Expand All @@ -106,7 +114,7 @@ type ReferencedZeroSchemas<
>[ColumnName]["_"] extends {
name: string;
}
? ColumnNames<TSchema[P]>
? ColumnIndexKeys<TSchema[P]>
: never;
}[keyof Columns<TSchema[P]>]
>;
Expand Down
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,8 +18,6 @@ type ColumnDefinition<T extends Table, K extends ColumnNames<T>> = {
: never;
}[keyof Columns<T>];

export type RelationsForTable<T extends Table> = Relations<TableName<T>>;

export type FindPrimaryKeyFromTable<TTable extends Table> = {
[K in keyof Columns<TTable>]: Columns<TTable>[K]["_"]["isPrimaryKey"] extends true
? ColumnName<Columns<TTable>[K]>
Expand Down
8 changes: 4 additions & 4 deletions tests/relationships.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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"
>,
Expand Down Expand Up @@ -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,
},
},
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"emitDeclarationOnly": true
}
}

0 comments on commit 7f8f1a7

Please sign in to comment.