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

fix optional types #4

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const createZeroSchema = <

if (!sourceFieldNames.length || !destFieldNames.length) {
throw new Error(
`No source or dest field names found for: ${relation.fieldName}`,
`No relationship found for: ${relation.fieldName} (${relation instanceof One ? "One" : "Many"} from ${tableName} to ${relation.referencedTableName}). Did you forget to define foreign keys?`,
);
}

Expand Down
12 changes: 7 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ type ZeroMappedCustomType<
? ColumnDefinition<T, K>["_"]["$type"]
: ZeroTypeToTypescriptType[ZeroMappedColumnType<T, K>];

type ZeroColumnDefinition<T extends Table, K extends ColumnNames<T>> = Readonly<
type ZeroColumnDefinition<
T extends Table,
K extends ColumnNames<T>,
CD extends ColumnDefinition<T, K>["_"] = ColumnDefinition<T, K>["_"],
> = Readonly<
{
readonly optional: ColumnDefinition<T, K>["_"]["notNull"] extends true
? boolean // false
: boolean; // true;
readonly optional: CD extends { notNull: true } ? false : true;
readonly type: ZeroMappedColumnType<T, K>;
readonly customType: ZeroMappedCustomType<T, K>;
} & (ColumnDefinition<T, K>["_"] extends { columnType: "PgEnumColumn" }
} & (CD extends { columnType: "PgEnumColumn" }
? { readonly kind: "enum" }
: {})
>;
Expand Down
58 changes: 30 additions & 28 deletions tests/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execSync } from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { expect, test } from "vitest";
import { expect, test, describe } from "vitest";

const runZeroBuildSchema = async (testName: string) => {
const schemaPath = path.join(
Expand Down Expand Up @@ -36,37 +36,39 @@ const runZeroBuildSchema = async (testName: string) => {
}
};

test("compile - one-to-one", async () => {
const result = await runZeroBuildSchema("one-to-one");
expect(result.schema.tables.user).toBeTruthy();
});
describe.concurrent("compile", () => {
test("compile - one-to-one", async () => {
const result = await runZeroBuildSchema("one-to-one");
expect(result.schema.tables.user).toBeTruthy();
});

test("compile - one-to-one-2", async () => {
const result = await runZeroBuildSchema("one-to-one-2");
expect(result.schema.tables.user).toBeTruthy();
});
test("compile - one-to-one-2", async () => {
const result = await runZeroBuildSchema("one-to-one-2");
expect(result.schema.tables.user).toBeTruthy();
});

test("compile - one-to-one-foreign-key", async () => {
const result = await runZeroBuildSchema("one-to-one-foreign-key");
expect(result.schema.tables.users).toBeTruthy();
});
test("compile - one-to-one-foreign-key", async () => {
const result = await runZeroBuildSchema("one-to-one-foreign-key");
expect(result.schema.tables.users).toBeTruthy();
});

test("compile - one-to-one-self", async () => {
const result = await runZeroBuildSchema("one-to-one-self");
expect(result.schema.tables.user).toBeTruthy();
});
test("compile - one-to-one-self", async () => {
const result = await runZeroBuildSchema("one-to-one-self");
expect(result.schema.tables.user).toBeTruthy();
});

test("compile - one-to-many", async () => {
const result = await runZeroBuildSchema("one-to-many");
expect(result.schema.tables.user).toBeTruthy();
});
test("compile - one-to-many", async () => {
const result = await runZeroBuildSchema("one-to-many");
expect(result.schema.tables.user).toBeTruthy();
});

test("compile - one-to-many-named", async () => {
const result = await runZeroBuildSchema("one-to-many-named");
expect(result.schema.tables.users).toBeTruthy();
});
test("compile - one-to-many-named", async () => {
const result = await runZeroBuildSchema("one-to-many-named");
expect(result.schema.tables.users).toBeTruthy();
});

test("compile - many-to-many", async () => {
const result = await runZeroBuildSchema("many-to-many");
expect(result.schema.tables.user).toBeTruthy();
test("compile - many-to-many", async () => {
const result = await runZeroBuildSchema("many-to-many");
expect(result.schema.tables.user).toBeTruthy();
});
});
Loading
Loading