import { Kysely, KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs, RootOperationNode, UnknownRow, QueryResult, InsertQueryNode, UpdateQueryNode, DeleteQueryNode, OperationNode, WhereNode, TableNode, FromNode, ColumnNode, ValueNode, ValueListNode, PrimitiveValueListNode, ValuesNode, BinaryOperationNode, AndNode, OrNode, ReferenceNode, ListNode, ReturningNode, SelectionNode, } from "kysely"; import { env } from "cloudflare:workers"; import { WorkerEnv } from "../worker/types"; /** * Data extracted from a query in transformQuery, to be used in transformResult. */ interface QueryData { operation: "INSERT" | "UPDATE" | "DELETE"; table: string; /** Whether we added RETURNING * to the query (true) or it already had a RETURNING clause (false) */ addedReturning: boolean; /** Fallback: For INSERT, row data extracted from VALUES clause (used when addedReturning is false) */ insertedRows?: Record[]; /** Fallback: For UPDATE/DELETE, IDs extracted from WHERE clause (used when addedReturning is false) */ ids?: string[]; } /** * Plugin that sends events for INSERT, UPDATE, and DELETE operations. * Automatically tracks changes and pushes events with the affected row data. * * Uses #data WeakMap to pass query-related data between transformQuery and transformResult, * with args.queryId as the key. This allows us to extract data BEFORE query execution * (important for DELETE operations where rows are gone after execution). */ export class D1EventPlugin implements KyselyPlugin { #getDb: () => Kysely; #queue: Queue<{ siteId: string; table: string; rowId: string; operation: "INSERT" | "UPDATE" | "DELETE"; row: unknown | null; }>; #data: WeakMap = new WeakMap(); constructor(getDb: () => Kysely) { this.#getDb = getDb; this.#queue = (env as WorkerEnv).DATASOURCES_EVENT_QUEUE; } transformQuery(args: PluginTransformQueryArgs): RootOperationNode { const { node, queryId } = args; if (node.kind === "InsertQueryNode") { const insertNode = node as InsertQueryNode; const table = this.#extractTableName(insertNode.into); if (table) { const hasReturning = !!insertNode.returning; if (hasReturning) { // RETURNING already exists - extract fallback data this.#data.set(queryId, { operation: "INSERT", table, addedReturning: false, insertedRows: this.#extractInsertedRows(insertNode), }); return node; } // Add RETURNING * to get the inserted rows this.#data.set(queryId, { operation: "INSERT", table, addedReturning: true, }); return { ...insertNode, returning: this.#createReturningAll() }; } } else if (node.kind === "UpdateQueryNode") { const updateNode = node as UpdateQueryNode; const table = this.#extractTableName(updateNode.table); if (table) { const hasReturning = !!updateNode.returning; if (hasReturning) { // RETURNING already exists - extract fallback data this.#data.set(queryId, { operation: "UPDATE", table, addedReturning: false, ids: this.#extractIdsFromWhere(updateNode.where), }); return node; } // Add RETURNING * to get the updated rows this.#data.set(queryId, { operation: "UPDATE", table, addedReturning: true, }); return { ...updateNode, returning: this.#createReturningAll() }; } } else if (node.kind === "DeleteQueryNode") { const deleteNode = node as DeleteQueryNode; const table = this.#extractTableName(deleteNode.from); if (table) { const hasReturning = !!deleteNode.returning; if (hasReturning) { // RETURNING already exists - extract fallback data this.#data.set(queryId, { operation: "DELETE", table, addedReturning: false, ids: this.#extractIdsFromWhere(deleteNode.where), }); return node; } // Add RETURNING * to get the deleted row IDs this.#data.set(queryId, { operation: "DELETE", table, addedReturning: true, }); return { ...deleteNode, returning: this.#createReturningAll() }; } } return node; } /** * Creates a RETURNING * node to add to INSERT/UPDATE/DELETE queries. */ #createReturningAll(): ReturningNode { return ReturningNode.create([SelectionNode.createSelectAll()]); } async transformResult(args: PluginTransformResultArgs): Promise> { const { queryId, result } = args; // Retrieve the pre-extracted data from transformQuery const queryData = this.#data.get(queryId); if (!queryData) { return result; } try { switch (queryData.operation) { case "INSERT": await this.#handleInsert(queryData, result); break; case "UPDATE": await this.#handleUpdate(queryData, result); break; case "DELETE": await this.#handleDelete(queryData, result); break; } } catch (error) { console.error("Error in D1EventPlugin:", error); // Don't throw - we don't want to break the query if event tracking fails } return result; } async #handleInsert(data: QueryData, result: QueryResult): Promise { const { table, addedReturning, insertedRows } = data; if (addedReturning && result.rows && result.rows.length > 0) { // Use rows from RETURNING * clause for (const row of result.rows) { const rowId = (row as Record).$id; if (rowId !== undefined && rowId !== null) { await this.#queue.send({ siteId: (env as WorkerEnv).UP_SITE_ID, table, rowId: String(rowId), operation: "INSERT", row, }); } } } else if (!addedReturning && insertedRows && insertedRows.length > 0) { // Fallback: use pre-extracted row data from VALUES clause for (const row of insertedRows) { const rowId = row.$id; if (rowId !== undefined && rowId !== null) { await this.#queue.send({ siteId: (env as WorkerEnv).UP_SITE_ID, table, rowId: String(rowId), operation: "INSERT", row, }); } } } } async #handleUpdate(data: QueryData, result: QueryResult): Promise { const { table, addedReturning, ids } = data; if (addedReturning && result.rows && result.rows.length > 0) { // Use rows from RETURNING * clause - contains full updated row state for (const row of result.rows) { const rowId = (row as Record).$id; if (rowId !== undefined && rowId !== null) { await this.#queue.send({ siteId: (env as WorkerEnv).UP_SITE_ID, table, rowId: String(rowId), operation: "UPDATE", row, }); } } } else if (!addedReturning && ids && ids.length > 0) { // Fallback: fetch rows by IDs extracted from WHERE clause for (const id of ids) { const row = await this.#fetchRowById(table, id); if (row) { await this.#queue.send({ siteId: (env as WorkerEnv).UP_SITE_ID, table, rowId: id, operation: "UPDATE", row, }); } } } } async #handleDelete(data: QueryData, result: QueryResult): Promise { const { table, addedReturning, ids } = data; if (addedReturning && result.rows && result.rows.length > 0) { // Use rows from RETURNING * clause to get deleted row IDs for (const row of result.rows) { const rowId = (row as Record).$id; if (rowId !== undefined && rowId !== null) { await this.#queue.send({ siteId: (env as WorkerEnv).UP_SITE_ID, table, rowId: String(rowId), operation: "DELETE", row: null, }); } } } else if (!addedReturning && ids && ids.length > 0) { // Fallback: use IDs extracted from WHERE clause for (const id of ids) { await this.#queue.send({ siteId: (env as WorkerEnv).UP_SITE_ID, table, rowId: id, operation: "DELETE", row: null, }); } } } #extractTableName(tableNode: OperationNode | undefined): string | null { if (!tableNode) return null; // Handle TableNode structure (has nested table.identifier) if (tableNode.kind === "TableNode") { const node = tableNode as TableNode; return node.table.identifier.name; } // Handle FromNode structure (used by DELETE queries) // FromNode contains a froms array with TableNode items if (tableNode.kind === "FromNode") { const fromNode = tableNode as FromNode; if (fromNode.froms && fromNode.froms.length > 0) { const firstFrom = fromNode.froms[0]; if (firstFrom.kind === "TableNode") { return (firstFrom as TableNode).table.identifier.name; } } } return null; } #extractIdsFromWhere(whereNode: WhereNode | undefined): string[] { const ids: string[] = []; if (!whereNode) return ids; const extractFromNode = (node: OperationNode | undefined): void => { if (!node) return; // Handle binary expressions like "$id = '123'" if (node.kind === "BinaryOperationNode") { const binaryNode = node as BinaryOperationNode; const left = binaryNode.leftOperand; const right = binaryNode.rightOperand; // Check if left side is $id column if (left.kind === "ReferenceNode") { const refNode = left as ReferenceNode; const columnNode = refNode.column; const columnName = columnNode?.kind === "ColumnNode" ? (columnNode as ColumnNode).column.name : undefined; if (columnName === "$id" && right.kind === "ValueNode") { const valueNode = right as ValueNode; ids.push(String(valueNode.value)); } // Handle IN operations like "$id IN ('1', '2', '3')" if (columnName === "$id" && right.kind === "ListNode") { const listNode = right as ListNode; for (const item of listNode.items) { if (item.kind === "ValueNode") { ids.push(String((item as ValueNode).value)); } } } } } // Handle AND operations else if (node.kind === "AndNode") { const andNode = node as AndNode; extractFromNode(andNode.left); extractFromNode(andNode.right); } // Handle OR operations else if (node.kind === "OrNode") { const orNode = node as OrNode; extractFromNode(orNode.left); extractFromNode(orNode.right); } }; extractFromNode(whereNode.where); return ids; } /** * Fallback: Fetches a row by its $id. Used when RETURNING clause already exists. */ async #fetchRowById(table: string, id: string): Promise | null> { try { const result = await this.#getDb() .selectFrom(table) .selectAll() .where("$id", "=", id) .executeTakeFirst(); return result ?? null; } catch (error) { console.error(`Error fetching row from ${table} with id ${id}:`, error); return null; } } /** * Extracts the row data being inserted from an INSERT query node. * Maps column names to their values from the VALUES clause. */ #extractInsertedRows(node: InsertQueryNode): Record[] { const rows: Record[] = []; // Get column names from the columns array const columnNames: string[] = []; if (node.columns) { for (const col of node.columns) { columnNames.push(col.column.name); } } if (columnNames.length === 0) return rows; // Extract values from the VALUES clause const valuesNode = node.values; if (!valuesNode || valuesNode.kind !== "ValuesNode") return rows; // ValuesNode contains an array of value rows const typedValuesNode = valuesNode as ValuesNode; for (const valueRow of typedValuesNode.values) { const row: Record = {}; if (valueRow.kind === "ValueListNode") { const valueListNode = valueRow as ValueListNode; // ValueListNode: array of ValueNode or other expression nodes for (let i = 0; i < valueListNode.values.length && i < columnNames.length; i++) { const vNode = valueListNode.values[i]; if (vNode.kind === "ValueNode") { row[columnNames[i]] = (vNode as ValueNode).value; } } } else if (valueRow.kind === "PrimitiveValueListNode") { const primitiveNode = valueRow as PrimitiveValueListNode; // PrimitiveValueListNode: optimized array of primitive values for (let i = 0; i < primitiveNode.values.length && i < columnNames.length; i++) { row[columnNames[i]] = primitiveNode.values[i]; } } // Only add the row if we extracted at least one value if (Object.keys(row).length > 0) { rows.push(row); } } return rows; } }