wovalle / fireorm

ORM for firestore 🔥
https://fireorm.js.org
MIT License
555 stars 74 forks source link

Add firestore timestamps to entities #317

Open gelouko opened 1 year ago

gelouko commented 1 year ago

When using the firestore admin sdk, we can retrieve the timestamps (createTime, updateTime, readTime) of a document. This can be helpful when we are using firestore for backend applications and don't want to manually create timestamps

Documentation: https://firebase.google.com/docs/reference/functions/test/test.firestore.DocumentSnapshotOptions


Proposal - Include the types to the entity IF a property with the same name does not exist (user could override this value):

Usage:

import { Collection, getRepository } from 'fireorm';

@Collection()
class Todo {
  id: string;
  text: string;
  done: Boolean;
}

const todoRepository = getRepository(Todo);

const todo = new Todo();
todo.text = "Check fireorm's Github Repository";
todo.done = false;

const todoDocument = await todoRepository.create(todo); // Create todo

const mySuperTodoDocument = await todoRepository.findById(todoDocument.id); // Read todo. Also retrieves createTime, updateTime and readTime from the firestore DocumentRef

mySuperTodoDocument.createTime // 2022-12-17 00:00:00
mySuperTodoDocument.updateTime // 2022-12-17 00:00:00
mySuperTodoDocument.readTime // 2022-12-17 00:00:01

For typescript, we can add a Timestamped wrapper to add some typing to the entity, so anyone could use it as:

export type Timestamped<T> = T & {
  createTime: Date,
  updateTime: Date,
  readTime: Date,
}

Does it make sense?

gelouko commented 1 year ago

Oh, just took a look at the PRs and looks like the repo is not maintained anymore :(