From ba4af16b5ae01f0f966e3eba9344627441d89b68 Mon Sep 17 00:00:00 2001 From: DynoTable Date: Sun, 22 Nov 2026 20:17:57 +0100 Subject: [PATCH 1/4] docs: explain types inferred from one schema --- README.md | 51 +++++++++++++++++++++++++++++ packages/dynotable-orm/src/model.ts | 6 +++- packages/dynotable-orm/src/types.ts | 9 ++++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0b8df3f..d79acca 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,57 @@ console.log(user?.plan, found.count); `create` writes only when the key is not stored yet; pass `{overwrite: true}` to replace. `User.update` with a wrong path or value type does not compile. +## Types from the schema + +Define the shape of an item once, as a schema. You write no interface: `model()` infers every type of the +model from the schema and its marks. The same schema validates each whole-item write at runtime, and with +the Zod adapter each update too. When you change a field, the types and the validation change together. + +```ts +import {model, type ItemOf, type KeyInputOf, type UpdateInputOf} from 'dynotable-orm'; +import {extendZod} from 'dynotable-orm/zod'; +import {z} from 'zod'; + +extendZod(z); + +export const Task = model( + 'Task', + z.object({ + projectId: z.string().hashKey(), + taskId: z.string().rangeKey(), + status: z.enum(['open', 'done']).default('open'), + points: z.number().int() + }) +); + +export type Task = ItemOf; // {projectId: string; taskId: string; status: 'open' | 'done'; points: number} +export type TaskKey = KeyInputOf; // {projectId: string; taskId: string} +export type TaskChanges = UpdateInputOf; // each path with its value type, plus $SET, $ADD, ... + +const key: TaskKey = {projectId: 'p1', taskId: 't1'}; +const task: Task | undefined = await Task.get(key); +console.log(task?.status); + +// @ts-expect-error: the key needs taskId, the range key +await Task.get({projectId: 'p1'}); + +// @ts-expect-error: points is a number +await Task.update(key, {points: '3'}); +``` + +| type | what it is | +| -------------------------------- | -------------------------------------------------------------------------------- | +| `ItemOf` | the item a read returns: the schema's output type, as `z.infer` gives it | +| `KeyInputOf` | the key `get`, `update` and `delete` take, from the hash key and range key marks | +| `UpdateInputOf` | what `update` takes: each path with its value type, and the update operators | +| `Parameters[0]` | what `create` takes: a `Partial` of the schema's input type | + +A write takes the schema's input type and a read returns its output type, so a `.default()` field is +optional on `create` and always present on a read. `timestamps: true` adds no field to the types: declare +`createdAt` and `updatedAt` in the schema. Without the Zod adapter, the item type still comes from the schema, +and the keys and indexes come from the model options. The +[Zod adapter docs](https://dynotable.com/orm/zod#types-from-the-schema) have the full rules. + ## Migrate from dynamoose to TypeScript and Zod The codemod rewrites dynamoose call sites and converts each dynamoose `Schema` to a Zod schema plus model diff --git a/packages/dynotable-orm/src/model.ts b/packages/dynotable-orm/src/model.ts index d9bf0fc..ed81c86 100644 --- a/packages/dynotable-orm/src/model.ts +++ b/packages/dynotable-orm/src/model.ts @@ -213,7 +213,11 @@ const projection = (project: IndexPlan['project']): Projection => ? {ProjectionType: 'KEYS_ONLY'} : {ProjectionType: 'INCLUDE', NonKeyAttributes: [...project]}; -/** Defines a model. Keys, indexes, dates and timestamps are resolved now, so a bad definition throws here. */ +/** + * Defines a model. Every type of the model is inferred from `schema` and `options`, so you write no interface: + * the item (`ItemOf`), the key (`KeyInputOf`), the changes `update` takes (`UpdateInputOf`) and the index names. + * Keys, indexes, dates and timestamps are resolved now, so a bad definition throws here. + */ export function model< const N extends string, S extends StandardSchema, diff --git a/packages/dynotable-orm/src/types.ts b/packages/dynotable-orm/src/types.ts index 2dc2bfc..8b4ffa1 100644 --- a/packages/dynotable-orm/src/types.ts +++ b/packages/dynotable-orm/src/types.ts @@ -104,7 +104,10 @@ type FieldIndexes = S extends {readonly shape: infer Shape} }[keyof Shape & string] : never; -/** Keys and indexes come from the options, else from the fields marked with `hashKey()`/`rangeKey()`/`index()`. */ +/** + * The types `model()` infers from schema `S` and options `O`. The item and the input come from the schema. Keys + * and indexes come from the options, else from the fields marked with `hashKey()`/`rangeKey()`/`index()`. + */ export type ModelTypesOf< S extends StandardSchema, O extends ModelOptions> @@ -124,6 +127,10 @@ export interface AnyModel { export type TypesOf = NonNullable; type KeyFields = {[P in K & keyof T]-?: Exclude}; +/** + * The item a read returns, as `ItemOf`: the schema's output type, as `z.infer` gives it. + * A write takes the schema's input type, which `.default()`, `.transform()` and `z.coerce` make differ. + */ export type ItemOf = TypesOf['item']; /** A key object, or a bare scalar for a model with only a hash key. */ From cef6adf236a37eaa52d8d8091da09958d4e16c04 Mon Sep 17 00:00:00 2001 From: DynoTable Date: Sun, 22 Nov 2026 20:38:40 +0100 Subject: [PATCH 2/4] feat: add CreateInputOf, the named type of what create takes --- README.md | 23 ++++++++++++++------ packages/dynotable-orm/src/index.ts | 1 + packages/dynotable-orm/src/model.ts | 3 ++- packages/dynotable-orm/src/types.ts | 7 ++++++ packages/dynotable-orm/tests/types.test-d.ts | 16 ++++++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d79acca..d1a8473 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,13 @@ model from the schema and its marks. The same schema validates each whole-item w the Zod adapter each update too. When you change a field, the types and the validation change together. ```ts -import {model, type ItemOf, type KeyInputOf, type UpdateInputOf} from 'dynotable-orm'; +import { + model, + type CreateInputOf, + type ItemOf, + type KeyInputOf, + type UpdateInputOf +} from 'dynotable-orm'; import {extendZod} from 'dynotable-orm/zod'; import {z} from 'zod'; @@ -109,8 +115,11 @@ export const Task = model( export type Task = ItemOf; // {projectId: string; taskId: string; status: 'open' | 'done'; points: number} export type TaskKey = KeyInputOf; // {projectId: string; taskId: string} export type TaskChanges = UpdateInputOf; // each path with its value type, plus $SET, $ADD, ... +export type NewTask = CreateInputOf; // status is optional here const key: TaskKey = {projectId: 'p1', taskId: 't1'}; +const draft: NewTask = {...key, points: 3}; +await Task.create(draft); const task: Task | undefined = await Task.get(key); console.log(task?.status); @@ -121,12 +130,12 @@ await Task.get({projectId: 'p1'}); await Task.update(key, {points: '3'}); ``` -| type | what it is | -| -------------------------------- | -------------------------------------------------------------------------------- | -| `ItemOf` | the item a read returns: the schema's output type, as `z.infer` gives it | -| `KeyInputOf` | the key `get`, `update` and `delete` take, from the hash key and range key marks | -| `UpdateInputOf` | what `update` takes: each path with its value type, and the update operators | -| `Parameters[0]` | what `create` takes: a `Partial` of the schema's input type | +| type | what it is | +| ------------------------- | -------------------------------------------------------------------------------- | +| `ItemOf` | the item a read returns: the schema's output type, as `z.infer` gives it | +| `KeyInputOf` | the key `get`, `update` and `delete` take, from the hash key and range key marks | +| `UpdateInputOf` | what `update` takes: each path with its value type, and the update operators | +| `CreateInputOf` | what `create` takes: a `Partial` of the schema's input type | A write takes the schema's input type and a read returns its output type, so a `.default()` field is optional on `create` and always present on a read. `timestamps: true` adds no field to the types: declare diff --git a/packages/dynotable-orm/src/index.ts b/packages/dynotable-orm/src/index.ts index 88f147e..99645fe 100644 --- a/packages/dynotable-orm/src/index.ts +++ b/packages/dynotable-orm/src/index.ts @@ -22,6 +22,7 @@ export type { CallOptions, ConditionFailureOptions, ConditionInput, + CreateInputOf, CreateOptions, DeleteOptions, GetOptions, diff --git a/packages/dynotable-orm/src/model.ts b/packages/dynotable-orm/src/model.ts index ed81c86..5a7dc0b 100644 --- a/packages/dynotable-orm/src/model.ts +++ b/packages/dynotable-orm/src/model.ts @@ -32,6 +32,7 @@ import type { CallOptions, ConditionFailureOptions, ConditionInput, + CreateInputOf, CreateOptions, DeleteOptions, GetOptions, @@ -46,7 +47,7 @@ import type { } from './types.js'; /** A write parses the item, so it takes the schema's input. */ -type WriteItem = Partial; +type WriteItem = CreateInputOf<{readonly '~types'?: T}>; type Attribute = keyof T['item'] & string; type Key = KeyInputOf<{readonly '~types'?: T}>; type KeyObject = Exclude, string | number | bigint | Uint8Array>; diff --git a/packages/dynotable-orm/src/types.ts b/packages/dynotable-orm/src/types.ts index 8b4ffa1..c79693b 100644 --- a/packages/dynotable-orm/src/types.ts +++ b/packages/dynotable-orm/src/types.ts @@ -141,6 +141,13 @@ export type KeyInputOf = ? KeyFields, TypesOf['hash']> | ItemOf[TypesOf['hash']] : KeyFields, TypesOf['hash'] | TypesOf['range']>; +/** + * What `create` takes, as `CreateInputOf`: a `Partial` of the schema's input, which it parses. A field + * with `.default()` is optional here and present on `ItemOf`. A missing required field compiles, and the runtime + * check rejects it. + */ +export type CreateInputOf = Partial['input']>; + /** What `update` takes: the schema's input, which it parses. */ export type UpdateInputOf = UpdateInput['input']>; diff --git a/packages/dynotable-orm/tests/types.test-d.ts b/packages/dynotable-orm/tests/types.test-d.ts index 78b4aec..4d6f75f 100644 --- a/packages/dynotable-orm/tests/types.test-d.ts +++ b/packages/dynotable-orm/tests/types.test-d.ts @@ -1,6 +1,7 @@ import {describe, expectTypeOf, it} from 'vitest'; import {z} from 'zod'; import type { + CreateInputOf, ItemOf, KeyInputOf, ModelOptions, @@ -36,6 +37,21 @@ describe('ItemOf', () => { }); }); +describe('CreateInputOf', () => { + // create parses its input, so a field whose input differs from its output takes the input type here. + it('is a Partial of the schema input, not of the item', () => { + const coerced = typed( + z.object({id: z.string(), n: z.string().transform(Number), on: z.boolean().default(false)}), + {hashKey: 'id'} + ); + expectTypeOf>().toEqualTypeOf<{ + id?: string; + n?: string; + on?: boolean | undefined; + }>(); + }); +}); + describe('KeyInputOf', () => { it('accepts a scalar only for a model with only a hash key', () => { expectTypeOf>().toEqualTypeOf<{id: string} | string>(); From 913e9e981b254e5da43eb78dae4477a89576c31e Mon Sep 17 00:00:00 2001 From: DynoTable Date: Sun, 22 Nov 2026 20:40:30 +0100 Subject: [PATCH 3/4] fix: keep the create parameter rendered as Partial of the input --- packages/dynotable-orm/src/model.ts | 3 +-- packages/dynotable-orm/tests/model.test-d.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/dynotable-orm/src/model.ts b/packages/dynotable-orm/src/model.ts index 5a7dc0b..ed81c86 100644 --- a/packages/dynotable-orm/src/model.ts +++ b/packages/dynotable-orm/src/model.ts @@ -32,7 +32,6 @@ import type { CallOptions, ConditionFailureOptions, ConditionInput, - CreateInputOf, CreateOptions, DeleteOptions, GetOptions, @@ -47,7 +46,7 @@ import type { } from './types.js'; /** A write parses the item, so it takes the schema's input. */ -type WriteItem = CreateInputOf<{readonly '~types'?: T}>; +type WriteItem = Partial; type Attribute = keyof T['item'] & string; type Key = KeyInputOf<{readonly '~types'?: T}>; type KeyObject = Exclude, string | number | bigint | Uint8Array>; diff --git a/packages/dynotable-orm/tests/model.test-d.ts b/packages/dynotable-orm/tests/model.test-d.ts index 47cb03f..b7d3a5f 100644 --- a/packages/dynotable-orm/tests/model.test-d.ts +++ b/packages/dynotable-orm/tests/model.test-d.ts @@ -1,7 +1,14 @@ import {describe, expectTypeOf, it} from 'vitest'; import {z} from 'zod'; import type {AttributeValue} from '@aws-sdk/client-dynamodb'; -import {type Condition, type ItemOf, type KeyInputOf, model, transaction} from '../src/index.js'; +import { + type Condition, + type CreateInputOf, + type ItemOf, + type KeyInputOf, + model, + transaction +} from '../src/index.js'; import {hashKey} from '../src/zod/index.js'; const schema = z.object({ @@ -30,6 +37,13 @@ describe('model types', () => { expectTypeOf>().toEqualTypeOf<{id: string; sk: number}>(); }); + // CreateInputOf is a separate alias of the create parameter type, so this pins the two together. + it('names what create takes', () => { + expectTypeOf>().toEqualTypeOf< + Parameters[0] + >(); + }); + it('narrows the item to the requested attributes', async () => { expectTypeOf(await Composite.get({id: 'a', sk: 1}, {attributes: ['id', 'n']})).toEqualTypeOf< Pick | undefined From 5065f5ed27d8b22700816c13d283244eb7cca533 Mon Sep 17 00:00:00 2001 From: DynoTable Date: Sun, 22 Nov 2026 21:03:28 +0100 Subject: [PATCH 4/4] docs: full TSDoc for KeyInputOf and UpdateInputOf, CreateInputOf on model() --- packages/dynotable-orm/src/model.ts | 3 ++- packages/dynotable-orm/src/types.ts | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/dynotable-orm/src/model.ts b/packages/dynotable-orm/src/model.ts index ed81c86..e783dfe 100644 --- a/packages/dynotable-orm/src/model.ts +++ b/packages/dynotable-orm/src/model.ts @@ -215,7 +215,8 @@ const projection = (project: IndexPlan['project']): Projection => /** * Defines a model. Every type of the model is inferred from `schema` and `options`, so you write no interface: - * the item (`ItemOf`), the key (`KeyInputOf`), the changes `update` takes (`UpdateInputOf`) and the index names. + * the item (`ItemOf`), the key (`KeyInputOf`), what `create` takes (`CreateInputOf`), the changes `update` takes + * (`UpdateInputOf`) and the index names. * Keys, indexes, dates and timestamps are resolved now, so a bad definition throws here. */ export function model< diff --git a/packages/dynotable-orm/src/types.ts b/packages/dynotable-orm/src/types.ts index c79693b..d1bdff6 100644 --- a/packages/dynotable-orm/src/types.ts +++ b/packages/dynotable-orm/src/types.ts @@ -133,7 +133,11 @@ type KeyFields = {[P in K & keyof T]-?: Exclude}; */ export type ItemOf = TypesOf['item']; -/** A key object, or a bare scalar for a model with only a hash key. */ +/** + * The key `get`, `update` and `delete` take, as `KeyInputOf`: an object with the hash key and the range + * key. A model with only a hash key also takes the bare value. The key attributes come from the field marks or + * from the `hashKey` and `rangeKey` options. + */ export type KeyInputOf = IsAny> extends true ? any @@ -148,7 +152,11 @@ export type KeyInputOf = */ export type CreateInputOf = Partial['input']>; -/** What `update` takes: the schema's input, which it parses. */ +/** + * The changes `update` takes, as `UpdateInputOf`. Each top-level field is an implicit `$SET` with its + * input type. `$SET`, `$REMOVE`, `$ADD` and `$DELETE` take paths and values typed by the schema. `$ADD` takes only + * a number, a Set or a list path. + */ export type UpdateInputOf = UpdateInput['input']>; export type IsAny = 0 extends 1 & T ? true : false;