Skip to content

Commit 1046bfb

Browse files
committed
fix: Make sure timestamps are correctly passed
1 parent 67b2eb7 commit 1046bfb

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

packages/effect-firebase/src/lib/firestore/schema/timestamp.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ describe('TimestampDateTimeUtc', () => {
171171

172172
describe('decoding', () => {
173173
it('should decode Timestamp to DateTime.Utc', () => {
174-
const input = { seconds: 1705315800, nanoseconds: 123000000 };
174+
const input = new Timestamp({
175+
seconds: 1705315800,
176+
nanoseconds: 123000000,
177+
});
175178
const dt = decode(input);
176179

177180
expect(DateTime.isDateTime(dt)).toBe(true);
@@ -191,10 +194,10 @@ describe('TimestampDateTimeUtc', () => {
191194
describe('roundtrip', () => {
192195
it('should maintain precision through roundtrip', () => {
193196
const originalMillis = 1705315800123;
194-
const input = {
197+
const input = new Timestamp({
195198
seconds: Math.floor(originalMillis / 1000),
196199
nanoseconds: (originalMillis % 1000) * 1000000,
197-
};
200+
});
198201
const dt = decode(input);
199202
const encoded = encode(dt);
200203

packages/effect-firebase/src/lib/firestore/schema/timestamp.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ export class Timestamp extends Schema.Class<Timestamp>('Timestamp')({
3434
}
3535
}
3636

37+
/**
38+
* Schema where Timestamp class instance is both Type and Encoded.
39+
* Using instanceOf ensures the class instance is preserved through Schema.encode.
40+
*/
41+
export const TimestampInstance = Schema.instanceOf(Timestamp);
42+
3743
/**
3844
* Schema representing a timestamp as a DateTime.Utc.
3945
*/
4046
export const TimestampDateTimeUtc = Schema.transform(
41-
Timestamp,
47+
TimestampInstance,
4248
Schema.DateTimeUtcFromSelf,
4349
{
4450
decode: (ts) => DateTime.unsafeMake(ts.toMillis()),
@@ -54,8 +60,14 @@ export class ServerTimestamp extends Schema.Class<ServerTimestamp>(
5460
'ServerTimestamp'
5561
)({}) {}
5662

63+
/**
64+
* Schema where ServerTimestamp class instance is both Type and Encoded.
65+
* Using instanceOf ensures the class instance is preserved through Schema.encode.
66+
*/
67+
export const ServerTimestampInstance = Schema.instanceOf(ServerTimestamp);
68+
5769
export const AnyTimestampDateTimeUtc = Schema.transformOrFail(
58-
Schema.Union(Timestamp, ServerTimestamp),
70+
Schema.Union(TimestampInstance, ServerTimestampInstance),
5971
Schema.DateTimeUtcFromSelf,
6072
{
6173
strict: true,

0 commit comments

Comments
 (0)