@softr/sdk 1.7.1 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @softr/sdk
2
2
 
3
+ ## 1.7.2
4
+
5
+ ### Patch Changes
6
+
7
+ - The `file-uploads` skill maps a record's attachments to `FileAttachment` before `initialAttachments`, giving a file without an id its url as id.
8
+ - The `file-uploads`, `record-mutations` and `record-queries` skills write a one-file attachment field as one value or `null`.
9
+ - The `file-uploads` skill sets the read, upload and write shapes of an attachment side by side.
10
+
3
11
  ## 1.7.1
4
12
 
5
13
  ### Patch Changes
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "1.7.1";
2
+ var version = "1.7.2";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: core
12
12
  library: softr-sdk
13
- library_version: 1.7.1
13
+ library_version: 1.7.2
14
14
  ---
15
15
 
16
16
  # Softr SDK core
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 1.7.1
16
+ library_version: 1.7.2
17
17
  ---
18
18
 
19
19
  **Boundary.** `ai` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineAiAction` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.7.1
13
+ library_version: 1.7.2
14
14
  ---
15
15
 
16
16
  **Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.7.1
13
+ library_version: 1.7.2
14
14
  ---
15
15
 
16
16
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.7.1
14
+ library_version: 1.7.2
15
15
  ---
16
16
 
17
17
  # The current user
@@ -12,7 +12,7 @@ description: >-
12
12
  metadata:
13
13
  type: sub-skill
14
14
  library: softr-sdk
15
- library_version: 1.7.1
15
+ library_version: 1.7.2
16
16
  ---
17
17
 
18
18
  **Boundary.** `email` and `EmailError` live on `@softr/sdk/server` and run only inside a `createServerFn` handler — importing them into a component fails the build. `defineEmailAction` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.7.1
10
+ library_version: 1.7.2
11
11
  ---
12
12
 
13
13
  **Boundary.** `upload` lives on `@softr/sdk/server` and runs inside server functions. `useFileUpload` and `FileItem` live on `@softr/sdk` and run in components. `FileAttachment` comes from `@softr/sdk` and can be imported anywhere.
@@ -47,8 +47,9 @@ function ApplicationForm() {
47
47
  <form onSubmit={async (e) => {
48
48
  e.preventDefault()
49
49
  if (isUploading) return
50
- const resume = files.filter(f => f.status === 'done') as FileAttachment[]
51
- await submitApplication({ data: { name: e.currentTarget.name.value, resume } })
50
+ const resume = files.filter(f => f.status === 'done')
51
+ const name = String(new FormData(e.currentTarget).get('name') ?? '')
52
+ await submitApplication({ data: { name, resume } })
52
53
  }}>
53
54
  <input name="name" />
54
55
  <input type="file" onChange={(e) => { const f = e.target.files?.[0]; if (f) upload(f) }} />
@@ -79,7 +80,23 @@ type FileAttachment = {
79
80
  }
80
81
  ```
81
82
 
82
- Pass `attachments` as the field value in `ds.create` / `ds.update`.
83
+ ## Attachment shapes: read, upload and write
84
+
85
+ An attachment value has three shapes, and none of them is the other:
86
+
87
+ | Where the value comes from | Its shape |
88
+ | --- | --- |
89
+ | A record you read, `record.fields.photo` | `{ id, filename, type, url, expiresAt }` plus optional `size` and `thumbnails`. `id`, `filename` and `type` can each be `null` |
90
+ | An upload: a `done` item of `useFileUpload`, or what `upload` returns | `FileAttachment`: `url`, `filename` and `id` are strings |
91
+ | A write, the `data` of `ds.create` / `ds.update` | `{ url }`, optionally with `filename`, `id` and `type` |
92
+
93
+ Whether the field holds one file or several decides the rest. `docs/datasources/` types a one-file field `ATTACHMENT` and a several-file field `ATTACHMENT[]`.
94
+
95
+ - A one-file field reads one attachment or `null`, never an array. Its write takes one object, or `null` to clear it. Never index it with `[0]`, and never wrap it in an array.
96
+ - A several-file field reads an array, and its write takes an array.
97
+ - A read attachment and a `FileAttachment` are both valid write values as they are. Copying a file from one record to another needs no mapping.
98
+ - A function or schema of your own that takes attachments types `filename`, `id` and `type` as `string | null`, the way a record reads them; in zod that is `.nullish()`, not `.optional()`. A parameter typed `filename: string` refuses every attachment a record reads, and a `type` that is only optional refuses an uploaded `FileAttachment`.
99
+ - `initialAttachments` takes `FileAttachment[]`, which a read attachment is not. Map it first, as the edit form below does.
83
100
 
