batch
The method allows to perform batch operations. It’s available as an extension that you import separately:
import { batch } from "typesaurus";
interface Post { title: string; text: string;}
const db = schema(($) => ({ posts: $.collection<Post>(),}));
const $ = batch(db);
// Import posts from RSSrss.forEach(({ slug, title, text }) => // Set the post with the given slug $.posts.set(db.posts.id(slug), { title, text }),);
// Commit the operationawait $();
The method accepts the database instance as the argument and returns a batch wrapper.
API
All batch wrapper collections have altered methods available:
set
- sets a documentupdate
- updates a documentupset
- sets or updates a document if it existsremove
- removes a document
Unlike the regular methods, batch methods are synchronous and return void
:
const $ = batch(db);
// Set a document$.users.set(userId, ($) => ({ name: "Sasha", createdAt: $.serverDate(),}));
// Update a document$.posts.update(postId, { title: "Hello",});
// Set or update a document$.posts(postId).comments.upset(commentId, { text: "Hello",});
// Remove a document$.subscriptions.remove(subscriptionId);
await $();
$()
The returned batch wrapper also acts as a function that commits the batch operation:
const $ = batch(db);
// ...add operations
// Commit all write operationsawait $();
It returns Promise<void>
.