Skip to content

Commit

Permalink
feat(automerge-repo): add export method
Browse files Browse the repository at this point in the history
  • Loading branch information
kid-icarus committed Dec 12, 2023
1 parent 2d6e23b commit f29a510
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/automerge-repo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ A `Repo` exposes these methods:
networks.
- `delete(docId: DocumentId)`
Deletes the local copy of a document from the local cache and local storage. _This does not currently delete the document from any other peers_.
- `export(docId: DocumentId)`
Exports the document. Returns a `Uint8Array` that can be saved to disk. See the [Automerge binary format spec](https://automerge.org/automerge-binary-format-spec/) for more details.
- `.on("document", ({handle: DocHandle}) => void)`
Registers a callback to be fired each time a new document is loaded or created.
- `.on("delete-document", ({handle: DocHandle}) => void)`
Expand Down
15 changes: 15 additions & 0 deletions packages/automerge-repo/src/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,21 @@ export class Repo extends EventEmitter<RepoEvents> {
this.emit("delete-document", { documentId })
}

/**
* Exports a document to a binary format.
* @param id - The url or documentId of the handle to export
*/
async export(
id: AnyDocumentId
): Promise<Uint8Array | undefined> {
const documentId = interpretAsDocumentId(id)

const handle = this.#getHandle(documentId, false)
const doc = await handle.doc()
if (!doc) return undefined
return Automerge.save(doc)
}

subscribeToRemotes = (remotes: StorageId[]) => {
this.#log("subscribeToRemotes", { remotes })
this.#remoteHeadsSubscriptions.subscribeToRemotes(remotes)
Expand Down
22 changes: 22 additions & 0 deletions packages/automerge-repo/test/Repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Repo } from "../src/Repo.js"
import { eventPromise } from "../src/helpers/eventPromise.js"
import { pause } from "../src/helpers/pause.js"
import {
AnyDocumentId,
AutomergeUrl,
DocHandle,
DocumentId,
Expand Down Expand Up @@ -321,6 +322,27 @@ describe("Repo", () => {
repo.delete(handle.documentId)
}))

it("exports a document", async () => {
const { repo } = setup()
const handle = repo.create<TestDoc>()
handle.change(d => {
d.foo = "bar"
})
assert.equal(handle.isReady(), true)

const exported = await repo.export(handle.documentId)
const loaded = A.load(exported)
const doc = await handle.doc()
assert.deepEqual(doc, loaded)
})

it("rejects when exporting a document that does not exist", async () => {
const { repo } = setup()
assert.rejects(async () => {
await repo.export("foo" as AnyDocumentId)
})
})

it("storage state doesn't change across reloads when the document hasn't changed", async () => {
const storage = new DummyStorageAdapter()

Expand Down

0 comments on commit f29a510

Please sign in to comment.