84
101
  ## FileItem shape
85
102
 
@@ -88,13 +105,13 @@ Pass `attachments` as the field value in `ds.create` / `ds.update`.
88
105
  ```ts
89
106
  type FileItem =
90
107
  | { status: 'uploading'; id: string; filename: string; progress: number; cancel: () => void }
91
- | { status: 'done'; id: string; filename: string; url: string } // FileAttachment + status
108
+ | (FileAttachment & { status: 'done' })
92
109
  | { status: 'error'; id: string; filename: string; error: Error }
93
110
  ```
94
111
 
95
112
  Items appear in insertion order and transition in place — a completed upload stays at its original position rather than moving to a separate list.
96
113
 
97
- For form submission, filter to `done` items: `files.filter(f => f.status === 'done') as FileAttachment[]`.
114
+ For form submission, filter to `done` items: `files.filter(f => f.status === 'done')` is a `FileAttachment[]` with no cast. A one-file field sends the first of them, `done[0] ?? null`, not the array.
98
115
 
99
116
  ## Blocking form submission
100
117
 
@@ -102,14 +119,58 @@ For form submission, filter to `done` items: `files.filter(f => f.status === 'do
102
119
 
103
120
  ## Edit forms: pre-loading existing attachments
104
121
 
105
- ```ts
106
- const { files, isUploading, upload, remove } = useFileUpload(
107
- updateApplication,
108
- { initialAttachments: record.fields.resume ?? [] },
109
- )
122
+ A record's attachments can have a `null` id or filename, and `initialAttachments` wants strings, so map each one to a `FileAttachment` first. The upload control tells its files apart by id, so a file without one takes its url as id: two files sharing `''` would be removed together. `ds` runs only on the server, so the page loads and saves the record through server functions. Here `resume` holds several files and `photo` holds one:
123
+
124
+ ```tsx
125
+ const loadApplication = createServerFn({ method: 'GET' })
126
+ .validator((id: string) => id)
127
+ .handler(async ({ data: id }) => {
128
+ const record = await ds.get(applicationDetail, { id })
129
+ if (!record) throw notFound()
130
+ return record
131
+ })
132
+
133
+ const saveApplication = createServerFn({ method: 'POST' })
134
+ .validator((data: { id: string; resume: FileAttachment[]; photo: FileAttachment | null }) => data)
135
+ .handler(async ({ data }) => {
136
+ await ds.update(updateApplication, { id: data.id, data: { resume: data.resume, photo: data.photo } })
137
+ })
138
+
139
+ export const Route = createFileRoute('/_app/applications/$id')({
140
+ loader: ({ params }) => loadApplication({ data: params.id }),
141
+ component: EditApplication,
142
+ })
143
+
144
+ type ReadFile = { url: string; filename: string | null; id: string | null }
145
+
146
+ function filesOf(files: ReadFile[] | null): FileAttachment[] {
147
+ return (files ?? []).map((file) => ({ url: file.url, filename: file.filename ?? '', id: file.id ?? file.url }))
148
+ }
149
+
150
+ function EditApplication() {
151
+ const record = Route.useLoaderData()
152
+ const resume = useFileUpload(updateApplication, { initialAttachments: filesOf(record.fields.resume) })
153
+ const photo = useFileUpload(updateApplication, {
154
+ initialAttachments: filesOf(record.fields.photo === null ? null : [record.fields.photo]),
155
+ })
156
+
157
+ // the several-file field sends every done file, the one-file field its first or null
158
+ async function save() {
159
+ const photoFiles = photo.files.filter((f) => f.status === 'done')
160
+ const resumeFiles = resume.files.filter((f) => f.status === 'done')
161
+ await saveApplication({ data: { id: record.id, resume: resumeFiles, photo: photoFiles[0] ?? null } })
162
+ }
163
+
164
+ // render resume.files and photo.files as in the core pattern above
165
+ return (
166
+ <button type="button" disabled={resume.isUploading || photo.isUploading} onClick={() => void save()}>
167
+ Save
168
+ </button>
169
+ )
170
+ }
110
171
  ```
111
172
 
112
- Pre-loaded attachments appear as `status: 'done'` items at the start of `files`. The existing attachments already have the `FileAttachment` shape from the datasource record.
173
+ Pre-loaded attachments appear as `status: 'done'` items at the start of `files`.
113
174
 
114
175
  ## Server-side uploads and plain forms
115
176
 
