Skip to content

Commit b57a2a6

Browse files
committed
primary keys are non-nullable
1 parent 62429e3 commit b57a2a6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/fmodata/src/orm/field-builders.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ export class FieldBuilder<
3636

3737
/**
3838
* Mark this field as the primary key for the table.
39-
* Primary keys are automatically read-only.
39+
* Primary keys are automatically read-only and non-nullable.
4040
*/
41-
primaryKey(): FieldBuilder<TOutput, TInput, TDbType, true> {
41+
primaryKey(): FieldBuilder<
42+
NonNullable<TOutput>,
43+
NonNullable<TInput>,
44+
NonNullable<TDbType>,
45+
true
46+
> {
4247
const builder = this._clone() as any;
4348
builder._primaryKey = true;
49+
builder._notNull = true; // Primary keys are automatically non-nullable
4450
builder._readOnly = true; // Primary keys are automatically read-only
4551
return builder;
4652
}

packages/fmodata/tests/typescript.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* helping ensure the API remains ergonomic and type-safe as the library evolves.
1919
*/
2020

21-
import { describe, expect, it, expectTypeOf, beforeEach } from "vitest";
21+
import { describe, expect, it, expectTypeOf } from "vitest";
2222
import { z } from "zod/v4";
2323
import {
2424
fmTableOccurrence,
@@ -28,6 +28,7 @@ import {
2828
FMTable,
2929
getTableColumns,
3030
eq,
31+
type InferTableSchema,
3132
} from "@proofkit/fmodata";
3233
import { createMockFetch } from "./utils/mock-fetch";
3334
import { createMockClient, contacts, users } from "./utils/test-setup";
@@ -554,4 +555,20 @@ describe("fmodata", () => {
554555
void _typeChecks;
555556
});
556557
});
558+
559+
describe("InferSchemaType", () => {
560+
it("Primary key fields should not be nullable in the inferred schema", () => {
561+
const specialUsers = fmTableOccurrence("specialUsers", {
562+
id: textField().primaryKey(),
563+
name: textField(),
564+
});
565+
type SpecialUserSchema = InferTableSchema<typeof specialUsers>;
566+
type IdField = SpecialUserSchema["id"];
567+
568+
const controlTest: string | null = null;
569+
570+
// @ts-expect-error - id should not be nullable
571+
const idData: IdField = null;
572+
});
573+
});
557574
});

0 commit comments

Comments
 (0)