kossnocorp / typesaurus

🦕 Type-safe TypeScript-first ODM for Firestore
https://typesaurus.com
415 stars 35 forks source link

Problem in gradual adoption #124

Closed MRDGH2821 closed 9 months ago

MRDGH2821 commented 9 months ago

I am referring to https://next.typesaurus.com/design/adopting/ but I can't seem to make a proper schema out of it. Seems like $<type>() should be $.collection<type>()

Incorrect syntax

So after putting .collection part, I get this linter error: Argument of type 'string' is not assignable to parameter of type 'Id<"servers">'. Type 'string' is not assignable to type '{ [idBrand]: "servers"; }'.

Code snippet:

import '../lib/Firestore';
import type { Guild, Webhook } from 'discord.js';
import { schema } from 'typesaurus';

const SettingsDB = schema(($) => ({
  servers: $.collection<Setting>(),
}));

interface Setting {
  guildId: Guild['id'];
  webhookId: Webhook['id'];
  sendBanCopyLog?: boolean | null;
  sendBanExportLog?: boolean | null;
  sendBanLog?: boolean | null;
  sendExitLog?: boolean | null;
  sendImportLog?: boolean | null;
  sendJoinLog?: boolean | null;
  sendKickLog?: boolean | null;
  sendMassBanLog?: boolean | null;
  sendMassUnbanLog?: boolean | null;
  sendTimeoutLog?: boolean | null;
  sendUnbanLog?: boolean | null;
  sendUnTimeoutLog?: boolean | null;
}

export default SettingsDB;

async function main() {
  console.info('Getting data');
  const server = await SettingsDB.servers.get('000000000000000').catch(console.error);
  console.log(server?.data);
}

void main();

I executed the code too (with correct id ofcourse), but didn't get any output in console.

Additionally I am wondering how does typesaurus actually authenticate to firebase, because as per the docs, I didn't see any section where I have to input service account of anything. So in a seperate file I wrote the logic of authentication & importing here.

MRDGH2821 commented 9 months ago

I did a minor change:

const SettingsDB = schema(($) => ({
  servers: $.collection<Setting, Guild['id']>(),
}));

export default SettingsDB;

async function main() {
  console.info('Getting data');
  await SettingsDB.servers.get('000000000000000000').then(console.info).catch(console.error);
}

void main();

So my type problem is solved, but I don't get any output

MRDGH2821 commented 9 months ago

Actually, I was running things with bun runtime. So, it actually didn't work. When I compiled my TS codebase to JS & executed the code, it worked properly.

You may want to explicitly specify that Bun runtime is not currently supported. Btw, I also noticed that Typesaurus automatically used the initialised/authenticated firestore object 🤔