tursodatabase / tursodatabase/libsql-client-ts

Design suggestions for beta CDC support

Open
#243 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
576
Forks
69
PR merge metrics
No merged PRs in 30d

Description

I've been looking into the new beta /listen endpoint and wondering how consumers would be using this from Typescript SDK.

There is currently no "native" support for SSE on the server side (nodejs process) for SSE that supports authorization headers.

I did manage to find a polly-fill of sorts that adds support for headers and usable from nodejs

API Suggestion

The browser based SSE implementation is based on EventEmitter interface and I think its a good idea to stick to that, would async/await being common we should support that too.

We could expose a cdc property with on(...) method for supported events insert | update | delete.

const turso = createClient({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
});

// non-async
turso.cdc.on("insert", "myTable", (event) => {
 // user code here
});

// async
turso.cdc.on("insert", "myTable", async (event) => {
 // user code here
});

Small problem is that current implementation doesn't provide information about what's actually changed, it's just a signal to say an event of insert | update | delete as occurred.

User on discord notrab suggested using triggers to track changes and manage a audit log table. I had a similar idea in previous discussion of CDC and think its clever and probably easiest to implement.

Decision now becomes, does this belong in the SDK as a plugin/extension that allows the user to install the triggers and audit log table OR should this be in documentation that a user will need to manually install ?

My two cents would be that we manage this as some sort of plugin/extension that is opt-in and is an optional configuration property. I think it could work like this:

  1. configure plugin in createClient constructor
  2. createClient would install triggers and name spaced audit log table
  3. consumer can register turso.cdc.on(...) handlers and listen for events
  4. "closing" the client should uninstall triggers and audit log table

Let me know your thoughts. I would also be happy to start on a prototype to get a feel for what works well internally and is easy to manage from consumer side.

Cheers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the beta /listen endpoint and linked pull request #1388, then inspect the TypeScript SDK's createClient entry point. The proposal needs a decided design for optional trigger and audit-log setup, cdc event handlers, and client cleanup; done would require an agreed API and implementation scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.