kossnocorp / typesaurus

πŸ¦• Type-safe TypeScript-first ODM for Firestore
https://typesaurus.com
412 stars 34 forks source link

Provide live examples for Firestore layout to Typesaurus schema conversion #123

Closed MRDGH2821 closed 7 months ago

MRDGH2821 commented 7 months ago

I didn't understand how to create a schema when I already have some data in Cloud Firestore. I mean to say, I did not understand how to translate the layout I see in Firebase console to the schema in typesaurus.

So it would be nice to see a comparative study & conversion of Cloud Firestore's DB layout into Typesaurus schema.

I use Cloud firestore in discord bots, so here is the layout I see in Firestore console (given in json format):

{
  "servers": {
    "<server_id_int_1>": {
      "name": "<server_name_1>",
      "id": "<server_id_int_1>",
      "date": "<server_date_1>"
    },
    "<server_id_int_2>": {
      "name": "<server_name_2>",
      "id": "<server_id_int_2>",
      "date": "<server_date_2>"
    }
  },

  "webhooks": {
    "<server_id_int_1>": "<webhook_url_1>",
    "<server_id_int_2>": "<webhook_url_2>"
  },
  "user_preferences": {
    "<user_id_int_1>": {
      "user_id": "<user_id_int_1>",
      "settings": {
        "feature_1": "true",
        "feature_2": "false"
      }
    }
  }
}

The value inside < & > indicate document ID with respect to Firestore. And with respect to the discord bot, it refers to specific values (not the autogenerated values).

kossnocorp commented 7 months ago

Hey! First of all, which version are you using? Please make sure you use the latest RC (10). It's ready for launch. I'm just waiting for the blog post announcement to be published.

The docs are here: https://next.typesaurus.com/get-started/

So it would be nice to see a comparative study & conversion of Cloud Firestore's DB layout into Typesaurus schema.

That would be nice indeed! I'll keep it in mind and probably write a doc about that in the future.


Regarding your JSON, sorry, I'm a bit confused. Is it a single document, or do you describe collections like that?

I assume that it's a document as webhooks can't be a document (unless I missed something fundamental), so here's the example:

import { schema } from "typesaurus";

const db = schema(($) => ({
  docs: $.collection<Doc>(),
}));

interface Doc {
  servers: Servers;
  webhooks: Webhooks;
  user_preferences: UserPreferences;
}

type Servers = Record<number, Server>;

interface Server {
  name: string;
  id: number;
  date: Date;
}

type Webhooks = Record<number, string>;

type UserPreferences = Record<number, UserPreference>;

interface UserPreference {
  user_id: number;
  settings: Settings;
}

type Settings = Record<string, string>;

Of course, it's a very simplified example, but if you could please tell me more, I'll try to help. That would be great. I would also appreciate it if you can read the "Designing schema" section in the docs and see if it answers your questions: https://next.typesaurus.com/design/best-practices/

MRDGH2821 commented 7 months ago

Actually the webhooks part was a mistake from my side πŸ˜…

So what I mean by live example is this:

Firestore Servers collection

There are 3 collections: servers, users & express-sessions. Focusing on servers & users for now.

So, inside the collection, I have server id as the "document" name & it has document of 3 properties a.k.a "fields".

Then there's users collection: Firestore Users Collection This one has an firestore autogenerated ID (by mistake) which I do not want (I was looking around & checking out stuff) And the other document has user id as the "document" name with 2 "fields".

What I need is a direct/live comparision/examples to convert existing firestore layout into typesaurus schema. Suppose this is the schema I give you, how would you translate it into typesaurus schema?

kossnocorp commented 7 months ago

@MRDGH2821 do you mind if I write a doc using your example and link to this issue? I think your schema is very straightforward but has some nuances, like express-sessions having a dash in it, so it will be a good quickstart example.

MRDGH2821 commented 7 months ago

@MRDGH2821 do you mind if I write a doc using your example and link to this issue? I think your schema is very straightforward but has some nuances, like express-sessions having a dash in it, so it will be a good quickstart example.

I have no problems, as long as those ID values are not in the example.

(Maybe I should have properly censored it from my sideπŸ˜¬πŸ˜…)

MRDGH2821 commented 7 months ago

Can I see how my existing firestore db console screenshot translates to Typesaurus schema?πŸ€”

I may have missed the doc update, so is there any link/reference?πŸ€”

kossnocorp commented 7 months ago

Hey, thank you for waiting! Here's the doc I promised: https://next.typesaurus.com/design/adopting. Please let me know if you have more questions or any feedback on this and other schema design docs on the website.

MRDGH2821 commented 7 months ago

Here’s an example of a simple real-life database. The top-level properties are the collections, {fooId} are the dynamic ids.

In the guide, I'm confused about dynamic ids, are these IDs autogenerated by firebase when a new entry is added?

If yes, then how can I enter a custom ID?πŸ€” My firebase DB screenshot has custom IDs

If no, then I feel I haven't understood the schema definition (i.e. how to enter custom ID) in the docs. Maybe I need to experiment a little after which I will come to understand πŸ€”

I will check this out this evening 🫑