@powersync/service-module-postgres 0.15.0 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,13 @@
1
1
  import { constructAfterRecord } from '@module/utils/pgwire_utils.js';
2
2
  import * as pgwire from '@powersync/service-jpgwire';
3
- import { SqliteRow } from '@powersync/service-sync-rules';
3
+ import {
4
+ applyRowContext,
5
+ CompatibilityContext,
6
+ SqliteInputRow,
7
+ DateTimeValue,
8
+ TimeValue,
9
+ CompatibilityEdition
10
+ } from '@powersync/service-sync-rules';
4
11
  import { describe, expect, test } from 'vitest';
5
12
  import { clearTestDb, connectPgPool, connectPgWire, TEST_URI } from './util.js';
6
13
  import { WalStream } from '@module/replication/WalStream.js';
@@ -158,9 +165,9 @@ VALUES(10, ARRAY['null']::TEXT[]);
158
165
  expect(transformed[2]).toMatchObject({
159
166
  id: 3n,
160
167
  date: '2023-03-06',
161
- time: '15:47:00',
162
- timestamp: '2023-03-06 15:47:00',
163
- timestamptz: '2023-03-06 13:47:00Z'
168
+ time: new TimeValue('15:47:00'),
169
+ timestamp: new DateTimeValue('2023-03-06T15:47:00.000000', '2023-03-06 15:47:00'),
170
+ timestamptz: new DateTimeValue('2023-03-06T13:47:00.000000Z', '2023-03-06 13:47:00Z')
164
171
  });
165
172
 
166
173
  expect(transformed[3]).toMatchObject({
@@ -175,26 +182,26 @@ VALUES(10, ARRAY['null']::TEXT[]);
175
182
  expect(transformed[4]).toMatchObject({
176
183
  id: 5n,
177
184
  date: '0000-01-01',
178
- time: '00:00:00',
179
- timestamp: '0000-01-01 00:00:00',
180
- timestamptz: '0000-01-01 00:00:00Z'
185
+ time: new TimeValue('00:00:00'),
186
+ timestamp: new DateTimeValue('0000-01-01T00:00:00'),
187
+ timestamptz: new DateTimeValue('0000-01-01T00:00:00Z')
181
188
  });
182
189
 
183
190
  expect(transformed[5]).toMatchObject({
184
191
  id: 6n,
185
- timestamp: '1970-01-01 00:00:00',
186
- timestamptz: '1970-01-01 00:00:00Z'
192
+ timestamp: new DateTimeValue('1970-01-01T00:00:00.000000', '1970-01-01 00:00:00'),
193
+ timestamptz: new DateTimeValue('1970-01-01T00:00:00.000000Z', '1970-01-01 00:00:00Z')
187
194
  });
188
195
 
189
196
  expect(transformed[6]).toMatchObject({
190
197
  id: 7n,
191
- timestamp: '9999-12-31 23:59:59',
192
- timestamptz: '9999-12-31 23:59:59Z'
198
+ timestamp: new DateTimeValue('9999-12-31T23:59:59'),
199
+ timestamptz: new DateTimeValue('9999-12-31T23:59:59Z')
193
200
  });
194
201
 
195
202
  expect(transformed[7]).toMatchObject({
196
203
  id: 8n,
197
- timestamptz: '0022-02-03 09:13:14Z'
204
+ timestamptz: new DateTimeValue('0022-02-03T09:13:14.000000Z', '0022-02-03 09:13:14Z')
198
205
  });
199
206
 
200
207
  expect(transformed[8]).toMatchObject({
@@ -235,8 +242,8 @@ VALUES(10, ARRAY['null']::TEXT[]);
235
242
  id: 3n,
236
243
  date: `["2023-03-06"]`,
237
244
  time: `["15:47:00"]`,
238
- timestamp: `["2023-03-06 15:47:00"]`,
239
- timestamptz: `["2023-03-06 13:47:00Z","2023-03-06 13:47:00.12345Z"]`
245
+ timestamp: '["2023-03-06 15:47:00"]',
246
+ timestamptz: '["2023-03-06 13:47:00Z","2023-03-06 13:47:00.12345Z"]'
240
247
  });
241
248
 
242
249
  expect(transformed[3]).toMatchObject({
@@ -339,7 +346,7 @@ VALUES(10, ARRAY['null']::TEXT[]);
339
346
 
340
347
  const transformed = [
341
348
  ...WalStream.getQueryData(pgwire.pgwireRows(await db.query(`SELECT * FROM test_data_arrays ORDER BY id`)))
342
- ];
349
+ ].map((e) => applyRowContext(e, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY));
343
350
 
344
351
  checkResultArrays(transformed);
345
352
  } finally {
@@ -415,7 +422,7 @@ VALUES(10, ARRAY['null']::TEXT[]);
415
422
  const transformed = await getReplicationTx(replicationStream);
416
423
  await pg.end();
417
424
 
418
- checkResultArrays(transformed);
425
+ checkResultArrays(transformed.map((e) => applyRowContext(e, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY)));
419
426
  } finally {
420
427
  await db.end();
421
428
  }
@@ -430,13 +437,46 @@ VALUES(10, ARRAY['null']::TEXT[]);
430
437
  // const schema = await api.getConnectionsSchema(db);
431
438
  // expect(schema).toMatchSnapshot();
432
439
  });
440
+
441
+ test('date formats', async () => {
442
+ const db = await connectPgWire();
443
+ try {
444
+ await setupTable(db);
445
+
446
+ await db.query(`
447
+ INSERT INTO test_data(id, time, timestamp, timestamptz) VALUES (1, '17:42:01.12', '2023-03-06 15:47:12.4', '2023-03-06 15:47+02');
448
+ `);
449
+
450
+ const [row] = [
451
+ ...WalStream.getQueryData(
452
+ pgwire.pgwireRows(await db.query(`SELECT time, timestamp, timestamptz FROM test_data`))
453
+ )
454
+ ];
455
+
456
+ const oldFormat = applyRowContext(row, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY);
457
+ expect(oldFormat).toMatchObject({
458
+ time: '17:42:01.12',
459
+ timestamp: '2023-03-06 15:47:12.4',
460
+ timestamptz: '2023-03-06 13:47:00Z'
461
+ });
462
+
463
+ const newFormat = applyRowContext(row, new CompatibilityContext(CompatibilityEdition.SYNC_STREAMS));
464
+ expect(newFormat).toMatchObject({
465
+ time: '17:42:01.120000',
466
+ timestamp: '2023-03-06T15:47:12.400000',
467
+ timestamptz: '2023-03-06T13:47:00.000000Z'
468
+ });
469
+ } finally {
470
+ await db.end();
471
+ }
472
+ });
433
473
  });
434
474
 
435
475
  /**
436
476
  * Return all the inserts from the first transaction in the replication stream.
437
477
  */
438
478
  async function getReplicationTx(replicationStream: pgwire.ReplicationStream) {
439
- let transformed: SqliteRow[] = [];
479
+ let transformed: SqliteInputRow[] = [];
440
480
  for await (const batch of replicationStream.pgoutputDecode()) {
441
481
  for (const msg of batch.messages) {
442
482
  if (msg.tag == 'insert') {