Skip to content

Commit c521c8d

Browse files
committed
Fix decoding dates
1 parent 0ef6399 commit c521c8d

File tree

7 files changed

+17
-16
lines changed

7 files changed

+17
-16
lines changed

libs/lib-services/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"keywords": [],
2222
"dependencies": {
2323
"@powersync/service-errors": "workspace:*",
24+
"@powersync/service-sync-rules": "workspace:*",
2425
"ajv": "^8.12.0",
2526
"better-ajv-errors": "^1.2.0",
2627
"bson": "^6.10.3",

libs/lib-services/src/codec/codecs.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as t from 'ts-codec';
22
import * as bson from 'bson';
3+
import { TimeValue } from '@powersync/service-sync-rules';
34

45
export const buffer = t.codec<Buffer, string>(
56
'Buffer',
@@ -12,7 +13,7 @@ export const buffer = t.codec<Buffer, string>(
1213
(buffer) => Buffer.from(buffer, 'base64')
1314
);
1415

15-
export const date = t.codec<Date, string>(
16+
export const date = t.codec<Date, string | TimeValue>(
1617
'Date',
1718
(date) => {
1819
if (!(date instanceof Date)) {
@@ -21,7 +22,9 @@ export const date = t.codec<Date, string>(
2122
return date.toISOString();
2223
},
2324
(date) => {
24-
const parsed = new Date(date);
25+
// In our jpgwire wrapper, we patch the row decoding logic to map timestamps into TimeValue instances, so we need to
26+
// support those here.
27+
const parsed = new Date(date instanceof TimeValue ? date.iso8601Representation : date);
2528
if (isNaN(parsed.getTime())) {
2629
throw new t.TransformError([`Invalid date`]);
2730
}

modules/module-mongodb/src/replication/MongoRelation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { storage } from '@powersync/service-core';
33
import { JSONBig, JsonContainer } from '@powersync/service-jsonbig';
44
import {
55
CustomSqliteValue,
6+
DatabaseInputValue,
67
SqliteInputRow,
78
SqliteInputValue,
89
SqliteRow,
@@ -98,7 +99,7 @@ export function toMongoSyncRulesValue(data: any): SqliteInputValue {
9899

99100
const DEPTH_LIMIT = 20;
100101

101-
function filterJsonData(data: any, depth = 0): any {
102+
function filterJsonData(data: any, depth = 0): DatabaseInputValue | undefined {
102103
const autoBigNum = true;
103104
if (depth > DEPTH_LIMIT) {
104105
// This is primarily to prevent infinite recursion

packages/jpgwire/src/pgwire_types.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,7 @@ export class PgType {
179179
const decoded = this.decode(unescaped, elemTypeOid);
180180
stack[0].push(decoded);
181181
}
182-
183-
if (ch == 0x7d /*}*/) {
184-
const entry = stack.shift();
185-
result = entry;
186-
} else {
187-
result = false;
188-
}
189-
182+
result = ch == 0x7d /*}*/ && stack.shift();
190183
elStart = i + 1; // TODO dry
191184
}
192185
}

packages/sync-rules/src/types/custom_sqlite_value.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export abstract class CustomSqliteValue {
2525

2626
abstract get sqliteType(): SqliteValueType;
2727

28-
static wrapArray(elements: DatabaseInputValue[]): SqliteInputValue {
28+
static wrapArray(elements: (DatabaseInputValue | undefined)[]): SqliteInputValue {
2929
const hasCustomValue = elements.some((v) => v instanceof CustomSqliteValue);
3030
if (hasCustomValue) {
3131
// We need access to the compatibility context before encoding contents as JSON.
@@ -38,7 +38,7 @@ export abstract class CustomSqliteValue {
3838
}
3939

4040
class CustomArray extends CustomSqliteValue {
41-
constructor(private readonly elements: DatabaseInputValue[]) {
41+
constructor(private readonly elements: (DatabaseInputValue | undefined)[]) {
4242
super();
4343
}
4444

packages/sync-rules/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function isJsonValue(value: SqliteValue): value is SqliteJsonValue {
6969
return value == null || typeof value == 'string' || typeof value == 'number' || typeof value == 'bigint';
7070
}
7171

72-
function filterJsonData(data: any, depth = 0): any {
72+
function filterJsonData(data: any, depth = 0): SqliteInputValue | undefined {
7373
if (depth > DEPTH_LIMIT) {
7474
// This is primarily to prevent infinite recursion
7575
// TODO: Proper error class
@@ -93,13 +93,13 @@ function filterJsonData(data: any, depth = 0): any {
9393
return undefined;
9494
} else if (data instanceof JsonContainer) {
9595
// Can be stringified directly when using our JSONBig implementation
96-
return data;
96+
return data as any;
9797
} else if (typeof data == 'object') {
9898
let record: Record<string, any> = {};
9999
for (let key of Object.keys(data)) {
100100
record[key] = filterJsonData(data[key], depth + 1);
101101
}
102-
return record;
102+
return record as any;
103103
} else {
104104
return undefined;
105105
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)