Skip to Content
Database

Collab

Comments, @mentions, activity timeline and version history for org records.

Usage

import { collabTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
import { createCollab } from "@dapplelight/signet-db/collab"
import { createDb } from "@dapplelight/signet-db/d1"
import { audit, tasks } from "../schema"
import { env } from "../env"
import { namesById } from "../people"
import { notifier } from "../notify"
import { orgs } from "../orgs"

const orgId = "org_1"
const userId = "u_1"
const taskId = "t_1"

export const { comments, recordVersions } = collabTables()

// Version history is opt-in per table.
const db = createDb(env.DB, { audit, versions: { table: recordVersions, track: [tasks] } })

const collab = createCollab(db, {
  tables: { comments },
  records: { tasks }, // what can be commented on, by name; each needs orgId
  orgs, // createOrgs(...)
  people: (ids) => namesById(ids),
  notify: (to, mention) => notifier.notify(to, "mention", { excerpt: mention.excerpt }),
})

const who = { orgId, userId }
const ref = { table: "tasks", id: taskId }
await collab.addComment(who, ref, { body: "Ready? @[Asha](user:u_1)" })
await collab.timeline(who, ref, { limit: 20 })
await collab.undo(who, ref, { expected: 5 }) // 409 if the record moved on

A record of another org, or any record for a non-member, is a 404. Writing mentions as @[Name](user:<id>) means a rename never breaks one.

API

ExportWhat it does
createCollab(db, options)The collaboration engine
mentionToken(id, name)The token a mention picker inserts
parseMentions(text)User ids mentioned in text
MENTION_PATTERNRegex for a mention token
describeItem(item)Default one-line timeline text
diffRows(table, before, after)Field changes between two rows
fieldLabel(key)"dueDate" to "due date"
TypesRecordRef, Who, Person, Actor, Comment, Thread, Mention, FieldChange, TimelineItem, TimelineDraft, RecordVersion, AccessCheck, CollabOptions, Collab

createCollab(db, options)

OptionTypeDefault
tables{ comments }requiredFrom collabTables()
recordsRecord<string, Table>requiredCommentable tables; each needs orgId
orgs{ membership, members }requiredcreateOrgs(...) fits
people(ids) => Promise<Record<id, { name?, email?, image? }>>id is the nameNames for people
notify(to, mention) => unknownnoneCalled once per newly mentioned person; a throw is ignored
access(check) => boolean | Promise<boolean>read: any member, write: <table>:writeReplace the access rule
describe(item) => stringdescribeItemTimeline one-liner
now() => Datenew DateClock for tests

Methods:

MethodWhat it does
addComment(who, ref, { body, parentId? })Comment or reply
editComment(who, id, { body })Author only
deleteComment(who, id)Soft delete; text cleared
comments(who, ref)Threads with replies
mentionable(who, query?, limit?)Org members for the mention picker
timeline(who, ref, { limit?, before? })Audit changes and comments, newest first
versions(who, ref, { limit?, before? })Versions with field changes
diff(who, ref, from, to)Changes between two versions
restore(who, ref, n)Put version n back as a new version
undo(who, ref, { expected? })Undo the last change

Restore and undo need <record>:write (for example tasks:write). Editing is the author's; deleting is the author's or needs comments:moderate.

Examples

Show a record's history

import { collab } from "../collab"

const who = { orgId: "org_1", userId: "u_1" }
const ref = { table: "tasks", id: "t_1" }

const { items, nextCursor } = await collab.timeline(who, ref, { limit: 20 })
// items[0].summary → "Asha changed status to Done"
const versions = await collab.versions(who, ref)
const { changes } = await collab.diff(who, ref, 2, 5) // [{ field, from, to }]

Mention someone from a picker

import { mentionToken } from "@dapplelight/signet-db/collab"
import { collab } from "../collab"

const who = { orgId: "org_1", userId: "u_1" }
const ref = { table: "tasks", id: "t_1" }

const [asha] = await collab.mentionable(who, "as")
await collab.addComment(who, ref, { body: `${mentionToken(asha.id, asha.name)} can you review?` })