@@ -7,37 +7,66 @@ exports.test = function(SQL, assert){
7
7
"INSERT INTO consoles VALUES (2, 'Microsoft', 'Xbox');"
8
8
) ;
9
9
10
- // {operation: undefined, tableName: undefined, rowId: undefined};
10
+ // {operation: undefined, databaseName: undefined, tableName: undefined, rowId: undefined};
11
11
var updateHookCalls = [ ]
12
12
13
- db . updateHook ( function ( operation , tableName , rowId ) {
14
- updateHookCalls . push ( { operation, tableName, rowId} ) ;
13
+ db . updateHook ( function ( operation , databaseName , tableName , rowId ) {
14
+ updateHookCalls . push ( { operation, databaseName , tableName, rowId} ) ;
15
15
} ) ;
16
16
17
17
// INSERT
18
18
db . exec ( "INSERT INTO consoles VALUES (3, 'Sega', 'Saturn');" ) ;
19
19
20
- assert . deepEqual ( updateHookCalls , [ { operation : 'insert' , tableName : 'consoles' , rowId : 3 } ] , 'insert a single row' ) ;
21
- updateHookCalls = [ ]
20
+ assert . deepEqual ( updateHookCalls , [
21
+ { operation : "insert" , databaseName : "main" , tableName : "consoles" , rowId : 3 }
22
+ ] , "insert a single row" ) ;
22
23
23
24
// UPDATE
25
+ updateHookCalls = [ ]
24
26
db . exec ( "UPDATE consoles SET name = 'Playstation 5' WHERE id = 1" ) ;
25
27
26
- assert . deepEqual ( updateHookCalls , [ { operation : 'update' , tableName : 'consoles' , rowId : 1 } ] , 'update a single row' ) ;
27
- updateHookCalls = [ ]
28
+ assert . deepEqual ( updateHookCalls , [
29
+ { operation : "update" , databaseName : "main" , tableName : "consoles" , rowId : 1 }
30
+ ] , "update a single row" ) ;
28
31
29
32
// UPDATE (multiple rows)
33
+ updateHookCalls = [ ]
30
34
db . exec ( "UPDATE consoles SET name = name + ' [legacy]' WHERE id IN (2,3)" ) ;
31
35
32
36
assert . deepEqual ( updateHookCalls , [
33
- { operation : 'update' , tableName : 'consoles' , rowId : 2 } ,
34
- { operation : 'update' , tableName : 'consoles' , rowId : 3 } ,
35
- ] , 'update two rows' ) ;
36
- updateHookCalls = [ ]
37
+ { operation : "update" , databaseName : "main" , tableName : "consoles" , rowId : 2 } ,
38
+ { operation : "update" , databaseName : "main" , tableName : "consoles" , rowId : 3 } ,
39
+ ] , "update two rows" ) ;
37
40
38
41
// DELETE
42
+ updateHookCalls = [ ]
39
43
db . exec ( "DELETE FROM consoles WHERE company = 'Sega'" ) ;
40
44
41
- assert . deepEqual ( updateHookCalls , [ { operation : 'delete' , tableName : 'consoles' , rowId : 3 } ] , 'delete a single row' ) ;
45
+ assert . deepEqual ( updateHookCalls , [
46
+ { operation : "delete" , databaseName : "main" , tableName : "consoles" , rowId : 3 }
47
+ ] , "delete a single row" ) ;
48
+
49
+ // UNREGISTER
42
50
updateHookCalls = [ ]
51
+
52
+ db . updateHook ( null ) ;
53
+
54
+ db . exec ( "DELETE FROM consoles WHERE company = 'Microsoft'" ) ;
55
+
56
+ assert . deepEqual ( updateHookCalls , [ ] , "unregister the update hook" ) ;
57
+
58
+ // REGISTER AGAIN
59
+ updateHookCalls = [ ]
60
+
61
+ db . updateHook ( function ( operation , databaseName , tableName , rowId ) {
62
+ updateHookCalls . push ( { operation, databaseName, tableName, rowId} ) ;
63
+ } ) ;
64
+
65
+ // need a where clause, just running "DELETE FROM consoles" would result in
66
+ // a TRUNCATE and not yield any update hook callbacks
67
+ db . exec ( "DELETE FROM consoles WHERE id > 0" ) ;
68
+
69
+ assert . deepEqual ( updateHookCalls , [
70
+ { operation : 'delete' , databaseName : 'main' , tableName : 'consoles' , rowId : 1 }
71
+ ] , "register the update hook again" ) ;
43
72
}
0 commit comments