libsql-client-ts icon indicating copy to clipboard operation
libsql-client-ts copied to clipboard

Design suggestions for beta CDC support

Open JayJamieson opened this issue 1 year ago • 1 comments

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.

  • https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource#parameters.

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

  • https://github.com/EventSource/eventsource

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.

JayJamieson avatar Jul 29 '24 12:07 JayJamieson