Managing Documents

Public Data

Remember, all data stored on Context is public. Avoid storing any secrets or private information without additional measures to protect confidentiality.

Fetch Documents

Fetch a specific document or template or asset, from any domain:

// Fetch a specific document
const document = await ctx.document("context.startup/link3");  // "domain/path/to/file"

Document Properties

Access and display properties of a document:

console.log(document.data.path);
console.log(document.data.versionNumber);
console.log(document.data.data);
console.log(document.data.metadata);
console.log(document.data.templates);
console.log(document.data.type); // Document | Template | Asset
console.log(document.data.txId);
console.log(document.data.createdAt);
console.log(document.data.updatedAt);
console.log(JSON.stringify(document.data));

List Document Versions

Fetch a list of all versions of a document:

const documentVersions = await document.data.versions();

Fetch a Specific Document Version

You can fetch a specific version of a document in two different ways:

// By using the version method of the document:
const document = await ctx.document("document_path");
const documentVersion = await document.data.getVersion("X.Y.Z");

// By specifying the version directly the document path:
const documentInVersionXYZ = await ctx.document("document_path?v=X.Y.Z");

Create a Document

Steps to create a new document within a domain:

const data = YOUR_JSON_DATA;  // JSON data for the document
const templates = ["template_path"];  // Optional array of template paths
const metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" };  // Optional metadata
const documentPath = "mypath/files/document";

const newDocument = await ctx.createDocument(documentPath, data, templates, metadata);

Update a Document

Update an existing document:

const updatedData = YOUR_UPDATED_JSON_DATA;  // Updated JSON data
const document_path = "mypath/files/document";

const document = await ctx.document(documentPath);
if (!document.success) {
    // Handle error
}
const result = await document.data.update(updatedData);

Adding Metadata to a Document

You can add metadata to a document using the addMetadata method. The metadata object should contain the following (optional) fields: name, description, and readme as shown below:

const metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" };
await document.data.addMetadata(metadata);

Last updated