Skip to content

question: Transforming firestore Timestamp works with firebase/firestore, but not firebase-admin/firestore #1825

Open
@tbhaxor

Description

@tbhaxor

This is coming from the existing issues #333 and #174 about Timestamp transformation.

For the same class, plainToInstance behaves differently with different firebase libraries.

class TestDto {
  @Transform(({ obj, key }) => obj[key].toDate(), { toClassOnly: true })
  createdAt: Date;
}

Case 1: With firebase/firestore it works

import { plainToInstance, Transform } from "class-transformer";
import { Timestamp } from "firebase/firestore";

console.log(plainToInstance(TestDto, { createdAt: Timestamp.now() })); // no error
// output: TestDto { createdAt: 2025-06-13T22:48:38.485Z }

Case 2: With firebase-admin/firestore it throws exception

import { plainToInstance, Transform } from "class-transformer";
import { Timestamp } from "firebase-admin/firestore";

console.log(plainToInstance(TestDto, { createdAt: Timestamp.now() })); // throws error

Full error stack trace

/private/tmp/app/node_modules/@google-cloud/firestore/build/src/validate.js:207
            throw new Error(invalidArgumentMessage(arg, 'integer'));
                  ^
Error: Value for argument "seconds" is not a valid integer.
    at validateInteger (/private/tmp/app/node_modules/@google-cloud/firestore/build/src/validate.js:207:19)
    at new Timestamp (/private/tmp/app/node_modules/@google-cloud/firestore/build/src/timestamp.js:136:40)
    at TransformOperationExecutor.transform (/private/tmp/app/node_modules/src/TransformOperationExecutor.ts:160:22)
    at TransformOperationExecutor.transform (/private/tmp/app/node_modules/src/TransformOperationExecutor.ts:333:33)
    at ClassTransformer.plainToInstance (/private/tmp/app/node_modules/src/ClassTransformer.ts:77:21)
    at plainToInstance (/private/tmp/app/node_modules/src/index.ts:84:27)
    at Object.<anonymous> (/private/tmp/app/app.ts:12:28)
    at Module._compile (node:internal/modules/cjs/loader:1529:14)
    at Module.m._compile (/private/tmp/app/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1613:10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions