Skip to content

Commit 5ef8078

Browse files
authored
feat(offline-storage): Batch operations and Blobs
1 parent c35fa16 commit 5ef8078

1 file changed

Lines changed: 19 additions & 47 deletions

File tree

‎src/pages/enterprise/offline-storage.md‎

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -226,53 +226,30 @@ If the property doesn’t exist in the document it will return the default value
226226

227227
### Batch Operations
228228

229-
Grouping together multiple changes to the database at once.
229+
If you need to make multiple changes to a database at once, it’s faster to group them together.
230230

231-
*Not yet supported.* Please [reach out](https://ionicframework.com/enterprise/contact) if you need this feature.
232-
<!--
233-
<h3 id="batch-operations"><a class="anchor" href="#batch-operations"></a>Batch operations</h3>
234-
<div class="paragraph">
235-
<p>If you’re making multiple changes to a database at once, it’s faster to group them together.
236-
The following example persists a few documents in batch. </p>
237-
</div>
238-
<div class="listingblock">
239-
<div class="content">
240-
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"><span class="hljs-keyword">try</span> {
241-
database.inBatch(<span class="hljs-keyword">new</span> Runnable() {
242-
<span class="hljs-meta">@Override</span>
243-
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
244-
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
245-
let doc = <span class="hljs-keyword">new</span> MutableDocument();
246-
doc.setValue(<span class="hljs-string">"type"</span>, <span class="hljs-string">"user"</span>);
247-
doc.setValue(<span class="hljs-string">"name"</span>, String.format(<span class="hljs-string">"user %d"</span>, i));
248-
doc.setBoolean(<span class="hljs-string">"admin"</span>, <span class="hljs-keyword">false</span>);
249-
<span class="hljs-keyword">try</span> {
250-
database.save(doc);
251-
} <span class="hljs-keyword">catch</span> (e) {
252-
console.log(e.toString());
253-
}
254-
console.log(String.format(<span class="hljs-string">"saved user document %s"</span>, doc.getString(<span class="hljs-string">"name"</span>)));
255-
}
256-
}
257-
});
258-
}
259-
<div class="paragraph">
260-
<p>At the <strong>local</strong> level this operation is still transactional: no other <code>Database</code> instances, including ones managed by the replicator can make changes during the execution of the block, and other instances will not see partial changes.
261-
But Couchbase Mobile is a distributed system, and due to the way replication works, there’s no guarantee that Sync Gateway or other devices will receive your changes all at once.</p>
262-
</div>
263-
-->
231+
The following example persists a few documents in batch:
264232

265-
## Blobs
233+
```typescript
234+
await this.database.inBatch(() => {
235+
for (let sdk of sdkDataToLoad) {
236+
let doc = new MutableDocument()
237+
.setNumber('version', sdk.version)
238+
.setString('type', sdk.type)
239+
.setString('company', sdk.company);
240+
241+
this.database.save(doc);
242+
}
243+
});
244+
```
266245

267-
Storing large data, such as images.
246+
At the <strong>local</strong> level this operation is still transactional: no other <code>Database</code> instances, including ones managed by the replicator can make changes during the execution of the block, and other instances will not see partial changes. But Couchbase Mobile is a distributed system, and due to the way replication works, there’s no guarantee that Sync Gateway or other devices will receive your changes all at once.
268247

269-
*Not yet supported.* Please [reach out](https://ionicframework.com/enterprise/contact) if you need this feature.
248+
## Blobs
270249

271-
<!--
272-
We’ve renamed "attachments" to "blobs".
273-
The new behavior should be clearer too: a <code>Blob</code> is now a normal object that can appear in a document as a property value.
274-
In other words, you just instantiate a <code>Blob</code> and set it as the value of a property, and then later you can get the property value, which will be a <code>Blob</code> object.
275-
The following code example adds a blob to the document under the <code>avatar</code> property.</p>
250+
Used to store large data, such as images.
251+
252+
The following code example adds a blob to the document under the <code>avatar</code> property.
276253

277254
```typescript
278255
let is = getAsset("avatar.jpg");
@@ -302,15 +279,10 @@ That key can be used to retrieve the <code>Blob</code> object at a later time.
302279

303280
When a document is synchronized, the Couchbase Lite replicator will add an <code>_attachments</code> dictionary to the document’s properties if it contains a blob.
304281
A random access name will be generated for each <code>Blob</code> which is different to the "avatar" key that was used in the example above.
305-
On the image below, the document now contains the <code>_attachments</code> dictionary when viewed in the Couchbase Server Admin Console.
306282

307283
A blob also has properties such as <code>"digest"</code> (a SHA-1 digest of the data), <code>"length"</code> (the length in bytes), and optionally <code>"content_type"</code> (the MIME type).
308284
The data is not stored in the document, but in a separate content-addressable store, indexed by the digest.
309285

310-
This <code>Blob</code> can be retrieved on the Sync Gateway REST API at <a href="http://localhost:4984/justdoit/user.david/blob_1" class="bare">http://localhost:4984/justdoit/user.david/blob_1</a>.
311-
Notice that the blob identifier in the URL path is "blob_1" (not "avatar").
312-
-->
313-
314286
## Query
315287

316288
Database queries are based on expressions, of the form "return __ from documents where __, ordered by __", with semantics based on Couchbase’s N1QL query language.

0 commit comments

Comments
 (0)