question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for Firestore Metadata fields (Id, CreateTimestamp, UpdateTimestamp, ReadTimestamp) in IFireStoreConverter<T>

See original GitHub issue

Is your feature request related to a problem? Please describe. I’m using a custom converter to convert my type to/from a document (IDictionary<string,object>) to avoid decorating my types with attributes. (I want my types to be shared in other libraries where I can’t reference the Firestore lib.)

The custom converter works great, except I don’t think it exposes the 4 metadata properties you can get using attribute serialization:

  • FirestoreDocumentId
  • FirestoreDocumentCreateTimestamp
  • FirestoreDocumentUpdateTimestamp
  • FirestoreDocumentReadTimestamp

Describe the solution you’d like It would be awesome of these 4 properties could be passed through the IFirestoreConverter<T> class

Describe alternatives you’ve considered I don’t see any way to get these values using IFirestoreConverter<T> today.

Additional context I was exploring this scenario with the below types/converter:


using AutoMapper;
using Google.Cloud.Firestore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        public static IMapper MapperInstance = new MapperConfiguration(cfg => { }).CreateMapper();

        static async Task Main(string[] args)
        {
            // Set GOOGLE_APPLICATION_CREDENTIALS env variable here

            FirestoreDbBuilder builder = new FirestoreDbBuilder();
            builder.ConverterRegistry = new ConverterRegistry();
            builder.ConverterRegistry.Add(new Converter());
            builder.ProjectId = "<project id>";

            FirestoreDb db = builder.Build();

            await db.Collection("Testing").AddAsync(new MyFireStoreType
            {
                Prop1 = "foo",
                Prop2 = 2
            });

            var result = await db.Collection("Testing").GetSnapshotAsync();

            MyFireStoreType deserializedType = result[0].ConvertTo<MyFireStoreType>();

            Console.WriteLine("Done");
        }
    }

    public sealed class MyFireStoreType : FireStoreBaseObject
    {
        public string Prop1 { get; set; }
        public int Prop2 { get; set; }
        public bool? Prop3 { get; set; }
    }

    public abstract class FireStoreBaseObject
    {
        public string FirestoreDocumentId { get; set; }

        public DateTimeOffset FirestoreDocumentCreateTimestamp { get; set; }

        public DateTimeOffset FirestoreDocumentUpdateTimestamp { get; set; }

        public DateTimeOffset FirestoreDocumentReadTimestamp { get; set; }
    }

    public class Converter : IFirestoreConverter<MyFireStoreType>
    {
        public MyFireStoreType FromFirestore(object value)
        {
            Dictionary<string, object> valueAsDict = (Dictionary<string, object>)value;
            // Issue - no Id, CreateTimestamp, UpdateTimestamp, ReadTimestamp values available in the above dictionary
            return Program.MapperInstance.Map<MyFireStoreType>(valueAsDict);
        }

        public object ToFirestore(MyFireStoreType value)
        {
            return Program.MapperInstance.Map<Dictionary<string, object>>(value);
        }
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
jskeetcommented, Nov 19, 2020

@ryandle: I released 2.3.0 yesterday - thanks for working so collaboratively on this.

1reaction
jskeetcommented, Oct 20, 2020

Okay, I’ve had an idea about how we should be able to make these available in an extensible, backwardly-compatible way. Basically you’d be able to configure additional properties that would be populated in the dictionary passed to FromFirestore. I’ll work on a PR today/tomorrow with that in…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting timestamp/creation date from Firebase doc, where ...
1 Answer. There is no way to get the creation timestamp from metadata of a Firestore document through the API. If you want...
Read more >
Timestamp | JavaScript SDK | Firebase JavaScript API reference
Convert a Timestamp to a JavaScript Date object. This conversion causes a loss of precision since Date objects only support millisecond precision.
Read more >
Update a Firestore document Timestamp
Update a Firestore document Timestamp. ... Document("new-city-id"); await cityRef.UpdateAsync("Timestamp", Timestamp.GetCurrentTimestamp());.
Read more >
Sharded timestamps | Firestore - Firebase
Delete existing composite indexes that contain the timestamp field. Create new composite indexes to support your updated queries. The order of the fields...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found