11import { mongo } from '@powersync/lib-service-mongodb' ;
2- import { SqliteRow , SqlSyncRules } from '@powersync/service-sync-rules' ;
2+ import {
3+ applyRowContext ,
4+ CompatibilityContext ,
5+ CompatibilityEdition ,
6+ SqliteInputRow ,
7+ SqlSyncRules
8+ } from '@powersync/service-sync-rules' ;
39import { describe , expect , test } from 'vitest' ;
410
511import { MongoRouteAPIAdapter } from '@module/api/MongoRouteAPIAdapter.js' ;
@@ -138,8 +144,10 @@ describe('mongo data types', () => {
138144 ] ) ;
139145 }
140146
141- function checkResults ( transformed : Record < string , any > [ ] ) {
142- expect ( transformed [ 0 ] ) . toMatchObject ( {
147+ function checkResults ( transformed : SqliteInputRow [ ] ) {
148+ const sqliteValue = transformed . map ( ( e ) => applyRowContext ( e , CompatibilityContext . FULL_BACKWARDS_COMPATIBILITY ) ) ;
149+
150+ expect ( sqliteValue [ 0 ] ) . toMatchObject ( {
143151 _id : 1n ,
144152 text : 'text' ,
145153 uuid : 'baeb2514-4c57-436d-b3cc-c1256211656d' ,
@@ -152,17 +160,17 @@ describe('mongo data types', () => {
152160 null : null ,
153161 decimal : '3.14'
154162 } ) ;
155- expect ( transformed [ 1 ] ) . toMatchObject ( {
163+ expect ( sqliteValue [ 1 ] ) . toMatchObject ( {
156164 _id : 2n ,
157165 nested : '{"test":"thing"}'
158166 } ) ;
159167
160- expect ( transformed [ 2 ] ) . toMatchObject ( {
168+ expect ( sqliteValue [ 2 ] ) . toMatchObject ( {
161169 _id : 3n ,
162170 date : '2023-03-06 13:47:00.000Z'
163171 } ) ;
164172
165- expect ( transformed [ 3 ] ) . toMatchObject ( {
173+ expect ( sqliteValue [ 3 ] ) . toMatchObject ( {
166174 _id : 4n ,
167175 objectId : '66e834cc91d805df11fa0ecb' ,
168176 timestamp : 1958505087099n ,
@@ -177,9 +185,9 @@ describe('mongo data types', () => {
177185 } ) ;
178186
179187 // This must specifically be null, and not undefined.
180- expect ( transformed [ 4 ] . undefined ) . toBeNull ( ) ;
188+ expect ( sqliteValue [ 4 ] . undefined ) . toBeNull ( ) ;
181189
182- expect ( transformed [ 5 ] ) . toMatchObject ( {
190+ expect ( sqliteValue [ 5 ] ) . toMatchObject ( {
183191 _id : 6n ,
184192 int4 : - 1n ,
185193 int8 : - 9007199254740993n ,
@@ -188,8 +196,10 @@ describe('mongo data types', () => {
188196 } ) ;
189197 }
190198
191- function checkResultsNested ( transformed : Record < string , any > [ ] ) {
192- expect ( transformed [ 0 ] ) . toMatchObject ( {
199+ function checkResultsNested ( transformed : SqliteInputRow [ ] ) {
200+ const sqliteValue = transformed . map ( ( e ) => applyRowContext ( e , CompatibilityContext . FULL_BACKWARDS_COMPATIBILITY ) ) ;
201+
202+ expect ( sqliteValue [ 0 ] ) . toMatchObject ( {
193203 _id : 1n ,
194204 text : `["text"]` ,
195205 uuid : '["baeb2514-4c57-436d-b3cc-c1256211656d"]' ,
@@ -204,30 +214,30 @@ describe('mongo data types', () => {
204214
205215 // Note: Depending on to what extent we use the original postgres value, the whitespace may change, and order may change.
206216 // We do expect that decimals and big numbers are preserved.
207- expect ( transformed [ 1 ] ) . toMatchObject ( {
217+ expect ( sqliteValue [ 1 ] ) . toMatchObject ( {
208218 _id : 2n ,
209219 nested : '[{"test":"thing"}]'
210220 } ) ;
211221
212- expect ( transformed [ 2 ] ) . toMatchObject ( {
222+ expect ( sqliteValue [ 2 ] ) . toMatchObject ( {
213223 _id : 3n ,
214224 date : '["2023-03-06 13:47:00.000Z"]'
215225 } ) ;
216226
217- expect ( transformed [ 3 ] ) . toMatchObject ( {
227+ expect ( sqliteValue [ 3 ] ) . toMatchObject ( {
218228 _id : 5n ,
219229 undefined : '[null]'
220230 } ) ;
221231
222- expect ( transformed [ 4 ] ) . toMatchObject ( {
232+ expect ( sqliteValue [ 4 ] ) . toMatchObject ( {
223233 _id : 6n ,
224234 int4 : '[-1]' ,
225235 int8 : '[-9007199254740993]' ,
226236 float : '[-3.14]' ,
227237 decimal : '["-3.14"]'
228238 } ) ;
229239
230- expect ( transformed [ 5 ] ) . toMatchObject ( {
240+ expect ( sqliteValue [ 5 ] ) . toMatchObject ( {
231241 _id : 10n ,
232242 objectId : '["66e834cc91d805df11fa0ecb"]' ,
233243 timestamp : '[1958505087099]' ,
@@ -522,13 +532,45 @@ bucket_definitions:
522532 errors : [ ]
523533 } ) ;
524534 } ) ;
535+
536+ test ( 'date format' , async ( ) => {
537+ const { db, client } = await connectMongoData ( ) ;
538+ const collection = db . collection ( 'test_data' ) ;
539+ try {
540+ await setupTable ( db ) ;
541+ await collection . insertOne ( {
542+ fraction : new Date ( '2023-03-06 15:47:01.123+02' ) ,
543+ noFraction : new Date ( '2023-03-06 15:47:01+02' )
544+ } ) ;
545+
546+ const rawResults = await db
547+ . collection ( 'test_data' )
548+ . find ( { } , { sort : { _id : 1 } } )
549+ . toArray ( ) ;
550+ const [ row ] = [ ...ChangeStream . getQueryData ( rawResults ) ] ;
551+
552+ const oldFormat = applyRowContext ( row , CompatibilityContext . FULL_BACKWARDS_COMPATIBILITY ) ;
553+ expect ( oldFormat ) . toMatchObject ( {
554+ fraction : '2023-03-06 13:47:01.123Z' ,
555+ noFraction : '2023-03-06 13:47:01.000Z'
556+ } ) ;
557+
558+ const newFormat = applyRowContext ( row , new CompatibilityContext ( CompatibilityEdition . SYNC_STREAMS ) ) ;
559+ expect ( newFormat ) . toMatchObject ( {
560+ fraction : '2023-03-06T13:47:01.123Z' ,
561+ noFraction : '2023-03-06T13:47:01.000Z'
562+ } ) ;
563+ } finally {
564+ await client . close ( ) ;
565+ }
566+ } ) ;
525567} ) ;
526568
527569/**
528570 * Return all the inserts from the first transaction in the replication stream.
529571 */
530572async function getReplicationTx ( replicationStream : mongo . ChangeStream , count : number ) {
531- let transformed : SqliteRow [ ] = [ ] ;
573+ let transformed : SqliteInputRow [ ] = [ ] ;
532574 for await ( const doc of replicationStream ) {
533575 // Specifically filter out map_input / map_output collections
534576 if ( ! ( doc as any ) ?. ns ?. coll ?. startsWith ( 'test_data' ) ) {
0 commit comments