@@ -27,89 +27,76 @@ export class MappingDatabase {
27
27
CREATE TABLE IF NOT EXISTS id_mappings (
28
28
local_id TEXT NOT NULL,
29
29
global_id TEXT NOT NULL,
30
- table_name TEXT NOT NULL,
31
30
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
32
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
33
- PRIMARY KEY (local_id, table_name),
34
- UNIQUE (global_id, table_name)
31
+ PRIMARY KEY (global_id)
35
32
)
36
33
` ) ;
37
34
38
35
await this . runAsync ( `
39
- CREATE INDEX IF NOT EXISTS idx_global_id ON id_mappings(global_id)
40
- ` ) ;
41
-
42
- await this . runAsync ( `
43
- CREATE INDEX IF NOT EXISTS idx_table_name ON id_mappings(table_name)
36
+ CREATE INDEX IF NOT EXISTS idx_local_id ON id_mappings(local_id)
44
37
` ) ;
45
38
}
46
39
47
40
/**
48
- * Store a mapping between local and global IDs (UPSERT)
41
+ * Store a mapping between local and global IDs
49
42
*/
50
43
public async storeMapping ( params : {
51
44
localId : string ;
52
45
globalId : string ;
53
- tableName : string ;
54
46
} ) : Promise < void > {
55
47
// Validate inputs
56
- if ( ! params . localId || ! params . globalId || ! params . tableName ) {
48
+ if ( ! params . localId || ! params . globalId ) {
57
49
throw new Error (
58
- "Invalid mapping parameters: all fields are required" ,
50
+ "Invalid mapping parameters: all fields are required"
59
51
) ;
60
52
}
61
53
54
+ console . log ( "storing mapping g:l" , params . globalId , params . localId ) ;
55
+
56
+ // Check if mapping already exists
57
+ const existingMapping = await this . getGlobalId ( params . localId ) ;
58
+
59
+ if ( existingMapping ) {
60
+ return ;
61
+ }
62
+
62
63
try {
63
- // Use UPSERT to handle both insert and update cases
64
64
await this . runAsync (
65
- `INSERT OR REPLACE INTO id_mappings (local_id, global_id, table_name, updated_at )
66
- VALUES (?, ?, ?, CURRENT_TIMESTAMP )` ,
67
- [ params . localId , params . globalId , params . tableName ] ,
65
+ `INSERT INTO id_mappings (local_id, global_id)
66
+ VALUES (?, ?)` ,
67
+ [ params . localId , params . globalId ]
68
68
) ;
69
69
70
- // Verify the mapping was stored correctly
71
- const storedMapping = await this . getGlobalId ( {
72
- localId : params . localId ,
73
- tableName : params . tableName ,
74
- } ) ;
70
+ const storedMapping = await this . getGlobalId ( params . localId ) ;
75
71
76
72
if ( storedMapping !== params . globalId ) {
77
- console . error ( "Mapping verification failed:" , {
78
- expected : params . globalId ,
79
- actual : storedMapping ,
80
- params
81
- } ) ;
82
- throw new Error ( "Failed to store mapping - verification failed" ) ;
73
+ console . log (
74
+ "storedMappingError" ,
75
+ storedMapping ,
76
+ params . globalId
77
+ ) ;
78
+ console . error ( "Failed to store mapping" ) ;
79
+ return ;
83
80
}
84
-
85
- console . log ( "Successfully stored mapping:" , {
86
- localId : params . localId ,
87
- globalId : params . globalId ,
88
- tableName : params . tableName
89
- } ) ;
90
81
} catch ( error ) {
91
- console . error ( "Error storing mapping:" , error , params ) ;
92
82
throw error ;
93
83
}
94
84
}
95
85
96
86
/**
97
87
* Get the global ID for a local ID
98
88
*/
99
- public async getGlobalId ( params : {
100
- localId : string ;
101
- tableName : string ;
102
- } ) : Promise < string | null > {
103
- if ( ! params . localId || ! params . tableName ) {
89
+ public async getGlobalId ( localId : string ) : Promise < string | null > {
90
+ if ( ! localId ) {
104
91
return null ;
105
92
}
106
93
107
94
try {
108
95
const result = await this . getAsync (
109
96
`SELECT global_id
110
97
FROM id_mappings
111
- WHERE local_id = ? AND table_name = ? ` ,
112
- [ params . localId , params . tableName ] ,
98
+ WHERE local_id = ?` ,
99
+ [ localId ]
113
100
) ;
114
101
return result ?. global_id ?? null ;
115
102
} catch ( error ) {
@@ -121,20 +108,17 @@ export class MappingDatabase {
121
108
/**
122
109
* Get the local ID for a global ID
123
110
*/
124
- public async getLocalId ( params : {
125
- globalId : string ;
126
- tableName : string ;
127
- } ) : Promise < string | null > {
128
- if ( ! params . globalId || ! params . tableName ) {
111
+ public async getLocalId ( globalId : string ) : Promise < string | null > {
112
+ if ( ! globalId ) {
129
113
return null ;
130
114
}
131
115
132
116
try {
133
117
const result = await this . getAsync (
134
118
`SELECT local_id
135
119
FROM id_mappings
136
- WHERE global_id = ? AND table_name = ? ` ,
137
- [ params . globalId , params . tableName ] ,
120
+ WHERE global_id = ?` ,
121
+ [ globalId ]
138
122
) ;
139
123
return result ?. local_id ?? null ;
140
124
} catch ( error ) {
@@ -145,44 +129,35 @@ export class MappingDatabase {
145
129
/**
146
130
* Delete a mapping
147
131
*/
148
- public async deleteMapping ( params : {
149
- localId : string ;
150
- tableName : string ;
151
- } ) : Promise < void > {
152
- if ( ! params . localId || ! params . tableName ) {
132
+ public async deleteMapping ( localId : string ) : Promise < void > {
133
+ if ( ! localId ) {
153
134
return ;
154
135
}
155
136
156
137
try {
157
138
await this . runAsync (
158
139
`DELETE FROM id_mappings
159
- WHERE local_id = ? AND table_name = ? ` ,
160
- [ params . localId , params . tableName ] ,
140
+ WHERE local_id = ?` ,
141
+ [ localId ]
161
142
) ;
162
143
} catch ( error ) {
163
144
throw error ;
164
145
}
165
146
}
166
147
167
148
/**
168
- * Get all mappings for a table
149
+ * Get all mappings
169
150
*/
170
- public async getTableMappings ( tableName : string ) : Promise <
151
+ public async getAllMappings ( ) : Promise <
171
152
Array < {
172
153
localId : string ;
173
154
globalId : string ;
174
155
} >
175
156
> {
176
- if ( ! tableName ) {
177
- return [ ] ;
178
- }
179
-
180
157
try {
181
158
const results = await this . allAsync (
182
159
`SELECT local_id, global_id
183
- FROM id_mappings
184
- WHERE table_name = ?` ,
185
- [ tableName ] ,
160
+ FROM id_mappings`
186
161
) ;
187
162
188
163
return results . map ( ( { local_id, global_id } ) => ( {
@@ -194,83 +169,6 @@ export class MappingDatabase {
194
169
}
195
170
}
196
171
197
- /**
198
- * Debug method to get all mappings
199
- */
200
- public async getAllMappings ( ) : Promise < Array < {
201
- localId : string ;
202
- globalId : string ;
203
- tableName : string ;
204
- createdAt : string ;
205
- updatedAt : string ;
206
- } > > {
207
- try {
208
- const results = await this . allAsync (
209
- `SELECT local_id, global_id, table_name, created_at, updated_at
210
- FROM id_mappings
211
- ORDER BY table_name, local_id`
212
- ) ;
213
-
214
- return results . map ( ( { local_id, global_id, table_name, created_at, updated_at } ) => ( {
215
- localId : local_id ,
216
- globalId : global_id ,
217
- tableName : table_name ,
218
- createdAt : created_at ,
219
- updatedAt : updated_at ,
220
- } ) ) ;
221
- } catch ( error ) {
222
- console . error ( "Error getting all mappings:" , error ) ;
223
- return [ ] ;
224
- }
225
- }
226
-
227
- /**
228
- * Debug method to check if a mapping exists
229
- */
230
- public async debugMapping ( params : {
231
- localId ?: string ;
232
- globalId ?: string ;
233
- tableName : string ;
234
- } ) : Promise < {
235
- exists : boolean ;
236
- mapping ?: any ;
237
- allMappingsForTable : Array < { localId : string ; globalId : string } > ;
238
- } > {
239
- const { localId, globalId, tableName } = params ;
240
-
241
- try {
242
- let mapping = null ;
243
-
244
- if ( localId ) {
245
- const globalIdResult = await this . getGlobalId ( { localId, tableName } ) ;
246
- if ( globalIdResult ) {
247
- mapping = { localId, globalId : globalIdResult , tableName } ;
248
- }
249
- }
250
-
251
- if ( globalId && ! mapping ) {
252
- const localIdResult = await this . getLocalId ( { globalId, tableName } ) ;
253
- if ( localIdResult ) {
254
- mapping = { localId : localIdResult , globalId, tableName } ;
255
- }
256
- }
257
-
258
- const allMappingsForTable = await this . getTableMappings ( tableName ) ;
259
-
260
- return {
261
- exists : ! ! mapping ,
262
- mapping,
263
- allMappingsForTable,
264
- } ;
265
- } catch ( error ) {
266
- console . error ( "Error debugging mapping:" , error ) ;
267
- return {
268
- exists : false ,
269
- allMappingsForTable : [ ] ,
270
- } ;
271
- }
272
- }
273
-
274
172
/**
275
173
* Close the database connection
276
174
*/
0 commit comments