@sanity/client 3.2.1 → 4.0.0-alpha.esm.1

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/README.md CHANGED
@@ -19,8 +19,8 @@ npm install -g @sanity/client
19
19
  ## API
20
20
 
21
21
  ```js
22
- const sanityClient = require('@sanity/client')
23
- const client = sanityClient({
22
+ import {createClient} from '@sanity/client'
23
+ const client = createClient({
24
24
  projectId: 'your-project-id',
25
25
  dataset: 'bikeshop',
26
26
  apiVersion: '2021-03-25', // use current UTC date - see "specifying API version"!
@@ -29,7 +29,7 @@ const client = sanityClient({
29
29
  })
30
30
  ```
31
31
 
32
- `const client = sanityClient(options)`
32
+ `const client = createClient(options)`
33
33
 
34
34
  Initializes a new Sanity Client. Required options are `projectId`, `dataset`, and `apiVersion`. Setting a value for `useCdn` is encouraged.
35
35
 
@@ -131,11 +131,12 @@ client.create(doc).then((res) => {
131
131
  })
132
132
  ```
133
133
 
134
- `client.create(doc)`
134
+ `client.create(doc)`
135
+ `client.create(doc, mutationOptions)`
135
136
 
136
137
  Create a document. Argument is a plain JS object representing the document. It must contain a `_type` attribute. It _may_ contain an `_id`. If an ID is not specified, it will automatically be created.
137
138
 
138
- To create a draft document, add an `_id` attribute set to `'drafts.'`.
139
+ To create a draft document, prefix the document ID with `drafts.` - eg `_id: 'drafts.myDocumentId'`. To auto-generate a draft document ID, set `_id` to `drafts.` (nothing after the `.`).
139
140
 
140
141
  ### Creating/replacing documents
141
142
 
@@ -152,7 +153,8 @@ client.createOrReplace(doc).then((res) => {
152
153
  })
153
154
  ```
154
155
 
155
- `client.createOrReplace(doc)`
156
+ `client.createOrReplace(doc)`
157
+ `client.createOrReplace(doc, mutationOptions)`
156
158
 
157
159
  If you are not sure whether or not a document exists but want to overwrite it if it does, you can use the `createOrReplace()` method. When using this method, the document must contain an `_id` attribute.
158
160
 
@@ -171,7 +173,8 @@ client.createIfNotExists(doc).then((res) => {
171
173
  })
172
174
  ```
173
175
 
174
- `client.createIfNotExists(doc)`
176
+ `client.createIfNotExists(doc)`
177
+ `client.createIfNotExists(doc, mutationOptions)`
175
178
 
176
179
  If you want to create a document if it does not already exist, but fall back without error if it does, you can use the `createIfNotExists()` method. When using this method, the document must contain an `_id` attribute.
177
180
 
@@ -194,6 +197,12 @@ client
194
197
 
195
198
  Modify a document. `patch` takes a document ID. `set` merges the partialDoc with the stored document. `inc` increments the given field with the given numeric value. `commit` executes the given `patch`. Returns the updated object.
196
199
 
200
+ ```
201
+ client.patch()
202
+ [operations]
203
+ .commit(mutationOptions)
204
+ ```
205
+
197
206
  ### Setting a field only if not already present
198
207
 
199
208
  ```js
@@ -233,19 +242,17 @@ client
233
242
  The patch operation `insert` takes a location (`before`, `after` or `replace`), a path selector and an array of elements to insert.
234
243
 
235
244
  ```js
236
- const {nanoid} = require('nanoid')
237
-
238
245
  client
239
246
  .patch('bike-123')
240
247
  // Ensure that the `reviews` arrays exists before attempting to add items to it
241
248
  .setIfMissing({reviews: []})
242
249
  // Add the items after the last item in the array (append)
243
- .insert('after', 'reviews[-1]', [
244
- // Add a `_key` unique within the array to ensure it can be addressed uniquely
245
- // in a real-time collaboration context
246
- {_key: nanoid(), title: 'Great bike!', stars: 5},
247
- ])
248
- .commit()
250
+ .insert('after', 'reviews[-1]', [{title: 'Great bike!', stars: 5}])
251
+ .commit({
252
+ // Adds a `_key` attribute to array items, unique within the array, to
253
+ // ensure it can be addressed uniquely in a real-time collaboration context
254
+ autoGenerateArrayKeys: true,
255
+ })
249
256
  ```
250
257
 
251
258
  ### Appending/prepending elements to an array
@@ -253,7 +260,7 @@ client
253
260
  The operations of appending and prepending to an array are so common that they have been given their own methods for better readability:
