#Dynamic typename is it possible? !resolved

5 messages · Page 1 of 1 (latest)

plucky narwhal
#

I'm exporting all my database tables and their types. In the end, I have the table names and the data types inside them.
For example:

type commentsType { ... }
type notificationsType { ... }
type projectsType { ... }

I also have an array of the collection names:
comments, notifications, projects...

Since the fetch calls are similar for fetching data, I'm trying to create a function in TypeScript that can also infer the type.
In JavaScript, I wrote something like this:

const db = {};
for (collection in collections) {
db[collection].getList = fetch...;
db[collection].getOne = fetch...;
}

The dream is to write this in TypeScript so that it also knows what it returns. For example:

db["comments"].getList = <commentsType[]>;
db["projects"].getList = <projectsType[]>;

I'm trying to make this generated automatically, instead of having to manually update it every time the database changes.|

the closest i got with AI was this .. but the compiler doesn't like it, and it doesn't work


type Collection = {
  name: string;
};

const collections2: Collection[] = [
  { name: "comments" },
  { name: "notes" },
  
];

type DbFunctions = {
  [K in Collection['name']]: {
    getList: () => allTypes[`${K}Type`][];
  };
};

export const dbFunctions: DbFunctions = Object.fromEntries(
  collections2.map(({ name }) => [
    name,
    {
      getList: (): allTypes[`${name}Type`][] => {
        // Implement the getList function here
        return [];
      },
    },
  ])
);
prisma ermine
#

possible to do that, but would involve a minimum of set up

#

also, can you elaborate on the "collections" you have? not too sure what they are and how they work. how are they different from tables you can query and then get objects?
what lib are you using?

plucky narwhal
#

Thanks for reaching out!
I managed to solve it,
here is how

/** My TYPES FILE */

import {Models} from 'node-appwrite'

export interface PartnersType extends Models.Document {
  title: string;
  // more fields
}

export interface CommentsType extends Models.Document {
  itemType: string;
  /// more fields
}

export type CollectionTypes = {
  Partners: PartnersType;
  Comments: CommentsType;
}

/** My Main file... */


import * as allTypes from './types';

export interface dbActions {
  list: <T extends Models.Document>(queries?: string[]) => Promise<Models.DocumentList<T>>;
}


type DbFunctions = {
  getList<K extends keyof allTypes.CollectionTypes>(collection: K, queries: string[]): Promise<Models.DocumentList<allTypes.CollectionTypes[K]>>;
}

export const db: DbFunctions = {
  async getList<K extends keyof allTypes.CollectionTypes>(collection: K, queries:string[]): Promise<Models.DocumentList<allTypes.CollectionTypes[K]>> {
    // my fetch function, that can take the collection name , and get it :)
// it works for all of my collections :) YES !
  }
}

took me a while 🙂

#

Dynamic typename is it possible? !resolved