Skip to content

Commit ce7e31c

Browse files
committed
docs(cbjs): introduce the .values() method
1 parent 4323e32 commit ce7e31c

File tree

1 file changed

+49
-46
lines changed
  • docs/src/guide/services

1 file changed

+49
-46
lines changed

docs/src/guide/services/kv.md

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,63 @@ If you have opt-in for the [cluster types](/guide/cluster-types) on the targeted
9999

100100
Cbjs introduce the ability to chain sub-doc operations. Using this syntax also enables path autocompletion :
101101

102-
```ts
103-
import { LookupInSpec } from '@cbjsdev/cbjs';
102+
```ts twoslash
103+
import { connect, DocDef } from '@cbjsdev/cbjs';
104+
105+
type MyClusterTypes = {
106+
store: {
107+
library: {
108+
books: [
109+
DocDef<`author::${string}`, { firstname: string; lastname: string }>,
110+
DocDef<`book::${string}`, { title: string; authors: string[]; metadata: { tags: string[] }; }>,
111+
]
112+
};
113+
};
114+
};
115+
116+
const cluster = await connect<MyClusterTypes>('...');
117+
const collection = cluster.bucket('store').scope('library').collection('books');
104118

105-
const lookups = collection
106-
.lookupIn('docKey')
119+
// ---cut-before---
120+
const { content: [
121+
{ value: title },
122+
{ value: authors },
123+
]} = await collection
124+
.lookupIn('book::001')
107125
.get('title')
126+
.get('authors')
127+
```
108128

109-
if (withTags) {
110-
lookups.get('metadata.tags');
111-
}
129+
If you are only interested in the values of the lookup, you can use the `.values()` method :
130+
131+
```ts twoslash
132+
import { connect, DocDef } from '@cbjsdev/cbjs';
133+
134+
type MyClusterTypes = {
135+
store: {
136+
library: {
137+
books: [
138+
DocDef<`author::${string}`, { firstname: string; lastname: string }>,
139+
DocDef<`book::${string}`, { title: string; authors: string[] }>,
140+
]
141+
};
142+
};
143+
};
144+
145+
const cluster = await connect<MyClusterTypes>('...');
146+
const collection = cluster.bucket('store').scope('library').collection('books');
112147

113-
return await lookups;
148+
// ---cut-before---
149+
const [title, authors] = await collection
150+
.lookupIn('book::001')
151+
.get('title')
152+
.get('authors')
153+
.values()
114154
```
115155

116156
> [!IMPORTANT]
117157
> When using the classic `lookupIn` syntax, the request is executed immediately, regardless of it being awaited or not.
118-
> A chained lookupIn will only perform the request once awaited `await lookups` or `.then()` is called on it.
158+
> A chained lookupIn will only perform the request once awaited `await lookups`, `execute()` or `.then()` is called on it.
119159
120160
### Error handling
121161

@@ -257,43 +297,6 @@ return await mutations;
257297
> When using the classic `mutateIn` syntax, the request is executed immediately, regardless of it being awaited or not.
258298
> A chained mutateIn will only perform the request once awaited `await mutations` or `.then()` is called on it.
259299
260-
Cbjs also introduce two new methods `insertIntoRecord` and `removeFromRecord` :
261-
262-
```ts twoslash
263-
import { connect, DocDef } from '@cbjsdev/cbjs';
264-
265-
type MyClusterTypes = {
266-
store: {
267-
library: {
268-
books: [
269-
DocDef<`book::${string}`, { title: string; editions: Record<string, { year: number; sales?: number }> }>,
270-
]
271-
};
272-
};
273-
};
274-
275-
const cluster = await connect<MyClusterTypes>('...');
276-
const collection = cluster.bucket('store').scope('library').collection('books');
277-
278-
// @errors: 2339 2345
279-
// ---cut-before---
280-
// Let's say we have defined a `Book` document as follow :
281-
type Book = {
282-
title: string;
283-
editions: Record<string, { year: number; sales?: number }>
284-
}
285-
286-
const result = await collection
287-
.mutateIn('book::001')
288-
// No error, yet it should show an error, because `year` is not an optional property
289-
.insert('editions.001.year', 2000)
290-
291-
// Show an error, as we would expect
292-
.insertIntoRecord('editions.edition::001', 'year', 2000)
293-
.removeFromRecord('editions.edition::000', 'year')
294-
;
295-
```
296-
297300
### Error handling
298301

299302
The whole `mutateIn` operation will fail if any of the mutations fail. There is no partial mutation.

0 commit comments

Comments
 (0)