254
261
 
255
262
  ```js
256
- const {nanoid} = require('nanoid')
263
+ import {nanoid} from 'nanoid'
257
264
 
258
265
  client
259
266
  .patch('bike-123')
@@ -277,7 +284,8 @@ client.patch('bike-123').unset(reviewsToRemove).commit()
277
284
 
278
285
  A single document can be deleted by specifying a document ID:
279
286
 
280
- `client.delete(docId)`
287
+ `client.delete(docId)`
288
+ `client.delete(docId, mutationOptions)`
281
289
 
282
290
  ```js
283
291
  client
@@ -296,7 +304,6 @@ One or more documents can be deleted by specifying a GROQ query (and optionally,
296
304
 
297
305
  ```js
298
306
  // Without params
299
-
300
307
  client
301
308
  .delete({query: '*[_type == "bike"][0]'})
302
309
  .then(() => {
@@ -309,7 +316,6 @@ client
309
316
 
310
317
  ```js
311
318
  // With params
312
-
313
319
  client
314
320
  .delete({query: '*[_type == $type][0..1]', params: {type: 'bike'}})
315
321
  .then(() => {
@@ -366,27 +372,27 @@ A `patch` can be performed inline on a `transaction`.
366
372
  Transactions and patches can also be built outside the scope of a client:
367
373
 
368
374
  ```js
369
- const sanityClient = require('@sanity/client')
370
- const client = sanityClient({
375
+ import {SanityClient} from '@sanity/client'
376
+ const client = new SanityClient({
371
377
  projectId: 'your-project-id',
372
378
  dataset: 'bikeshop',
373
379
  })
374
380
 
375
381
  // Patches:
376
- const patch = new sanityClient.Patch('<documentId>')
382
+ const patch = new SanityClient.Patch('<documentId>')
377
383
  client.mutate(patch.inc({count: 1}).unset(['visits']))
378
384
 
379
385
  // Transactions:
380
- const transaction = new sanityClient.Transaction()
386
+ const transaction = new SanityClient.Transaction()
381
387
  .create({_id: '123', name: 'FooBike'})
382
388
  .delete('someDocId')
383
389
 
384
390
  client.mutate(transaction)
385
391
  ```
386
392
 
387
- `const patch = new sanityClient.Patch(docId)`
393
+ `const patch = new SanityClient.Patch(docId)`
388
394
 
389
- `const transaction = new sanityClient.Transaction()`
395
+ `const transaction = new SanityClient.Transaction()`
390
396
 
391
397
  An important note on this approach is that you cannot call `commit()` on transactions or patches instantiated this way, instead you have to pass them to `client.mutate()`
392
398
 
@@ -484,7 +490,7 @@ client.assets
484
490
  Deleting an asset document will also trigger deletion of the actual asset.
485
491
 
486
492
  ```
487
- client.delete(id: string): Promise
493
+ client.delete(assetDocumentId: string): Promise
488
494
  ```
489
495
 
490
496
  ```js
@@ -493,6 +499,17 @@ client.delete('image-abc123_someAssetId-500x500-png').then((result) => {
493
499
  })
494
500
  ```
495
501
 
502
+ ### Mutation options
503
+
504
+ The following options are available for mutations, and can be applied either as the second argument to `create()`, `createOrReplace`, `createIfNotExist`, `delete()` and `mutate()` - or as an argument to the `commit()` method on patches and transactions.
505
+
506
+ - `visibility` (`'sync'|'async'|'deferred'`) - default `'sync'`
507
+ - `sync`: request will not return until the requested changes are visible to subsequent queries.
508
+ - `async`: request will return immediately when the changes have been committed, but it might still be a second or more until changes are reflected in a query. Unless you are immediately re-querying for something that includes the mutated data, this is the preferred choice.
509
+ - `deferred`: fastest way to write - bypasses real-time indexing completely, and should be used in cases where you are bulk importing/mutating a large number of documents and don't need to see that data in a query for tens of seconds.
510
+ - `dryRun` (`true|false`) - default `false`. If true, the mutation will be a dry run - the response will be identical to the one returned had this property been omitted or false (including error responses) but no documents will be affected.
511
+ - `autoGenerateArrayKeys` (`true|false`) - default `false`. If true, the mutation API will automatically add `_key` attributes to objects in arrays that is missing them. This makes array operations more robust by having a unique key within the array available for selections, which helps prevent race conditions in real-time, collaborative editing.
512
+
496
513
  ### Get client configuration
497
514
 
498
515
  ```js