@@ -123,7 +184,7 @@ const submitResume = createServerFn({ method: 'POST' })
123
184
  .handler(async ({ data }) => {
124
185
  const uploaded = await upload(createApplication, data.get('cv') as File)
125
186
  await ds.create(createApplication, {
126
- data: { fullName: data.get('fullName') as string, cv: [uploaded] },
187
+ data: { fullName: data.get('fullName') as string, cv: [uploaded] }, // cv holds several files; a one-file field takes `uploaded` itself
127
188
  })
128
189
  return new Response(null, { status: 303, headers: { Location: '/thank-you' } })
129
190
  })
@@ -145,6 +206,12 @@ function ApplyForm() {
145
206
 
146
207
  **Not passing `initialAttachments` on an edit form.** The hook starts with an empty list. Without pre-loading, the first save erases existing attachments.
147
208
 
209
+ **Passing a record's attachments straight to `initialAttachments`.** Their `id` and `filename` can be `null`, and `FileAttachment` wants strings, so type-checking refuses them. Map them with `filesOf` as in the edit form above.
210
+
211
+ **Filling every missing attachment id with `''`.** Files that share an id are one file to the upload control: removing one removes them all, and the save drops them.
212
+
213
+ **Treating a one-file field as an array.** A one-file field reads one attachment or `null` and writes one object or `null`. Indexing it with `[0]`, or writing `[file]` or the whole `done` list to it, fails type-checking.
214
+
148
215
  ## References
149
216
 
150
217
  - [record-mutations/SKILL.md](../record-mutations/SKILL.md): creating and updating records with the collected attachments
@@ -8,7 +8,7 @@ description: >-
8
8
  metadata:
9
9
  type: sub-skill
10
10
  library: softr-sdk
11
- library_version: 1.7.1
11
+ library_version: 1.7.2
12
12
  ---
13
13
 
14
14
  # Dates, times and numbers
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.7.1
10
+ library_version: 1.7.2
11
11
  ---
12
12
 
13
13
  **Boundary.** `integration` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineIntegration` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.1
12
+ library_version: 1.7.2
13
13
  ---
14
14
 
15
15
  **Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.1
12
+ library_version: 1.7.2
13
13
  ---
14
14
 
15
15
  # Push notifications
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.1
12
+ library_version: 1.7.2
13
13
  ---
14
14
 
15
15
  # Installing the app
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.7.1
13
+ library_version: 1.7.2
14
14
  ---
15
15
 
16
16
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -208,7 +208,7 @@ Each key of `data` accepts exactly what its field takes (see [record-queries](..
208
208
 
209
209
  - SELECT and LINKED_RECORD take `{ id }`. Not a bare string, and not the `{ id, label }` a read returns; the label is display data and a write does not accept it. For a form select that should follow the field's choices in Studio, read them with `ds.choices` (see [record-queries](../record-queries/SKILL.md)).
210
210
  - CHECKBOX takes a boolean. Dates take ISO strings, never `Date` instances.
211
- - ATTACHMENT takes `{ url }`, optionally with `filename`, `id` and `type`.
211
+ - ATTACHMENT takes `{ url }`, optionally with `filename`, `id` and `type`: one object, or `null`, for a one-file field. A record's attachment and an uploaded file are both written as they are. [file-uploads](../file-uploads/SKILL.md#attachment-shapes-read-upload-and-write) sets the read, upload and write shapes side by side.
212
212
  - A multi-entry field takes an array of the value.
213
213
 
214
214
  ## References
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.7.1
14
+ library_version: 1.7.2
15
15
  ---
16
16
 
17
17
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. The same holds for any helper that calls `ds`: declare it inside the handler. A function at module level that calls `ds` keeps the server-only import in the browser bundle, and every page stops loading until it moves. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -60,6 +60,8 @@ Every scalar above is `| null` on the record unless the field allows multiple en
60
60
 
61
61
  **USER** can lack a name or an email: show `owner?.name ?? owner?.email ?? '—'`, never assume either is there.
62
62
 
63
+ **ATTACHMENT** can have a `null` `id`, `filename` or `type`, so show the name as `file.filename ?? 'Untitled file'`, and type a function of your own that takes attachments the same way. A one-file field is one attachment or `null`, never an array; only an `ATTACHMENT[]` field is an array. An upload's `FileAttachment` is a different shape; see [file-uploads](../../file-uploads/SKILL.md#attachment-shapes-read-upload-and-write).
64
+
63
65
  **DURATION** is milliseconds, whatever format the field displays in: `3600000` is one hour.
64
66
 
65
67
  **DATETIME at date precision** is generated as a `DATE` field and holds `'2026-03-14'`, not a timestamp.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.1
12
+ library_version: 1.7.2
13
13
  ---
14
14
 
15
15
  **Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.1
12
+ library_version: 1.7.2
13
13
  ---
14
14
 
15
15
  **Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.1
12
+ library_version: 1.7.2
13
13
  ---
14
14
 
15
15
  **Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere.