1- import { DatabaseInputRow , SqliteInputRow , toSyncRulesRow } from '@powersync/service-sync-rules' ;
21import * as pgwire from '@powersync/service-jpgwire' ;
3- import { CustomTypeRegistry } from './registry.js ' ;
2+ import { DatabaseInputRow , SqliteInputRow , toSyncRulesRow } from '@powersync/service-sync-rules ' ;
43import semver from 'semver' ;
54import { getServerVersion } from '../utils/postgres_version.js' ;
5+ import { CustomTypeRegistry } from './registry.js' ;
66
77/**
88 * Resolves descriptions used to decode values for custom postgres types.
@@ -11,11 +11,9 @@ import { getServerVersion } from '../utils/postgres_version.js';
1111 */
1212export class PostgresTypeResolver {
1313 private cachedVersion : semver . SemVer | null = null ;
14+ readonly registry : CustomTypeRegistry ;
1415
15- constructor (
16- readonly registry : CustomTypeRegistry ,
17- private readonly pool : pgwire . PgClient
18- ) {
16+ constructor ( private readonly pool : pgwire . PgClient ) {
1917 this . registry = new CustomTypeRegistry ( ) ;
2018 }
2119
@@ -188,6 +186,11 @@ WHERE a.attnum > 0
188186 return toSyncRulesRow ( record ) ;
189187 }
190188
189+ constructRowRecord ( columnMap : Record < string , number > , tupleRaw : Record < string , any > ) : SqliteInputRow {
190+ const record = this . decodeTupleForTable ( columnMap , tupleRaw ) ;
191+ return toSyncRulesRow ( record ) ;
192+ }
193+
191194 /**
192195 * We need a high level of control over how values are decoded, to make sure there is no loss
193196 * of precision in the process.
@@ -206,5 +209,23 @@ WHERE a.attnum > 0
206209 return result ;
207210 }
208211
212+ /**
213+ * We need a high level of control over how values are decoded, to make sure there is no loss
214+ * of precision in the process.
215+ */
216+ private decodeTupleForTable ( columnMap : Record < string , number > , tupleRaw : Record < string , any > ) : DatabaseInputRow {
217+ let result : Record < string , any > = { } ;
218+ for ( let columnName in tupleRaw ) {
219+ const rawval = tupleRaw [ columnName ] ;
220+ const typeOid = columnMap [ columnName ] ;
221+ if ( typeof rawval == 'string' && typeOid ) {
222+ result [ columnName ] = this . registry . decodeDatabaseValue ( rawval , typeOid ) ;
223+ } else {
224+ result [ columnName ] = rawval ;
225+ }
226+ }
227+ return result ;
228+ }
229+
209230 private static minVersionForMultirange : semver . SemVer = semver . parse ( '14.0.0' ) ! ;
210231}
0 commit comments