Skip to content

Commit 7db9ff2

Browse files
fix: annotate db.schema so declaration emit stays under TS7056
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 087b9ff commit 7db9ff2

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

packages/experimental/src/declaration-emit.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,16 @@ describe("declaration emit (TS2883 / package entry)", () => {
109109
rmSync(consumerDir, { recursive: true, force: true });
110110
}
111111
});
112+
113+
it("emits plugins/db schema under the serialize limit", () => {
114+
const dtsPath = join(root, "dist/plugins/db.d.mts");
115+
expect(
116+
existsSync(dtsPath),
117+
"dist/plugins/db.d.mts missing - run pnpm build",
118+
).toBe(true);
119+
const dts = readFileSync(dtsPath, "utf8");
120+
expect(dts.length).toBeLessThan(MAX_DTS_BYTES);
121+
expect(dts).toMatch(/declare const schema:/);
122+
expect(dts).toMatch(/export \{[^}]*\bschema\b/);
123+
});
112124
});

packages/experimental/src/plugins/db.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import { v } from "..";
22
import { createRandomStringGenerator } from "../helpers/random";
3-
import { type TypeDefination, withAttrs } from "../schema";
3+
import {
4+
type DefineOutput,
5+
type InferArgs,
6+
type TypeDefination,
7+
withAttrs,
8+
} from "../schema";
49
import type { LiteralString } from "../types";
10+
import type { VarDefination } from "../var";
11+
12+
/** Named stand-in for `v.var(name, { default: null, schema: v.object(shape) })`.
13+
* Inferring that return from `v.var` exceeds TS7056 on declaration emit. */
14+
type ModelVar<N extends LiteralString, S> = VarDefination<
15+
N,
16+
DefineOutput<S> | null,
17+
TypeDefination<InferArgs<S>, DefineOutput<S>, never> & { shape: S }
18+
>;
519

620
export const generateId = v.fn(
721
"db.generate_id",
@@ -49,8 +63,11 @@ export const id = <T, O>(
4963

5064
/** A model var: `v.var(name, { default: null, schema: v.object(shape) })`.
5165
* Import it from the db plugin: `import { schema } from "better-call/plugins/db"`. */
52-
export const schema = <N extends LiteralString, S>(name: N, shape: S) =>
53-
v.var(name, { default: null, schema: v.object(shape) });
66+
export const schema = <N extends LiteralString, S>(
67+
name: N,
68+
shape: S,
69+
): ModelVar<N, S> =>
70+
v.var(name, { default: null, schema: v.object(shape) }) as ModelVar<N, S>;
5471

5572
export const db = {
5673
unique,

0 commit comments

Comments
 (0)