@@ -2,11 +2,15 @@ import { EventEmitter } from './event_emitter.ts';
2
2
import OperationsMap from './operations_map.ts' ;
3
3
import type { TableRuntimeTypeInfo } from './spacetime_module.ts' ;
4
4
5
- import { type EventContextInterface } from './db_connection_impl.ts' ;
5
+ import {
6
+ BinaryWriter ,
7
+ type EventContextInterface ,
8
+ } from './db_connection_impl.ts' ;
6
9
import { stdbLogger } from './logger.ts' ;
7
10
8
11
export type Operation = {
9
12
type : 'insert' | 'delete' ;
13
+ // rowId: string;
10
14
rowId : string ;
11
15
row : any ;
12
16
} ;
@@ -60,17 +64,25 @@ export class TableCache<RowType = any> {
60
64
ctx : EventContextInterface
61
65
) : PendingCallback [ ] => {
62
66
const pendingCallbacks : PendingCallback [ ] = [ ] ;
63
- if ( this . tableTypeInfo . primaryKey !== undefined ) {
64
- const primaryKey = this . tableTypeInfo . primaryKey ;
67
+ if ( this . tableTypeInfo . primaryKeyInfo !== undefined ) {
68
+ const primaryKeyCol = this . tableTypeInfo . primaryKeyInfo . colName ;
69
+ const primaryKeyType = this . tableTypeInfo . primaryKeyInfo . colType ;
70
+ const getPrimaryKey = ( row : any ) => {
71
+ const primaryKeyValue = row [ primaryKeyCol ] ;
72
+ const writer = new BinaryWriter ( 10 ) ;
73
+ primaryKeyType . serialize ( writer , primaryKeyValue ) ;
74
+ return writer . toBase64 ( ) ;
75
+ } ;
65
76
const insertMap = new OperationsMap < any , [ Operation , number ] > ( ) ;
66
77
const deleteMap = new OperationsMap < any , [ Operation , number ] > ( ) ;
67
78
for ( const op of operations ) {
79
+ const primaryKey = getPrimaryKey ( op . row ) ;
68
80
if ( op . type === 'insert' ) {
69
- const [ _ , prevCount ] = insertMap . get ( op . row [ primaryKey ] ) || [ op , 0 ] ;
70
- insertMap . set ( op . row [ primaryKey ] , [ op , prevCount + 1 ] ) ;
81
+ const [ _ , prevCount ] = insertMap . get ( primaryKey ) || [ op , 0 ] ;
82
+ insertMap . set ( primaryKey , [ op , prevCount + 1 ] ) ;
71
83
} else {
72
- const [ _ , prevCount ] = deleteMap . get ( op . row [ primaryKey ] ) || [ op , 0 ] ;
73
- deleteMap . set ( op . row [ primaryKey ] , [ op , prevCount + 1 ] ) ;
84
+ const [ _ , prevCount ] = deleteMap . get ( primaryKey ) || [ op , 0 ] ;
85
+ deleteMap . set ( primaryKey , [ op , prevCount + 1 ] ) ;
74
86
}
75
87
}
76
88
for ( const {
@@ -175,6 +187,7 @@ export class TableCache<RowType = any> {
175
187
} ,
176
188
} ;
177
189
}
190
+ console . log ( `previousCount of ${ previousCount } for ${ operation . rowId } ` ) ;
178
191
return undefined ;
179
192
} ;
180
193
0 commit comments