Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a document with a provided identifier #263

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/automerge-repo/src/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ export class Repo extends EventEmitter<RepoEvents> {

/**
* Creates a new document and returns a handle to it. The initial value of the document is
* an empty object `{}`. Its documentId is generated by the system. we emit a `document` event
* to advertise interest in the document.
* an empty object `{}`. Its documentId is generated by the system if not procided.
* We emit a `document` event to advertise interest in the document.
*/
create<T>(): DocHandle<T> {
create<T>(opts?: { documentId?: AnyDocumentId }): DocHandle<T> {
// TODO:
// either
// - pass an initial value and do something like this to ensure that you get a valid initial value
Expand All @@ -374,8 +374,15 @@ export class Repo extends EventEmitter<RepoEvents> {
// or
// - pass a "reify" function that takes a `<any>` and returns `<T>`

// Generate a new UUID and store it in the buffer
const { documentId } = parseAutomergeUrl(generateAutomergeUrl())
let documentId: DocumentId | undefined = opts?.documentId
? interpretAsDocumentId(opts.documentId)
: undefined;

if (!documentId) {
// If no identifier is provided, generate a new UUID and store it in the buffer
({ documentId } = parseAutomergeUrl(generateAutomergeUrl()))
}

const handle = this.#getHandle<T>(documentId, true) as DocHandle<T>
this.emit("document", { handle, isNew: true })
return handle
Expand Down
1 change: 1 addition & 0 deletions packages/automerge-repo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
isValidAutomergeUrl,
parseAutomergeUrl,
stringifyAutomergeUrl,
interpretAsDocumentId,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposed for applications to do the same as this piece of code in the tests to be able to fetch such a document.

} from "./AutomergeUrl.js"
export { Repo } from "./Repo.js"
export { NetworkAdapter } from "./network/NetworkAdapter.js"
Expand Down
19 changes: 17 additions & 2 deletions packages/automerge-repo/test/Repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from "assert"
import * as Uuid from "uuid"
import { describe, expect, it } from "vitest"
import { READY } from "../src/DocHandle.js"
import { parseAutomergeUrl } from "../src/AutomergeUrl.js"
import { binaryToDocumentId, interpretAsDocumentId, parseAutomergeUrl } from "../src/AutomergeUrl.js"
import {
generateAutomergeUrl,
stringifyAutomergeUrl,
Expand All @@ -14,7 +14,7 @@ import { eventPromise } from "../src/helpers/eventPromise.js"
import { pause } from "../src/helpers/pause.js"
import {
AnyDocumentId,
AutomergeUrl,
AutomergeUrl, BinaryDocumentId,
DocHandle,
DocumentId,
LegacyDocumentId,
Expand Down Expand Up @@ -67,6 +67,21 @@ describe("Repo", () => {
assert.equal(handle.isReady(), true)
})

it("can create a document with a custom documentId", () => {
const predictableId = interpretAsDocumentId(Uuid.v5(
"MyIdentifier1234",
"2bbbfa6a-a640-11ee-8228-62cbdfde7c1d",
new Uint8Array(16)
) as BinaryDocumentId);

const { repo } = setup()
const handle = repo.create({
documentId: predictableId,
});

assert.equal(handle.documentId, predictableId)
});

it("can find a document by url", () => {
const { repo } = setup()
const handle = repo.create<TestDoc>()
Expand Down