1- import { DiffTriggerOperation , TriggerDiffRecord } from '@powersync/common' ;
1+ import { DiffTriggerOperation , ExtractedTriggerDiffRecord , TriggerDiffRecord } from '@powersync/common' ;
22// import 'source-map-support/register';
33import { describe , expect , vi } from 'vitest' ;
44import { Database , databaseTest } from './utils' ;
@@ -15,15 +15,15 @@ describe('Triggers', () => {
1515 source : 'lists' ,
1616 destination : tempTable ,
1717 columns : [ 'name' ] ,
18- operations : [ DiffTriggerOperation . INSERT , DiffTriggerOperation . UPDATE ]
18+ operations : [ DiffTriggerOperation . INSERT , DiffTriggerOperation . UPDATE , DiffTriggerOperation . DELETE ]
1919 } ) ;
2020
2121 const results = [ ] as TriggerDiffRecord [ ] ;
2222
2323 database . onChange (
2424 {
2525 // This callback async processed. Invocations are sequential.
26- onChange : async ( change ) => {
26+ onChange : async ( ) => {
2727 await database . writeLock ( async ( tx ) => {
2828 // API exposes a context to run things here.
2929 // using execute seems to be important on Node.js
@@ -48,13 +48,15 @@ describe('Triggers', () => {
4848 // Do some changes to the source table
4949 await database . execute ( 'INSERT INTO lists (id, name) VALUES (uuid(), ?);' , [ 'test list' ] ) ;
5050 await database . execute ( `UPDATE lists SET name = 'wooo'` ) ;
51+ await database . execute ( 'DELETE FROM lists WHERE name = ?' , [ 'wooo' ] ) ;
5152
5253 // Wait for the changes to be processed and results to be collected
5354 await vi . waitFor (
5455 ( ) => {
55- expect ( results . length ) . toEqual ( 2 ) ;
56+ expect ( results . length ) . toEqual ( 3 ) ;
5657 expect ( results [ 0 ] . operation ) . toEqual ( 'INSERT' ) ;
5758 expect ( results [ 1 ] . operation ) . toEqual ( 'UPDATE' ) ;
59+ expect ( results [ 2 ] . operation ) . toEqual ( 'DELETE' ) ;
5860 } ,
5961 { timeout : 1000 }
6062 ) ;
@@ -63,7 +65,7 @@ describe('Triggers', () => {
6365 /**
6466 * Uses the automatic handlers for triggers to track changes.
6567 */
66- databaseTest ( 'Should be able to handle table inserts' , async ( { database } ) => {
68+ databaseTest ( 'Should be able to track table inserts' , async ( { database } ) => {
6769 await database . execute (
6870 /* sql */ `
6971 INSERT INTO
@@ -98,8 +100,8 @@ describe('Triggers', () => {
98100 SELECT
99101 todos.*
100102 FROM
101- diff
102- JOIN todos ON diff .id = todos.id
103+ DIFF
104+ JOIN todos ON DIFF .id = todos.id
103105 ` ) ;
104106
105107 results . push ( ...newTodos ) ;
@@ -124,7 +126,7 @@ describe('Triggers', () => {
124126 ( ) => {
125127 expect ( results . length ) . toEqual ( 1 ) ;
126128 } ,
127- { timeout : 10000 }
129+ { timeout : 1000 }
128130 ) ;
129131
130132 // Do further inserts
@@ -144,11 +146,11 @@ describe('Triggers', () => {
144146 ( ) => {
145147 expect ( results . length ) . toEqual ( 2 ) ;
146148 } ,
147- { timeout : 10000 }
149+ { timeout : 1000 }
148150 ) ;
149151 } ) ;
150152
151- databaseTest ( 'Should be able to handle table updates' , async ( { database } ) => {
153+ databaseTest ( 'Should be able to track table updates' , async ( { database } ) => {
152154 const { rows } = await database . execute (
153155 /* sql */ `
154156 INSERT INTO
@@ -161,18 +163,18 @@ describe('Triggers', () => {
161163
162164 const list = rows ! . item ( 0 ) as Database [ 'lists' ] ;
163165
164- const changes : Database [ 'lists' ] [ ] = [ ] ;
166+ const changes : ExtractedTriggerDiffRecord < Database [ 'lists' ] > [ ] = [ ] ;
165167
166168 /**
167169 * Watch the todos table for changes. Only track the diff for rows belonging to the first list.
168170 */
169171 await database . triggers . trackTableDiff ( {
170172 source : 'lists' ,
171- when : { [ DiffTriggerOperation . INSERT ] : `NEW.id = '${ list . id } '` } ,
172- operations : [ DiffTriggerOperation . UPDATE ] ,
173+ when : { [ DiffTriggerOperation . UPDATE ] : `NEW.id = '${ list . id } '` } ,
174+ operations : [ DiffTriggerOperation . UPDATE , DiffTriggerOperation . DELETE ] ,
173175 onChange : async ( context ) => {
174176 // Fetches the todo records that were inserted during this diff
175- const diffs = await context . withExtractedDiff < Database [ 'lists' ] > ( /* sql */ `
177+ const diffs = await context . withExtractedDiff < ExtractedTriggerDiffRecord < Database [ 'lists' ] > > ( /* sql */ `
176178 SELECT
177179 *
178180 FROM
@@ -203,7 +205,27 @@ describe('Triggers', () => {
203205 expect ( changes . length ) . toEqual ( updateCount ) ;
204206 expect ( changes . map ( ( c ) => c . name ) ) . toEqual ( Array . from ( { length : updateCount } , ( _ , i ) => `updated ${ i } ` ) ) ;
205207 } ,
206- { timeout : 10000 }
208+ { timeout : 1000 }
209+ ) ;
210+
211+ // clear the items
212+ await database . execute (
213+ /* sql */ `
214+ DELETE FROM lists
215+ WHERE
216+ id = ?
217+ ` ,
218+ [ list . id ]
219+ ) ;
220+
221+ await vi . waitFor (
222+ ( ) => {
223+ expect ( changes . length ) . toEqual ( updateCount + 1 ) ;
224+ expect ( changes [ changes . length - 1 ] . __operation ) . eq ( DiffTriggerOperation . DELETE ) ;
225+ // The delete diff should contain the previous value
226+ expect ( changes [ changes . length - 1 ] . name ) . eq ( `updated ${ updateCount - 1 } ` ) ;
227+ } ,
228+ { timeout : 1000 }
207229 ) ;
208230 } ) ;
209231
0 commit comments